package com.hai.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.config.PayloService;
import com.hai.config.ShellGroupService;
import com.hai.entity.*;
import com.hai.enum_type.GasChannel;
import com.hai.enum_type.GasChannelPayPlatformType;
import com.hai.enum_type.MerStoreAmountSourceTypeEnum;
import com.hai.enum_type.MerStoreAmountTypeEnum;
import com.hai.msg.entity.MsgTopic;
import com.hai.order.service.OrderCancelHandleService;
import com.hai.order.service.OrderRefundHandleService;
import com.hai.order.type.OrderOilStatus;
import com.hai.service.HighGasOrderService;
import com.hai.service.HighMerchantStoreService;
import com.hai.service.HighOrderService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

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

/**
 * @className: OrderRefundHandleServiceImpl
 * @author: HuRui
 * @date: 2022/9/23
 **/
@Service("orderRefundHandleService")
public class OrderRefundHandleServiceImpl implements OrderRefundHandleService {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private HighMerchantStoreService storeService;
    @Resource
    private HighGasOrderService gasOrderService;

    @Resource
    private ShellGroupService shellGroupService;
    @Resource
    private PayloService payloService;

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private OrderCancelHandleService orderCancelHandleService;


    @Override
    public void couponHandle(HighOrderRefund orderRefund) {

    }

    @Override
    public void oilHandle(HighOrderRefund orderRefund) {
        // 查询加油订单
        HighGasOrder gasOrder = gasOrderService.getDetailByChildOrderNo(orderRefund.getOrderChildNo());
        if (gasOrder == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到加油订单");
        }
        if (!gasOrder.getStatus().equals(OrderOilStatus.STATUS2.getNumber())) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "加油订单不处于已支付状态");
        }
        gasOrder.setRefundTime(orderRefund.getRefundTime());
        gasOrder.setStatus(OrderOilStatus.STATUS4.getNumber());
        gasOrderService.updateGasOrder(gasOrder);

        try {
            // 油站渠道，根据不同的渠道，请求不同的退款业务接口。
            // 团油     退款需要申请。不是实时退款
            // 加好油   实时退款，退款成功就可以退款给用户
            // 金猪加油  没有线上退款接口，线下核实后直接退款给用户。
            if (gasOrder.getChannelType().equals(GasChannel.type1.getType())) {
                // 查询门店信息
                HighMerchantStore store = storeService.getDetailById(gasOrder.getId());
                if (store == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到加油站");
                }
                // 预存类型  0：非预存  1：预存门店
                if (store.getPrestoreType().equals(1)) {
                    Map<String, Object> pushParam = new HashMap<>();
                    pushParam.put("businessType", MerStoreAmountTypeEnum.type1.getType());
                    pushParam.put("storeId", store.getId());
                    pushParam.put("price", gasOrder.getGasRefuelPrice());
                    pushParam.put("sourceType", MerStoreAmountSourceTypeEnum.type3.getType());
                    pushParam.put("sourceId", gasOrder.getOrderNo());
                    pushParam.put("sourceContent", "订单号：" + gasOrder.getOrderNo() + ",退款金额：￥" + gasOrder.getGasRefuelPrice());
                    pushParam.put("opUserId", orderRefund.getOpUserId());
                    pushParam.put("opUserName", orderRefund.getOpUserName());
                    // 退款到预存余额
                    this.redisTemplate.boundListOps(MsgTopic.MerStoreAccount.getName()).leftPush(pushParam);
                }

            } else if (gasOrder.getChannelType().equals(GasChannel.type2.getType())) {

            } else if (gasOrder.getChannelType().equals(GasChannel.type3.getType())) {
                JSONObject object = shellGroupService.gasSyncRefund(gasOrder.getPayTime(), gasOrder.getOrderNo());
                if (!object.getString("status").equals("SUCCESS")) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "退款失败，原因：" + object.getString("message"));
                }
            } else if (gasOrder.getChannelType().equals(GasChannel.type4.getType())) {


            } else if (gasOrder.getChannelType().equals(GasChannel.type5.getType())) {
                JSONObject object = payloService.syncYcShellRefundNotify(gasOrder.getPayTime(), gasOrder.getOrderNo());
                if (!object.getString("status").equals("SUCCESS")) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "退款失败，原因：" + object.getString("message"));
                }
            }
        } catch (Exception e) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, e);
        }

    }


    @Override
    public void goodsHandle(HighOrderRefund orderRefund) {

        HighOrder order = highOrderService.getOrderByOrderNo(orderRefund.getOrderNo());

        List<HighChildOrder> childOrderList = order.getHighChildOrderList();

        for (HighChildOrder childOrder : childOrderList) {
            orderCancelHandleService.goodsHandle(childOrder);
        }

    }
}
