package com.izouma.nineth.web;

import com.alibaba.fastjson.JSONObject;
import com.izouma.nineth.config.GeneralProperties;
import com.izouma.nineth.domain.Collection;
import com.izouma.nineth.domain.Order;
import com.izouma.nineth.domain.PaymentInfo;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.dto.PageWrapDTO;
import com.izouma.nineth.dto.pay.PayResult;
import com.izouma.nineth.enums.OrderStatus;
import com.izouma.nineth.enums.PayMethod;
import com.izouma.nineth.enums.PayStatus;
import com.izouma.nineth.event.OrderNotifyEvent;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.OrderRepo;
import com.izouma.nineth.repo.PaymentInfoRepo;
import com.izouma.nineth.service.OrderService;
import com.izouma.nineth.service.PaymentInfoService;
import com.izouma.nineth.service.pay.UniformPayUtil;
import com.izouma.nineth.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/paymentInfo")
@Slf4j
@AllArgsConstructor
public class PaymentInfoController {

    private PaymentInfoRepo paymentInfoRepo;

    private PaymentInfoService paymentInfoService;

    private OrderRepo orderRepo;

    private OrderService orderService;


    @PostMapping("/all")
    public PageWrapDTO<PaymentInfo> all(@RequestBody PageQuery pageQuery) {
        PageWrapDTO pageWrapDTO = paymentInfoService.all(pageQuery);
        return pageWrapDTO;
    }



    @PostMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, @RequestBody PageQuery pageQuery) throws IOException {
        List<PaymentInfo> data = paymentInfoService.all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }


    /**
     * 查询支付结果
     * @param orderNo
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping("checkPayResult")
    @ResponseBody
    public PayResult checkPayResult(@RequestParam String orderNo){
        PayResult payResult = paymentInfoService.checkPayResult(orderNo);
        return payResult;
    }

    /**
     * 批量退款
     * @param orderNoList
     * @throws Exception
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/batchRefund")
    public void batchRefund(@RequestBody List<String> orderNoList) throws Exception{
        orderService.batchRefund(orderNoList);
    }


    /**
     * 手动完成订单
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("sendNotifyOrder")
    public void sendNotifyOrder(@RequestBody JSONObject reqData){
        String orderNo = reqData.getString("orderNo");

        PaymentInfo paymentInfo = paymentInfoRepo.findByOrderNo(orderNo).orElseThrow(new BusinessException("支付记录不存在"));
        if(paymentInfo.getStatus() != PayStatus.HAVE_PAID){
            throw new BusinessException("支付状态不是已支付");
        }

        Order orderInfo = orderRepo.findById(paymentInfo.getOrderId()).orElseThrow(new BusinessException("订单不存在"));
        if(orderInfo.getStatus() != OrderStatus.NOT_PAID){
            throw new BusinessException("订单不是待支付状态");
        }

        orderService.notifyOrder(orderInfo.getId(), paymentInfo.getPayMethod(), paymentInfo.getTransactionId());

        log.info("手动调用完成订单, 订单编号：" + orderNo + ",订单ID:" + orderInfo.getId());

    }


}
