package com.hai.order.service.impl;

import com.alibaba.fastjson.JSON;
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.WxOrderConfig;
import com.hai.entity.*;
import com.hai.enum_type.GasChannel;
import com.hai.enum_type.GasChannelPayPlatformType;
import com.hai.enum_type.MerchantStoreSourceType;
import com.hai.model.HighMerchantStoreModel;
import com.hai.order.service.OrderPayBeforeService;
import com.hai.order.type.OrderProductType;
import com.hai.service.HighGasChannelConfigService;
import com.hai.service.HighGasService;
import com.hai.service.HighMerchantStoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @className: payOrderCheckImpl
 * @author: HuRui
 * @date: 2023/1/11
 **/
@Service("orderPayBeforeService")
public class OrderPayBeforeServiceImpl implements OrderPayBeforeService {

    @Resource
    private HighMerchantStoreService merchantStoreService;
    @Resource
    private HighGasService gasService;
    @Resource
    private HighGasChannelConfigService gasChannelConfigService;

    @Override
    public void payOrderCheck(HighOrder order) {
        if (order.getProductType().equals(OrderProductType.PRODUCT_TYPE6.getNumber())) {
            // 查询油站
            HighMerchantStore store = merchantStoreService.getDetailById(order.getHighChildOrderList().get(0).getGoodsId());
            if (store == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的加油站");
            }
            if (store.getPrestoreType().equals(1)) {
                if (order.getTotalPrice().compareTo(gasService.getMerPriceByMerId(store.getMerchantId())) == 1) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "暂时无法进行支付，请联系平台客服");
                }
            }

            if (store.getSourceType().equals(MerchantStoreSourceType.type1.getNumber())) {
                // 查询支付配置
                HighGasChannelConfig gasChannelConfig = gasChannelConfigService.getConfig(GasChannel.type1, GasChannelPayPlatformType.type1 );
                if (gasChannelConfig == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未配置支付参数，请稍后重试！");
                }

            } else if (store.getSourceType().equals(MerchantStoreSourceType.type2.getNumber())) {
                // 查询支付配置
                HighGasChannelConfig gasChannelConfig = gasChannelConfigService.getConfig(GasChannel.type2,GasChannelPayPlatformType.type1 );
                if (gasChannelConfig == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未配置支付参数，请稍后重试！");
                }

            } else if (store.getSourceType().equals(MerchantStoreSourceType.type3.getNumber())) {
                // 查询支付配置
                HighGasChannelConfig gasChannelConfig = gasChannelConfigService.getConfig(GasChannel.type3,GasChannelPayPlatformType.type1 );
                if (gasChannelConfig == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未配置支付参数，请稍后重试！");
                }

            } else if (store.getSourceType().equals(MerchantStoreSourceType.type4.getNumber())) {
                // 查询支付配置
                HighGasChannelConfig gasChannelConfig = gasChannelConfigService.getConfig(GasChannel.type4,GasChannelPayPlatformType.type1 );
                if (gasChannelConfig == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未配置支付参数，请稍后重试！");
                }

            } else if (store.getSourceType().equals(MerchantStoreSourceType.type5.getNumber())) {
                // 查询支付配置
                HighGasChannelConfig gasChannelConfig = gasChannelConfigService.getConfig(GasChannel.type5,GasChannelPayPlatformType.type1 );
                if (gasChannelConfig == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未配置支付参数，请稍后重试！");
                }
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未配置支付参数，请稍后重试！");
            }

        }
    }
}
