package com.ruoyi.xq.manager;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.xq.domain.VipOrder;
import com.ruoyi.xq.domain.VipPrice;
import com.ruoyi.xq.domain.WxTransOrder;
import com.ruoyi.xq.dto.app.pay.ConsumeResp;
import com.ruoyi.xq.dto.app.pay.PayOrderInfoDTO;
import com.ruoyi.xq.enums.common.OrderTypeEnum;
import com.ruoyi.xq.enums.pay.PayStatusEnum;
import com.ruoyi.xq.enums.pay.PlatformTypeEnum;
import com.ruoyi.xq.mq.AmqpProducer;
import com.ruoyi.xq.mq.handle.dto.CalculateSalesHandleDTO;
import com.ruoyi.xq.service.*;
import com.ruoyi.xq.service.impl.AuthOrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class PayManager {
    @Autowired
    private VipOrderService vipOrderService;
    @Autowired
    private UserExtendService userExtendService;
    @Autowired
    private AmqpProducer amqpProducer;
    @Autowired
    private VipPriceService vipPriceService;
    @Autowired
    private WxTransOrderService wxTransOrderService;
    @Autowired
    private AuthOrderService authOrderService;

    public PayOrderInfoDTO getOrderInfo(String orderNo){
        OrderTypeEnum orderTypeEnum = OrderNoUtil.getType(orderNo);
        if(orderTypeEnum == null){
            log.error("订单类型有误！orderNo={}",orderNo);
            throw new ServiceException("支付失败，请检查订单号");
        }
        PayOrderInfoDTO dto = null;
        switch (orderTypeEnum) {
            case VIP:
                VipOrder vipOrder = vipOrderService.getByOrderNo(orderNo);
                if(vipOrder == null){
                    throw new ServiceException("订单不存在，请重新下单支付");
                }
                if(!PayStatusEnum.READY_PAY.getCode().equals(vipOrder.getPayStatus())){
                    throw new ServiceException("订单状态有误，请重新下单支付");
                }
                dto = new PayOrderInfoDTO();
                dto.setBody(vipOrder.getBody());
                dto.setSubject(vipOrder.getBody());
                dto.setPrice(vipOrder.getVipPrice());
                dto.setOrderNo(vipOrder.getOrderNo());
                break;
            case WX_TRANS:
                WxTransOrder wxTransOrder = wxTransOrderService.getByOrderNo(orderNo);
                if(wxTransOrder == null){
                    throw new ServiceException("订单不存在，请重新下单支付");
                }
                if(!PayStatusEnum.READY_PAY.getCode().equals(wxTransOrder.getPayStatus())){
                    throw new ServiceException("订单状态有误，请重新下单支付");
                }
                dto = new PayOrderInfoDTO();
                dto.setBody(wxTransOrder.getBody());
                dto.setSubject(wxTransOrder.getBody());
                dto.setPrice(wxTransOrder.getWxPrice());
                dto.setOrderNo(wxTransOrder.getOrderNo());
                break;
            default:
                break;
        }
        return dto;
    }


    public void callBack(String orderNo, Map<String,String> params, String appId, PlatformTypeEnum payTypeEnum){
        OrderTypeEnum orderTypeEnum = OrderNoUtil.getType(orderNo);
        if(orderTypeEnum == null){
            log.error("订单类型有误！orderNo={}",orderNo);
            return;
        }
        switch (orderTypeEnum){
            case VIP:
                ConsumeResp vipResp = vipOrderService.doSuccess(orderNo,params,appId,payTypeEnum);
                if(vipResp.isSuccess()){
                    try {
                        // 用户消费统计
                        userExtendService.incsConsumeTotal(vipResp.getUserId(), vipResp.getPrice());
                    }catch (Exception e){
                        log.error("用户VIP消费统计",e);
                    }
                    try {
                        if(vipResp.getConsumerId() != null){
                            CalculateSalesHandleDTO dto = new CalculateSalesHandleDTO();
                            dto.setConsumerLogId(vipResp.getConsumerId());
                            amqpProducer.sendCommonMq(dto);
                        }
                    }catch (Exception e){
                        log.error("RabbitMq 发送失败, 充值分销流程流转失败！",e);
                    }
                    try {
                        if(vipResp.getVipId() != null){
                            VipPrice vipPrice = vipPriceService.getById(vipResp.getVipId());
                            if(vipPrice != null && vipPrice.getGiveWxTransNum() > 0){
                            }
                        }
                    }catch (Exception e){
                        log.error("开通会员赠送次数失败",e);
                    }
                }
                break;
            case WX_TRANS:
                ConsumeResp consumeResp = wxTransOrderService.doSuccess(orderNo, params, appId, payTypeEnum);
                if(consumeResp.isSuccess()){
                    try {
                        // 用户消费统计
                        userExtendService.incsConsumeTotal(consumeResp.getUserId(), consumeResp.getPrice());
                    }catch (Exception e){
                        log.error("用户微信交换次数消费统计",e);
                    }
                }
                break;
            case AUTH_ORDER:
                ConsumeResp authResp = authOrderService.doSuccess(orderNo, params, appId, payTypeEnum);
                break;
            default:
                break;
        }
    }
}
