package com.ots.project.system.dict.service; import com.ots.common.constant.UserConstants; import com.ots.common.exception.BusinessException; import com.ots.common.utils.StringUtils; import com.ots.common.utils.security.ShiroUtils; import com.ots.common.utils.text.Convert; import com.ots.framework.web.domain.Ztree; import com.ots.project.system.dict.domain.DictType; import com.ots.project.system.dict.mapper.DictDataMapper; import com.ots.project.system.dict.mapper.DictTypeMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; @Service public class DictTypeServiceImpl implements IDictTypeService { @Autowired private DictTypeMapper dictTypeMapper; @Autowired private DictDataMapper dictDataMapper; @Override public List selectDictTypeList(DictType dictType) { return dictTypeMapper.selectDictTypeList(dictType); } @Override public List selectDictTypeAll() { return dictTypeMapper.selectDictTypeAll(); } @Override public DictType selectDictTypeById(Long dictId) { return dictTypeMapper.selectDictTypeById(dictId); } public DictType selectDictTypeByType(String dictType) { return dictTypeMapper.selectDictTypeByType(dictType); } @Override public int deleteDictTypeById(Long dictId) { return dictTypeMapper.deleteDictTypeById(dictId); } @Override public int deleteDictTypeByIds(String ids) throws BusinessException { Long[] dictIds = Convert.toLongArray(ids); for (Long dictId : dictIds) { DictType dictType = selectDictTypeById(dictId); if (dictDataMapper.countDictDataByType(dictType.getDictType()) > 0) { throw new BusinessException(String.format("%1$s已分配,不能删除", dictType.getDictName())); } } return dictTypeMapper.deleteDictTypeByIds(dictIds); } @Override public int insertDictType(DictType dictType) { dictType.setOtherType("demography"); dictType.setCreateBy(ShiroUtils.getLoginName()); return dictTypeMapper.insertDictType(dictType); } @Override @Transactional public int updateDictType(DictType dictType) { dictType.setOtherType("demography"); dictType.setUpdateBy(ShiroUtils.getLoginName()); DictType oldDict = dictTypeMapper.selectDictTypeById(dictType.getDictId()); dictDataMapper.updateDictDataType(oldDict.getDictType(), dictType.getDictType()); return dictTypeMapper.updateDictType(dictType); } @Override public String checkDictTypeUnique(DictType dict) { Long dictId = StringUtils.isNull(dict.getDictId()) ? -1L : dict.getDictId(); DictType dictType = dictTypeMapper.checkDictTypeUnique(dict.getDictType()); if (StringUtils.isNotNull(dictType) && dictType.getDictId().longValue() != dictId.longValue()) { return UserConstants.DICT_TYPE_NOT_UNIQUE; } return UserConstants.DICT_TYPE_UNIQUE; } public List selectDictTree(DictType dictType) { List ztrees = new ArrayList(); List dictList = dictTypeMapper.selectDictTypeList(dictType); for (DictType dict : dictList) { if (UserConstants.DICT_NORMAL.equals(dict.getStatus())) { Ztree ztree = new Ztree(); ztree.setId(dict.getDictId()); ztree.setName(transDictName(dict)); ztree.setTitle(dict.getDictType()); ztrees.add(ztree); } } return ztrees; } public String transDictName(DictType dictType) { StringBuffer sb = new StringBuffer(); sb.append("(" + dictType.getDictName() + ")"); sb.append("   " + dictType.getDictType()); return sb.toString(); } }