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.modular.app.service.impl; |
|
26 |
|
|
27 |
import cn.hutool.core.bean.BeanUtil; |
|
28 |
import cn.hutool.core.collection.CollectionUtil; |
|
29 |
import cn.hutool.core.lang.Dict; |
|
30 |
import cn.hutool.core.util.ObjectUtil; |
|
31 |
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
32 |
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
33 |
import org.springframework.stereotype.Service; |
|
34 |
import org.springframework.transaction.annotation.Transactional; |
|
35 |
import vip.xiaonuo.core.consts.CommonConstant; |
|
36 |
import vip.xiaonuo.core.enums.CommonStatusEnum; |
|
37 |
import vip.xiaonuo.core.enums.YesOrNotEnum; |
|
38 |
import vip.xiaonuo.core.exception.ServiceException; |
|
39 |
import vip.xiaonuo.core.factory.PageFactory; |
|
40 |
import vip.xiaonuo.core.pojo.page.PageResult; |
|
41 |
import vip.xiaonuo.sys.core.enums.AdminTypeEnum; |
|
42 |
import vip.xiaonuo.sys.modular.app.entity.SysApp; |
|
43 |
import vip.xiaonuo.sys.modular.app.enums.SysAppExceptionEnum; |
|
44 |
import vip.xiaonuo.sys.modular.app.mapper.SysAppMapper; |
|
45 |
import vip.xiaonuo.sys.modular.app.param.SysAppParam; |
|
46 |
import vip.xiaonuo.sys.modular.app.service.SysAppService; |
|
47 |
import vip.xiaonuo.sys.modular.menu.service.SysMenuService; |
|
48 |
import vip.xiaonuo.sys.modular.user.entity.SysUser; |
|
49 |
import vip.xiaonuo.sys.modular.user.service.SysUserService; |
|
50 |
|
|
51 |
import javax.annotation.Resource; |
|
52 |
import java.util.List; |
|
53 |
import java.util.concurrent.atomic.AtomicBoolean; |
|
54 |
|
|
55 |
/** |
|
56 |
* 系统应用service接口实现类 |
|
57 |
* |
|
58 |
* @author xuyuxiang |
|
59 |
* @date 2020/3/13 16:15 |
|
60 |
*/ |
|
61 |
@Service |
|
62 |
public class SysAppServiceImpl extends ServiceImpl<SysAppMapper, SysApp> implements SysAppService { |
|
63 |
|
|
64 |
@Resource |
|
65 |
private SysUserService sysUserService; |
|
66 |
|
|
67 |
@Resource |
|
68 |
private SysMenuService sysMenuService; |
|
69 |
|
|
70 |
@Override |
|
71 |
public List<Dict> getLoginApps(Long userId, List<Long> roleIdList) { |
|
72 |
List<Dict> userAppDictList = CollectionUtil.newArrayList(); |
|
73 |
|
|
74 |
LambdaQueryWrapper<SysApp> queryWrapper = new LambdaQueryWrapper<>(); |
|
75 |
queryWrapper.eq(SysApp::getStatus, CommonStatusEnum.ENABLE.getCode()); |
|
76 |
|
|
77 |
|
|
78 |
SysUser sysUser = sysUserService.getById(userId); |
|
79 |
Integer adminType = sysUser.getAdminType(); |
|
80 |
|
|
81 |
//如果不是超级管理员则有自己的菜单对应的应用编码 |
|
82 |
if (!AdminTypeEnum.SUPER_ADMIN.getCode().equals(adminType)) { |
|
83 |
//获取用户菜单对应的应用编码集合 |
|
84 |
List<String> appCodeList = sysMenuService.getUserMenuAppCodeList(userId, roleIdList); |
|
85 |
//当应用编码不为空时,则限制查询范围 |
|
86 |
if (ObjectUtil.isNotEmpty(appCodeList)) { |
|
87 |
queryWrapper.in(SysApp::getCode, appCodeList); |
|
88 |
} else { |
|
89 |
//没查到应用编码则直接返回 |
|
90 |
return userAppDictList; |
|
91 |
} |
|
92 |
} |
|
93 |
//定义是否有默认激活的应用标志 |
|
94 |
AtomicBoolean hasDefaultActive = new AtomicBoolean(false); |
|
95 |
//遍历 |
|
96 |
this.list(queryWrapper).forEach(sysApp -> { |
|
97 |
Dict dict = Dict.create(); |
|
98 |
dict.put(CommonConstant.CODE, sysApp.getCode()); |
|
99 |
dict.put(CommonConstant.NAME, sysApp.getName()); |
|
100 |
//如果有默认激活的 |
|
101 |
if (YesOrNotEnum.Y.getCode().equals(sysApp.getActive())) { |
|
102 |
hasDefaultActive.set(true); |
|
103 |
dict.put("active", true); |
|
104 |
//将其放在第一个 |
|
105 |
userAppDictList.add(0, dict); |
|
106 |
} else { |
|
107 |
dict.put("active", false); |
|
108 |
userAppDictList.add(dict); |
|
109 |
} |
|
110 |
|
|
111 |
}); |
|
112 |
if (ObjectUtil.isNotEmpty(userAppDictList)) { |
|
113 |
//如果默认激活的系统没有,则第一个为默认激活的系统 |
|
114 |
if (!hasDefaultActive.get()) { |
|
115 |
Dict dict = userAppDictList.get(0); |
|
116 |
dict.put("active", true); |
|
117 |
} |
|
118 |
} |
|
119 |
return userAppDictList; |
|
120 |
} |
|
121 |
|
|
122 |
@Override |
|
123 |
public PageResult<SysApp> page(SysAppParam sysAppParam) { |
|
124 |
LambdaQueryWrapper<SysApp> queryWrapper = new LambdaQueryWrapper<>(); |
|
125 |
if (ObjectUtil.isNotNull(sysAppParam)) { |
|
126 |
//根据名称模糊查询 |
|
127 |
if (ObjectUtil.isNotEmpty(sysAppParam.getName())) { |
|
128 |
queryWrapper.like(SysApp::getName, sysAppParam.getName()); |
|
129 |
} |
|
130 |
//根据编码模糊查询 |
|
131 |
if (ObjectUtil.isNotEmpty(sysAppParam.getCode())) { |
|
132 |
queryWrapper.like(SysApp::getCode, sysAppParam.getCode()); |
|
133 |
} |
|
134 |
} |
|
135 |
queryWrapper.eq(SysApp::getStatus, CommonStatusEnum.ENABLE.getCode()); |
|
136 |
return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper)); |
|
137 |
} |
|
138 |
|
|
139 |
@Override |
|
140 |
public void add(SysAppParam sysAppParam) { |
|
141 |
//校验参数,检查是否存在相同的名称和编码,以及默认激活的系统的数量是否合理 |
|
142 |
checkParam(sysAppParam, false); |
|
143 |
SysApp sysApp = new SysApp(); |
|
144 |
BeanUtil.copyProperties(sysAppParam, sysApp); |
|
145 |
sysApp.setStatus(CommonStatusEnum.ENABLE.getCode()); |
|
146 |
this.save(sysApp); |
|
147 |
} |
|
148 |
|
|
149 |
@Override |
|
150 |
public void delete(SysAppParam sysAppParam) { |
|
151 |
SysApp sysApp = this.querySysApp(sysAppParam); |
|
152 |
String code = sysApp.getCode(); |
|
153 |
//该应用下是否有状态为正常的菜单 |
|
154 |
boolean hasMenu = sysMenuService.hasMenu(code); |
|
155 |
//只要有,则不能删 |
|
156 |
if (hasMenu) { |
|
157 |
throw new ServiceException(SysAppExceptionEnum.APP_CANNOT_DELETE); |
|
158 |
} |
|
159 |
sysApp.setStatus(CommonStatusEnum.DELETED.getCode()); |
|
160 |
this.updateById(sysApp); |
|
161 |
} |
|
162 |
|
|
163 |
@Override |
|
164 |
public void edit(SysAppParam sysAppParam) { |
|
165 |
SysApp sysApp = this.querySysApp(sysAppParam); |
|
166 |
//校验参数,检查是否存在相同的名称和编码,以及默认激活的系统的数量是否合理 |
|
167 |
checkParam(sysAppParam, true); |
|
168 |
BeanUtil.copyProperties(sysAppParam, sysApp); |
|
169 |
//不能修改状态,用修改状态接口修改状态 |
|
170 |
sysApp.setStatus(null); |
|
171 |
this.updateById(sysApp); |
|
172 |
} |
|
173 |
|
|
174 |
@Override |
|
175 |
public SysApp detail(SysAppParam sysAppParam) { |
|
176 |
return this.querySysApp(sysAppParam); |
|
177 |
} |
|
178 |
|
|
179 |
@Override |
|
180 |
public List<SysApp> list(SysAppParam sysAppParam) { |
|
181 |
LambdaQueryWrapper<SysApp> appQueryWrapper = new LambdaQueryWrapper<>(); |
|
182 |
appQueryWrapper.eq(SysApp::getStatus, CommonStatusEnum.ENABLE.getCode()); |
|
183 |
return this.list(appQueryWrapper); |
|
184 |
} |
|
185 |
|
|
186 |
@Transactional(rollbackFor = Exception.class) |
|
187 |
@Override |
|
188 |
public void setAsDefault(SysAppParam sysAppParam) { |
|
189 |
SysApp currentApp = this.querySysApp(sysAppParam); |
|
190 |
//所有已激活的改为未激活 |
|
191 |
LambdaQueryWrapper<SysApp> appQueryWrapper = new LambdaQueryWrapper<>(); |
|
192 |
appQueryWrapper.eq(SysApp::getStatus, CommonStatusEnum.ENABLE.getCode()) |
|
193 |
.eq(SysApp::getActive, YesOrNotEnum.Y.getCode()); |
|
194 |
this.list(appQueryWrapper).forEach(sysApp -> { |
|
195 |
sysApp.setActive(YesOrNotEnum.N.getCode()); |
|
196 |
this.updateById(sysApp); |
|
197 |
}); |
|
198 |
//当前的设置为已激活 |
|
199 |
currentApp.setActive(YesOrNotEnum.Y.getCode()); |
|
200 |
this.updateById(currentApp); |
|
201 |
} |
|
202 |
|
|
203 |
/** |
|
204 |
* 校验参数,检查是否存在相同的名称和编码,以及默认激活的系统的数量是否合理 |
|
205 |
* |
|
206 |
* @author xuyuxiang |
|
207 |
* @date 2020/3/25 21:23 |
|
208 |
*/ |
|
209 |
private void checkParam(SysAppParam sysAppParam, boolean isExcludeSelf) { |
|
210 |
Long id = sysAppParam.getId(); |
|
211 |
String name = sysAppParam.getName(); |
|
212 |
String code = sysAppParam.getCode(); |
|
213 |
String active = sysAppParam.getActive(); |
|
214 |
|
|
215 |
// 查询名称有无重复 |
|
216 |
LambdaQueryWrapper<SysApp> appQueryWrapperByName = new LambdaQueryWrapper<>(); |
|
217 |
appQueryWrapperByName.eq(SysApp::getName, name) |
|
218 |
.ne(SysApp::getStatus, CommonStatusEnum.DELETED.getCode()); |
|
219 |
|
|
220 |
// 查询编码有无重复 |
|
221 |
LambdaQueryWrapper<SysApp> appQueryWrapperByCode = new LambdaQueryWrapper<>(); |
|
222 |
appQueryWrapperByCode.eq(SysApp::getCode, code) |
|
223 |
.ne(SysApp::getStatus, CommonStatusEnum.DELETED.getCode()); |
|
224 |
|
|
225 |
// 查询激活状态有无已经有Y的,也就是激活的 |
|
226 |
LambdaQueryWrapper<SysApp> appQueryWrapperByActive = new LambdaQueryWrapper<>(); |
|
227 |
appQueryWrapperByActive.eq(SysApp::getActive, active) |
|
228 |
.ne(SysApp::getStatus, CommonStatusEnum.DELETED.getCode()); |
|
229 |
|
|
230 |
if (isExcludeSelf) { |
|
231 |
appQueryWrapperByName.ne(SysApp::getId, id); |
|
232 |
appQueryWrapperByCode.ne(SysApp::getId, id); |
|
233 |
appQueryWrapperByActive.ne(SysApp::getId, id); |
|
234 |
} |
|
235 |
int countByName = this.count(appQueryWrapperByName); |
|
236 |
int countByCode = this.count(appQueryWrapperByCode); |
|
237 |
int countByActive = this.count(appQueryWrapperByActive); |
|
238 |
|
|
239 |
if (countByName >= 1) { |
|
240 |
throw new ServiceException(SysAppExceptionEnum.APP_NAME_REPEAT); |
|
241 |
} |
|
242 |
if (countByCode >= 1) { |
|
243 |
throw new ServiceException(SysAppExceptionEnum.APP_CODE_REPEAT); |
|
244 |
} |
|
245 |
|
|
246 |
// 只判断激活状态为Y时候数量是否大于1了 |
|
247 |
if (countByActive >= 1 && YesOrNotEnum.Y.getCode().equals(sysAppParam.getActive())) { |
|
248 |
throw new ServiceException(SysAppExceptionEnum.APP_ACTIVE_REPEAT); |
|
249 |
} |
|
250 |
} |
|
251 |
|
|
252 |
/** |
|
253 |
* 获取系统应用 |
|
254 |
* |
|
255 |
* @author xuyuxiang |
|
256 |
* @date 2020/3/26 9:56 |
|
257 |
*/ |
|
258 |
private SysApp querySysApp(SysAppParam sysAppParam) { |
|
259 |
SysApp sysApp = this.getById(sysAppParam.getId()); |
|
260 |
if (ObjectUtil.isNull(sysApp)) { |
|
261 |
throw new ServiceException(SysAppExceptionEnum.APP_NOT_EXIST); |
|
262 |
} |
|
263 |
return sysApp; |
|
264 |
} |
|
265 |
} |