package com.prolog.rdc.tms.service.basicData.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.esotericsoftware.minlog.Log;
import com.prolog.rdc.tms.model.vo.ShipbillLocationVO;
import com.prolog.rdc.tms.rmi.feign.BusinessComingRecordFeign;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.google.common.collect.Lists;
import com.prolog.common.util.ToolKits;
import com.prolog.rdc.tms.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.dao.mapper.TmsBaseBranchMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseShipperMapper;
import com.prolog.rdc.tms.dao.mapper.TmsCustomerConsigneeMapper;
import com.prolog.rdc.tms.model.dto.basicData.LocationByIdDTO;
import com.prolog.rdc.tms.model.entity.line.TmsBaseBranchModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseShipperModel;
import com.prolog.rdc.tms.model.entity.line.TmsCustomerConsignee;
import com.prolog.rdc.tms.model.vo.basicData.LocationByIdVO;
import com.prolog.rdc.tms.service.basicData.MaindataCommonService;

/**
 * 运单公共处理实现层
 *
 * @author :fanzw
 * @version :1.0.0
 * @className :ShipbillCommonServiceImpl
 * @creationTime :2022/09/08
 * @description :
 * @modifier :fanzw
 * @modifyTime :2022/09/08
 * @reviseRemarks:
 */
@Service
public class MaindataCommonServiceImpl implements MaindataCommonService {

    @Autowired
    private TmsBaseBranchMapper tmsBaseBranchMapper;

    @Autowired
    private TmsCustomerConsigneeMapper tmsCustomerConsigneeMapper;

    @Autowired
    private TmsBaseShipperMapper tmsBaseShipperMapper;

    @Autowired
    private BusinessComingRecordFeign businessComingRecordFeign;

    @Override
    public List<LocationByIdDTO> locationByIdData(List<LocationByIdVO> locationByIdVOs) {
        ToolKits.isParamNull(locationByIdVOs, "请求参数不为空");

        // 响应集合
        List<LocationByIdDTO> locationByIdDTOs = Lists.newArrayList();

        for (LocationByIdVO locationByIdVO : locationByIdVOs) {
            ToolKits.isParamNull(locationByIdVO.getShipperType(), "发货方类型不为空");
            ToolKits.isParamNull(locationByIdVO.getShipperId(), "发货方ID不为空");
            ToolKits.isParamNull(locationByIdVO.getConsigneeType(), "收货方类型不为空");
            ToolKits.isParamNull(locationByIdVO.getConsigneeId(), "收货方ID不为空");

            // 响应对象
            LocationByIdDTO locationByIdDTO = new LocationByIdDTO();

            // 发货方处理
            if (locationByIdVO.getShipperType().intValue() == DataDictionaryConstant.SHIPPER_TYPE_KH.intValue()) {
                TmsCustomerConsignee tmsCustomerConsignee = tmsCustomerConsigneeMapper.findById(locationByIdVO.getShipperId(), TmsCustomerConsignee.class);
                Assert.isTrue(ToolKits.isNotEmpty(tmsCustomerConsignee), "发货方【" + locationByIdVO.getShipperId() + "】ID不存在");

                locationByIdDTO.setShipperType(locationByIdVO.getShipperType());
                locationByIdDTO.setShipperId(locationByIdVO.getShipperId());
                locationByIdDTO.setShipperLongitude(tmsCustomerConsignee.getLongitude());
                locationByIdDTO.setShipperLatitude(tmsCustomerConsignee.getLatitude());
                locationByIdDTO.setShipperAddress(tmsCustomerConsignee.getAddress());
            } else if (locationByIdVO.getShipperType().intValue() == DataDictionaryConstant.SHIPPER_TYPE_CK.intValue()) {
                TmsBaseBranchModel tmsBaseBranchModel = tmsBaseBranchMapper.findById(locationByIdVO.getShipperId(), TmsBaseBranchModel.class);
                Assert.isTrue(ToolKits.isNotEmpty(tmsBaseBranchModel), "发货方【" + locationByIdVO.getShipperId() + "】ID不存在");

                locationByIdDTO.setShipperType(locationByIdVO.getShipperType());
                locationByIdDTO.setShipperId(locationByIdVO.getShipperId());
                locationByIdDTO.setShipperLongitude(tmsBaseBranchModel.getLongitude());
                locationByIdDTO.setShipperLatitude(tmsBaseBranchModel.getLatitude());
                locationByIdDTO.setShipperAddress(tmsBaseBranchModel.getAddress());
            } else {
                TmsBaseShipperModel tmsBaseShipperModel = tmsBaseShipperMapper.findById(locationByIdVO.getShipperId(), TmsBaseShipperModel.class);
                Assert.isTrue(ToolKits.isNotEmpty(tmsBaseShipperModel), "发货方【" + locationByIdVO.getShipperId() + "】ID不存在");

                locationByIdDTO.setShipperType(locationByIdVO.getShipperType());
                locationByIdDTO.setShipperId(locationByIdVO.getShipperId());
                locationByIdDTO.setShipperLongitude(tmsBaseShipperModel.getLongitude());
                locationByIdDTO.setShipperLatitude(tmsBaseShipperModel.getLongitude());
                locationByIdDTO.setShipperAddress(tmsBaseShipperModel.getAddress());
            }

            // 收货方处理
            if (locationByIdVO.getConsigneeType().intValue() == DataDictionaryConstant.CONSIGNEE_TYPE_KH.intValue()) {
                TmsCustomerConsignee tmsCustomerConsignee = tmsCustomerConsigneeMapper.findById(locationByIdVO.getConsigneeId(), TmsCustomerConsignee.class);
                Assert.isTrue(ToolKits.isNotEmpty(tmsCustomerConsignee), "收货方【" + locationByIdVO.getConsigneeId() + "】ID不存在");

                locationByIdDTO.setConsigneeType(locationByIdVO.getConsigneeType());
                locationByIdDTO.setConsigneeId(locationByIdVO.getConsigneeId());
                locationByIdDTO.setConsigneeLongitude(tmsCustomerConsignee.getLongitude());
                locationByIdDTO.setConsigneeLatitude(tmsCustomerConsignee.getLatitude());
                locationByIdDTO.setConsigneeAddress(tmsCustomerConsignee.getAddress());
            } else if (locationByIdVO.getConsigneeType().intValue() == DataDictionaryConstant.CONSIGNEE_TYPE_CK.intValue()) {
                TmsBaseBranchModel tmsBaseBranchModel = tmsBaseBranchMapper.findById(locationByIdVO.getConsigneeId(), TmsBaseBranchModel.class);
                Assert.isTrue(ToolKits.isNotEmpty(tmsBaseBranchModel), "收货方【" + locationByIdVO.getConsigneeId() + "】ID不存在");

                locationByIdDTO.setConsigneeType(locationByIdVO.getConsigneeType());
                locationByIdDTO.setConsigneeId(locationByIdVO.getConsigneeId());
                locationByIdDTO.setConsigneeLongitude(tmsBaseBranchModel.getLongitude());
                locationByIdDTO.setConsigneeLatitude(tmsBaseBranchModel.getLatitude());
                locationByIdDTO.setConsigneeAddress(tmsBaseBranchModel.getAddress());
            } else {
                TmsBaseShipperModel tmsBaseShipperModel = tmsBaseShipperMapper.findById(locationByIdVO.getConsigneeId(), TmsBaseShipperModel.class);
                Assert.isTrue(ToolKits.isNotEmpty(tmsBaseShipperModel), "收货方【" + locationByIdVO.getConsigneeId() + "】ID不存在");

                locationByIdDTO.setConsigneeType(locationByIdVO.getConsigneeType());
                locationByIdDTO.setConsigneeId(locationByIdVO.getConsigneeId());
                locationByIdDTO.setConsigneeLongitude(tmsBaseShipperModel.getLongitude());
                locationByIdDTO.setConsigneeLatitude(tmsBaseShipperModel.getLongitude());
                locationByIdDTO.setConsigneeAddress(tmsBaseShipperModel.getAddress());
            }
            locationByIdDTO.setShipbillId(locationByIdVO.getShipbillId());
            locationByIdDTOs.add(locationByIdDTO);
        }
        if (ToolKits.isNotEmpty(locationByIdDTOs)) {
            List<ShipbillLocationVO> shipbillLocationVOS = Lists.newArrayList();
            for (LocationByIdDTO locationDTO : locationByIdDTOs) {
                ShipbillLocationVO vo = new ShipbillLocationVO();
                BeanUtils.copyProperties(locationDTO, vo);
                shipbillLocationVOS.add(vo);
            }
			Log.info("调用更新运单定位信息服务={}", JSON.toJSONString(shipbillLocationVOS));
            businessComingRecordFeign.modifyShipbillHeaderLocationExecute(shipbillLocationVOS);
        }
        return locationByIdDTOs;
    }

}
