package com.hfkj.service.order;

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.common.utils.RedisUtil;
import com.hfkj.entity.*;
import com.hfkj.model.order.OrderChildModel;
import com.hfkj.model.order.OrderModel;
import com.hfkj.service.agent.BsAgentMerAccountService;
import com.hfkj.service.gas.BsGasOrderService;
import com.hfkj.sysenum.gas.OrderOilStatus;
import com.hfkj.sysenum.merchant.MerchantAccountRecordSourceTypeEnum;
import com.hfkj.sysenum.order.OrderChildProductTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 取消订单业务
 * @className: OrderCancelService
 * @author: HuRui
 * @date: 2024/5/7
 **/
@Component
public class OrderCancelService {
    Logger log = LoggerFactory.getLogger(OrderCancelService.class);
    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private BsOrderService orderService;
    @Resource
    private BsOrderChildService orderChildService;
    @Resource
    private BsGasOrderService gasOrderService;
    @Resource
    private BsAgentMerAccountService agentMerAccountService;
    /**
     * 订单业务处理
     * @param order
     * @throws Exception
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED, isolation = Isolation.READ_UNCOMMITTED)
    public void orderBusHandle(OrderModel order) {
        for (OrderChildModel childOrder : order.getOrderChildList()) {
            if (childOrder.getProductType().equals(OrderChildProductTypeEnum.type1.getCode())) {
                oilHandle(childOrder.getChildOrderNo());
            }
        }
    }

    /**
     * 加油业务
     * @param orderChildNo
     * @return
     */
    public void oilHandle(String orderChildNo) {
        // 查询加油订单
        BsGasOrder gasOrder = gasOrderService.getDetailByOrderChildNo(orderChildNo);
        if (gasOrder != null) {
            gasOrder.setStatus(OrderOilStatus.STATUS3.getNumber());
            gasOrder.setCancelTime(new Date());
            gasOrderService.updateGasOrder(gasOrder);

            if (gasOrder.getAgentId() != null) {
                try {
                    // 是否可以退款
                    boolean isTrade = agentMerAccountService.isTrade(gasOrder.getAgentId(), gasOrder.getMerNo(), gasOrder.getOrderNo());
                    if (isTrade) {
                        // 代理商商户金额
                        Map<String,Object> param = new HashMap<>();
                        param.put("sourceType", MerchantAccountRecordSourceTypeEnum.type4.getType());
                        param.put("sourceOrderNo", gasOrder.getOrderNo());
                        param.put("sourceOrderContent", "取消加油订单："+gasOrder.getOrderNo());
                        agentMerAccountService.orderCancel(gasOrder.getAgentId(), gasOrder.getMerNo(), gasOrder.getGasAgentSettlePrice(), param);
                    }
                } catch (BaseException e) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, e.getErrorMsg());
                } catch (Exception e) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "扣款失败，请稍后重新提交订单");
                }
            }
        }
    }

}
