1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package cn.chnmuseum.party.web.controller;
import cn.chnmuseum.party.common.log.MethodLog;
import cn.chnmuseum.party.common.log.OperModule;
import cn.chnmuseum.party.common.log.OperType;
import cn.chnmuseum.party.common.util.DateUtil80;
import cn.chnmuseum.party.common.util.ImportExcelUtil;
import cn.chnmuseum.party.common.vo.GenericPageParam;
import cn.chnmuseum.party.model.TArea;
import cn.chnmuseum.party.model.TOrgan;
import cn.chnmuseum.party.model.TUser;
import cn.chnmuseum.party.service.TAreaService;
import cn.chnmuseum.party.service.TOrganService;
import cn.chnmuseum.party.web.controller.base.BaseController;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* <pre>
* 机构 前端控制器
* </pre>
*
* @author Danny Lee
* @since 2021-03-22
*/
@Slf4j
@RestController
@RequestMapping("/organ")
@Api(tags = {"机构操作接口"})
public class TOrganController extends BaseController {
@Resource
private TOrganService tOrganService;
@Resource
private TAreaService tAreaService;
@PostMapping("/add")
@RequiresAuthentication //@RequiresPermissions("/organ/add")
@ApiOperation(value = "添加机构", notes = "添加机构")
@MethodLog(operModule = OperModule.ORG, operType = OperType.ADD)
public Map<String, Object> add(TOrgan organ) {
HashMap<String, Object> resultMap = new HashMap<>();
QueryWrapper<TOrgan> ew = new QueryWrapper<>();
if (StringUtils.isBlank(organ.getParentId())) {
organ.setParentId("0");
}
if (StringUtils.isNoneBlank(organ.getName().trim())) {
organ.setName(organ.getName().trim());
ew.eq("is_deleted", 0);
ew.eq("name", organ.getName());
List<TOrgan> list = this.tOrganService.list(ew);
if (list != null && list.size() > 0 && list.get(0) != null) {
resultMap.put("resultCode", "400");
resultMap.put("message", "机构名称不能重复!");
return resultMap;
}
} else {
resultMap.put("resultCode", "400");
resultMap.put("message", "机构名称不能为空!");
return resultMap;
}
organ.setCreateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
organ.setUpdateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
String organCode = getOrganCode(organ.getParentId());
organ.setLevel(organCode.length() / 3);
if (organ.getLevel() > 3) {
resultMap.put("resultCode", "400");
resultMap.put("message", "不能添加4级机构!");
return resultMap;
}
organ.setCode(organCode);
// 保存业务节点信息
boolean result = tOrganService.save(organ);
// 返回操作结果
if (result) {
return getSuccessResult();
} else {
// 保存失败
return getFailResult();
}
}
@PutMapping("/update")
@RequiresAuthentication //@RequiresPermissions("/organ/update")
@ApiOperation(value = "update", notes = "修改机构信息")
@MethodLog(operModule = OperModule.ORG, operType = OperType.UPDATE)
public Map<String, Object> updateTOrgan(TOrgan tOrgan) {
try {
if (tOrgan.getParentId() != null && tOrgan.getId() != null && tOrgan.getId().equals(tOrgan.getParentId())) {
HashMap<String, Object> resultMap = new HashMap<>(2);
resultMap.put("resultCode", "400");
resultMap.put("message", "父级机构不能是自己!");
return resultMap;
}
if (StringUtils.isBlank(tOrgan.getName().trim())) {
HashMap<String, Object> resultMap = new HashMap<>();
resultMap.put("resultCode", "400");
resultMap.put("message", "机构名称不能为空!");
return resultMap;
}
TOrgan byId = tOrganService.selectById(tOrgan.getId());
if (StringUtils.isNotBlank(tOrgan.getParentId())) {
if (!byId.getParentId().equals(tOrgan.getParentId())) {
tOrgan.setCode(getOrganCode(tOrgan.getParentId()));
}
}else {
tOrgan.setParentId("0");
tOrgan.setCode(getOrganCode(tOrgan.getParentId()));
}
if (StringUtils.isNotBlank(tOrgan.getCode())) {
if (byId.getChildren().size() > 0 && !byId.getParentId().equals(tOrgan.getParentId())) {
HashMap<String, Object> map = new HashMap<>();
map.put("resultCode", "400");
map.put("message", "机构存在下级机构,不能修改机构级别!");
map.put("data", "");
return map;
}
tOrgan.setLevel(tOrgan.getCode().length() / 3);
if (tOrgan.getLevel() > 3) {
HashMap<String, Object> resultMap = new HashMap<>();
resultMap.put("resultCode", "400");
resultMap.put("message", "不能修改机构为4级!");
return resultMap;
}
}
// 机构存在机顶盒用户,不让修改地理位置和名称
TOrgan oldOrgan = tOrganService.getById(tOrgan.getId());
List<TUser> list = userService.list(new UpdateWrapper<TUser>().eq("org_id", tOrgan.getId()).eq("is_deleted", "0"));
if (CollectionUtil.isNotEmpty(list) ) {
if (!oldOrgan.getAreaId().equals(tOrgan.getAreaId())) {
return getFailResult("机构存在机顶盒账号,不能修改地理位置!");
}
if (!oldOrgan.getName().equals(tOrgan.getName())) {
return getFailResult("机构存在机顶盒账号,不能修改名称!");
}
}
tOrgan.setUpdateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
boolean flag;
flag = tOrganService.updateById(tOrgan);
if (!flag) {
return getFailResult();
}
return getSuccessResult();
} catch (Exception e) {
e.printStackTrace();
}
return getFailResult();
}
@DeleteMapping("/delete")
@RequiresAuthentication //@RequiresPermissions("/organ/delete")
@ApiOperation(value = "根据ID删除机构", notes = "根据ID删除机构")
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "id", value = "标识ID", paramType = "query", dataType = "String")
})
@MethodLog(operModule = OperModule.ORG, operType = OperType.DELETE)
public Map<String, Object> deleteTOrgan(String id) {
boolean result;
try {
List<TUser> list = userService.list(new UpdateWrapper<TUser>().eq("org_id", id).eq("is_deleted", "0"));
if (list!=null&&list.size()>0&&list.get(0)!=null){
HashMap<String, Object> map = new HashMap<>();
map.put("resultCode","500");
map.put("message","机构下存在用户,不可以删除!");
map.put("data","");
return map;
}
TOrgan tOrgan = tOrganService.getById(id);
tOrgan.setIsDeleted(true);
tOrgan.setUpdateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
result = tOrganService.removeById(tOrgan);
if (!result) {
return getFailResult();
}
return getSuccessResult();
} catch (Exception e) {
e.printStackTrace();
}
return getFailResult();
}
@GetMapping("/getList")
@RequiresAuthentication //@RequiresPermissions("/organ/getList")
@ApiOperation(value = "获取机构全部列表(无分页)", notes = "获取机构全部列表(无分页)")
@MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
public Map<String, Object> getTOrganList() {
TUser user;
try {
user = getcurUser();
TOrgan tOrgan = new TOrgan();
if (user.getRoleList().size() > 0 && !user.getRoleList().contains("1")) {
//设置用户数据权限
tOrgan.setCode(user.getOrgCode());
}
List<TOrgan> list = tOrganService.getAllList(tOrgan);
return getResult(list);
} catch (Exception e) {
e.printStackTrace();
}
return getFailResult();
}
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
@ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
@ApiImplicitParam(name = "nameOrCode", value = "名称或编码", paramType = "query", dataType = "String"),
@ApiImplicitParam(name = "startDate", value = "创建时间-开始", paramType = "query", dataType = "String"),
@ApiImplicitParam(name = "endDate", value = "创建时间-结束", paramType = "query", dataType = "String")
})
@PostMapping("/getPageList")
@RequiresAuthentication //@RequiresPermissions("/organ/getPageList")
@ApiOperation(value = "获取机构分页列表", notes = "获取机构分页列表")
@MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
public Map<String, Object> getTOrganPageList(GenericPageParam genericPageParam) {
TUser user = getcurUser();
Page<TOrgan> page;
try {
LambdaQueryWrapper<TOrgan> queryWrapper = new LambdaQueryWrapper<>();
// 对名称或编码模糊查询
if (StringUtils.isNotBlank(genericPageParam.getNameOrCode())) {
queryWrapper.like(TOrgan::getName, genericPageParam.getNameOrCode());
}
// 根据创建时间区间检索
if (genericPageParam.getStartDate() != null && genericPageParam.getEndDate() != null) {
queryWrapper.ge(TOrgan::getCreateTime, genericPageParam.getStartDate().atTime(0, 0, 0))
.le(TOrgan::getCreateTime, genericPageParam.getEndDate().atTime(23, 59, 59));
}
if (user.getRoleList().size() > 0 && !user.getRoleList().contains("1")) {
//设置用户数据权限
queryWrapper.likeRight(TOrgan::getCode, user.getOrgCode());
}
queryWrapper.eq(TOrgan::getIsDeleted, false);
// 设置排序规则
queryWrapper.orderByDesc(TOrgan::getCreateTime);
page = this.tOrganService.page(getPage(), queryWrapper);
return getResult(page);
} catch (Exception e) {
e.printStackTrace();
}
return getFailResult();
}
@ApiOperation(value = "获取机构详情", notes = "获取机构详情")
@GetMapping("/getById")
@RequiresAuthentication //@RequiresPermissions("/organ/getById")
@MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
public Map<String, Object> getById(String id) {
TOrgan tOrgan;
try {
tOrgan = tOrganService.selectById(id);
return getResult(tOrgan);
} catch (Exception e) {
e.printStackTrace();
}
return getFailResult();
}
@GetMapping("/getTree")
@RequiresAuthentication //@RequiresPermissions("/organ/getTree")
@ApiOperation(value = "获取机构树", notes = "获取机构树")
@MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
public Map<String, Object> getTree(String name) {
TUser user = getcurUser();
List<TOrgan> list;
UpdateWrapper<TOrgan> wrapper = new UpdateWrapper<>();
try {
if (user.getRoleList().size() > 0 && !user.getRoleList().contains("1")) {
//设置用户数据权限
wrapper.likeRight("code", user.getOrgCode());
} else {
user.setOrgCode("");
}
if (StringUtils.isBlank(name)) {
list = tOrganService.getTree(user.getOrgCode());
} else {
wrapper.like("name", name);
list = tOrganService.list(wrapper);
}
return getResult(list);
} catch (Exception e) {
e.printStackTrace();
}
return getFailResult();
}
@GetMapping("/getAreaTree")
@RequiresAuthentication //@RequiresPermissions("/organ/getAreaTree")
@ApiOperation(value = "获取区域树", notes = "获取区域树")
@MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
public Map<String, Object> getAreaTree() {
List<TArea> list;
try {
list = tAreaService.getAreaTree();
return getResult(list);
} catch (Exception e) {
e.printStackTrace();
}
return getFailResult();
}
// 导入EXCEL
@ApiOperation(value = "导入EXCEL", notes = "导入EXCEL", httpMethod = "POST")
@RequestMapping(value = "/import", method = RequestMethod.POST)
@RequiresAuthentication //@RequiresPermissions("/organ/import")
@MethodLog(operModule = OperModule.ORG, operType = OperType.IMPORT)
public Map<String, Object> upload(MultipartFile file) {
Map<String, Object> resultMap = new LinkedHashMap<>();
try {
boolean flag;
//使用工具类从文件中读取数据
final List<Map<String, String>> excelList = ImportExcelUtil.readExcel(file.getOriginalFilename(), file.getInputStream());
flag = tOrganService.batchUpload(excelList);
if (!flag) {
resultMap.put("resultCode", "500");
resultMap.put("message", "导入失败!");
return resultMap;
}
resultMap.put("resultCode", "200");
resultMap.put("message", "导入成功!");
return resultMap;
} catch (Exception e) {
resultMap.put("resultCode", "500");
resultMap.put("message", e.getMessage());
return resultMap;
}
}
private String getOrganCode(String parentId) {
TOrgan max = tOrganService.selectCodeMax(parentId);
TOrgan byId = tOrganService.getById(parentId);
if (max == null) {
if (byId != null) {
return byId.getCode() + "001";
} else {
return "001";
}
}
String s = max.getCode().substring(max.getCode().length() - 3);
String s1 = max.getCode().substring(0, max.getCode().length() - 3);
int integer = Integer.parseInt(s) + 1;
if (integer < 10) {
s1 = s1 + "00" + integer;
} else if (integer < 100) {
s1 = s1 + "0" + integer;
} else {
s1 = s1 + integer;
}
return s1;
}
}