package com.miaxis.app.controller.fulu;


import com.alibaba.fastjson.JSONObject;
import com.miaxis.common.config.WxpayConfig;
import com.miaxis.common.constant.Constants;
import com.miaxis.common.enums.OrderStatusEnum;
import com.miaxis.common.exception.CustomException;
import com.miaxis.wx.domain.WxOrder;
import com.miaxis.wx.dto.FilmWxOrderJsonData;
import com.miaxis.wx.dto.FuluWxOrderJsonData;
import com.miaxis.wx.dto.WxFuluOrderDetailDTO;
import com.miaxis.wx.dto.WxOrderNotifyDTO;
import com.miaxis.wx.service.IRefundRecordService;
import com.miaxis.wx.service.IWxOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.miaxis.common.utils.OrderCodeFactory.getOrderCode;

@RestController
@RequiredArgsConstructor
@RequestMapping(Constants.OPEN_PREFIX+"/fulu/notify")
@Api(tags = {"【小程序-福禄回调】"})
@Slf4j
public class FuluNotifyController {


    @Autowired
    private WxpayConfig wxpayConfig;


    @Autowired
    private IWxOrderService wxOrderService;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Autowired
    private  IRefundRecordService  refundRecordService;



    /**
     * 电影回调接口
     */
    @PostMapping(value = "/fuluOrder")
    @ApiOperation("福禄订单回调")
    public FilmNotifyReturnDTO wxOrderNotify(WxFuluOrderDetailDTO wxFuluOrderDetailDTO) {
        //todo 验签sign
        WxOrder wxOrder = wxOrderService.getByOutTradeNo(wxFuluOrderDetailDTO.getCustomer_order_no());
        String orderDataJson = wxOrder.getOrderDataJson();
        FuluWxOrderJsonData fuluWxOrderJsonData = JSONObject.parseObject(orderDataJson, FuluWxOrderJsonData.class);
        WxFuluOrderDetailDTO dbWxFuluOrderDetailDTO= fuluWxOrderJsonData.getWxFuluOrderDetailDTO();
        if (wxFuluOrderDetailDTO.getCharge_finish_time()!=null){
            dbWxFuluOrderDetailDTO.setCharge_finish_time(wxFuluOrderDetailDTO.getCharge_finish_time());
        }
        dbWxFuluOrderDetailDTO.setOrder_status(wxFuluOrderDetailDTO.getOrder_status());
        //如果订单失败，则调用微信退款
        if ("failed".equals(wxFuluOrderDetailDTO.getOrder_status())){
            String refundCode = getOrderCode(null);
            wxOrder.setOutRefundNo(refundCode);
            wxOrder.setOrderStatus(OrderStatusEnum.FAILD.getCode());
            threadPoolTaskExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        refundRecordService.refund(wxOrder,refundCode);
                    } catch (Exception e) {
                        throw new CustomException("订单:"+wxOrder.getOutTradeNo()+"退款失败");
                    }
                }
            });
        }
        fuluWxOrderJsonData.setWxFuluOrderDetailDTO(dbWxFuluOrderDetailDTO);
        wxOrder.setOrderDataJson(fuluWxOrderJsonData.toString());
        wxOrderService.updateById(wxOrder);

        FilmNotifyReturnDTO filmNotifyReturnDTO = new FilmNotifyReturnDTO();
        filmNotifyReturnDTO.setCode("200");
        filmNotifyReturnDTO.setMessage("请求成功");
        filmNotifyReturnDTO.setSuccess(true);
        return filmNotifyReturnDTO;
    }



    @Data
    public class FilmNotifyReturnDTO {
        String code;
        String message;
        Boolean success;
    }

}
