|  | @ -26,8 +26,10 @@ package cn.stylefeng.guns.sys.core.context;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import cn.hutool.core.lang.Dict;
 | 
	
		
			
				|  |  | import cn.hutool.core.util.ObjectUtil;
 | 
	
		
			
				|  |  | import cn.hutool.core.util.StrUtil;
 | 
	
		
			
				|  |  | import cn.hutool.log.Log;
 | 
	
		
			
				|  |  | import cn.stylefeng.guns.core.context.system.SystemContext;
 | 
	
		
			
				|  |  | import cn.stylefeng.guns.core.pojo.base.validate.UniqueValidateParam;
 | 
	
		
			
				|  |  | import cn.stylefeng.guns.core.pojo.login.SysLoginUser;
 | 
	
		
			
				|  |  | import cn.stylefeng.guns.sys.modular.auth.service.AuthService;
 | 
	
		
			
				|  |  | import cn.stylefeng.guns.sys.modular.dict.service.SysDictDataService;
 | 
	
	
		
			
				|  | @ -117,27 +119,93 @@ public class SystemContextImpl implements SystemContext {
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Override
 | 
	
		
			
				|  |  |     public boolean tableUniValueFlag(String tableName,
 | 
	
		
			
				|  |  |                                      String columnName,
 | 
	
		
			
				|  |  |                                      String value,
 | 
	
		
			
				|  |  |                                      Boolean excludeCurrentRecord,
 | 
	
		
			
				|  |  |                                      Long id) {
 | 
	
		
			
				|  |  |         int resultCount;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 如果排除当前这条记录
 | 
	
		
			
				|  |  |         if (excludeCurrentRecord) {
 | 
	
		
			
				|  |  |             if (id == null) {
 | 
	
		
			
				|  |  |                 throw new IllegalArgumentException("当前table字段值唯一性校验失败,id为null");
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |     public boolean tableUniValueFlag(UniqueValidateParam uniqueValidateParam) {
 | 
	
		
			
				|  |  |         int resultCount = 0;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 参数校验
 | 
	
		
			
				|  |  |         paramValidate(uniqueValidateParam);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 不排除当前记录,不排除逻辑删除的内容
 | 
	
		
			
				|  |  |         if (!uniqueValidateParam.getExcludeCurrentRecord()
 | 
	
		
			
				|  |  |                 && !uniqueValidateParam.getExcludeLogicDeleteItems()) {
 | 
	
		
			
				|  |  |             resultCount = SqlRunner.db().selectCount(
 | 
	
		
			
				|  |  |                     "select count(*) from " + uniqueValidateParam.getTableName() + " where " + uniqueValidateParam.getColumnName() + " = {0}",
 | 
	
		
			
				|  |  |                     uniqueValidateParam.getValue());
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 不排除当前记录,排除逻辑删除的内容
 | 
	
		
			
				|  |  |         if (!uniqueValidateParam.getExcludeCurrentRecord()
 | 
	
		
			
				|  |  |                 && uniqueValidateParam.getExcludeLogicDeleteItems()) {
 | 
	
		
			
				|  |  |             resultCount = SqlRunner.db().selectCount(
 | 
	
		
			
				|  |  |                     "select count(*) from " + tableName + " where " + columnName + " = {0} and id <> {1}", value, id);
 | 
	
		
			
				|  |  |         } else {
 | 
	
		
			
				|  |  |                     "select count(*) from " + uniqueValidateParam.getTableName()
 | 
	
		
			
				|  |  |                             + " where " + uniqueValidateParam.getColumnName() + " = {0} "
 | 
	
		
			
				|  |  |                             + " and "
 | 
	
		
			
				|  |  |                             + uniqueValidateParam.getLogicDeleteFieldName() + " <> " + uniqueValidateParam.getLogicDeleteValue(),
 | 
	
		
			
				|  |  |                     uniqueValidateParam.getValue());
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 排除当前记录,不排除逻辑删除的内容
 | 
	
		
			
				|  |  |         if (uniqueValidateParam.getExcludeCurrentRecord()
 | 
	
		
			
				|  |  |                 && !uniqueValidateParam.getExcludeLogicDeleteItems()) {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             // id判空
 | 
	
		
			
				|  |  |             paramIdValidate(uniqueValidateParam);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             resultCount = SqlRunner.db().selectCount(
 | 
	
		
			
				|  |  |                     "select count(*) from " + uniqueValidateParam.getTableName()
 | 
	
		
			
				|  |  |                             + " where " + uniqueValidateParam.getColumnName() + " = {0} "
 | 
	
		
			
				|  |  |                             + " and id <> {1}",
 | 
	
		
			
				|  |  |                     uniqueValidateParam.getValue(), uniqueValidateParam.getId());
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 排除当前记录,排除逻辑删除的内容
 | 
	
		
			
				|  |  |         if (uniqueValidateParam.getExcludeCurrentRecord()
 | 
	
		
			
				|  |  |                 && uniqueValidateParam.getExcludeLogicDeleteItems()) {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             // id判空
 | 
	
		
			
				|  |  |             paramIdValidate(uniqueValidateParam);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             resultCount = SqlRunner.db().selectCount(
 | 
	
		
			
				|  |  |                     "select count(*) from " + tableName + " where " + columnName + " = {0}", value);
 | 
	
		
			
				|  |  |                     "select count(*) from " + uniqueValidateParam.getTableName()
 | 
	
		
			
				|  |  |                             + " where " + uniqueValidateParam.getColumnName() + " = {0} "
 | 
	
		
			
				|  |  |                             + " and id <> {1} "
 | 
	
		
			
				|  |  |                             + " and "
 | 
	
		
			
				|  |  |                             + uniqueValidateParam.getLogicDeleteFieldName() + " <> " + uniqueValidateParam.getLogicDeleteValue(),
 | 
	
		
			
				|  |  |                     uniqueValidateParam.getValue(), uniqueValidateParam.getId());
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 如果大于0,代表不是唯一的当前校验的值
 | 
	
		
			
				|  |  |         return resultCount <= 0;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 几个参数的为空校验
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @author fengshuonan
 | 
	
		
			
				|  |  |      * @date 2020/8/17 22:00
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     private void paramValidate(UniqueValidateParam uniqueValidateParam) {
 | 
	
		
			
				|  |  |         if (StrUtil.isBlank(uniqueValidateParam.getTableName())) {
 | 
	
		
			
				|  |  |             throw new IllegalArgumentException("当前table字段值唯一性校验失败,tableName为空");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if (StrUtil.isBlank(uniqueValidateParam.getColumnName())) {
 | 
	
		
			
				|  |  |             throw new IllegalArgumentException("当前table字段值唯一性校验失败,columnName为空");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if (StrUtil.isBlank(uniqueValidateParam.getValue())) {
 | 
	
		
			
				|  |  |             throw new IllegalArgumentException("当前table字段值唯一性校验失败,字段值value为空");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * id参数的为空校验
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @author fengshuonan
 | 
	
		
			
				|  |  |      * @date 2020/8/17 22:00
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     private void paramIdValidate(UniqueValidateParam uniqueValidateParam) {
 | 
	
		
			
				|  |  |         if (uniqueValidateParam.getId() == null) {
 | 
	
		
			
				|  |  |             throw new IllegalArgumentException("当前table字段值唯一性校验失败,id为空");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |