package com.ruoyi.cai.pay;

import com.ruoyi.cai.dto.ConsumeResp;
import com.ruoyi.cai.manager.AwardManager;
import com.ruoyi.cai.manager.ConsumerManager;
import com.ruoyi.cai.service.PayTotalService;
import com.ruoyi.cai.service.VipOrderService;
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 AwardManager awardManager;
    @Autowired
    private PayTotalService payTotalService;

    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:
                ConsumeResp 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;
        }
    }
}
