package com.prolog.rdc.tms.util;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.rdc.tms.model.dto.basicConfigure.ShipbillMatchLineDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.StrategyBusinessMatchingDTO;
import com.prolog.rdc.tms.model.entity.TmsShipbillDetailModel;
import com.prolog.rdc.tms.model.entity.TmsShipbillHeaderModel;
import com.prolog.rdc.tms.model.vo.basicConfigure.ShipbillMatchLineVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.StrategyBusinessItemVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.StrategyBusinessMatchingVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class StrategyUtil {

    @Autowired
    private MaindataFeign maindataTmsResource;

    /**
     * 运单匹配业务规则
     *
     * @param model
     * @return
     */
    public StrategyBusinessMatchingDTO shipbillMatchBusinessRule(TmsShipbillHeaderModel model, List<TmsShipbillDetailModel> detailModels) {
        // 新增后自动提交判定
        StrategyBusinessMatchingVO matchBusinessVO = new StrategyBusinessMatchingVO();
        // 配送方式
        matchBusinessVO.setDeliveryMethodType(model.getDeliveryMethodType());
        // 发货方类型
        matchBusinessVO.setShipperType(model.getShipperType());
        // 发货方单位ID
        matchBusinessVO.setShipperUnitId(model.getShipperUnitId());
        // 收货方类型
        matchBusinessVO.setConsigneeType(model.getConsigneeType());
        // 收货方单位ID
        matchBusinessVO.setConsigneeUnitId(model.getConsigneeUnitId());
        // 订单类型
        matchBusinessVO.setOrderType(model.getOrderType());
        // 订单业务类型
        matchBusinessVO.setOrderBusinessType(model.getOrderBusinessType());
        // 运输模式
        matchBusinessVO.setTransportType(model.getTransportType());
        // 温度要求
        matchBusinessVO.setTemperatureRequirement(model.getTemperatureRequirement());
        // 货主ID
        matchBusinessVO.setCargoOwnerId(model.getCargoOwnerId());
        // 部门
        matchBusinessVO.setDepartmentId(model.getDepartmentId());
        matchBusinessVO.setTotalPcs(model.getTotalPcs());
        matchBusinessVO.setTotalWeight(model.getTotalWeight());

        matchBusinessVO.setShipperProvince(model.getShipperProvince());
        matchBusinessVO.setShipperCity(model.getShipperCity());
        matchBusinessVO.setShipperDistrict(model.getShipperDistrict());

        matchBusinessVO.setConsigneeProvince(model.getConsigneeProvince());
        matchBusinessVO.setConsigneeCity(model.getConsigneeCity());
        matchBusinessVO.setConsigneeDistrict(model.getConsigneeDistrict());


        if (ToolKits.isNotEmpty(detailModels)) {
            List<StrategyBusinessItemVO> businessItemVOS = Lists.newArrayList();
            for (TmsShipbillDetailModel detailModel:detailModels) {
                StrategyBusinessItemVO businessItemVO = new StrategyBusinessItemVO();
                businessItemVO.setItemId(detailModel.getItemId());
                businessItemVO.setItemCategoryId(detailModel.getItemCategoryId());
                businessItemVO.setPcs(detailModel.getPcs());
                businessItemVO.setWeight(detailModel.getWeight());

                businessItemVOS.add(businessItemVO);
            }
            matchBusinessVO.setBusinessItemVOS(businessItemVOS);
        }
        log.info("运单匹配业务规则入参:" + JSON.toJSONString(matchBusinessVO));
        RestMessage<List<StrategyBusinessMatchingDTO>> matchRs = maindataTmsResource.matchingBusinessExecute(matchBusinessVO);
        if (!matchRs.isSuccess()) {
            throw new PrologException("运单【" + model.getShipbillNo() + "】调用maindata服务运单匹配业务规则失败");
        }
        List<StrategyBusinessMatchingDTO> strategyBusinessMatchingDTOS = matchRs.getData();
        log.info("运单匹配到的业务规则:" + JSON.toJSONString(strategyBusinessMatchingDTOS));
        if (ToolKits.isEmpty(strategyBusinessMatchingDTOS)) {
            return null;
        }
        if (strategyBusinessMatchingDTOS.size() > 1) {
            throw new PrologException("运单【" + model.getShipbillNo() + "】匹配到多个业务规则策略");
        }
        return strategyBusinessMatchingDTOS.get(0);
    }

    /**
     * 运单匹配线路
     *
     * @param model
     * @return
     */
    public ShipbillMatchLineDTO shipbillMatchLine(TmsShipbillHeaderModel model) {
        // 新增后自动提交判定
        ShipbillMatchLineVO matchLineVO = new ShipbillMatchLineVO();
        // 配送方式
        matchLineVO.setDeliveryMethodType(model.getDeliveryMethodType());
        // 发货方类型
        matchLineVO.setShipperType(model.getShipperType());
        // 发货方单位ID
        matchLineVO.setShipperUnitId(model.getShipperUnitId());
        matchLineVO.setShipperProvince(model.getShipperProvince());
        matchLineVO.setShipperCity(model.getShipperCity());
        matchLineVO.setShipperDistrict(model.getShipperDistrict());
        // 收货方类型
        matchLineVO.setConsigneeType(model.getConsigneeType());
        // 收货方单位ID
        matchLineVO.setConsigneeUnitId(model.getConsigneeUnitId());
        matchLineVO.setConsigneeId(model.getConsigneeId());
        matchLineVO.setConsigneeProvince(model.getConsigneeProvince());
        matchLineVO.setConsigneeCity(model.getConsigneeCity());
        matchLineVO.setConsigneeDistrict(model.getConsigneeDistrict());
        // 订单类型
        matchLineVO.setOrderType(model.getOrderType());
        // 订单业务类型
        matchLineVO.setOrderBusinessType(model.getOrderBusinessType());
        // 运输模式
        matchLineVO.setTransportType(model.getTransportType());
        // 温度要求
        matchLineVO.setTemperatureRequirement(model.getTemperatureRequirement());
        // 货主ID
        matchLineVO.setCargoOwnerId(model.getCargoOwnerId());
        matchLineVO.setDepartmentId(model.getDepartmentId());
        matchLineVO.setEnterpriseId(model.getEnterpriseId());

        RestMessage<ShipbillMatchLineDTO> matchRs = maindataTmsResource.shipbillMatchLineExecute(matchLineVO);
        if (!matchRs.isSuccess()) {
            throw new PrologException("运单【" + model.getShipbillNo() + "】调用maindata服务运单匹配线路策略失败");
        }
        return matchRs.getData();
    }

    // 根据业务规则,修改运单
    public void updateShipbillHeaderByBusinessRule(TmsShipbillHeaderModel headerModel, List<TmsShipbillDetailModel> detailModels) {
        StrategyBusinessMatchingDTO businessData = this.shipbillMatchBusinessRule(headerModel, detailModels);
        if (ToolKits.isNotEmpty(businessData)) {
            if (ToolKits.isNotEmpty(businessData.getDeliveryMethodSelect()) && businessData.getDeliveryMethodSelect().intValue() == 1) {
                headerModel.setDeliveryMethodType(businessData.getDeliveryMethodType());
            }
            if (ToolKits.isNotEmpty(businessData.getRtnOrderPicSelect()) && businessData.getRtnOrderPicSelect().intValue() == 1) {
                headerModel.setConsigneeReturnOrderPicture(businessData.getRtnOrderPicEnable());
            }
            if (ToolKits.isNotEmpty(businessData.getRtnOrderPaperSelect()) && businessData.getRtnOrderPaperSelect().intValue() == 1) {
                headerModel.setReturnOrderFlag(businessData.getRtnOrderPaperEnable());
            }
            if (ToolKits.isNotEmpty(businessData.getRtnOrderDeadlineSelect()) && businessData.getRtnOrderDeadlineSelect().intValue() == 1) {
                headerModel.setReturnOrderDeadline(businessData.getRtnOrderDeadline());
            }
            if (ToolKits.isNotEmpty(businessData.getDeliveryDirectionSelect()) && businessData.getDeliveryDirectionSelect().intValue() == 1) {
                headerModel.setLineDirection(businessData.getDeliveryDirection());
            }
            if (ToolKits.isNotEmpty(businessData.getRecordTagSelect()) && businessData.getRecordTagSelect().intValue() == 1) {
                headerModel.setRecordTagWord(businessData.getRecordTagWord());
            }
            if (ToolKits.isNotEmpty(businessData.getColorTagSelect()) && businessData.getColorTagSelect().intValue() == 1) {
                headerModel.setColorTagBgdColor(businessData.getColorTagBgdColor());
                headerModel.setColorTagFontColor(businessData.getColorTagFontColor());
            }
        }
    }
}
