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

import java.util.Map;

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

import com.google.common.collect.Maps;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.exception.ParameterException;
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.TmsShipbillDetailMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillHeaderMapper;
import com.prolog.rdc.tms.model.entity.TmsShipbillDetailModel;
import com.prolog.rdc.tms.model.entity.TmsShipbillHeaderModel;
import com.prolog.rdc.tms.model.vo.ClearFieldByExtendedObjectVO;
import com.prolog.rdc.tms.service.ExtendedFieldService;
import com.prolog.rdc.tms.util.PublicMethods;

/**
 * 
 *
 * 公共服务-扩展字段处理实现层
 *
 * @className :ExtendedFieldServiceImpl
 * @author :fanzw
 * @creationTime :2022/04/01
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/04/01
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Service
public class ExtendedFieldServiceImpl implements ExtendedFieldService {

	@Autowired
	private TmsShipbillHeaderMapper tmsShipbillHeaderMapper;

	@Autowired
	private TmsShipbillDetailMapper tmsShipbillDetailMapper;

	/**
	 * 扩展字段置空
	 */
	public void clearFieldData(ClearFieldByExtendedObjectVO clearFieldByExtendedObjectVO) {
		ToolKits.isParamNull(clearFieldByExtendedObjectVO, "扩展字段置空请求不为空");
		ToolKits.isParamNull(clearFieldByExtendedObjectVO.getExtendedObjectCode(), "扩展对象编码不为空");
		ToolKits.isParamNull(clearFieldByExtendedObjectVO.getField(), "字段不为空");

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

		// 清空扩展对象对应扩展字段的值
		Map<String, Object> map = Maps.newHashMap();
		map.put(clearFieldByExtendedObjectVO.getField(), null);

		switch (clearFieldByExtendedObjectVO.getExtendedObjectCode()) {
		case DataDictionaryConstant.EXTENDED_OBJECT_SHIPBILL_HEADER:
			// 清运单汇总扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", user.getEnterpriseId(), map, tmsShipbillHeaderMapper, TmsShipbillHeaderModel.class);

			break;
		case DataDictionaryConstant.EXTENDED_OBJECT_SHIPBILL_DETAIL:
			// 清运单明细扩展字段的值
			PublicMethods.updateMapByCriteria("enterpriseId", user.getEnterpriseId(), map, tmsShipbillDetailMapper, TmsShipbillDetailModel.class);

			break;

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

}
