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

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.google.common.collect.Maps;
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.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Order;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
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.DataDictionaryConstant;
import com.prolog.rdc.tms.dao.mapper.SysExtendedFieldMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseBranchMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseCargoOwnerMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseCarportMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseCarriersMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseCustomerMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseDeliverymanMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseDriverMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseItemCategoryMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseItemMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseProviderMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseTranslineMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseVehicleMapper;
import com.prolog.rdc.tms.dao.mapper.TmsBaseVehicleTypeMapper;
import com.prolog.rdc.tms.dao.mapper.TmsCustomerConsigneeMapper;
import com.prolog.rdc.tms.dao.mapper.TmsTransferStationMapper;
import com.prolog.rdc.tms.model.dto.basicConfigure.SysExtendedFieldDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.SysExtendedFieldListDTO;
import com.prolog.rdc.tms.model.entity.SysExtendedFieldModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseBranchModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseCargoOwnerModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseCarportModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseCarriersModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseCustomerModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseDeliverymanModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseDriverModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseItemCategoryModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseItemModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseLineModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseProviderModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseVehicleModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseVehicleTypeModel;
import com.prolog.rdc.tms.model.entity.line.TmsCustomerConsignee;
import com.prolog.rdc.tms.model.entity.line.TmsTransferStationModel;
import com.prolog.rdc.tms.model.vo.ClearFieldByExtendedObjectVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SysExtendedFieldDeleteVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SysExtendedFieldListVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SysExtendedFieldQueryVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SysExtendedFieldSaveVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SysExtendedFieldUpdataVO;
import com.prolog.rdc.tms.rmi.feign.BusinessComingRecordFeign;
import com.prolog.rdc.tms.service.basicConfigure.SysExtendedFieldService;
import com.prolog.rdc.tms.util.PublicMethods;

import cn.hutool.core.date.DateUtil;

/**
 * 
 *
 * 基础配置-扩展字段实现层
 *
 * @className :SysExtendedFieldServiceImpl
 * @author :fanzw
 * @creationTime :2022/01/12
 * @description :
 * @modifier :fanzw
 * @modifyTime :2022/01/12
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Service
public class SysExtendedFieldServiceImpl implements SysExtendedFieldService {

	@Autowired
	private SysExtendedFieldMapper sysExtendedFieldMapper;

	@Autowired
	private TmsBaseItemCategoryMapper tmsBaseItemCategoryMapper;

	@Autowired
	private TmsBaseItemMapper tmsBaseItemMapper;

	@Autowired
	private TmsBaseCargoOwnerMapper tmsBaseCargoOwnerMapper;

	@Autowired
	private TmsBaseCustomerMapper tmsBaseCustomerMapper;

	@Autowired
	private TmsCustomerConsigneeMapper tmsCustomerConsigneeMapper;

	@Autowired
	private TmsBaseBranchMapper tmsBaseBranchMapper;

	@Autowired
	private TmsBaseCarportMapper tmsBaseCarportMapper;

	@Autowired
	private TmsBaseCarriersMapper tmsBaseCarriersMapper;

	@Autowired
	private TmsBaseTranslineMapper tmsBaseTranslineMapper;

	@Autowired
	private TmsTransferStationMapper tmsTransferStationMapper;

	@Autowired
	private TmsBaseDriverMapper tmsBaseDriverMapper;

	@Autowired
	private TmsBaseDeliverymanMapper tmsBaseDeliverymanMapper;

	@Autowired
	private TmsBaseVehicleTypeMapper tmsBaseVehicleTypeMapper;

	@Autowired
	private TmsBaseVehicleMapper tmsBaseVehicleMapper;

	@Autowired
	private TmsBaseProviderMapper tmsBaseProviderMapper;

	@Autowired
	private BusinessComingRecordFeign businessComingRecordFeign;

	/**
	 * 扩展字段分页查询
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<SysExtendedFieldDTO> pageExtendedFieldData(SysExtendedFieldQueryVO sysExtendedFieldQueryVO) {
		ToolKits.isParamNull(sysExtendedFieldQueryVO, "扩展字段查询请求不为空");
		ToolKits.isParamNull(sysExtendedFieldQueryVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(sysExtendedFieldQueryVO.getPageSize(), "页码数量不为空");

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

		// 响应集合
		List<SysExtendedFieldDTO> sysExtendedFieldDTOs = new ArrayList<SysExtendedFieldDTO>();

		// 分页
		PageUtils.startPage(sysExtendedFieldQueryVO.getPageNum(), sysExtendedFieldQueryVO.getPageSize());

		// 扩展字段查询
		Criteria criteria = Criteria.forClass(SysExtendedFieldModel.class);
		Restriction r1 = Restrictions.like("extendedObjectName", "%" + sysExtendedFieldQueryVO.getSearch() + "%");
		Restriction r2 = Restrictions.like("displayName", "%" + sysExtendedFieldQueryVO.getSearch() + "%");
		Restriction r3 = Restrictions.like("fieldName", "%" + sysExtendedFieldQueryVO.getSearch() + "%");
		Restriction r4 = ToolKits.isEmpty(sysExtendedFieldQueryVO.getSearch()) ? null : Restrictions.or(r1, r2, r3);
		Restriction r5 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r4, r5));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		List<SysExtendedFieldModel> sysExtendedFieldModels = sysExtendedFieldMapper.findByCriteria(criteria);
		for (SysExtendedFieldModel sysExtendedFieldModel : sysExtendedFieldModels) {
			SysExtendedFieldDTO sysExtendedFieldDTO = new SysExtendedFieldDTO();
			BeanUtils.copyProperties(sysExtendedFieldModel, sysExtendedFieldDTO);
			sysExtendedFieldDTOs.add(sysExtendedFieldDTO);
		}

		Page<SysExtendedFieldDTO> page = PageUtils.getPage(sysExtendedFieldDTOs);
		if (sysExtendedFieldModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<SysExtendedFieldModel> p0 = (com.github.pagehelper.Page<SysExtendedFieldModel>) sysExtendedFieldModels;
			page.setPageCount(p0.getPages());
			page.setPageNum(p0.getPageNum());
			page.setPageSize(p0.getPageSize());
			page.setTotalCount(p0.getTotal());
			page.setStartRow(p0.getStartRow());
			page.setEndRow(p0.getEndRow());
		}
		return page;
	}

	/**
	 * 根据扩展对象查询扩展字段
	 */
	@Override
	public List<SysExtendedFieldListDTO> listExtendedFieldData(SysExtendedFieldListVO sysExtendedFieldListVO) {
		ToolKits.isParamNull(sysExtendedFieldListVO, "根据扩展对象查询扩展字段请求不为空");
		ToolKits.isParamNull(sysExtendedFieldListVO.getExtendedObjectCode(), "扩展对象编码不为空");

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

		// 响应集合
		List<SysExtendedFieldListDTO> sysExtendedFieldListDTOs = new ArrayList<SysExtendedFieldListDTO>();

		// 扩展字段查询
		Criteria criteria = Criteria.forClass(SysExtendedFieldModel.class);
		Restriction r1 = Restrictions.eq("extendedObjectCode", sysExtendedFieldListVO.getExtendedObjectCode());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		List<SysExtendedFieldModel> sysExtendedFieldModels = sysExtendedFieldMapper.findByCriteria(criteria);
		for (SysExtendedFieldModel sysExtendedFieldModel : sysExtendedFieldModels) {
			SysExtendedFieldListDTO sysExtendedFieldListDTO = new SysExtendedFieldListDTO();
			BeanUtils.copyProperties(sysExtendedFieldModel, sysExtendedFieldListDTO);
			sysExtendedFieldListDTOs.add(sysExtendedFieldListDTO);
		}

		return sysExtendedFieldListDTOs;
	}

	/**
	 * 扩展字段保存
	 */
	@Override
	public void saveExtendedFieldData(SysExtendedFieldSaveVO sysExtendedFieldSaveVO) throws Exception {
		ToolKits.isParamNull(sysExtendedFieldSaveVO, "扩展字段保存请求不为空");
		ToolKits.isParamNull(sysExtendedFieldSaveVO.getExtendedObjectCode(), "扩展对象编码");
		ToolKits.isParamNull(sysExtendedFieldSaveVO.getExtendedObjectName(), "扩展对象名称");
		ToolKits.isParamNull(sysExtendedFieldSaveVO.getDisplayName(), "显示名称不为空");
		ToolKits.isParamNull(sysExtendedFieldSaveVO.getFieldType(), "字段类型不为空");
		ToolKits.isParamNull(sysExtendedFieldSaveVO.getFieldTypeName(), "字段类型名称不为空");
		ToolKits.isParamNull(sysExtendedFieldSaveVO.getStatus(), "状态不为空");

		UserEntity plgfxuser = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(plgfxuser, "当前人员没有操作权限");

		// 当前日期
		Date date = DateUtil.date();

		// 判断同一对象下显示名称是否重复
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("extendedObjectCode", sysExtendedFieldSaveVO.getExtendedObjectCode());
		map.put("displayName", sysExtendedFieldSaveVO.getDisplayName());
		map.put("enterpriseId", plgfxuser.getEnterpriseId());
		PublicMethods.doesItExistByMap(sysExtendedFieldMapper, map, SysExtendedFieldModel.class, "显示名称【" + sysExtendedFieldSaveVO.getDisplayName() + "】已存在");

		// 根据扩展字段类型判断扩展字段是否超过5(包含5)
		Criteria criteria = Criteria.forClass(SysExtendedFieldModel.class);
		Restriction r1 = Restrictions.eq("enterpriseId", plgfxuser.getEnterpriseId());
		Restriction r2 = Restrictions.eq("extendedObjectCode", sysExtendedFieldSaveVO.getExtendedObjectCode());
		Restriction r3 = null;
		if (sysExtendedFieldSaveVO.getFieldType().equals(DataDictionaryConstant.FIELD_TYPE_STRING) || sysExtendedFieldSaveVO.getFieldType().equals(DataDictionaryConstant.FIELD_TYPE_RADIO)) {
			r3 = Restrictions.in("fieldType", new String[] { DataDictionaryConstant.FIELD_TYPE_STRING, DataDictionaryConstant.FIELD_TYPE_RADIO });
		} else {
			r3 = Restrictions.eq("fieldType", sysExtendedFieldSaveVO.getFieldType());
		}
		criteria.setRestriction(Restrictions.and(r1, r2, r3));
		List<SysExtendedFieldModel> sysExtendedFieldModels = sysExtendedFieldMapper.findByCriteria(criteria);

		Assert.isTrue(sysExtendedFieldModels.size() < DataDictionaryConstant.EXTENDED_FIELD_NUMBER, "可配置扩展字段超出数量限制");

		Integer minimum = 1;
		// 获取已配置的扩展字段序号
		List<Integer> list1 = sysExtendedFieldModels.stream().map(sysExtendedFieldModel -> Integer.valueOf(sysExtendedFieldModel.getFieldName().replaceAll("[^\\d]", ""))).distinct().collect(Collectors.toList());

		if (list1.size() > 0) {
			// 获取未配置的扩展字段序号
			List<Integer> list2 = DataDictionaryConstant.EXTENDED_FIELD_SERIAL_NUMBER.stream().filter(list -> !list1.contains(list)).collect(Collectors.toList());

			// 获取未配置的扩展字段序号最小值
			minimum = list2.stream().min(Integer::compare).get();
		}

		SysExtendedFieldModel sysExtendedFieldModel = new SysExtendedFieldModel();
		BeanUtils.copyProperties(sysExtendedFieldSaveVO, sysExtendedFieldModel);
		sysExtendedFieldModel.setLineid(ToolKits.getUUID());
		switch (sysExtendedFieldSaveVO.getFieldType()) {
		case DataDictionaryConstant.FIELD_TYPE_STRING:
			if (minimum == 1) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR1);
			} else if (minimum == 2) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR2);
			} else if (minimum == 3) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR3);
			} else if (minimum == 4) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR4);
			} else {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR5);
			}

			break;
		case DataDictionaryConstant.FIELD_TYPE_NUMBER:
			if (minimum == 1) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_NUM1);
			} else if (minimum == 2) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_NUM2);
			} else if (minimum == 3) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_NUM3);
			} else if (minimum == 4) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_NUM4);
			} else {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_NUM5);
			}

			break;

		case DataDictionaryConstant.FIELD_TYPE_DATE:
			if (minimum == 1) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_DATE1);
			} else if (minimum == 2) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_DATE2);
			} else if (minimum == 3) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_DATE3);
			} else if (minimum == 4) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_DATE4);
			} else {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_DATE5);
			}

			break;

		case DataDictionaryConstant.FIELD_TYPE_RADIO:
			if (minimum == 1) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR1);
			} else if (minimum == 2) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR2);
			} else if (minimum == 3) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR3);
			} else if (minimum == 4) {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR4);
			} else {
				sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR5);
			}

			break;

		default:
			throw new ParameterException("字段类型错误，请检查");
		}

		sysExtendedFieldModel.setEnterpriseId(plgfxuser.getEnterpriseId());
		sysExtendedFieldModel.setEnterpriseName(plgfxuser.getEnterpriseName());
		sysExtendedFieldModel.setCreateperson(plgfxuser.getNickname());
		sysExtendedFieldModel.setCreatetime(date);
		sysExtendedFieldModel.setUpdateperson(plgfxuser.getNickname());
		sysExtendedFieldModel.setUpdatetime(date);
		// 执行保存
		sysExtendedFieldMapper.save(sysExtendedFieldModel);
	}

	/**
	 * 扩展字段修改
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updataExtendedFieldData(SysExtendedFieldUpdataVO sysExtendedFieldUpdataVO) throws Exception {
		ToolKits.isParamNull(sysExtendedFieldUpdataVO, "扩展字段修改请求不为空");
		ToolKits.isParamNull(sysExtendedFieldUpdataVO.getLineid(), "数据ID不为空");
		ToolKits.isParamNull(sysExtendedFieldUpdataVO.getFieldType(), "字段类型不为空");
		ToolKits.isParamNull(sysExtendedFieldUpdataVO.getFieldTypeName(), "字段类型名称不为空");
		ToolKits.isParamNull(sysExtendedFieldUpdataVO.getStatus(), "状态不为空");

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

		// 按ID查询扩展字段对象
		SysExtendedFieldModel sysExtendedFieldModel = sysExtendedFieldMapper.findById(sysExtendedFieldUpdataVO.getLineid(), SysExtendedFieldModel.class);
		if (ToolKits.isNotEmpty(sysExtendedFieldModel)) {
			// 扩展字段类型不同执行修改
			if (!sysExtendedFieldModel.getFieldType().equals(sysExtendedFieldUpdataVO.getFieldType())) {
				// 置空原扩展字段值
				this.specifyFieldDataBlank(sysExtendedFieldModel, user.getEnterpriseId());

				// 剔除自定义配置中的指定扩展字段
//				CustomizeClearFieldByExtendedObject customizeClearFieldByExtendedObject = new CustomizeClearFieldByExtendedObject();
//				customizeClearFieldByExtendedObject.setExtendedObjectCode(sysExtendedFieldModel.getExtendedObjectCode());
//				customizeClearFieldByExtendedObject.setField(sysExtendedFieldModel.getFieldName());
//				RestMessage<String> restMessage = userTmsResource.clearCustomizeFieldByExtendedObjectExecute(customizeClearFieldByExtendedObject);
//				if (!restMessage.isSuccess()) {
//					throw new ParameterException("清除关联自定义配置失败，请重试");
//				}

				// 根据扩展字段类型判断扩展字段是否超过5(包含5)
				Criteria criteria = Criteria.forClass(SysExtendedFieldModel.class);
				Restriction r1 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
				Restriction r2 = Restrictions.eq("extendedObjectCode", sysExtendedFieldModel.getExtendedObjectCode());
				Restriction r3 = null;
				if (sysExtendedFieldUpdataVO.getFieldType().equals(DataDictionaryConstant.FIELD_TYPE_STRING) || sysExtendedFieldUpdataVO.getFieldType().equals(DataDictionaryConstant.FIELD_TYPE_RADIO)) {
					r3 = Restrictions.in("fieldType", new String[] { DataDictionaryConstant.FIELD_TYPE_STRING, DataDictionaryConstant.FIELD_TYPE_RADIO });
				} else {
					r3 = Restrictions.eq("fieldType", sysExtendedFieldUpdataVO.getFieldType());
				}
				criteria.setRestriction(Restrictions.and(r1, r2, r3));
				List<SysExtendedFieldModel> sysExtendedFieldModels = sysExtendedFieldMapper.findByCriteria(criteria);

				Assert.isTrue(sysExtendedFieldModels.size() < DataDictionaryConstant.EXTENDED_FIELD_NUMBER, "可配置扩展字段超出数量限制");

				Integer minimum = 1;
				// 获取已配置的扩展字段序号
				List<Integer> list1 = sysExtendedFieldModels.stream().map(list -> Integer.valueOf(list.getFieldName().replaceAll("[^\\d]", ""))).distinct().collect(Collectors.toList());

				if (list1.size() > 0) {
					// 获取未配置的扩展字段序号
					List<Integer> list2 = DataDictionaryConstant.EXTENDED_FIELD_SERIAL_NUMBER.stream().filter(list -> !list1.contains(list)).collect(Collectors.toList());

					// 获取未配置的扩展字段序号最小值
					minimum = list2.stream().min(Integer::compare).get();
				}

				switch (sysExtendedFieldUpdataVO.getFieldType()) {
				case DataDictionaryConstant.FIELD_TYPE_STRING:
					if (minimum == 1) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR1);
					} else if (minimum == 2) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR2);
					} else if (minimum == 3) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR3);
					} else if (minimum == 4) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR4);
					} else {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR5);
					}

					break;
				case DataDictionaryConstant.FIELD_TYPE_NUMBER:
					if (minimum == 1) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_NUM1);
					} else if (minimum == 2) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_NUM2);
					} else if (minimum == 3) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_NUM3);
					} else if (minimum == 4) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_NUM4);
					} else {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_NUM5);
					}

					break;

				case DataDictionaryConstant.FIELD_TYPE_DATE:
					if (minimum == 1) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_DATE1);
					} else if (minimum == 2) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_DATE2);
					} else if (minimum == 3) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_DATE3);
					} else if (minimum == 4) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_DATE4);
					} else {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_DATE5);
					}

					break;

				case DataDictionaryConstant.FIELD_TYPE_RADIO:
					if (minimum == 1) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR1);
					} else if (minimum == 2) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR2);
					} else if (minimum == 3) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR3);
					} else if (minimum == 4) {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR4);
					} else {
						sysExtendedFieldModel.setFieldName(DataDictionaryConstant.EXTENDED_FIELD_STR5);
					}

					break;

				default:
					throw new ParameterException("字段类型错误，请检查");
				}

				sysExtendedFieldModel.setFieldType(sysExtendedFieldUpdataVO.getFieldType());
				sysExtendedFieldModel.setFieldTypeName(sysExtendedFieldUpdataVO.getFieldTypeName());
				sysExtendedFieldModel.setFieldChoiceValue(sysExtendedFieldUpdataVO.getFieldChoiceValue());
				sysExtendedFieldModel.setFieldSort(sysExtendedFieldUpdataVO.getFieldSort());
				sysExtendedFieldModel.setStatus(sysExtendedFieldUpdataVO.getStatus());
				sysExtendedFieldModel.setUpdateperson(user.getNickname());
				sysExtendedFieldModel.setUpdatetime(DateUtil.date());
				// 执行修改
				sysExtendedFieldMapper.update(sysExtendedFieldModel);
			}
		} else {
			throw new ParameterException("数据ID【" + sysExtendedFieldUpdataVO.getLineid() + "】不存在");
		}
	}

	/**
	 * 扩展字段删除
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteExtendedFieldData(List<SysExtendedFieldDeleteVO> sysExtendedFieldDeleteVOs) throws Exception {
		ToolKits.isParamNull(sysExtendedFieldDeleteVOs, "扩展字段删除请求不为空");

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

		// 循环删除
		for (SysExtendedFieldDeleteVO sysExtendedFieldDeleteVO : sysExtendedFieldDeleteVOs) {
			ToolKits.isParamNull(sysExtendedFieldDeleteVO.getLineid(), "数据ID不为空");

			// 根据ID查询扩展对象
			SysExtendedFieldModel sysExtendedFieldModel = sysExtendedFieldMapper.findById(sysExtendedFieldDeleteVO.getLineid(), SysExtendedFieldModel.class);
			ToolKits.isParamNull(sysExtendedFieldModel, "扩展字段ID【" + sysExtendedFieldDeleteVO.getLineid() + "】不存在");

			// 置空删除的扩展字段值
			this.specifyFieldDataBlank(sysExtendedFieldModel, user.getEnterpriseId());

			// 剔除自定义配置中的指定扩展字段
//			CustomizeClearFieldByExtendedObject customizeClearFieldByExtendedObject = new CustomizeClearFieldByExtendedObject();
//			customizeClearFieldByExtendedObject.setExtendedObjectCode(sysExtendedFieldModel.getExtendedObjectCode());
//			customizeClearFieldByExtendedObject.setField(sysExtendedFieldModel.getFieldName());
//			RestMessage<String> restMessage = userTmsResource.clearCustomizeFieldByExtendedObjectExecute(customizeClearFieldByExtendedObject);
//			if (!restMessage.isSuccess()) {
//				throw new ParameterException("清除关联自定义配置失败，请重试");
//			}

			// 执行删除
			sysExtendedFieldMapper.deleteById(sysExtendedFieldDeleteVO.getLineid(), SysExtendedFieldModel.class);
		}
	}

	/**
	 * 指定字段数据置空
	 */
	public void specifyFieldDataBlank(SysExtendedFieldModel sysExtendedFieldModel, Long enterpriseid) {
		// 清空扩展对象对应扩展字段的值
		Map<String, Object> map = Maps.newHashMap();
		map.put(sysExtendedFieldModel.getFieldName(), null);

		switch (sysExtendedFieldModel.getExtendedObjectCode()) {
		case DataDictionaryConstant.EXTENDED_OBJECT_ITEM_CATEGORY:
			// 清商品品类相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsBaseItemCategoryMapper, TmsBaseItemCategoryModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_ITEM:
			// 清商品相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsBaseItemMapper, TmsBaseItemModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_CARGO_OWNER:
			// 清货主相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsBaseCargoOwnerMapper, TmsBaseCargoOwnerModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_CUSTOMER:
			// 清客户相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsBaseCustomerMapper, TmsBaseCustomerModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_CUSTOMER_RECEIVER:
			// 清客户收货方相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsCustomerConsigneeMapper, TmsCustomerConsignee.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_BRANCH:
			// 清仓库相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsBaseBranchMapper, TmsBaseBranchModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_BRANCH_CARPORT:
			// 清仓库车位相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsBaseCarportMapper, TmsBaseCarportModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_CARRIER:
			// 清承运商相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsBaseCarriersMapper, TmsBaseCarriersModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_LINE:
			// 清线路相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsBaseTranslineMapper, TmsBaseLineModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_TRANSFER_STATION:
			// 清中转站相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsTransferStationMapper, TmsTransferStationModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_DRIVER:
			// 清司机相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsBaseDriverMapper, TmsBaseDriverModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_DELIVERYMAN:
			// 清送货员相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsBaseDeliverymanMapper, TmsBaseDeliverymanModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_VEHICLE_TYPE:
			// 清车辆类型相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsBaseVehicleTypeMapper, TmsBaseVehicleTypeModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_VEHICLE:
			// 清车辆相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsBaseVehicleMapper, TmsBaseVehicleModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_SHIPPER:
			// 清供应商相应扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", enterpriseid, map, tmsBaseProviderMapper, TmsBaseProviderModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_SHIPBILL_HEADER:
			ClearFieldByExtendedObjectVO clearFieldByExtendedObjectVO = new ClearFieldByExtendedObjectVO();
			clearFieldByExtendedObjectVO.setExtendedObjectCode(DataDictionaryConstant.EXTENDED_OBJECT_SHIPBILL_HEADER);
			clearFieldByExtendedObjectVO.setField(sysExtendedFieldModel.getFieldName());
			RestMessage<String> restMessage1 = businessComingRecordFeign.clearFieldExecute(clearFieldByExtendedObjectVO);
			if (!restMessage1.isSuccess()) {
				throw new ParameterException("运单汇总对象扩展字段置空失败：" + restMessage1.getMessage());
			}

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_SHIPBILL_DETAIL:
			clearFieldByExtendedObjectVO = new ClearFieldByExtendedObjectVO();
			clearFieldByExtendedObjectVO.setExtendedObjectCode(DataDictionaryConstant.EXTENDED_OBJECT_SHIPBILL_DETAIL);
			clearFieldByExtendedObjectVO.setField(sysExtendedFieldModel.getFieldName());
			RestMessage<String> restMessage2 = businessComingRecordFeign.clearFieldExecute(clearFieldByExtendedObjectVO);
			if (!restMessage2.isSuccess()) {
				throw new ParameterException("运单明细对象扩展字段置空失败：" + restMessage2.getMessage());
			}

			break;

		default:
			throw new ParameterException("请配置【" + sysExtendedFieldModel.getExtendedObjectCode() + "】的扩展对象");
		}
	}

}
