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

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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.ParameterException;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.constant.BussinessConstant;
import com.prolog.rdc.tms.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.constant.StatusConstant;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillDetailMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillHeaderMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillPackageDetailMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillPackageMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillTransportLogMapper;
import com.prolog.rdc.tms.model.dto.ConsignmentHandoverPageDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.SignconfigPopDTO;
import com.prolog.rdc.tms.model.entity.SysRefdatadModel;
import com.prolog.rdc.tms.model.vo.ConsignmentHandoverPageVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SignconfigPopVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.service.ConsignmentHandoverService;
import com.prolog.rdc.tms.service.ShipbillNodeTrackingService;
import com.prolog.rdc.tms.service.ShipbillOperateLogService;
import com.prolog.rdc.tms.util.ConvertorUtil;
import com.prolog.rdc.tms.util.PublicMethods;

import lombok.extern.slf4j.Slf4j;

/**
 * 订单管理实现层
 *
 * @author :zhangsj
 * @version :1.0.0
 * @className :ShipbillServiceImpl
 * @creationTime :2022/03/31
 * @description :
 * @modifier :zhangsj
 * @modifyTime :2022/03/31
 * @reviseRemarks:
 */
@Slf4j
@Service
public class ConsignmentHandoverServiceImpl implements ConsignmentHandoverService {

	@Autowired
	private TmsShipbillHeaderMapper tmsShipbillHeaderMapper;

	@Autowired
	private MaindataFeign maindataTmsResource;

	@Autowired
	private TmsShipbillDetailMapper tmsShipbillDetailMapper;

	@Autowired
	private ShipbillNodeTrackingService shipbillNodeTrackingService;

	@Autowired
	private ShipbillOperateLogService shipbillOperateLogService;

	@Autowired
	private TmsShipbillTransportLogMapper tmsShipbillTransportLogMapper;

	@Autowired
	private TmsShipbillPackageMapper tmsShipbillPackageMapper;

	@Autowired
	private TmsShipbillPackageDetailMapper tmsShipbillPackageDetailMapper;

	@Autowired
	private ConvertorUtil convertorUtil;

	/**
	 * 托运交接（运单）查询
	 */
	@Override
	public Page<ConsignmentHandoverPageDTO> pageConsignmentHandoverShipbillData(ConsignmentHandoverPageVO consignmentHandoverPageVO) {
		ToolKits.isParamNull(consignmentHandoverPageVO, "请求参数不为空！");
		ToolKits.isParamNull(consignmentHandoverPageVO.getPageNum(), "当前页码不为空");
		ToolKits.isParamNull(consignmentHandoverPageVO.getPageSize(), "每页条数不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有权限");
		ToolKits.isParamNull(user.getDepts(), "当前人员没有部门权限");

		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new ParameterException("当前人员没有操作权限");
		}

		consignmentHandoverPageVO.setAllData(user.getOwnedAllDataAuthFlag());
		consignmentHandoverPageVO.setDepartmentIds(user.getDepts().stream().toArray(String[]::new));
		if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_N)) {
			consignmentHandoverPageVO.setBranchIds(user.getWarehouses());
			consignmentHandoverPageVO.setCargoOwnerIds(user.getConsignors());
			consignmentHandoverPageVO.setCustomerIds(user.getCustomers());
			consignmentHandoverPageVO.setProviderIds(user.getSuppliers());
		}

		// 数据字典翻译数据集合
		List<SignconfigPopDTO> data9 = Lists.newArrayList();
		// 标记配置
		SignconfigPopVO signconfigPopVO = new SignconfigPopVO();
		RestMessage<List<SignconfigPopDTO>> restMessage9 = maindataTmsResource.getSignConfig(signconfigPopVO);
		if (!restMessage9.isSuccess()) {
			throw new RuntimeException("标记配置查询失败!");
		} else {
			data9 = restMessage9.getData();
		}

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] {
				DataDictionaryConstant.DELIVERY_METHOD_TYPE, // 配送方式
				DataDictionaryConstant.TRANSPORT_TYPE, // 运输类型(运输模式)
				DataDictionaryConstant.TEMPERATURE_REQUIREMENT, // 温度要求
				DataDictionaryConstant.SHIPPER_TYPE, // 发货方类型
				DataDictionaryConstant.CONSIGNEE_TYPE, // 收货方类型
				DataDictionaryConstant.ORDER_TYPE, // 订单类型
				DataDictionaryConstant.SHIPBILL_STATUS, // 运单状态
				DataDictionaryConstant.LINE_DIRECTION, // 线路方向
				DataDictionaryConstant.ORDER_BUSINESS_TYPE, // 订单业务类型
				DataDictionaryConstant.CONSIGNMENT_HANDOVER_STATUS
		});

		PageUtils.startPage(consignmentHandoverPageVO.getPageNum(), consignmentHandoverPageVO.getPageSize());
		List<ConsignmentHandoverPageDTO> consignmentHandoverPageDTOs = tmsShipbillHeaderMapper.pageConsignmentHandoverShipbillSql(consignmentHandoverPageVO);

		for (ConsignmentHandoverPageDTO consignmentHandoverPageDTO : consignmentHandoverPageDTOs) {
			// 收货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(consignmentHandoverPageDTO.getConsigneeLongitude()) && ToolKits.isNotEmpty(consignmentHandoverPageDTO.getConsigneeLatitude())) {
				consignmentHandoverPageDTO.setConsigneePositioning(StatusConstant.POSITIONING_YDW);
			} else {
				consignmentHandoverPageDTO.setConsigneePositioning(StatusConstant.POSITIONING_WDW);
			}

			// 发货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(consignmentHandoverPageDTO.getShipperLongitude()) && ToolKits.isNotEmpty(consignmentHandoverPageDTO.getShipperLatitude())) {
				consignmentHandoverPageDTO.setShipperPositioning(StatusConstant.POSITIONING_YDW);
			} else {
				consignmentHandoverPageDTO.setShipperPositioning(StatusConstant.POSITIONING_WDW);
			}

			// 类型翻译
			consignmentHandoverPageDTO.setOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_TYPE, String.valueOf(consignmentHandoverPageDTO.getOrderType())));
			consignmentHandoverPageDTO.setOrderBusinessTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_BUSINESS_TYPE, String.valueOf(consignmentHandoverPageDTO.getOrderBusinessType())));
			consignmentHandoverPageDTO.setDeliveryMethodTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.DELIVERY_METHOD_TYPE, String.valueOf(consignmentHandoverPageDTO.getDeliveryMethodType())));
			consignmentHandoverPageDTO.setTransportTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_TYPE, String.valueOf(consignmentHandoverPageDTO.getTransportType())));
			consignmentHandoverPageDTO.setTemperatureRequirementName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TEMPERATURE_REQUIREMENT, String.valueOf(consignmentHandoverPageDTO.getTemperatureRequirement())));
			consignmentHandoverPageDTO.setShipperTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPPER_TYPE, String.valueOf(consignmentHandoverPageDTO.getShipperType())));
			consignmentHandoverPageDTO.setConsigneeTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONSIGNEE_TYPE, String.valueOf(consignmentHandoverPageDTO.getConsigneeType())));
			consignmentHandoverPageDTO.setLineDirectionName(ConvertorUtil.getName(dicData, DataDictionaryConstant.LINE_DIRECTION, String.valueOf(consignmentHandoverPageDTO.getLineDirection())));
			consignmentHandoverPageDTO.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPBILL_STATUS, String.valueOf(consignmentHandoverPageDTO.getStatus())));
			consignmentHandoverPageDTO.setConsignmentHandoverStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONSIGNMENT_HANDOVER_STATUS, String.valueOf(consignmentHandoverPageDTO.getConsignmentHandoverStatus())));

			// 获取背景颜色和字体颜色
			Signconfig: for (SignconfigPopDTO signconfigPopDTO : data9) {
				// 标记类型匹配
				switch (signconfigPopDTO.getType()) {
				case DataDictionaryConstant.SIGNCONFIG_TYPE_DDLX: // 订单类型
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						// 判断是否匹配
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(consignmentHandoverPageDTO.getOrderTypeName()) ? consignmentHandoverPageDTO.getOrderTypeName() : "")) {
							consignmentHandoverPageDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							consignmentHandoverPageDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							// 返回前端所需标记ID
							consignmentHandoverPageDTO.setSignConfigId(signconfigPopDTO.getId());

							// 匹配上结束循环
							break Signconfig;
						}
					}
					break;
				case DataDictionaryConstant.SIGNCONFIG_TYPE_YWLX: // 业务类型
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(consignmentHandoverPageDTO.getOrderBusinessTypeName()) ? consignmentHandoverPageDTO.getOrderBusinessTypeName() : "")) {
							consignmentHandoverPageDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							consignmentHandoverPageDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							// 返回前端所需标记ID
							consignmentHandoverPageDTO.setSignConfigId(signconfigPopDTO.getId());

							break Signconfig;
						}
					}
					break;
				case DataDictionaryConstant.SIGNCONFIG_TYPE_WDYQ: // 配送方式
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(consignmentHandoverPageDTO.getTemperatureRequirementName()) ? consignmentHandoverPageDTO.getTemperatureRequirementName() : "")) {
							consignmentHandoverPageDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							consignmentHandoverPageDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							// 返回前端所需标记ID
							consignmentHandoverPageDTO.setSignConfigId(signconfigPopDTO.getId());

							break Signconfig;
						}
					}
					break;
				case DataDictionaryConstant.SIGNCONFIG_TYPE_PSFS: // 订单备注
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(consignmentHandoverPageDTO.getDeliveryMethodTypeName()) ? consignmentHandoverPageDTO.getDeliveryMethodTypeName() : "")) {
							consignmentHandoverPageDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							consignmentHandoverPageDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							// 返回前端所需标记ID
							consignmentHandoverPageDTO.setSignConfigId(signconfigPopDTO.getId());

							break Signconfig;
						}
					}
					break;
				case DataDictionaryConstant.SIGNCONFIG_TYPE_DDBZ:
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(consignmentHandoverPageDTO.getMemo()) ? consignmentHandoverPageDTO.getMemo() : "")) {
							consignmentHandoverPageDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							consignmentHandoverPageDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							// 返回前端所需标记ID
							consignmentHandoverPageDTO.setSignConfigId(signconfigPopDTO.getId());

							break Signconfig;
						}
					}
					break;
				}
			}
		}

		return PageUtils.getPage(consignmentHandoverPageDTOs);
	}


}
