inleft
2022-02-09 9bcb19959eeb9da9bde2561e7278f6d0a55eb151
commit | author | age
9bcb19 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.core.validation.unique;
26
27 import cn.hutool.core.util.ObjectUtil;
28 import vip.xiaonuo.core.context.group.RequestGroupContext;
29 import vip.xiaonuo.core.context.group.RequestParamIdContext;
30 import vip.xiaonuo.core.context.system.SystemContextHolder;
31 import vip.xiaonuo.core.pojo.base.param.BaseParam;
32 import vip.xiaonuo.core.pojo.base.validate.UniqueValidateParam;
33
34 import javax.validation.ConstraintValidator;
35 import javax.validation.ConstraintValidatorContext;
36
37 /**
38  * 验证表的的某个字段值是否在是唯一值
39  *
40  * @author yubaoshan
41  * @date 2020/4/14 23:49
42  */
43 public class TableUniqueValueValidator implements ConstraintValidator<TableUniqueValue, String> {
44
45     private String tableName;
46
47     private String columnName;
48
49     private boolean excludeLogicDeleteItems;
50
51     private String logicDeleteFieldName;
52
53     private String logicDeleteValue;
54
55     @Override
56     public void initialize(TableUniqueValue constraintAnnotation) {
57         this.tableName = constraintAnnotation.tableName();
58         this.columnName = constraintAnnotation.columnName();
59         this.excludeLogicDeleteItems = constraintAnnotation.excludeLogicDeleteItems();
60         this.logicDeleteFieldName = constraintAnnotation.logicDeleteFieldName();
61         this.logicDeleteValue = constraintAnnotation.logicDeleteValue();
62     }
63
64     @Override
65     public boolean isValid(String fieldValue, ConstraintValidatorContext context) {
66
67         if (ObjectUtil.isNull(fieldValue)) {
68             return true;
69         }
70
71         Class<?> validateGroupClass = RequestGroupContext.get();
72
73         // 如果属于add group,则校验库中所有行
74         if (BaseParam.add.class.equals(validateGroupClass)) {
75             return SystemContextHolder.me().tableUniValueFlag(createAddParam(fieldValue));
76         }
77
78         // 如果属于edit group,校验时需要排除当前修改的这条记录
79         if (BaseParam.edit.class.equals(validateGroupClass)) {
80             return SystemContextHolder.me().tableUniValueFlag(createEditParam(fieldValue));
81         }
82
83         // 默认校验所有的行
84         return SystemContextHolder.me().tableUniValueFlag(createAddParam(fieldValue));
85     }
86
87     /**
88      * 创建校验新增的参数
89      *
90      * @author xuyuxiang
91      * @date 2020/8/17 21:55
92      */
93     private UniqueValidateParam createAddParam(String fieldValue) {
94         return UniqueValidateParam.builder()
95                 .tableName(tableName)
96                 .columnName(columnName)
97                 .value(fieldValue)
98                 .excludeCurrentRecord(Boolean.FALSE)
99                 .excludeLogicDeleteItems(excludeLogicDeleteItems)
100                 .logicDeleteFieldName(logicDeleteFieldName)
101                 .logicDeleteValue(logicDeleteValue).build();
102     }
103
104     /**
105      * 创建修改的参数校验
106      *
107      * @author xuyuxiang
108      * @date 2020/8/17 21:56
109      */
110     private UniqueValidateParam createEditParam(String fieldValue) {
111         return UniqueValidateParam.builder()
112                 .tableName(tableName)
113                 .columnName(columnName)
114                 .value(fieldValue)
115                 .excludeCurrentRecord(Boolean.TRUE)
116                 .id(RequestParamIdContext.get())
117                 .excludeLogicDeleteItems(excludeLogicDeleteItems)
118                 .logicDeleteFieldName(logicDeleteFieldName)
119                 .logicDeleteValue(logicDeleteValue).build();
120     }
121
122 }