DataDictionaryServiceImpl.java 6.58 KB
/**
 * 
 */
package com.zteits.sys.biz;

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

import com.clouds.common.cache.sys.SysCodeValueCacheUtil;
import com.google.common.collect.Maps;
import com.zteits.clouds.api.apibase.constants.DataStatusEnum;
import com.zteits.clouds.api.dto.sys.SysCodeValueDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.zteits.clouds.api.apibase.bean.BizResult;
import com.zteits.clouds.api.apibase.bean.PageBean;
import com.zteits.clouds.api.apibase.constants.ErrorType;
import com.zteits.clouds.api.apibase.exception.BizException;
import com.zteits.clouds.api.dto.sys.DataDicKindDetailDTO;
import com.zteits.clouds.api.dto.sys.DataDictionaryKindDTO;
import com.zteits.clouds.api.dto.sys.param.DataDicKindDetailRequest;
import com.zteits.clouds.api.dto.sys.param.DataDicKindItemObjRequest;
import com.zteits.clouds.api.dto.sys.param.DataDictionaryKindListReuest;
import com.zteits.clouds.api.dto.sys.param.DataDictionaryKindObjRequest;
import com.zteits.clouds.api.dto.sys.param.DeleteDataDicRequest;
import com.zteits.clouds.api.service.sys.DataDictionaryService;

import com.zteits.sys.dao.SysCodeDefDao;
import com.zteits.sys.dao.SysCodeValueDao;
import com.zteits.sys.domain.DataDicKindDetailDO;
import com.zteits.sys.domain.SysCodeDef;
import com.zteits.sys.domain.SysCodeDefExample;
import com.zteits.sys.domain.SysCodeValueExample;
import com.zteits.sys.domain.SysCodeDefExample.Criteria;
import com.zteits.sys.domain.SysCodeValue;
import com.zteits.sys.pagepaper.PageBeanUtil;

/**
 * @author hxz
 *
 */
@Component
public class DataDictionaryServiceImpl implements DataDictionaryService {

	@Autowired
	private SysCodeValueDao sysCodeValueDao;
	
	@Autowired
	private SysCodeDefDao sysCodeDefDao;

	/**
	 * 通过分类编号或者分类名称模糊查询数据字典分类列表
	 */
	@Override
	public BizResult<List<DataDictionaryKindDTO>> getDataDictionaryKindList(
			DataDictionaryKindListReuest requestObject) {
		if (null == requestObject) {
            throw new BizException(ErrorType.PARAMM_NULL, "请求对象为空");
        }
		
		SysCodeDefExample example = new SysCodeDefExample();

		Criteria criteria1 = example.createCriteria();
		criteria1.andCodeNameLike(requestObject.getCodeName());
		
		Criteria criteria2 = example.createCriteria(); 
		criteria2.andCodeKindLike(requestObject.getCodeKind());
		
		example.or(criteria2);
		
		List<SysCodeDef> sysCodeDefDOList = sysCodeDefDao.getDataDictionaryKindList(example);
		List<DataDictionaryKindDTO> dataDictionaryKindDTOList = null;
		if (null != sysCodeDefDOList) {
			dataDictionaryKindDTOList = JSON.parseArray(JSON.toJSONString(sysCodeDefDOList, false), DataDictionaryKindDTO.class);
		}
		return new BizResult<List<DataDictionaryKindDTO>>(dataDictionaryKindDTOList);
	}

	/**
	 * 新增一条数据字典大类记录
	 */
	@Override
	public BizResult<Long> insertDataDictionaryKind(DataDictionaryKindObjRequest requestObject) {
		if (null == requestObject) {
            throw new BizException(ErrorType.PARAMM_NULL, "请求对象为空");
        }
		
		SysCodeDef sysCodeDef= new SysCodeDef();
		BeanUtils.copyProperties(requestObject, sysCodeDef);
		
		return new BizResult<Long> (sysCodeDefDao.insertDataDictionaryKind(sysCodeDef));
	}

	/**
	 * 删除一条数据字典大类
	 */
	@Override
	public BizResult<Boolean> deleteDataDictionaryKindByCodeKind(DeleteDataDicRequest requestObject) {
		if (null == requestObject) {
            throw new BizException(ErrorType.PARAMM_NULL, "请求对象为空");
        }
		
		SysCodeDefExample example = new SysCodeDefExample();
		example.createCriteria().andCodeKindEqualTo(requestObject.getCodeKind());
	
		return new BizResult<Boolean> (sysCodeDefDao.deleteDataDictionaryKindByCodeKind(example));
	}

	@Override
	public BizResult<List<DataDicKindDetailDTO>> getDataDictionaryKindDetails(DataDicKindDetailRequest requestObject) {	
		if (null == requestObject) {
            throw new BizException(ErrorType.PARAMM_NULL, "请求对象为空");
        }
		
		SysCodeValueExample example = new SysCodeValueExample ();
		example.createCriteria().andCodeKindEqualTo(requestObject.getCodeKind()).andDataStateEqualTo(DataStatusEnum.DATA_STATUS_VALID.value());
		List<SysCodeValue> sysCodeValueDOList = sysCodeValueDao.getDataDictionaryKindDetails(example);
		List<DataDicKindDetailDTO> dataDicKindDetailDTOList = null;
		if (null != sysCodeValueDOList) {
			//放入缓存
			Map<String,SysCodeValueDTO> sysCodeDTOs = Maps.newLinkedHashMap();
			for (SysCodeValue codeValue : sysCodeValueDOList) {
				SysCodeValueDTO dto = new SysCodeValueDTO();
				BeanUtils.copyProperties(codeValue, dto);
				sysCodeDTOs.put(codeValue.getCodeValue(),dto);
			}
			SysCodeValueCacheUtil.setCodeValues(requestObject.getCodeKind().toUpperCase(),sysCodeDTOs);
			dataDicKindDetailDTOList = JSON.parseArray(JSON.toJSONString(sysCodeValueDOList, false), DataDicKindDetailDTO.class);
		}
		return new BizResult<List<DataDicKindDetailDTO>>(dataDicKindDetailDTOList);
	}
	
	@Override
	public BizResult<Long> insertDataDictionaryKindItem(DataDicKindItemObjRequest requestObject) {
		if (null == requestObject) {
            throw new BizException(ErrorType.PARAMM_NULL, "请求对象为空");
        }
		
		SysCodeValue sysCodeValue = new SysCodeValue();
		BeanUtils.copyProperties(requestObject, sysCodeValue);
		
		return new BizResult<Long> (sysCodeValueDao.insertDataDictionaryKindItem(sysCodeValue));
	}

	@Override
	public BizResult<Boolean> deleteDataDictionaryKindItemByCodeKind(DeleteDataDicRequest requestObject) {
		if (null == requestObject) {
            throw new BizException(ErrorType.PARAMM_NULL, "请求对象为空");
        }
		
		SysCodeValueExample example = new SysCodeValueExample ();
		example.createCriteria().andCodeKindEqualTo(requestObject.getCodeKind());
		return new BizResult<Boolean> (sysCodeValueDao.deleteDataDictionaryKindItemByCodeKind(example));
	}

	@Override
	public BizResult<PageBean<DataDicKindDetailDTO>> getDataDictionaryInfo(DataDictionaryKindListReuest requestObject) {
		if (null == requestObject.getBaseRequest()) {
			throw new BizException(ErrorType.PARAMM_NULL, "分页参数为空");
		}
		PageInfo<DataDicKindDetailDO> pageInfo = sysCodeValueDao.getDataDictionaryInfo(requestObject);
		
		PageBean<DataDicKindDetailDTO> pageBean = new PageBean<DataDicKindDetailDTO>();
		
		if (pageInfo != null) {
			PageBeanUtil.copyProperties(pageInfo, pageBean, DataDicKindDetailDTO.class);
		}
		
		return new BizResult<PageBean<DataDicKindDetailDTO>>(pageBean);
	}

}