From aa566e55023f6d4fa0d4223434b95cd064c3d0d0 Mon Sep 17 00:00:00 2001
From: yuxc <653031404@qq.com>
Date: 星期一, 27 三月 2023 09:25:10 +0800
Subject: [PATCH] 直接使用业务字典完成功能,检查字典是否存在,存在即返回,不存在新增

---
 Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/service/IDictService.java         |    8 ++++++++
 Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/controller/DictController.java    |   11 +++++++++++
 Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/service/impl/DictServiceImpl.java |   19 +++++++++++++++++++
 3 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/controller/DictController.java b/Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/controller/DictController.java
index cd3708b..542855a 100644
--- a/Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/controller/DictController.java
+++ b/Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/controller/DictController.java
@@ -192,5 +192,16 @@
 		return R.data(list);
 	}
 
+	/**
+	 * 妫�鏌ュ瓧鍏告槸鍚﹀瓨鍦紝瀛樺湪鍗宠繑鍥烇紝涓嶅瓨鍦ㄦ柊澧�
+	 */
+	@PostMapping("/check")
+	@ApiOperationSupport(order = 6)
+	@ApiOperation(value = "鏂板鎴栦慨鏀�", notes = "浼犲叆dict")
+	public R checkOrInsert(@Valid @RequestBody Dict dict) {
+		CacheUtil.clear(DICT_CACHE, Boolean.FALSE);
+		return dictService.checkOrInsert(dict);
+	}
+
 
 }
diff --git a/Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/service/IDictService.java b/Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/service/IDictService.java
index 28d3a33..4030e5a 100644
--- a/Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/service/IDictService.java
+++ b/Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/service/IDictService.java
@@ -19,6 +19,7 @@
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.IService;
 import org.springblade.core.mp.support.Query;
+import org.springblade.core.tool.api.R;
 import org.springblade.system.entity.Dict;
 import org.springblade.system.vo.DictVO;
 
@@ -106,4 +107,11 @@
 	 */
 	List<DictVO> childList(Map<String, Object> dict, Long parentId);
 
+	/**
+	 * 鏂板鎴栦慨鏀�
+	 *
+	 * @param dict
+	 * @return
+	 */
+	R checkOrInsert(Dict dict);
 }
diff --git a/Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/service/impl/DictServiceImpl.java b/Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/service/impl/DictServiceImpl.java
index 087520b..965e7df 100644
--- a/Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/service/impl/DictServiceImpl.java
+++ b/Source/BladeX/blade-service/blade-system/src/main/java/org/springblade/system/service/impl/DictServiceImpl.java
@@ -25,6 +25,7 @@
 import org.springblade.core.log.exception.ServiceException;
 import org.springblade.core.mp.support.Condition;
 import org.springblade.core.mp.support.Query;
+import org.springblade.core.tool.api.R;
 import org.springblade.core.tool.constant.BladeConstant;
 import org.springblade.core.tool.node.ForestNodeMerger;
 import org.springblade.core.tool.utils.Func;
@@ -122,4 +123,22 @@
 		List<Dict> list = this.list(Condition.getQueryWrapper(dict, Dict.class).lambda().ne(Dict::getId, parentId).eq(Dict::getCode, parentDict.getCode()).orderByAsc(Dict::getSort));
 		return DictWrapper.build().listNodeVO(list);
 	}
+
+	@Override
+	public R checkOrInsert(Dict dict) {
+		LambdaQueryWrapper<Dict> lqw = Wrappers.<Dict>query().lambda().eq(Dict::getCode, dict.getCode()).eq(Dict::getDictKey, dict.getDictKey());
+		Long cnt = baseMapper.selectCount((Func.isEmpty(dict.getId())) ? lqw : lqw.notIn(Dict::getId, dict.getId()));
+		if (cnt > 0L) {
+			R.fail("瀛楀吀宸茬粡瀛樺湪锛�");
+		}
+		if (Func.isEmpty(dict.getParentId())) {
+			dict.setParentId(BladeConstant.TOP_PARENT_ID);
+		}
+		dict.setIsDeleted(BladeConstant.DB_NOT_DELETED);
+		CacheUtil.clear(DICT_CACHE, Boolean.FALSE);
+		if(saveOrUpdate(dict) == true){
+			return R.success("鎿嶄綔鎴愬姛!");
+		}
+		return R.fail("鎿嶄綔澶辫触锛�");
+	}
 }

--
Gitblit v1.9.3