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

import java.util.Date;
import java.util.List;
import java.util.Map;

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

import com.google.common.collect.Lists;
import com.prolog.common.util.DateUtils;
import com.prolog.common.util.ToolKits;
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.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.dao.mapper.TmsBaseSignconfigMapper;
import com.prolog.rdc.tms.model.dto.basicConfigure.SignconfigPageDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.SignconfigPopDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.SignconfigViewDTO;
import com.prolog.rdc.tms.model.entity.line.TmsBaseSignconfigModel;
import com.prolog.rdc.tms.model.vo.basicConfigure.SignconfigPageVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SignconfigPopVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SignconfigSaveVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SignconfigUpdateVO;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.SysRefdatadService;
import com.prolog.rdc.tms.service.basicConfigure.SignconfigService;
import com.prolog.rdc.tms.util.PublicMethods;

/**
 * 标记配置表实现层
 *
 * @author zhangsj
 * @since 2022-04-26 15:45:36
 */
@Service
public class SignconfigServiceImpl implements SignconfigService {

	@Autowired
	private TmsBaseSignconfigMapper tmsBaseSignconfigMapper;

	@Autowired
	private SysRefdatadService sysRefdatadService;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	/**
	 * 分页查询
	 * 
	 * @param signconfigPageVO
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<SignconfigPageDTO> queryData(SignconfigPageVO signconfigPageVO) {
		ToolKits.isParamNull(signconfigPageVO, "参数不为空！");
		ToolKits.isParamNull(signconfigPageVO.getPageNum(), "当前页码不为空");
		ToolKits.isParamNull(signconfigPageVO.getPageSize(), "每页条数不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "没有操作权限！");

		// 标记类型
		List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.SIGNCONFIG_TYPE, null, null, null);
		// 匹配
		List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.SIGNCONFIG_MATCHING, null, null, null);
		// 优先级
		List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(DataDictionaryConstant.SIGNCONFIG_PRIORITY, null, null, null);

		List<SignconfigPageDTO> dtoList = Lists.newArrayList();

		Criteria criteria = Criteria.forClass(TmsBaseSignconfigModel.class);
		Restriction r1 = ToolKits.isEmpty(signconfigPageVO.getType()) ? null : Restrictions.eq("type", signconfigPageVO.getType());
		Restriction r2 = ToolKits.isEmpty(signconfigPageVO.getName()) ? null : Restrictions.like("name", "%" + signconfigPageVO.getName() + "%");
		Restriction r3 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));

		// 设置分页条件
		PageUtils.startPage(signconfigPageVO.getPageNum(), signconfigPageVO.getPageSize());

		// 执行查询
		List<TmsBaseSignconfigModel> tmsBaseSignconfigModels = tmsBaseSignconfigMapper.findByCriteria(criteria);

		for (TmsBaseSignconfigModel tmsBaseSignconfigModel : tmsBaseSignconfigModels) {
			SignconfigPageDTO dto = new SignconfigPageDTO();
			BeanUtils.copyProperties(tmsBaseSignconfigModel, dto);

			dto.setTypeName(PublicMethods.change(restMessage1, String.valueOf(dto.getType())));
			dto.setPriorityName(PublicMethods.change(restMessage3, String.valueOf(dto.getPriority())));

			// 拼接储存值
			String str = ToolKits.isEmpty(tmsBaseSignconfigModel.getStoredValue()) ? "" : tmsBaseSignconfigModel.getStoredValue();
			dto.setMatchingValue(PublicMethods.change(restMessage2, String.valueOf(tmsBaseSignconfigModel.getMatching())) + " " + str);

			// 字体颜色字段转换
			dto.setColor(tmsBaseSignconfigModel.getFontColor());

			dtoList.add(dto);
		}

		// 分页重组
		Page<SignconfigPageDTO> page = PageUtils.getPage(dtoList);
		if (tmsBaseSignconfigModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsBaseSignconfigModel> p0 = (com.github.pagehelper.Page<TmsBaseSignconfigModel>) tmsBaseSignconfigModels;
			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;
	}

	/**
	 * 标记配置弹窗查询
	 * 
	 * @param signconfigPopVO
	 * @return
	 */
	@Override
	public List<SignconfigPopDTO> popData(SignconfigPopVO signconfigPopVO) {
		ToolKits.isParamNull(signconfigPopVO, "参数不为空！");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "没有操作权限！");

		// 标记类型
		List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.SIGNCONFIG_TYPE, null, null, null);
		// 匹配
		List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.SIGNCONFIG_MATCHING, null, null, null);
		// 优先级
		List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(DataDictionaryConstant.SIGNCONFIG_PRIORITY, null, null, null);

		List<SignconfigPopDTO> dtoList = Lists.newArrayList();

		Criteria criteria = Criteria.forClass(TmsBaseSignconfigModel.class);
		Restriction r1 = ToolKits.isEmpty(signconfigPopVO.getType()) ? null : Restrictions.eq("type", signconfigPopVO.getType());
		Restriction r2 = ToolKits.isEmpty(signconfigPopVO.getName()) ? null : Restrictions.like("name", "%" + signconfigPopVO.getName() + "%");
		Restriction r3 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2, r3));
		criteria.setOrder(Order.newInstance().asc(new String[] { "priority" }));

		// 执行查询
		List<TmsBaseSignconfigModel> tmsBaseSignconfigModels = tmsBaseSignconfigMapper.findByCriteria(criteria);

		for (TmsBaseSignconfigModel tmsBaseSignconfigModel : tmsBaseSignconfigModels) {
			SignconfigPopDTO dto = new SignconfigPopDTO();
			BeanUtils.copyProperties(tmsBaseSignconfigModel, dto);

			// 字典翻译
			dto.setTypeName(PublicMethods.change(restMessage1, String.valueOf(dto.getType())));
			dto.setPriorityName(PublicMethods.change(restMessage3, String.valueOf(dto.getPriority())));
			dto.setMatchingName(PublicMethods.change(restMessage2, String.valueOf(tmsBaseSignconfigModel.getMatching())));

			// 字体颜色字段转换
			dto.setColor(tmsBaseSignconfigModel.getFontColor());

			dtoList.add(dto);
		}
		return dtoList;
	}

	/**
	 * 标记配置查看
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public SignconfigViewDTO viewData(Long id) {
		ToolKits.isParamNull(id, "标记id不为空！");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "没有操作权限！");

		// 标记类型
		List<Map<String, Object>> restMessage1 = sysRefdatadService.getDropDownById(DataDictionaryConstant.SIGNCONFIG_TYPE, null, null, null);
		// 匹配
		List<Map<String, Object>> restMessage2 = sysRefdatadService.getDropDownById(DataDictionaryConstant.SIGNCONFIG_MATCHING, null, null, null);
		// 优先级
		List<Map<String, Object>> restMessage3 = sysRefdatadService.getDropDownById(DataDictionaryConstant.SIGNCONFIG_PRIORITY, null, null, null);

		TmsBaseSignconfigModel model = tmsBaseSignconfigMapper.findById(id, TmsBaseSignconfigModel.class);
		ToolKits.isParamNull(model, "标记ID[" + id + "]有误,查询为空!");

		SignconfigViewDTO dto = new SignconfigViewDTO();
		BeanUtils.copyProperties(model, dto);

		// 类型翻译
		dto.setTypeName(PublicMethods.change(restMessage1, String.valueOf(dto.getType())));
		dto.setMatchingName(PublicMethods.change(restMessage2, String.valueOf(dto.getMatching())));
		dto.setPriorityName(PublicMethods.change(restMessage3, String.valueOf(dto.getPriority())));
		// 字体颜色字段转换
		dto.setColor(model.getFontColor());

		return dto;
	}

	/**
	 * 标记配置新增
	 * 
	 * @param signconfigSaveVO
	 */
	@Override
	public String saveData(SignconfigSaveVO signconfigSaveVO) {
		ToolKits.isParamNull(signconfigSaveVO.getName(), "标记名称不为空");
		ToolKits.isParamNull(signconfigSaveVO.getType(), "标记类型不为空");
		ToolKits.isParamNull(signconfigSaveVO.getPriority(), "优先级不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "没有操作权限！");

		Date date = DateUtils.nowDate();

		// 判断标记名称是否已存在
		PublicMethods.isThereAnyinformation(TmsBaseSignconfigModel.class, "name", signconfigSaveVO.getName(), "enterpriseId", user.getEnterpriseId(), tmsBaseSignconfigMapper, "标记名称已存在");
		// 判断优先级是否已存在
		PublicMethods.isThereAnyinformation(TmsBaseSignconfigModel.class, "priority", String.valueOf(signconfigSaveVO.getPriority()), "enterpriseId", user.getEnterpriseId(), tmsBaseSignconfigMapper, "优先级已存在");

		String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_BASE_SIGNCONFIG_ID, "L");
		ToolKits.isParamNull(id, "获取标记配置ID失败！");

		TmsBaseSignconfigModel model = new TmsBaseSignconfigModel();
		BeanUtils.copyProperties(signconfigSaveVO, model);

		// 设置ID
		model.setId(Long.valueOf(id));

		// 字体颜色字段转换
		model.setFontColor(signconfigSaveVO.getColor());

		// 设置企业
		model.setEnterpriseId(user.getEnterpriseId());
		model.setEnterpriseName(user.getEnterpriseName());

		// 设置创建人
		model.setCreatetime(date);
		model.setCreateperson(user.getNickname());

		tmsBaseSignconfigMapper.save(model);

		return id;

	}

	/**
	 * 标记配置修改
	 * 
	 * @param signconfigUpdateVO
	 */
	@Override
	public Long updateData(SignconfigUpdateVO signconfigUpdateVO) {
		ToolKits.isParamNull(signconfigUpdateVO.getId(), "标记id不为空！");
		ToolKits.isParamNull(signconfigUpdateVO.getPriority(), "优先级不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "没有操作权限！");

		Date date = DateUtils.nowDate();

		TmsBaseSignconfigModel model = tmsBaseSignconfigMapper.findById(signconfigUpdateVO.getId(), TmsBaseSignconfigModel.class);
		ToolKits.isParamNull(model, "标记ID[" + signconfigUpdateVO.getId() + "]有误,查询为空!");

		if (model.getPriority() != signconfigUpdateVO.getPriority()) {
			// 判断优先级是否已存在
			PublicMethods.isThereAnyinformation(TmsBaseSignconfigModel.class, "priority", String.valueOf(signconfigUpdateVO.getPriority()), "enterpriseId", user.getEnterpriseId(), tmsBaseSignconfigMapper, "优先级已存在");
		}

		BeanUtils.copyProperties(signconfigUpdateVO, model);

		// 字体颜色字段转换
		model.setFontColor(signconfigUpdateVO.getColor());

		// 设置修改人
		model.setUpdatetime(date);
		model.setUpdateperson(user.getNickname());

		tmsBaseSignconfigMapper.update(model);

		return model.getId();
	}

	/**
	 * 标记配置删除
	 * 
	 * @param list
	 */
	@Override
	public void deleteData(List<String> list) {
		ToolKits.isParamNull(list, "删除请求不为空！");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "没有操作权限！");

		// 执行删除
		Criteria criteria = Criteria.forClass(TmsBaseSignconfigModel.class);
		Restriction r1 = Restrictions.in("id", list.toArray());
		criteria.setRestriction(Restrictions.and(r1));
		tmsBaseSignconfigMapper.deleteByCriteria(criteria);
	}
}
