|
|
@@ -0,0 +1,151 @@
|
|
|
+package com.zhongshu.card.server.core.service.dictionary;
|
|
|
+
|
|
|
+import com.github.microservice.net.ResultContent;
|
|
|
+import com.zhongshu.card.client.model.dictionary.AddDictionaryItemParam;
|
|
|
+import com.zhongshu.card.client.model.dictionary.AddDictionaryParam;
|
|
|
+import com.zhongshu.card.client.model.dictionary.UpdateDictionaryParam;
|
|
|
+import com.zhongshu.card.client.type.DictionaryType;
|
|
|
+import com.zhongshu.card.server.core.dao.dictionary.DictionaryDao;
|
|
|
+import com.zhongshu.card.server.core.dao.dictionary.DictionaryItemDao;
|
|
|
+import com.zhongshu.card.server.core.domain.dictionary.Dictionary;
|
|
|
+import com.zhongshu.card.server.core.domain.dictionary.DictionaryItem;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class DictionaryService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DictionaryDao dictionaryDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DictionaryItemDao dictionaryItemDao;
|
|
|
+
|
|
|
+ public ResultContent init(){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加字典
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent addDictionary(AddDictionaryParam param){
|
|
|
+ Dictionary byProjectIdAndKey = dictionaryDao.findTopByProjectIdAndKey(param.getProjectId(), param.getKey());
|
|
|
+ if (byProjectIdAndKey != null){
|
|
|
+ return ResultContent.buildFail("字典编码已存在");
|
|
|
+ }
|
|
|
+ Dictionary dictionary = new Dictionary();
|
|
|
+ BeanUtils.copyProperties(param, dictionary);
|
|
|
+ dictionary.setType(DictionaryType.Customize);
|
|
|
+ dictionaryDao.save(dictionary);
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑字典
|
|
|
+ */
|
|
|
+ public ResultContent updateDictionary(UpdateDictionaryParam param){
|
|
|
+ Dictionary dictionary = dictionaryDao.findTopById(param.getId());
|
|
|
+ if (dictionary == null){
|
|
|
+ return ResultContent.buildFail("数据不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ DictionaryType type = dictionary.getType();
|
|
|
+ if (DictionaryType.Standard.equals(type)){//如果为标准类型
|
|
|
+ if (StringUtils.isNotBlank(param.getKey())){
|
|
|
+ return ResultContent.buildFail("标准类型字典编码不允许修改");
|
|
|
+ }
|
|
|
+ if (param.isDisabled()){
|
|
|
+ return ResultContent.buildFail("标准类型字典不允许禁用");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Dictionary byProjectIdAndKey = dictionaryDao.findTopByProjectIdAndKey(dictionary.getProjectId(), param.getKey());
|
|
|
+ if (byProjectIdAndKey != null && !byProjectIdAndKey.getId().equals(param.getId())){
|
|
|
+ return ResultContent.buildFail("字典编码已存在");
|
|
|
+ }
|
|
|
+ com.zhongshu.card.server.core.util.BeanUtils.copyPropertiesWithoutNull(param, dictionary);
|
|
|
+ dictionaryDao.save(dictionary);
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除字典
|
|
|
+ */
|
|
|
+ public ResultContent deleteDictionary(String id){
|
|
|
+ Dictionary dictionary = dictionaryDao.findTopById(id);
|
|
|
+ if (dictionary == null){
|
|
|
+ return ResultContent.buildFail("数据不存在");
|
|
|
+ }
|
|
|
+ if (DictionaryType.Standard.equals(dictionary.getType())){
|
|
|
+ return ResultContent.buildFail("标准字典不允许删除");
|
|
|
+ }
|
|
|
+ dictionaryDao.deleteById(id);
|
|
|
+ //TODO 删除字典下的字典项
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加项
|
|
|
+ */
|
|
|
+ public ResultContent addItem(AddDictionaryItemParam param){
|
|
|
+ Dictionary dictionary = dictionaryDao.findTopById(param.getDictionaryId());
|
|
|
+ if (dictionary == null){
|
|
|
+ return ResultContent.buildFail("字典不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ DictionaryItem dictionaryItem = new DictionaryItem();
|
|
|
+ BeanUtils.copyProperties(param, dictionaryItem, "id");
|
|
|
+ dictionaryItem.setType(DictionaryType.Customize);
|
|
|
+ dictionaryItem.setDictionary(dictionary);
|
|
|
+ dictionaryItemDao.save(dictionaryItem);
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑项
|
|
|
+ */
|
|
|
+ public ResultContent updateItem(AddDictionaryItemParam param){
|
|
|
+ if (StringUtils.isBlank(param.getId())){
|
|
|
+ return ResultContent.buildFail("id不能为null");
|
|
|
+ }
|
|
|
+ DictionaryItem dictionaryItem = dictionaryItemDao.findTopById(param.getId());
|
|
|
+ if (dictionaryItem == null){
|
|
|
+ return ResultContent.buildFail("数据不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (dictionaryItem.getType().equals(DictionaryType.Standard)){//标准
|
|
|
+ if (StringUtils.isNotBlank(param.getKey())){
|
|
|
+ return ResultContent.buildFail("标准字典的项编码不允许修改");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DictionaryItem updateItem = dictionaryItemDao.findTopByDictionary_IdAndKey(param.getDictionaryId(), param.getKey());
|
|
|
+ if (updateItem != null && !updateItem.getId().equals(param.getId())){
|
|
|
+ return ResultContent.buildFail("项编码已存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ BeanUtils.copyProperties(param, dictionaryItem, "parentId", "dictionaryId");
|
|
|
+ dictionaryItemDao.save(dictionaryItem);
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除项
|
|
|
+ */
|
|
|
+ public ResultContent deleteItem(String id){
|
|
|
+ DictionaryItem dictionaryItem = dictionaryItemDao.findTopById(id);
|
|
|
+ if (dictionaryItem == null){
|
|
|
+ return ResultContent.buildFail("数据不存在");
|
|
|
+ }
|
|
|
+ if (DictionaryType.Standard.equals(dictionaryItem.getType())){
|
|
|
+ return ResultContent.buildFail("标准字典的项不允许删除");
|
|
|
+ }
|
|
|
+ dictionaryItemDao.deleteById(id);
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|