package com.hfkj.service.order;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.channel.gas.newlink.NewLinkRequestService;
import com.hfkj.channel.gas.shell.CqShellPetroleumRequestService;
import com.hfkj.common.exception.BaseException;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.entity.BsGasOrder;
import com.hfkj.entity.BsMerchant;
import com.hfkj.entity.BsOrderRefund;
import com.hfkj.model.order.OrderChildModel;
import com.hfkj.service.agent.BsAgentMerAccountService;
import com.hfkj.service.gas.BsGasOrderService;
import com.hfkj.service.merchant.BsMerchantAccountService;
import com.hfkj.service.merchant.BsMerchantChainBrandAccountService;
import com.hfkj.service.merchant.BsMerchantService;
import com.hfkj.sysenum.merchant.MerchantChainBrandSettleTypeEnum;
import com.hfkj.sysenum.merchant.MerchantSourceTypeEnum;
import com.hfkj.sysenum.gas.OrderOilStatus;
import com.hfkj.sysenum.order.OrderChildProductTypeEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 退款业务
 * @className: OrderRefundService
 * @author: HuRui
 * @date: 2024/6/24
 **/
@Component
public class OrderRefundBusiness {
    @Resource
    private BsOrderChildService orderChildService;
    @Resource
    private BsGasOrderService gasOrderService;
    @Resource
    private BsMerchantService merchantService;
    @Resource
    private BsMerchantAccountService merchantAccountService;
    @Resource
    private BsMerchantChainBrandAccountService merchantChainBrandAccountService;
    @Resource
    private BsAgentMerAccountService agentMerAccountService;

    /**
     * 前置业务处理
     * @param refund
     */
    public BsOrderRefund prepose(BsOrderRefund refund) {
        // 查询商品订单
        OrderChildModel orderChild = orderChildService.getDetail(refund.getOrderChildNo());
        if (orderChild.getProductType().equals(OrderChildProductTypeEnum.type1.getCode())) {
            // 查询加油订单
            BsGasOrder gasOrder = gasOrderService.getDetailByOrderNo(refund.getOrderNo());
            if (gasOrder != null) {
                if (gasOrder.getChannelType().equals(MerchantSourceTypeEnum.type2.getNumber())) {
                    try {
                        // 请求团油渠道申请退款
                        JSONObject object = NewLinkRequestService.refuelingOrderRefund(gasOrder.getUserPhone(), gasOrder.getOrderNo(), refund.getRefundRemark());
                        if (object == null || !object.getString("code").equals("200")) {
                            gasOrder.setAbnormal(true);
                            gasOrder.setAbnormalContent("渠道申请退款失败！原因：" + object.getString("message"));
                            gasOrderService.updateGasOrder(gasOrder);
                        } else {
                            gasOrder.setStatus(OrderOilStatus.STATUS5.getNumber());
                            gasOrderService.updateGasOrder(gasOrder);
                        }
                    } catch (Exception e) {
                        gasOrder.setAbnormal(true);
                        gasOrder.setAbnormalContent("渠道申请退款失败！原因：未知原因");
                        gasOrderService.updateGasOrder(gasOrder);
                    }
                }
            }
        }
        return refund;
    }

    /**
     * 加油订单业务
     * @param refund
     */
    public void gasOrder(BsOrderRefund refund) {
        // 查询加油订单
        BsGasOrder gasOrder = gasOrderService.getDetailByOrderNo(refund.getOrderNo());
        if (gasOrder != null) {
            gasOrder.setStatus(OrderOilStatus.STATUS4.getNumber());
            gasOrder.setRefundTime(refund.getFinishTime());
            gasOrder.setRefundRemarks(refund.getRefundRemark());
            gasOrderService.updateGasOrder(gasOrder);

            if (gasOrder.getAgentId() != null && MerchantSourceTypeEnum.type1.getNumber().equals(gasOrder.getChannelType())) {
                try {
                    // 代理商商户账户退款
                    agentMerAccountService.refund(gasOrder.getAgentId(), gasOrder.getMerNo(), gasOrder.getGasRefuelPrice(), refund);

                } catch (BaseException e) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, e.getErrorMsg());
                } catch (Exception e) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "扣款失败，请稍后重新提交订单");
                }
            }
            try {
                // 订单是否异常 false：无异常  true：有异常
                if (gasOrder.getAbnormal().equals(false)) {
                    if (gasOrder.getChannelType().equals(MerchantSourceTypeEnum.type1.getNumber())) {
                        // 连锁品牌
                        if (gasOrder.getMerChainBrandId() != null) {
                            // 查询商户
                            BsMerchant merchant = merchantService.getMerchant(gasOrder.getMerNo());
                            if (MerchantChainBrandSettleTypeEnum.type1.getNumber().equals(merchant.getChainBrandSettleType())) {
                                // 退回连锁额度
                                merchantChainBrandAccountService.refund(gasOrder.getMerChainBrandId(), merchant.getMerNo(), gasOrder.getGasRefuelPrice(), refund);
                            } else {
                                // 退回商户额度
                                merchantAccountService.refund(gasOrder.getMerNo(), gasOrder.getGasRefuelPrice(), refund);
                            }
                        } else {
                            // 退回商户额度
                            merchantAccountService.refund(gasOrder.getMerNo(), gasOrder.getGasRefuelPrice(), refund);
                        }
                    } else if (gasOrder.getChannelType().equals(MerchantSourceTypeEnum.type2.getNumber())) {
                        // 请求团油渠道退款
                        JSONObject object = NewLinkRequestService.refuelingOrderRefund(gasOrder.getUserPhone(), gasOrder.getOrderNo(), refund.getRefundRemark());
                        if (object == null || !object.getString("code").equals("200")) {
                            gasOrder.setAbnormal(true);
                            gasOrder.setAbnormalContent("渠道退款失败！原因：" + object.getString("message"));
                            gasOrderService.updateGasOrder(gasOrder);
                        }
                    } else if (gasOrder.getChannelType().equals(MerchantSourceTypeEnum.type3.getNumber())) {
                        try {
                            // 请求重庆壳牌渠道退款
                            CqShellPetroleumRequestService.gasSyncRefund(gasOrder.getPayTime(), gasOrder.getOrderNo());
                        } catch (BaseException e) {
                            gasOrder.setAbnormal(true);
                            gasOrder.setAbnormalContent("渠道退款失败！原因：" + e.getErrorMsg());
                            gasOrderService.updateGasOrder(gasOrder);
                        } catch (Exception e) {
                            gasOrder.setAbnormal(true);
                            gasOrder.setAbnormalContent("渠道退款失败！未知原因");
                            gasOrderService.updateGasOrder(gasOrder);
                        }
                    }
                }
            } catch (Exception e) {}
        }
    }

}
