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.consts.service.impl; |
|
26 |
|
|
27 |
import cn.hutool.core.bean.BeanUtil; |
|
28 |
import cn.hutool.core.util.ObjectUtil; |
|
29 |
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
30 |
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
31 |
import org.springframework.stereotype.Service; |
|
32 |
import vip.xiaonuo.core.context.constant.ConstantContext; |
|
33 |
import vip.xiaonuo.core.enums.CommonStatusEnum; |
|
34 |
import vip.xiaonuo.core.enums.YesOrNotEnum; |
|
35 |
import vip.xiaonuo.core.exception.ServiceException; |
|
36 |
import vip.xiaonuo.core.factory.PageFactory; |
|
37 |
import vip.xiaonuo.core.pojo.page.PageResult; |
|
38 |
import vip.xiaonuo.sys.modular.consts.entity.SysConfig; |
|
39 |
import vip.xiaonuo.sys.modular.consts.enums.SysConfigExceptionEnum; |
|
40 |
import vip.xiaonuo.sys.modular.consts.mapper.SysConfigMapper; |
|
41 |
import vip.xiaonuo.sys.modular.consts.param.SysConfigParam; |
|
42 |
import vip.xiaonuo.sys.modular.consts.service.SysConfigService; |
|
43 |
|
|
44 |
import java.util.List; |
|
45 |
|
|
46 |
|
|
47 |
/** |
|
48 |
* 系统参数配置service接口实现类 |
|
49 |
* |
|
50 |
* @author xuyuxiang |
|
51 |
* @date 2020/4/14 11:16 |
|
52 |
*/ |
|
53 |
@Service |
|
54 |
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService { |
|
55 |
|
|
56 |
@Override |
|
57 |
public PageResult<SysConfig> page(SysConfigParam sysConfigParam) { |
|
58 |
|
|
59 |
//构造查询条件 |
|
60 |
LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>(); |
|
61 |
|
|
62 |
if (ObjectUtil.isNotNull(sysConfigParam)) { |
|
63 |
//如果名称不为空,则带上名称搜素搜条件 |
|
64 |
if (ObjectUtil.isNotEmpty(sysConfigParam.getName())) { |
|
65 |
queryWrapper.like(SysConfig::getName, sysConfigParam.getName()); |
|
66 |
} |
|
67 |
//如果常量编码不为空,则带上常量编码搜素搜条件 |
|
68 |
if (ObjectUtil.isNotEmpty(sysConfigParam.getCode())) { |
|
69 |
queryWrapper.like(SysConfig::getCode, sysConfigParam.getCode()); |
|
70 |
} |
|
71 |
//如果分类编码不为空,则带上分类编码 |
|
72 |
if (ObjectUtil.isNotEmpty(sysConfigParam.getGroupCode())) { |
|
73 |
queryWrapper.eq(SysConfig::getGroupCode, sysConfigParam.getGroupCode()); |
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
//查询未删除的 |
|
78 |
queryWrapper.ne(SysConfig::getStatus, CommonStatusEnum.DELETED.getCode()); |
|
79 |
|
|
80 |
//按类型升序排列,同类型的排在一起 |
|
81 |
queryWrapper.orderByDesc(SysConfig::getGroupCode); |
|
82 |
|
|
83 |
//查询分页结果 |
|
84 |
return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper)); |
|
85 |
} |
|
86 |
|
|
87 |
@Override |
|
88 |
public List<SysConfig> list(SysConfigParam sysConfigParam) { |
|
89 |
|
|
90 |
//构造条件 |
|
91 |
LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>(); |
|
92 |
|
|
93 |
//查询未删除的 |
|
94 |
wrapper.ne(SysConfig::getStatus, CommonStatusEnum.DELETED.getCode()); |
|
95 |
|
|
96 |
return this.list(wrapper); |
|
97 |
} |
|
98 |
|
|
99 |
@Override |
|
100 |
public SysConfig detail(SysConfigParam sysConfigParam) { |
|
101 |
return this.querySysConfig(sysConfigParam); |
|
102 |
} |
|
103 |
|
|
104 |
@Override |
|
105 |
public void add(SysConfigParam sysConfigParam) { |
|
106 |
|
|
107 |
//1.校验参数,是否有重复的名称和编码,不排除当前记录 |
|
108 |
checkRepeatParam(sysConfigParam, false); |
|
109 |
|
|
110 |
//2.构造实体 |
|
111 |
SysConfig sysConfig = new SysConfig(); |
|
112 |
BeanUtil.copyProperties(sysConfigParam, sysConfig); |
|
113 |
sysConfig.setStatus(CommonStatusEnum.ENABLE.getCode()); |
|
114 |
|
|
115 |
//3.保存到库中 |
|
116 |
this.save(sysConfig); |
|
117 |
|
|
118 |
//4.添加对应context |
|
119 |
ConstantContext.putConstant(sysConfigParam.getCode(), sysConfigParam.getValue()); |
|
120 |
} |
|
121 |
|
|
122 |
@Override |
|
123 |
public void delete(SysConfigParam sysConfigParam) { |
|
124 |
|
|
125 |
//1.根据id获取常量 |
|
126 |
SysConfig sysConfig = this.querySysConfig(sysConfigParam); |
|
127 |
|
|
128 |
//2.不能删除系统参数 |
|
129 |
if (YesOrNotEnum.Y.getCode().equals(sysConfig.getSysFlag())) { |
|
130 |
throw new ServiceException(SysConfigExceptionEnum.CONFIG_SYS_CAN_NOT_DELETE); |
|
131 |
} |
|
132 |
|
|
133 |
//3.设置状态为已删除 |
|
134 |
sysConfig.setStatus(CommonStatusEnum.DELETED.getCode()); |
|
135 |
this.updateById(sysConfig); |
|
136 |
|
|
137 |
//4.删除对应context |
|
138 |
ConstantContext.deleteConstant(sysConfigParam.getCode()); |
|
139 |
} |
|
140 |
|
|
141 |
@Override |
|
142 |
public void edit(SysConfigParam sysConfigParam) { |
|
143 |
|
|
144 |
//1.根据id获取常量信息 |
|
145 |
SysConfig sysConfig = this.querySysConfig(sysConfigParam); |
|
146 |
|
|
147 |
//2.校验参数,是否有重复的名称和编码,排除本条记录 |
|
148 |
checkRepeatParam(sysConfigParam, true); |
|
149 |
|
|
150 |
//请求参数转化为实体 |
|
151 |
BeanUtil.copyProperties(sysConfigParam, sysConfig); |
|
152 |
//不能修改状态,用修改状态接口修改状态 |
|
153 |
sysConfig.setStatus(null); |
|
154 |
|
|
155 |
//3.更新记录 |
|
156 |
this.updateById(sysConfig); |
|
157 |
|
|
158 |
//4.更新对应常量context |
|
159 |
ConstantContext.putConstant(sysConfigParam.getCode(), sysConfigParam.getValue()); |
|
160 |
} |
|
161 |
|
|
162 |
/** |
|
163 |
* 校验参数,是否有重复的名称和编码 |
|
164 |
* |
|
165 |
* @author xuyuxiang |
|
166 |
* @date 2020/4/14 11:18 |
|
167 |
*/ |
|
168 |
private void checkRepeatParam(SysConfigParam sysConfigParam, boolean isExcludeSelf) { |
|
169 |
Long id = sysConfigParam.getId(); |
|
170 |
String name = sysConfigParam.getName(); |
|
171 |
String code = sysConfigParam.getCode(); |
|
172 |
|
|
173 |
//构建带name和code的查询条件 |
|
174 |
LambdaQueryWrapper<SysConfig> queryWrapperByName = new LambdaQueryWrapper<>(); |
|
175 |
queryWrapperByName.eq(SysConfig::getName, name); |
|
176 |
|
|
177 |
LambdaQueryWrapper<SysConfig> queryWrapperByCode = new LambdaQueryWrapper<>(); |
|
178 |
queryWrapperByCode.eq(SysConfig::getCode, code); |
|
179 |
|
|
180 |
//如果排除自己,则增加查询条件主键id不等于本条id |
|
181 |
if (isExcludeSelf) { |
|
182 |
queryWrapperByName.ne(SysConfig::getId, id); |
|
183 |
queryWrapperByCode.ne(SysConfig::getId, id); |
|
184 |
} |
|
185 |
int countByName = this.count(queryWrapperByName); |
|
186 |
int countByCode = this.count(queryWrapperByCode); |
|
187 |
|
|
188 |
//如果存在重复的记录,抛出异常,直接返回前端 |
|
189 |
if (countByName >= 1) { |
|
190 |
throw new ServiceException(SysConfigExceptionEnum.CONFIG_NAME_REPEAT); |
|
191 |
} |
|
192 |
if (countByCode >= 1) { |
|
193 |
throw new ServiceException(SysConfigExceptionEnum.CONFIG_CODE_REPEAT); |
|
194 |
} |
|
195 |
} |
|
196 |
|
|
197 |
/** |
|
198 |
* 获取系统参数配置 |
|
199 |
* |
|
200 |
* @author xuyuxiang |
|
201 |
* @date 2020/4/14 11:19 |
|
202 |
*/ |
|
203 |
private SysConfig querySysConfig(SysConfigParam sysConfigParam) { |
|
204 |
SysConfig sysConfig = this.getById(sysConfigParam.getId()); |
|
205 |
if (ObjectUtil.isEmpty(sysConfig)) { |
|
206 |
throw new ServiceException(SysConfigExceptionEnum.CONFIG_NOT_EXIST); |
|
207 |
} |
|
208 |
return sysConfig; |
|
209 |
} |
|
210 |
} |