package com.hai.config;

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.entity.*;
import com.hai.order.model.CreateOrderChildModel;
import com.hai.service.ApiProductService;
import com.hai.service.BsConfigService;
import com.hai.service.HighCouponService;
import com.hai.service.HighOrderService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author hurui
 * @version 1.0
 * @ClassName ThirdProductConfig
 * @description: 第三方接口配置文件
 * @date 2021/7/5 14:18
 */
@Component
public class ThirdProductConfig {

    @Resource
    private BsConfigService bsConfigService;

    @Resource
    private ApiProductService apiProductService;

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private HighCouponService highCouponService;

    // 产品折扣比例
    public BigDecimal getProductConfigNum(Integer productType , Long companyId) {

        Map<String, Object> listMap = new HashMap<>();
        listMap.put("productType", productType);
        listMap.put("companyId", companyId);

        List<BsProductConfig> productConfigs = apiProductService.getProductConfig(listMap);

        if (productConfigs.size() > 0 ) {
            return productConfigs.get(0).getDiscount();
        }
        return  null;
    }

    // 产品折扣比例
    public BsProductConfig getProductConfig(Integer productType , Long companyId , Long productId) {

        Map<String, Object> listMap = new HashMap<>();
        listMap.put("productType", productType);
        listMap.put("companyId", companyId);
        listMap.put("productId", productId);

        List<BsProductConfig> productConfigs = apiProductService.getProductConfig(listMap);

        if (productConfigs.size() > 0 ) {
            return productConfigs.get(0);
         }
        return  null;
    }

    public JSONObject getThirdPartyByDetail(Integer platformId , Integer productType , Long companyId , Long productId , String orderNo , String payType) {
        BsProductConfig productConfig = getProductConfig(productType, companyId , productId);

        if (productConfig == null && productType != 9) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR,  "暂未配置当前内容");
        }

        JSONObject object = new JSONObject();

        Map<String, Object> listMap = new HashMap<>();
        listMap.put("productType", productType);
        listMap.put("returnType", 1);
        if (productType == 9) {
            if (orderNo != null) {
                HighOrder highOrder  = highOrderService.getOrderByOrderNo(orderNo);
                listMap.put("sourceId", highOrder.getHighChildOrderList().get(0).getGoodsId());
            }

        } else if (productType == 5) {
            if (orderNo != null) {
                HighOrder highOrder  = highOrderService.getOrderByOrderNo(orderNo);

                for (HighChildOrder childOrder : highOrder.getHighChildOrderList()) {
                    if (!childOrder.getGiveawayType()) {
                        HighCoupon coupon = highCouponService.getCouponById(childOrder.getGoodsId());
                        BsProductConfig productConfigCoupon = getProductConfig(productType, companyId , Long.valueOf(coupon.getCouponSource()));
                        listMap.put("sourceId", productConfigCoupon.getId());
                    }
                }

            } else {
                listMap.put("sourceId", productConfig.getId());
            }
        }else {
            listMap.put("sourceId", productConfig.getId());
        }

        listMap.put("platformId", platformId);
        if (bsConfigService.getProductDiscountByMap(listMap) != null) {
            object.put("integralDiscount" , bsConfigService.getProductDiscountByMap(listMap).getDiscount());
        }

        if (productConfig != null) {
            object.put("priceDiscount" , productConfig.getDiscount());
        }

        if (payType != null && payType.length() != 0) {
            object.put("productPayType" , payType);
        } else {
            object.put("productPayType" , bsConfigService.getProductPayTypeByMap(listMap));
        }
        object.put("productPayTypeNew" , bsConfigService.getProductPayTypeNewByMap(listMap));
        object.put("productPlatform" , bsConfigService.getProductPlatformByMap(listMap));

        return object;
    }

    // 组装星巴克 肯德基 第三方会员充值 话费 订单组装
    public JSONObject assembleOrder(HighOrder order , CreateOrderChildModel child) {
        /*********** 组装肯德基，星巴克，第三方会员充值 请求数据************/
        JSONObject object = new JSONObject();
        object.put("customerMobile" , child.getCustomerMobile());
        object.put("productType" , child.getProductType());
        object.put("platformId" , child.getPlatformId());
        object.put("eatType" , child.getEatType());
        object.put("storeCode" , child.getStoreCode());
        object.put("companyId" , order.getCompanyId());
        object.put("userId" , child.getMemId());
        object.put("orderItems" , child.getOrderItems());

        /*********** 话费 请求数据************/
        object.put("rechargeContent" , child.getRechargeContent());
        object.put("goodsId" , child.getGoodsId());

        return object;
    }

}
