package com.ruoyi.cai.pay;

import com.ruoyi.cai.domain.RechargeOrder;
import com.ruoyi.cai.domain.VipOrder;
import com.ruoyi.cai.dto.ConsumeResp;
import com.ruoyi.cai.dto.commom.consumer.RechargeConsumerResp;
import com.ruoyi.cai.manager.AwardManager;
import com.ruoyi.cai.manager.ConsumerManager;
import com.ruoyi.cai.service.PayTotalService;
import com.ruoyi.cai.service.RechargeOrderService;
import com.ruoyi.cai.service.VipOrderService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
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 ConsumerManager consumerManager;
    @Autowired
    private RechargeOrderService rechargeOrderService;
    @Autowired
    private AwardManager awardManager;
    @Autowired
    private PayTotalService payTotalService;

    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_ORDER_SUB:
                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.getVipName());
                dto.setSubject(vipOrder.getVipName());
                dto.setPrice(vipOrder.getPrice());
                break;
            case RECHARGE_ORDER_SUB:
                RechargeOrder rechargeOrder = rechargeOrderService.getByOrderNo(orderNo);
                if(rechargeOrder == null){
                    throw new ServiceException("订单不存在，请重新下单支付");
                }
                if(!PayStatusEnum.READY_PAY.getCode().equals(rechargeOrder.getPayStatus())){
                    throw new ServiceException("订单状态有误，请重新下单支付");
                }
                dto = new PayOrderInfoDTO();
                dto.setBody(rechargeOrder.getRechargeName());
                dto.setSubject(rechargeOrder.getRechargeName());
                dto.setPrice(rechargeOrder.getPrice());
                break;
            default:
                break;
        }
        return dto;
    }

    public void callBack(String orderNo){
        callBack(orderNo,null,null,null,null);
    }

    public void callBack(String orderNo, String transId, Map<String,String> params, String appId,PayTypeEnum payTypeEnum){
        OrderTypeEnum orderTypeEnum = OrderNoUtil.getType(orderNo);
        if(orderTypeEnum == null){
            log.error("订单类型有误！orderNo={}",orderNo);
            return;
        }
        switch (orderTypeEnum){
            case VIP_ORDER_SUB:
                ConsumeResp vipResp = vipOrderService.orderSuccess(orderNo,params,appId,payTypeEnum);
                if(vipResp.isSuccess()){
                    if(appId != null){
                        payTotalService.incsPrice(payTypeEnum,appId,vipResp.getPrice());
                    }
                }
                break;
            case RECHARGE_ORDER_SUB:
                RechargeConsumerResp resp = consumerManager.rechargeOrderSuccess(orderNo,params,appId,payTypeEnum);
                if(resp.isSuccess()){
                    // 领取首充奖励
                    awardManager.giveFastPayAsync(resp.getUserId(), resp.getTraceId());
                    if(appId != null){
                        payTotalService.incsPrice(payTypeEnum,appId,resp.getPrice());
                    }
                }
                break;
            default:
                break;
        }
    }
}
