inleft
2022-08-31 aae48957ba971ed0a1bfbfa182701cd22bbe8694
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.sys.core.context;
26
27 import cn.hutool.core.lang.Dict;
28 import cn.hutool.core.util.ObjectUtil;
29 import cn.hutool.core.util.StrUtil;
30 import cn.hutool.log.Log;
31 import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
32 import org.springframework.stereotype.Component;
33 import vip.xiaonuo.core.context.system.SystemContext;
34 import vip.xiaonuo.core.pojo.base.validate.UniqueValidateParam;
35 import vip.xiaonuo.core.pojo.login.SysLoginUser;
36 import vip.xiaonuo.sys.modular.auth.service.AuthService;
37 import vip.xiaonuo.sys.modular.dict.service.SysDictDataService;
38 import vip.xiaonuo.sys.modular.role.entity.SysRole;
39 import vip.xiaonuo.sys.modular.role.param.SysRoleParam;
40 import vip.xiaonuo.sys.modular.role.service.SysRoleService;
41 import vip.xiaonuo.sys.modular.user.entity.SysUser;
42 import vip.xiaonuo.sys.modular.user.param.SysUserParam;
43 import vip.xiaonuo.sys.modular.user.service.SysUserService;
44
45 import javax.annotation.Resource;
46 import java.util.List;
47
48 /**
49  * 系统相关上下文接口实现类
50  *
51  * @author xuyuxiang
52  * @date 2020/5/6 14:59
53  */
54 @Component
55 public class SystemContextImpl implements SystemContext {
56
57     Log log = Log.get();
58
59     @Resource
60     private AuthService authService;
61
62     @Resource
63     private SysUserService sysUserService;
64
65     @Resource
66     private SysRoleService sysRoleService;
67
68     @Resource
69     private SysDictDataService sysDictDataService;
70
71     @Override
72     public String getNameByUserId(Long userId) {
73         return sysUserService.getNameByUserId(userId);
74     }
75
76     @Override
77     public String getNameByRoleId(Long roleId) {
78         return sysRoleService.getNameByRoleId(roleId);
79     }
80
81     @Override
82     public SysLoginUser getLoginUserByToken(String token) {
83         return authService.getLoginUserByToken(token);
84     }
85
86     @Override
87     public List<Dict> listUser(String account) {
88         SysUserParam sysUserParam = new SysUserParam();
89         if (ObjectUtil.isNotEmpty(account)) {
90             sysUserParam.setAccount(account);
91         }
92         return sysUserService.list(sysUserParam);
93     }
94
95     @Override
96     public List<Dict> listRole(String name) {
97         SysRoleParam sysRoleParam = new SysRoleParam();
98         if (ObjectUtil.isNotEmpty(name)) {
99             sysRoleParam.setName(name);
100         }
101         return sysRoleService.list(sysRoleParam);
102     }
103
104     @Override
105     public boolean isUser(Long userOrRoleId) {
106         SysUser sysUser = sysUserService.getById(userOrRoleId);
107         return !ObjectUtil.isNull(sysUser);
108     }
109
110     @Override
111     public boolean isRole(Long userOrRoleId) {
112         SysRole sysRole = sysRoleService.getById(userOrRoleId);
113         return !ObjectUtil.isNull(sysRole);
114     }
115
116     @Override
117     public List<String> getDictCodesByDictTypeCode(String... dictTypeCodes) {
118         return sysDictDataService.getDictCodesByDictTypeCode(dictTypeCodes);
119     }
120
121     @Override
122     public boolean tableUniValueFlag(UniqueValidateParam uniqueValidateParam) {
123         int resultCount = 0;
124
125         // 参数校验
126         paramValidate(uniqueValidateParam);
127
128         // 不排除当前记录,不排除逻辑删除的内容
129         if (!uniqueValidateParam.getExcludeCurrentRecord()
130                 && !uniqueValidateParam.getExcludeLogicDeleteItems()) {
131             resultCount = SqlRunner.db().selectCount(
132                     "select count(*) from " + uniqueValidateParam.getTableName() + " where " + uniqueValidateParam.getColumnName() + " = {0}",
133                     uniqueValidateParam.getValue());
134         }
135
136         // 不排除当前记录,排除逻辑删除的内容
137         if (!uniqueValidateParam.getExcludeCurrentRecord()
138                 && uniqueValidateParam.getExcludeLogicDeleteItems()) {
139             resultCount = SqlRunner.db().selectCount(
140                     "select count(*) from " + uniqueValidateParam.getTableName()
141                             + " where " + uniqueValidateParam.getColumnName() + " = {0} "
142                             + " and "
143                             + "(" + uniqueValidateParam.getLogicDeleteFieldName() + " is null || "
144                             + uniqueValidateParam.getLogicDeleteFieldName() + " <> " + uniqueValidateParam.getLogicDeleteValue() + ")",
145                     uniqueValidateParam.getValue());
146         }
147
148         // 排除当前记录,不排除逻辑删除的内容
149         if (uniqueValidateParam.getExcludeCurrentRecord()
150                 && !uniqueValidateParam.getExcludeLogicDeleteItems()) {
151
152             // id判空
153             paramIdValidate(uniqueValidateParam);
154
155             resultCount = SqlRunner.db().selectCount(
156                     "select count(*) from " + uniqueValidateParam.getTableName()
157                             + " where " + uniqueValidateParam.getColumnName() + " = {0} "
158                             + " and id <> {1}",
159                     uniqueValidateParam.getValue(), uniqueValidateParam.getId());
160         }
161
162         // 排除当前记录,排除逻辑删除的内容
163         if (uniqueValidateParam.getExcludeCurrentRecord()
164                 && uniqueValidateParam.getExcludeLogicDeleteItems()) {
165
166             // id判空
167             paramIdValidate(uniqueValidateParam);
168
169             resultCount = SqlRunner.db().selectCount(
170                     "select count(*) from " + uniqueValidateParam.getTableName()
171                             + " where " + uniqueValidateParam.getColumnName() + " = {0} "
172                             + " and id <> {1} "
173                             + " and "
174                             + "(" + uniqueValidateParam.getLogicDeleteFieldName() + " is null || "
175                             + uniqueValidateParam.getLogicDeleteFieldName() + " <> " + uniqueValidateParam.getLogicDeleteValue() + ")",
176                     uniqueValidateParam.getValue(), uniqueValidateParam.getId());
177         }
178
179         // 如果大于0,代表不是唯一的当前校验的值
180         return resultCount <= 0;
181     }
182
183     @Override
184     public List<Long> getAllUserIdList() {
185         return sysUserService.getAllUserIdList();
186     }
187
188     /**
189      * 几个参数的为空校验
190      *
191      * @author xuyuxiang
192      * @date 2020/8/17 22:00
193      */
194     private void paramValidate(UniqueValidateParam uniqueValidateParam) {
195         if (StrUtil.isBlank(uniqueValidateParam.getTableName())) {
196             throw new IllegalArgumentException("当前table字段值唯一性校验失败,tableName为空");
197         }
198         if (StrUtil.isBlank(uniqueValidateParam.getColumnName())) {
199             throw new IllegalArgumentException("当前table字段值唯一性校验失败,columnName为空");
200         }
201         if (StrUtil.isBlank(uniqueValidateParam.getValue())) {
202             throw new IllegalArgumentException("当前table字段值唯一性校验失败,字段值value为空");
203         }
204     }
205
206     /**
207      * id参数的为空校验
208      *
209      * @author xuyuxiang
210      * @date 2020/8/17 22:00
211      */
212     private void paramIdValidate(UniqueValidateParam uniqueValidateParam) {
213         if (uniqueValidateParam.getId() == null) {
214             throw new IllegalArgumentException("当前table字段值唯一性校验失败,id为空");
215         }
216     }
217
218 }