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

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.prolog.common.util.BeanUtils;
import com.prolog.common.util.JsonUtils;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.exception.ParameterException;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.FieldSelector;
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.dao.mapper.SysRefdatadMapper;
import com.prolog.rdc.tms.dao.mapper.SysRefdatahMapper;
import com.prolog.rdc.tms.model.dto.SysRefdatadDTO;
import com.prolog.rdc.tms.model.entity.SysRefdatadModel;
import com.prolog.rdc.tms.model.entity.SysRefdatahModel;
import com.prolog.rdc.tms.model.vo.DropDownBoxVO;
import com.prolog.rdc.tms.model.vo.RefdataVO;
import com.prolog.rdc.tms.model.vo.SysRefdatadDeleteVO;
import com.prolog.rdc.tms.model.vo.SysRefdatadQueryVO;
import com.prolog.rdc.tms.model.vo.SysRefdatadSaveVO;
import com.prolog.rdc.tms.model.vo.SysRefdatadSearchVO;
import com.prolog.rdc.tms.model.vo.SysRefdatadUpdateVO;
import com.prolog.rdc.tms.model.vo.SysRefdatahDeleteVO;
import com.prolog.rdc.tms.model.vo.SysRefdatahQueryVO;
import com.prolog.rdc.tms.model.vo.SysRefdatahSaveVO;
import com.prolog.rdc.tms.model.vo.SysRefdatahUpdateVO;
import com.prolog.rdc.tms.service.SysRefdatadService;

import cn.hutool.core.util.ObjectUtil;

/**
 * 
 *
 * 参照数据明细 IMPL
 *
 * @className :SysRefdatadServiceImpl
 * @author :fanzw
 * @creationTime :2020/07/02
 * @description :
 * @modifier :fanzw
 * @modifyTime :2020/07/02
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Service
public class SysRefdatadServiceImpl implements SysRefdatadService {
	@Resource
	private SysRefdatahMapper sysRefdatahMapper;

	@Resource
	private SysRefdatadMapper sysRefdatadMapper;

	/**
	 * 数据字典分页查询
	 */
	@Override
	public Page<SysRefdatahModel> listSysRefdatahPageData(SysRefdatahQueryVO sysRefdatahQueryVO) throws Exception {
		ToolKits.isParamNull(sysRefdatahQueryVO, "数据字典分页查询请求参数不为空");
		ToolKits.isParamNull(sysRefdatahQueryVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(sysRefdatahQueryVO.getPageSize(), "页码数量不为空");
		// 分页
		PageUtils.startPage(sysRefdatahQueryVO.getPageNum(), sysRefdatahQueryVO.getPageSize());

		// 数据字典查询
		Criteria criteria = Criteria.forClass(SysRefdatahModel.class);
		Restriction r1 = Restrictions.like("objectcode", "%" + sysRefdatahQueryVO.getSearch() + "%");
		Restriction r2 = Restrictions.like("description", "%" + sysRefdatahQueryVO.getSearch() + "%");
		Restriction r3 = ToolKits.isEmpty(sysRefdatahQueryVO.getSearch()) ? null : Restrictions.or(r1, r2);
		criteria.setRestriction(Restrictions.and(r3));
		criteria.setOrder(Order.newInstance().desc(new String[] { "createtime" }));
		// 执行查询
		List<SysRefdatahModel> sysRefdatahModels = sysRefdatahMapper.findByCriteria(criteria);

		return PageUtils.getPage(sysRefdatahModels);
	}

	/**
	 * 数据字典明细查询
	 */
	@Override
	public Map<String, Object> listSysRefdatadPageData(SysRefdatadSearchVO sysRefdatadSearchVO) throws Exception {
		ToolKits.isParamNull(sysRefdatadSearchVO, "数据字典明细查询请求参数不为空");
		ToolKits.isParamNull(sysRefdatadSearchVO.getObjectcode(), "字典编码不为空");

		// 校验字典编码是否已经维护
		SysRefdatahModel sysRefdatahModel = sysRefdatahMapper.findById(sysRefdatadSearchVO.getObjectcode(), SysRefdatahModel.class);
		if (ToolKits.isEmpty(sysRefdatahModel)) {
			throw new ParameterException("字典编码[" + sysRefdatadSearchVO.getObjectcode() + "]未维护");
		}

		// 配置列表JSON
		String colnamelist = sysRefdatahModel.getColnamelist();
		StringBuilder sb = new StringBuilder();
		if (ToolKits.isNotEmpty(colnamelist)) {
			@SuppressWarnings("unchecked")
			Collection<Map<String, Object>> colList = (Collection<Map<String, Object>>) JsonUtils.toList(colnamelist);
			if (ToolKits.isNotEmpty(colList)) {
				for (Map<String, Object> map : colList) {
					String ifshow = ToolKits.convertToString(map.get("ifshow"));
					String column_name = ToolKits.convertToString(map.get("column_name"));
					if ("1".equals(ifshow)) {
						sb.append(column_name).append(",");
					}
				}
			}
		} else {
			sb.append("objectcode,code,name,groupcode1,sortnum,");
		}

		String str = sb.toString().substring(0, sb.toString().length() - 1);
		String[] fields = str.split(",");

		// 动态构造sql语句
		FieldSelector fieldSelector = FieldSelector.newInstance();
		fieldSelector.include(fields);
		// 动态构造查询条件
		Criteria criteria = Criteria.forClass(SysRefdatadModel.class);
		Restriction r1 = Restrictions.eq("objectcode", sysRefdatadSearchVO.getObjectcode());
		criteria.setRestriction(r1);
		List<SysRefdatadModel> refd = sysRefdatadMapper.findFieldsByCriteria(fieldSelector, criteria);
		List<Map<String, Object>> result = BeanUtils.convertBean(refd, fields);
		Map<String, Object> map = new HashMap<>();
		map.put("list", result);// 前端表格需要这种数据格式
		map.put("totalCount", 0);
		map.put("pageCount", 0);
		map.put("pageNum", 0);
		map.put("pageSize", 0);
		map.put("startRow", 0);
		map.put("endRow", 0);
		return map;
	}

	/**
	 * 数据字典修改
	 */
	@Override
	public void updateSysRefdatahData(SysRefdatahUpdateVO sysRefdatahUpdateVO) throws Exception {
		ToolKits.isParamNull(sysRefdatahUpdateVO, "数据字典修改请求参数不为空");
		ToolKits.isParamNull(sysRefdatahUpdateVO.getObjectcode(), "字典编码不为空");
		ToolKits.isParamNull(sysRefdatahUpdateVO.getDescription(), "字典描述不为空");
		ToolKits.isParamNull(sysRefdatahUpdateVO.getIsmodify(), "是否允许修改不为空");

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

		// 查询数据字典对象
		SysRefdatahModel sysRefdatahModel = sysRefdatahMapper.findById(sysRefdatahUpdateVO.getObjectcode(), SysRefdatahModel.class);
		if (ToolKits.isNotEmpty(sysRefdatahModel)) {
			BeanUtils.beanCopyToBean(sysRefdatahUpdateVO, sysRefdatahModel);
			sysRefdatahModel.setUpdateperson(user.getNickname());
			sysRefdatahModel.setUpdatetime(new Date());
			sysRefdatahMapper.update(sysRefdatahModel);
		} else {
			throw new ParameterException("字典编码[" + sysRefdatahUpdateVO.getObjectcode() + "]不存在");
		}
	}

	/**
	 * 数据字典删除
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteSysRefdatahData(SysRefdatahDeleteVO sysRefdatahDeleteVO) throws Exception {
		ToolKits.isParamNull(sysRefdatahDeleteVO, "数据字典删除请求参数不为空");
		ToolKits.isParamNull(sysRefdatahDeleteVO.getObjectcode(), "字典编码不为空");

		// 删除数据字典数据
		long count1 = sysRefdatahMapper.deleteById(sysRefdatahDeleteVO.getObjectcode(), SysRefdatahModel.class);
		if (count1 != 1) {
			throw new ParameterException("字典编码[" + sysRefdatahDeleteVO.getObjectcode() + "]删除失败");
		}

		// 删除数据字典明细数据
		Criteria criteria = Criteria.forClass(SysRefdatadModel.class);
		Restriction r1 = Restrictions.eq("objectcode", sysRefdatahDeleteVO.getObjectcode());
		criteria.setRestriction(Restrictions.and(r1));
		// 执行删除
		sysRefdatadMapper.deleteByCriteria(criteria);
	}

	/**
	 * 数据字典保存
	 */
	@Override
	public void saveSysRefdatahData(SysRefdatahSaveVO sysRefdatahSaveVO) throws Exception {
		ToolKits.isParamNull(sysRefdatahSaveVO, "数据字典保存请求参数不为空");
		ToolKits.isParamNull(sysRefdatahSaveVO.getObjectcode(), "字典编码不为空");
		ToolKits.isParamNull(sysRefdatahSaveVO.getDescription(), "字典描述不为空");
		ToolKits.isParamNull(sysRefdatahSaveVO.getIsmodify(), "是否允许修改不为空");

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

		// 判断字典编码是否重复
		SysRefdatahModel sysRefdatahModel = sysRefdatahMapper.findById(sysRefdatahSaveVO.getObjectcode(), SysRefdatahModel.class);
		if (ToolKits.isNotEmpty(sysRefdatahModel)) {
			throw new ParameterException("字典编码[" + sysRefdatahSaveVO.getObjectcode() + "]已存在");
		}

		// 数据字典对象
		sysRefdatahModel = new SysRefdatahModel();
		BeanUtils.beanCopyToBean(sysRefdatahSaveVO, sysRefdatahModel);
		sysRefdatahModel.setCreateperson(user.getNickname());
		sysRefdatahModel.setCreatetime(new Date());
		sysRefdatahMapper.save(sysRefdatahModel);
	}

	/**
	 * 数据字典下拉框
	 */
	@Override
	public Map<String, Object> listDropDownBoxData(List<DropDownBoxVO> dropDownBoxVOs) throws Exception {
		ToolKits.isParamNull(dropDownBoxVOs, "数据字典下拉框请求参数不为空");

		Map<String, Object> data = new HashMap<>();
		for (DropDownBoxVO dropDownBoxVO : dropDownBoxVOs) {
			String objectcode = dropDownBoxVO.getObjectcode();
			if (ToolKits.isEmpty(objectcode)) {
				continue;
			}

			List<Map<String, Object>> list = sysRefdatadMapper.listDropDownBoxSql(dropDownBoxVO);
			list = (List<Map<String, Object>>) BeanUtils.keyToLower(list);
			data.put(objectcode, list);
		}

		return data;
	}

	/**
	 * 数据字典明细新增
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveSysRefdatadData(List<SysRefdatadSaveVO> sysRefdatadSaveVOs) throws Exception {
		ToolKits.isParamNull(sysRefdatadSaveVOs, "数据字典明细新增请求参数不为空");

		// 当前时间
		Date date = new Date();

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

		for (SysRefdatadSaveVO sysRefdatadSaveVO : sysRefdatadSaveVOs) {
			ToolKits.isParamNull(sysRefdatadSaveVO.getObjectcode(), "字典编码不为空");
			ToolKits.isParamNull(sysRefdatadSaveVO.getCode(), "字典值不为空");
			ToolKits.isParamNull(sysRefdatadSaveVO.getName(), "字典名称不为空");
			ToolKits.isParamNull(sysRefdatadSaveVO.getSortnum(), "排序编号不为空");

			// 校验数据字典是否已维护
			SysRefdatahModel sysRefdatahModel = sysRefdatahMapper.findById(sysRefdatadSaveVO.getObjectcode(), SysRefdatahModel.class);
			if (ToolKits.isEmpty(sysRefdatahModel)) {
				throw new ParameterException("数据字典[" + sysRefdatadSaveVO.getObjectcode() + "]未维护,请先维护数据字典");
			}

			// 判断排序编号是否重复
			Criteria criteria = Criteria.forClass(SysRefdatadModel.class);
			Restriction r1 = Restrictions.eq("objectcode", sysRefdatadSaveVO.getObjectcode());
			Restriction r2 = Restrictions.eq("sortnum", sysRefdatadSaveVO.getSortnum());
			criteria.setRestriction(Restrictions.and(r1, r2));
			// 执行查询
			long count = sysRefdatadMapper.findCountByCriteria(criteria);
			if (count > 0) {
				throw new ParameterException("数据字典[" + sysRefdatadSaveVO.getObjectcode() + "]排序编号[" + sysRefdatadSaveVO.getSortnum() + "]重复");
			}

			// 校验是否新增重复
			criteria = Criteria.forClass(SysRefdatadModel.class);
			Restriction r3 = Restrictions.eq("objectcode", sysRefdatadSaveVO.getObjectcode());
			Restriction r4 = Restrictions.eq("code", sysRefdatadSaveVO.getCode());
			criteria.setRestriction(Restrictions.and(r3, r4));
			// 执行查询
			List<SysRefdatadModel> sysRefdatadModels = sysRefdatadMapper.findByCriteria(criteria);
			if (ToolKits.isEmpty(sysRefdatadModels)) {
				SysRefdatadModel sysRefdatadModel = new SysRefdatadModel();
				BeanUtils.beanCopyToBean(sysRefdatadSaveVO, sysRefdatadModel);
				sysRefdatadModel.setCreateperson(user.getNickname());
				sysRefdatadModel.setCreatetime(date);
				sysRefdatadMapper.save(sysRefdatadModel);
			} else {
				throw new ParameterException("数据字典[" + sysRefdatadSaveVO.getObjectcode() + "]明细[" + sysRefdatadSaveVO.getCode() + "]维护重复");
			}
		}
	}

	/**
	 * 数据字典明细修改
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateSysRefdatadData(List<SysRefdatadUpdateVO> sysRefdatadUpdateVOs) throws Exception {
		ToolKits.isParamNull(sysRefdatadUpdateVOs, "数据字典明细新增请求参数不为空");

		// 当前时间
		Date date = new Date();

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

		for (SysRefdatadUpdateVO sysRefdatadUpdateVO : sysRefdatadUpdateVOs) {
			ToolKits.isParamNull(sysRefdatadUpdateVO.getObjectcode(), "字典编码不为空");
			ToolKits.isParamNull(sysRefdatadUpdateVO.getCode(), "字典值不为空");
			ToolKits.isParamNull(sysRefdatadUpdateVO.getName(), "字典名称不为空");
			ToolKits.isParamNull(sysRefdatadUpdateVO.getSortnum(), "排序编号不为空");

			SysRefdatadModel sysRefdatadModel = new SysRefdatadModel();
			sysRefdatadModel.setName(sysRefdatadUpdateVO.getName());
			sysRefdatadModel.setGroupcode1(sysRefdatadUpdateVO.getGroupcode1());
			sysRefdatadModel.setUpdateperson(user.getNickname());
			sysRefdatadModel.setUpdatetime(date);

			// 待更新字段
			FieldSelector fieldSelector = FieldSelector.newInstance().include(new String[] { "name", "groupcode1", "updateperson", "updatetime" });

			// 校验是否新增重复
			Criteria criteria = Criteria.forClass(SysRefdatadModel.class);
			Restriction r1 = Restrictions.eq("objectcode", sysRefdatadUpdateVO.getObjectcode());
			Restriction r2 = Restrictions.eq("code", sysRefdatadUpdateVO.getCode());
			Restriction r3 = Restrictions.eq("sortnum", sysRefdatadUpdateVO.getSortnum());
			criteria.setRestriction(Restrictions.and(r1, r2, r3));
			// 执行更新
			long count = sysRefdatadMapper.updateFieldsByCriteria(sysRefdatadModel, fieldSelector, criteria);
			if (count > 1) {
				throw new ParameterException("数据字典[" + sysRefdatadUpdateVO.getObjectcode() + "]明细[" + sysRefdatadUpdateVO.getCode() + "]重复");
			}

			if (count < 1) {
				throw new ParameterException("数据字典[" + sysRefdatadUpdateVO.getObjectcode() + "]明细[" + sysRefdatadUpdateVO.getCode() + "]不存在");
			}
		}
	}

	/**
	 * 数据字典明细删除
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteSysRefdatadData(List<SysRefdatadDeleteVO> sysRefdatadDeleteVOs) throws Exception {
		ToolKits.isParamNull(sysRefdatadDeleteVOs, "数据字典明细删除请求参数不为空");

		for (SysRefdatadDeleteVO sysRefdatadDeleteVO : sysRefdatadDeleteVOs) {
			ToolKits.isParamNull(sysRefdatadDeleteVO.getObjectcode(), "字典编码不为空");
			ToolKits.isParamNull(sysRefdatadDeleteVO.getCode(), "字典值不为空");
			ToolKits.isParamNull(sysRefdatadDeleteVO.getSortnum(), "排序编号不为空");

			Criteria criteria = Criteria.forClass(SysRefdatadModel.class);
			Restriction r1 = Restrictions.eq("objectcode", sysRefdatadDeleteVO.getObjectcode());
			Restriction r2 = Restrictions.eq("code", sysRefdatadDeleteVO.getCode());
			Restriction r3 = Restrictions.eq("sortnum", sysRefdatadDeleteVO.getSortnum());
			criteria.setRestriction(Restrictions.and(r1, r2, r3));
			// 执行删除
			sysRefdatadMapper.deleteByCriteria(criteria);
		}
	}

	@Override
	public SysRefdatahModel getSysRefdatahById(String objectcode) {

		return sysRefdatahMapper.findById(objectcode, SysRefdatahModel.class);
	}

	@Override
	public List<SysRefdatadModel> getSysRefdatad(String objectcode, String code, String name) {

		if (ToolKits.isEmpty(objectcode)) {
			throw new ParameterException("参数编码不能为空！");
		}
		Criteria criteria = Criteria.forClass(SysRefdatadModel.class);
		Restriction r1 = Restrictions.eq("objectcode", objectcode);
		Restriction r2 = null;
		Restriction r3 = null;
		if (ToolKits.isNotEmpty(name)) {
			r2 = Restrictions.eq("name", name);
		}
		if (ToolKits.isNotEmpty(code)) {
			r3 = Restrictions.eq("code", code);
		}

		Restriction r5 = Restrictions.and(r1, r2, r3);
		criteria.setRestriction(r5);
		return sysRefdatadMapper.findByCriteria(criteria);
	}

	@Override
	public SysRefdatadModel getSysRefdatadById(String objectcode, String sortnum) {

		return sysRefdatadMapper.getSysRefdatadById(objectcode, sortnum);
	}

	public List<Map<String, Object>> findsysrefdatadn(Map<String, Object> map) {
		String objectcode = ToolKits.convertToString(map.get("objectcode"));
		String code = ToolKits.convertToString(map.get("code"));
		List<String> codes = ToolKits.convertToList(map.get("codes"));
		return sysRefdatadMapper.findsysrefdatadn(objectcode, code, codes);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public List<Map<String, Object>> getSysByNames(Map<String, Object> map) {
		return sysRefdatadMapper.getSysByNames(BeanUtils.keyToLower(map));
	}

	@Override
	public List<Map<String, Object>> batchGetNamesByCodes(RefdataVO vo) {
		if (ToolKits.isEmpty(vo)) {
			throw new ParameterException("参数不能为空");
		}
		List<String> objectcodes = vo.getObjectcodes();
		List<String> codes = vo.getCodes();
		if (CollectionUtils.isEmpty(objectcodes) || CollectionUtils.isEmpty(codes)) {
			throw new RuntimeException("参数不能为空");
		}
		return sysRefdatadMapper.batchGetNamesByCodes(objectcodes, codes);
	}

	@Override
	public List<SysRefdatadModel> getSysRefdatadCom(String objectcode, String code, String groupcode1) {

		if (ToolKits.isEmpty(objectcode)) {
			throw new ParameterException("字段编码不能为空！");
		}
		if (ToolKits.isEmpty(code)) {
			throw new ParameterException("存储值不能为空！");
		}
		Criteria criteria = Criteria.forClass(SysRefdatadModel.class);
		Restriction r1 = Restrictions.eq("objectcode", objectcode);
		Restriction r2 = Restrictions.eq("code", code);

		Restriction r4 = Restrictions.and(r1, r2);
		if (ToolKits.isEmpty(groupcode1)) {
			criteria.setRestriction(r4);

		} else {
			Restriction r3 = Restrictions.eq("groupcode1", groupcode1);
			Restriction r5 = Restrictions.and(r4, r3);
			criteria.setRestriction(r5);

		}

		return sysRefdatadMapper.findByCriteria(criteria);
	}

	/**
	 * 参照数据明细查询
	 */
	@Override
	public List<SysRefdatadDTO> getSysRefdatadData(SysRefdatadQueryVO sysrefdatadqueryvo) throws Exception {
		ToolKits.isParamNull(sysrefdatadqueryvo, "参照数据明细查询请求报文不为空");

		// 参照数据明细查询
		Criteria criteria = Criteria.forClass(SysRefdatadModel.class);
		Restriction r2 = ToolKits.isEmpty(sysrefdatadqueryvo.getBranchId()) ? null : Restrictions.eq("branchid", sysrefdatadqueryvo.getBranchId());
		Restriction r3 = ToolKits.isEmpty(sysrefdatadqueryvo.getObjectcode()) ? null : Restrictions.eq("objectcode", sysrefdatadqueryvo.getObjectcode());
		Restriction r4 = ToolKits.isEmpty(sysrefdatadqueryvo.getCode()) ? null : Restrictions.eq("code", sysrefdatadqueryvo.getCode());
		Restriction r5 = ToolKits.isEmpty(sysrefdatadqueryvo.getName()) ? null : Restrictions.eq("name", sysrefdatadqueryvo.getName());
		Restriction r6 = ToolKits.isEmpty(sysrefdatadqueryvo.getGroupcode1()) ? null : Restrictions.eq("groupcode1", sysrefdatadqueryvo.getGroupcode1());
		Restriction r7 = ToolKits.isEmpty(sysrefdatadqueryvo.getGroupcode2()) ? null : Restrictions.eq("groupcode2", sysrefdatadqueryvo.getGroupcode2());
		Restriction r8 = ToolKits.isEmpty(sysrefdatadqueryvo.getGroupcode3()) ? null : Restrictions.eq("groupcode3", sysrefdatadqueryvo.getGroupcode3());
		Restriction r9 = ToolKits.isEmpty(sysrefdatadqueryvo.getGroupcode4()) ? null : Restrictions.eq("groupcode4", sysrefdatadqueryvo.getGroupcode4());
		Restriction r10 = ToolKits.isEmpty(sysrefdatadqueryvo.getGroupcode5()) ? null : Restrictions.eq("groupcode5", sysrefdatadqueryvo.getGroupcode5());
		Restriction r11 = ToolKits.isEmpty(sysrefdatadqueryvo.getObjectcodes()) ? null : Restrictions.in("objectcode", sysrefdatadqueryvo.getObjectcodes());
		criteria.setRestriction(Restrictions.and(r2, r3, r4, r5, r6, r7, r8, r9, r10, r11));
		criteria.setOrder(Order.newInstance().asc(new String[] { "sortnum" }));
		// 执行查询
		List<SysRefdatadModel> sysrefdatadmodels = sysRefdatadMapper.findByCriteria(criteria);

		// 参照数据明细集合
		List<SysRefdatadDTO> sysrefdataddtos = new ArrayList<SysRefdatadDTO>();

		// 循环处理
		for (SysRefdatadModel sysrefdatadmodel : sysrefdatadmodels) {
			// 参照数据明细对象
			SysRefdatadDTO sysrefdataddto = new SysRefdatadDTO();
			BeanUtils.beanCopyToBean(sysrefdatadmodel, sysrefdataddto);
			// 添加参照数据明细对象
			sysrefdataddtos.add(sysrefdataddto);
		}

		return sysrefdataddtos;
	}

	/**
	 * 获取数据字典键值对
	 */
	@Override
	public Map<String, String> getRefdKeyValue(String[] strArr, String enterpriseid) throws Exception {
		ToolKits.isParamNull(strArr, "获取数据字典键值对请求报文不为空");
		// 参照数据明细查询
		Criteria criteria = Criteria.forClass(SysRefdatadModel.class);
		Restriction r2 = Restrictions.in("objectcode", strArr);
		criteria.setRestriction(Restrictions.and(r2));
		// 执行查询
		List<SysRefdatadModel> sysrefdatadmodels = sysRefdatadMapper.findByCriteria(criteria);
		if (ToolKits.isEmpty(sysrefdatadmodels)) {
			throw new RuntimeException("未找到对应的数据字典！");
		}
		Map<String, String> resultMap = new HashMap<String, String>();
		for (SysRefdatadModel model : sysrefdatadmodels) {
			String key = model.getObjectcode() + "-" + model.getCode();
			resultMap.put(key, model.getName());
		}
		return resultMap;
	}

	@Override
	public List<Map<String, Object>> getDropDownById(String objectcode, String groupcode1, String groupcode2, String code) {

		if (ToolKits.isEmpty(objectcode)) {
			throw new ParameterException("字段编码不能为空！");
		}
		List<Map<String, Object>> list = sysRefdatadMapper.getDropDownById(objectcode, groupcode1, groupcode2, code);
		list = (List<Map<String, Object>>) BeanUtils.keyToLower(list);
		return list;
	}

	/**
	 * 数据字典批量查询
	 * 
	 * @param param
	 * @return
	 */
	@Override
	public Map<String, Object> ListDropDownData(List<Map<String, Object>> param) {
		ToolKits.isParamNull(param, "请求报文不为空");

		Map<String, Object> data = new HashMap<>();
		for (Map<String, Object> map : param) {
			String objectcode = ToolKits.convertToString(map.get("objectcode"));
			if (ToolKits.isEmpty(objectcode)) {
				continue;
			}

			String groupcode1 = ToolKits.convertToString(map.get("groupcode1"));
			String groupcode2 = ToolKits.convertToString(map.get("groupcode2"));
			String code = ToolKits.convertToString(map.get("code"));

			List<Map<String, Object>> list = sysRefdatadMapper.getDropDownById(objectcode, groupcode1, groupcode2, code);
			list = (List<Map<String, Object>>) BeanUtils.keyToLower(list);

			data.put(objectcode, list);
		}

		return data;
	}

	/**
	 * 通用字典翻译查询
	 */
	@Override
	public List<SysRefdatadModel> getSysRefdatadDatas(SysRefdatadQueryVO vo) throws Exception {
		if (ObjectUtil.isEmpty(vo.getObjectcode()) && (null == vo.getObjectcodes() || vo.getObjectcodes().length == 0 || ObjectUtil.equal("", vo.getObjectcodes()) || ObjectUtil.isEmpty(vo.getObjectcodes()))) {
			throw new PrologException("字典编码参数【objectcode】和字典编码集合参数【objectcodes】不能同时为空!");
		}
		List<SysRefdatadModel> list = sysRefdatadMapper.getSysRefdatadDatas(vo, vo.getObjectcodes());
		return list;
	}

	@Override
	public List<SysRefdatadModel> getSysRefdatadByName(SysRefdatadSearchVO sysRefdatadSearchVO) {
		if (ToolKits.isEmpty(sysRefdatadSearchVO)) {
			throw new ParameterException("获取数据字典请求不能为空！");
		}
		if (ToolKits.isEmpty(sysRefdatadSearchVO.getObjectcode())) {
			throw new ParameterException("参数编码不能为空！");
		}
		Criteria criteria = Criteria.forClass(SysRefdatadModel.class);
		Restriction r1 = Restrictions.eq("objectcode", sysRefdatadSearchVO.getObjectcode());

		criteria.setRestriction(Restrictions.and(r1));
		List<SysRefdatadModel> list = sysRefdatadMapper.findByCriteria(criteria);
		return list;
	}

}
