RoleController.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. package jnpf.permission.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import cn.dev33.satoken.annotation.SaMode;
  4. import cn.hutool.core.collection.CollectionUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import io.swagger.v3.oas.annotations.Operation;
  7. import io.swagger.v3.oas.annotations.Parameter;
  8. import io.swagger.v3.oas.annotations.Parameters;
  9. import io.swagger.v3.oas.annotations.tags.Tag;
  10. import jakarta.validation.Valid;
  11. import jnpf.base.ActionResult;
  12. import jnpf.base.controller.SuperController;
  13. import jnpf.base.util.ExcelTool;
  14. import jnpf.base.vo.DownloadVO;
  15. import jnpf.base.vo.ListVO;
  16. import jnpf.base.vo.PaginationVO;
  17. import jnpf.config.ConfigValueUtil;
  18. import jnpf.constant.FileTypeConstant;
  19. import jnpf.constant.MsgCode;
  20. import jnpf.constant.PermissionConst;
  21. import jnpf.exception.DataException;
  22. import jnpf.model.ExcelColumnAttr;
  23. import jnpf.model.ExcelImportForm;
  24. import jnpf.model.ExcelImportVO;
  25. import jnpf.model.ExcelModel;
  26. import jnpf.permission.constant.RoleColumnMap;
  27. import jnpf.permission.entity.RoleEntity;
  28. import jnpf.permission.entity.RoleRelationEntity;
  29. import jnpf.permission.model.check.CheckResult;
  30. import jnpf.permission.model.position.PosConModel;
  31. import jnpf.permission.model.role.*;
  32. import jnpf.permission.model.user.mod.UserIdModel;
  33. import jnpf.permission.service.OrganizeService;
  34. import jnpf.permission.service.RoleRelationService;
  35. import jnpf.permission.service.RoleService;
  36. import jnpf.util.*;
  37. import org.springframework.beans.factory.annotation.Autowired;
  38. import org.springframework.transaction.annotation.Transactional;
  39. import org.springframework.web.bind.annotation.*;
  40. import java.io.IOException;
  41. import java.util.*;
  42. import java.util.stream.Collectors;
  43. /**
  44. * 角色管理
  45. *
  46. * @author JNPF开发平台组
  47. * @version V3.1.0
  48. * @copyright 引迈信息技术有限公司
  49. * @date 2019年9月26日 上午9:18
  50. */
  51. @Tag(name = "角色管理", description = "Role")
  52. @RestController
  53. @RequestMapping("/api/permission/Role")
  54. public class RoleController extends SuperController<RoleService, RoleEntity> {
  55. @Autowired
  56. private RoleService roleService;
  57. @Autowired
  58. private RoleRelationService roleRelationService;
  59. @Autowired
  60. private OrganizeService organizeService;
  61. @Autowired
  62. private ConfigValueUtil configValueUtil;
  63. @Operation(summary = "获取角色列表")
  64. @SaCheckPermission(value = {"permission.auth", "permission.role"}, mode = SaMode.OR)
  65. @GetMapping
  66. public ActionResult list(RolePagination pagination) {
  67. pagination.setDataType(1);
  68. if (StringUtil.isNotEmpty(pagination.getKeyword())) {
  69. pagination.setDataType(null);
  70. }
  71. List<RoleEntity> list = roleService.getList(pagination);
  72. List<RoleListVO> listVO = new ArrayList<>();
  73. for (RoleEntity entity : list) {
  74. // 角色类型展示
  75. RoleListVO vo = JsonUtil.getJsonToBean(entity, RoleListVO.class);
  76. if (Objects.equals(entity.getGlobalMark(), 1)) {
  77. vo.setIsSystem(1);
  78. }
  79. listVO.add(vo);
  80. }
  81. PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
  82. return ActionResult.page(listVO, paginationVO);
  83. }
  84. @Operation(summary = "新建角色")
  85. @Parameters({
  86. @Parameter(name = "roleCrForm", description = "角色模型", required = true)
  87. })
  88. @SaCheckPermission("permission.role")
  89. @PostMapping
  90. @Transactional
  91. public ActionResult<String> create(@RequestBody @Valid RoleCrForm roleCrForm) {
  92. RoleEntity entity = JsonUtil.getJsonToBean(roleCrForm, RoleEntity.class);
  93. if (roleService.isExistByFullName(roleCrForm.getFullName(), null, roleCrForm.getType())) {
  94. return ActionResult.fail(MsgCode.EXIST001.get());
  95. }
  96. if (roleService.isExistByEnCode(roleCrForm.getEnCode(), null)) {
  97. return ActionResult.fail(MsgCode.EXIST002.get());
  98. }
  99. //约束判断
  100. PosConModel posConModel = new PosConModel();
  101. if (PermissionConst.USER.equals(entity.getType()) && Objects.equals(entity.getIsCondition(), 1)) {
  102. posConModel = JsonUtil.getJsonToBean(entity.getConditionJson(), PosConModel.class);
  103. posConModel.init();
  104. String errStr = posConModel.checkCondition(entity.getId());
  105. if (StringUtil.isNotEmpty(errStr)) {
  106. return ActionResult.fail(errStr);
  107. }
  108. if (posConModel.getPrerequisiteFlag()) {
  109. for (String item : posConModel.getPrerequisite()) {
  110. RoleEntity itemInfo = roleService.getInfo(item);
  111. //先决只能1级
  112. if (Objects.equals(itemInfo.getIsCondition(), 1)) {
  113. PosConModel itemModel = JsonUtil.getJsonToBean(itemInfo.getConditionJson(), PosConModel.class);
  114. itemModel.init();
  115. if (itemModel.getPrerequisiteFlag()) {
  116. return ActionResult.fail(MsgCode.SYS143.get());
  117. }
  118. }
  119. }
  120. }
  121. }
  122. roleService.create(entity);
  123. roleService.linkUpdate(entity.getId(), posConModel);
  124. return ActionResult.success(MsgCode.SU001.get());
  125. }
  126. @Operation(summary = "更新角色")
  127. @Parameters({
  128. @Parameter(name = "roleUpForm", description = "角色模型", required = true),
  129. @Parameter(name = "id", description = "主键值", required = true)
  130. })
  131. @SaCheckPermission("permission.role")
  132. @PutMapping("/{id}")
  133. @Transactional
  134. public ActionResult<String> update(@RequestBody @Valid RoleUpForm roleUpForm, @PathVariable("id") String id) throws DataException {
  135. RoleEntity info = roleService.getInfo(id);
  136. if (info == null) {
  137. return ActionResult.fail(MsgCode.FA001.get());
  138. }
  139. RoleEntity entity = JsonUtil.getJsonToBean(roleUpForm, RoleEntity.class);
  140. entity.setId(id);
  141. if (roleService.isExistByFullName(roleUpForm.getFullName(), id, roleUpForm.getType())) {
  142. return ActionResult.fail(MsgCode.EXIST001.get());
  143. }
  144. if (roleService.isExistByEnCode(roleUpForm.getEnCode(), id)) {
  145. return ActionResult.fail(MsgCode.EXIST002.get());
  146. }
  147. PosConModel posConModel = new PosConModel();
  148. //约束判断
  149. if (PermissionConst.USER.equals(entity.getType()) && Objects.equals(entity.getIsCondition(), 1)) {
  150. posConModel = JsonUtil.getJsonToBean(entity.getConditionJson(), PosConModel.class);
  151. posConModel.init();
  152. String errStr = posConModel.checkCondition(entity.getId());
  153. if (StringUtil.isNotEmpty(errStr)) {
  154. return ActionResult.fail(errStr);
  155. }
  156. //修改角色的时候,角色有人的话,互斥角色(选择的角色下的人一个都不能重叠),先决角色(选择的角色下的人必须包含当前角色人员)
  157. Set<String> userIds = roleRelationService.getListByRoleId(entity.getId(), PermissionConst.USER)
  158. .stream().map(RoleRelationEntity::getObjectId).collect(Collectors.toSet());
  159. if (posConModel.getMutualExclusionFlag()) {
  160. for (String item : posConModel.getMutualExclusion()) {
  161. Set<String> itemUserIds = roleRelationService.getListByRoleId(item, PermissionConst.USER)
  162. .stream().map(RoleRelationEntity::getObjectId).collect(Collectors.toSet());
  163. Set<String> commonIds = new HashSet<>(userIds);
  164. commonIds.retainAll(itemUserIds);
  165. if (!commonIds.isEmpty()) {
  166. return ActionResult.fail(MsgCode.SYS137.get());
  167. }
  168. }
  169. }
  170. if (posConModel.getPrerequisiteFlag()) {
  171. //获取全部的先决列表。用于判断当前是否开启先决(存在就不能开启)
  172. List<RoleEntity> list = roleService.getList(true, PermissionConst.USER, null);
  173. Set<String> allPrePos = new HashSet<>();
  174. for (RoleEntity t : list) {
  175. if (Objects.equals(t.getIsCondition(), 1)) {
  176. PosConModel tm = JsonUtil.getJsonToBean(t.getConditionJson(), PosConModel.class);
  177. tm.init();
  178. if (tm.getPrerequisiteFlag()) {
  179. allPrePos.addAll(tm.getPrerequisite());
  180. }
  181. }
  182. }
  183. if (allPrePos.contains(id)) {
  184. return ActionResult.fail(MsgCode.SYS143.get());
  185. }
  186. //用户冲突--当前角色里的用户要判断先决包含先决的所有角色
  187. if (CollectionUtil.isNotEmpty(userIds)) {
  188. for (String userId : userIds) {
  189. Set<String> roleIds = roleRelationService.getListByObjectId(userId, PermissionConst.USER)
  190. .stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toSet());
  191. //用户冲突,先决修改失败
  192. if (!roleIds.containsAll(posConModel.getPrerequisite())) {
  193. return ActionResult.fail(MsgCode.SYS138.get());
  194. }
  195. }
  196. }
  197. //先决只能1级--选中的先决自己不能是先决
  198. for (String item : posConModel.getPrerequisite()) {
  199. RoleEntity itemInfo = roleService.getInfo(item);
  200. if (Objects.equals(itemInfo.getIsCondition(), 1)) {
  201. PosConModel itemModel = JsonUtil.getJsonToBean(itemInfo.getConditionJson(), PosConModel.class);
  202. itemModel.init();
  203. if (itemModel.getPrerequisiteFlag()) {
  204. return ActionResult.fail(MsgCode.SYS143.get());
  205. }
  206. }
  207. }
  208. }
  209. }
  210. boolean flag = roleService.update(id, entity);
  211. if (!flag) {
  212. return ActionResult.fail(MsgCode.FA002.get());
  213. }
  214. roleService.linkUpdate(id, posConModel);
  215. return ActionResult.success(MsgCode.SU004.get());
  216. }
  217. @Operation(summary = "获取角色信息")
  218. @Parameters({
  219. @Parameter(name = "id", description = "主键值", required = true)
  220. })
  221. @SaCheckPermission("permission.role")
  222. @GetMapping("/{id}")
  223. public ActionResult<RoleInfoVO> getInfo(@PathVariable("id") String id) throws DataException {
  224. RoleEntity entity = roleService.getInfo(id);
  225. RoleInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, RoleInfoVO.class);
  226. //判断当前角色是否已经是先决角色
  227. Set<String> allPrePos = new HashSet<>();
  228. List<RoleEntity> list = roleService.getList(false, PermissionConst.USER, 0);
  229. for (RoleEntity t : list) {
  230. if (Objects.equals(t.getIsCondition(), 1)) {
  231. PosConModel tm = JsonUtil.getJsonToBean(t.getConditionJson(), PosConModel.class);
  232. tm.init();
  233. if (tm.getPrerequisiteFlag()) {
  234. allPrePos.addAll(tm.getPrerequisite());
  235. }
  236. }
  237. }
  238. if (allPrePos.contains(id)) {
  239. vo.setIsPrePosition(true);
  240. }
  241. return ActionResult.success(vo);
  242. }
  243. @Operation(summary = "删除角色")
  244. @Parameters({
  245. @Parameter(name = "id", description = "主键值", required = true)
  246. })
  247. @SaCheckPermission("permission.role")
  248. @DeleteMapping("/{id}")
  249. public ActionResult<String> delete(@PathVariable("id") String id) {
  250. // 当角色绑定用户不让其删除
  251. // if (roleRelationService.getListByRoleId(id, null).size() > 0) {
  252. // return ActionResult.fail(MsgCode.FA024.get());
  253. // }
  254. RoleEntity entity = roleService.getInfo(id);
  255. if (entity != null) {
  256. roleService.delete(entity);
  257. return ActionResult.success(MsgCode.SU003.get());
  258. }
  259. return ActionResult.fail(MsgCode.FA003.get());
  260. }
  261. //+++++++++++++++++++++++++++++++++其他接口++++++++++++++++++++++++++++++++++++++++++++++++
  262. @Operation(summary = "角色下拉框列表")
  263. @GetMapping("/Selector")
  264. public ActionResult<ListVO<RoleSelectorVO>> listAll(@RequestParam(name = "type", required = false) String type,
  265. @RequestParam(name = "isSystem", required = false) Integer isSystem) {
  266. List<RoleEntity> list = roleService.getList(true, type, isSystem);
  267. List<RoleSelectorVO> modelList = JsonUtil.getJsonToList(list, RoleSelectorVO.class);
  268. for (RoleSelectorVO vo : modelList) {
  269. vo.setIcon(PermissionConst.ROLE_ICON);
  270. }
  271. ListVO vo = new ListVO();
  272. vo.setList(modelList);
  273. return ActionResult.success(vo);
  274. }
  275. @Operation(summary = "获取角色下拉框(自定义范围)")
  276. @Parameters({
  277. @Parameter(name = "idModel", description = "ids", required = true)
  278. })
  279. @PostMapping("/RoleCondition")
  280. public ActionResult<List<RoleSelectorVO>> roleCondition(@RequestBody UserIdModel idModel) {
  281. List<RoleEntity> list = roleService.getListByIds(idModel.getIds());
  282. list = list.stream().filter(t -> !Objects.equals(t.getGlobalMark(), 1)).collect(Collectors.toList());
  283. List<RoleSelectorVO> modelList = JsonUtil.getJsonToList(list, RoleSelectorVO.class);
  284. for (RoleSelectorVO vo : modelList) {
  285. vo.setIcon(PermissionConst.ROLE_ICON);
  286. }
  287. return ActionResult.success(modelList);
  288. }
  289. @Operation(summary = "模板下载")
  290. @SaCheckPermission("permission.role")
  291. @GetMapping("/TemplateDownload")
  292. public ActionResult<DownloadVO> TemplateDownload() {
  293. RoleColumnMap columnMap = new RoleColumnMap();
  294. String excelName = columnMap.getExcelName();
  295. Map<String, String> keyMap = columnMap.getColumnByType(0);
  296. List<ExcelColumnAttr> models = columnMap.getFieldsModel(false);
  297. List<Map<String, Object>> list = columnMap.getDefaultList();
  298. Map<String, String[]> optionMap = getOptionMap();
  299. ExcelModel excelModel = ExcelModel.builder().models(models).selectKey(new ArrayList<>(keyMap.keySet())).optionMap(optionMap).build();
  300. DownloadVO vo = ExcelTool.getImportTemplate(FileTypeConstant.TEMPORARY, excelName, keyMap, list, excelModel);
  301. return ActionResult.success(vo);
  302. }
  303. /**
  304. * 上传Excel
  305. *
  306. * @return
  307. */
  308. @Operation(summary = "上传导入Excel")
  309. @SaCheckPermission("permission.role")
  310. @PostMapping("/Uploader")
  311. public ActionResult<Object> Uploader() {
  312. return ExcelTool.uploader();
  313. }
  314. /**
  315. * 导入预览
  316. *
  317. * @return
  318. */
  319. @Operation(summary = "导入预览")
  320. @SaCheckPermission("permission.role")
  321. @GetMapping("/ImportPreview")
  322. public ActionResult<Map<String, Object>> ImportPreview(String fileName) throws Exception {
  323. // 导入字段
  324. RoleColumnMap columnMap = new RoleColumnMap();
  325. Map<String, String> keyMap = columnMap.getColumnByType(0);
  326. Map<String, Object> headAndDataMap = ExcelTool.importPreview(FileTypeConstant.TEMPORARY, fileName, keyMap);
  327. return ActionResult.success(headAndDataMap);
  328. }
  329. /**
  330. * 导出异常报告
  331. *
  332. * @return
  333. */
  334. @Operation(summary = "导出异常报告")
  335. @SaCheckPermission("permission.role")
  336. @PostMapping("/ExportExceptionData")
  337. public ActionResult<DownloadVO> ExportExceptionData(@RequestBody ExcelImportForm visualImportModel) {
  338. List<Map<String, Object>> dataList = visualImportModel.getList();
  339. RoleColumnMap columnMap = new RoleColumnMap();
  340. String excelName = columnMap.getExcelName();
  341. Map<String, String> keyMap = columnMap.getColumnByType(0);
  342. List<ExcelColumnAttr> models = columnMap.getFieldsModel(true);
  343. ExcelModel excelModel = ExcelModel.builder().optionMap(getOptionMap()).models(models).build();
  344. DownloadVO vo = ExcelTool.exportExceptionReport(FileTypeConstant.TEMPORARY, excelName, keyMap, dataList, excelModel);
  345. return ActionResult.success(vo);
  346. }
  347. /**
  348. * 导入数据
  349. *
  350. * @return
  351. */
  352. @Operation(summary = "导入数据")
  353. @SaCheckPermission("permission.role")
  354. @PostMapping("/ImportData")
  355. public ActionResult<ExcelImportVO> ImportData(@RequestBody ExcelImportForm visualImportModel) throws Exception {
  356. List<Map<String, Object>> listData = new ArrayList<>();
  357. List<Map<String, Object>> headerRow = new ArrayList<>();
  358. if (visualImportModel.isType()) {
  359. ActionResult result = ImportPreview(visualImportModel.getFileName());
  360. if (result == null) {
  361. throw new Exception(MsgCode.FA018.get());
  362. }
  363. if (result.getCode() != 200) {
  364. throw new Exception(result.getMsg());
  365. }
  366. if (result.getData() instanceof Map) {
  367. Map<String, Object> data = (Map<String, Object>) result.getData();
  368. listData = (List<Map<String, Object>>) data.get("dataRow");
  369. headerRow = (List<Map<String, Object>>) data.get("headerRow");
  370. }
  371. } else {
  372. listData = visualImportModel.getList();
  373. }
  374. List<RoleEntity> addList = new ArrayList<RoleEntity>();
  375. List<Map<String, Object>> failList = new ArrayList<>();
  376. Map<String, List<List<String>>> addOrganizeIdMap = new HashMap<>();
  377. // 对数据做校验
  378. this.validateImportData(listData, addList, addOrganizeIdMap, failList);
  379. //正常数据插入
  380. for (RoleEntity each : addList) {
  381. roleService.create(each);
  382. }
  383. ExcelImportVO importModel = new ExcelImportVO();
  384. importModel.setSnum(addList.size());
  385. importModel.setFnum(failList.size());
  386. importModel.setResultType(failList.size() > 0 ? 1 : 0);
  387. importModel.setFailResult(failList);
  388. importModel.setHeaderRow(headerRow);
  389. return ActionResult.success(importModel);
  390. }
  391. private void validateImportData(List<Map<String, Object>> listData, List<RoleEntity> addList, Map<String, List<List<String>>> addOrganizeIdMap, List<Map<String, Object>> failList) {
  392. RoleColumnMap columnMap = new RoleColumnMap();
  393. Map<String, String> keyMap = columnMap.getColumnByType(0);
  394. QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
  395. queryWrapper.lambda().isNull(RoleEntity::getDeleteMark);
  396. queryWrapper.lambda().orderByAsc(RoleEntity::getSortCode).orderByDesc(RoleEntity::getCreatorTime);
  397. List<RoleEntity> allPositionList = roleService.list(queryWrapper);
  398. Map<String, Object> allOrgsTreeName = organizeService.getAllOrgsTreeName();
  399. for (int i = 0, len = listData.size(); i < len; i++) {
  400. Map<String, Object> eachMap = listData.get(i);
  401. Map<String, Object> realMap = JsonUtil.getJsonToBean(eachMap, Map.class);
  402. StringJoiner errInfo = new StringJoiner(",");
  403. int globalMark = "组织".equals(eachMap.get("globalMark")) ? 0 : 1;
  404. List<List<String>> organizeIdList = null;
  405. for (String column : keyMap.keySet()) {
  406. Object valueObj = eachMap.get(column);
  407. String value = valueObj == null ? null : String.valueOf(valueObj);
  408. String columnName = keyMap.get(column);
  409. switch (column) {
  410. case "fullName":
  411. if (StringUtil.isEmpty(value)) {
  412. errInfo.add(columnName + "不能为空");
  413. break;
  414. }
  415. if (value.length() > 50) {
  416. errInfo.add(columnName + "值超出最多输入字符限制");
  417. }
  418. //值不能含有特殊符号
  419. if (!RegexUtils.checkSpecoalSymbols(value)) {
  420. errInfo.add(columnName + "值不能含有特殊符号");
  421. }
  422. //库里重复
  423. int finalGlobalMark = globalMark;
  424. long fullNameCount = allPositionList.stream().filter(t -> t.getFullName().equals(value) && t.getGlobalMark().equals(finalGlobalMark)).count();
  425. if (fullNameCount > 0) {
  426. errInfo.add(columnName + "值已存在");
  427. break;
  428. }
  429. //表格内重复
  430. fullNameCount = addList.stream().filter(t -> t.getFullName().equals(value) && t.getGlobalMark().equals(finalGlobalMark)).count();
  431. if (fullNameCount > 0) {
  432. errInfo.add(columnName + "值已存在");
  433. break;
  434. }
  435. break;
  436. case "enCode":
  437. if (StringUtil.isEmpty(value)) {
  438. errInfo.add(columnName + "不能为空");
  439. break;
  440. }
  441. if (value.length() > 50) {
  442. errInfo.add(columnName + "值超出最多输入字符限制");
  443. }
  444. if (!RegexUtils.checkEnCode(value)) {
  445. errInfo.add(columnName + "只能输入英文、数字和小数点且小数点不能放在首尾");
  446. }
  447. //库里重复
  448. long enCodeCount = allPositionList.stream().filter(t -> t.getEnCode().equals(value)).count();
  449. if (enCodeCount > 0) {
  450. errInfo.add(columnName + "值已存在");
  451. break;
  452. }
  453. //表格内重复
  454. enCodeCount = addList.stream().filter(t -> t.getEnCode().equals(value)).count();
  455. if (enCodeCount > 0) {
  456. errInfo.add(columnName + "值已存在");
  457. break;
  458. }
  459. break;
  460. case "globalMark":
  461. if (StringUtil.isEmpty(value)) {
  462. errInfo.add(columnName + "不能为空");
  463. break;
  464. }
  465. if ("全局".equals(value)) {
  466. globalMark = 1;
  467. } else if ("组织".equals(value)) {
  468. globalMark = 0;
  469. } else {
  470. errInfo.add(columnName + "值不正确");
  471. break;
  472. }
  473. realMap.put("globalMark", globalMark);
  474. break;
  475. case "organizeId":
  476. if (globalMark == 0) {
  477. if (StringUtil.isEmpty(value)) {
  478. errInfo.add("所属组织" + "不能为空");
  479. break;
  480. }
  481. }
  482. if (globalMark == 1) {
  483. realMap.put("organizeId", null);
  484. break;
  485. }
  486. if (StringUtil.isEmpty(value)) {
  487. break;
  488. }
  489. CheckResult organizeIdCheckResult = this.checkOrganizes(value, allOrgsTreeName);
  490. if (!organizeIdCheckResult.isPass()) {
  491. errInfo.add(organizeIdCheckResult.getErrorMsg());
  492. break;
  493. }
  494. organizeIdList = (List<List<String>>) organizeIdCheckResult.getValue();
  495. break;
  496. case "sortCode":
  497. if (StringUtil.isEmpty(value)) {
  498. realMap.put("sortCode", 0);
  499. break;
  500. }
  501. Long numValue = 0l;
  502. try {
  503. numValue = Long.parseLong(value);
  504. } catch (Exception e) {
  505. errInfo.add(columnName + "值不正确");
  506. break;
  507. }
  508. if (numValue < 0) {
  509. errInfo.add(columnName + "值不能小于0");
  510. break;
  511. }
  512. if (numValue > 1000000) {
  513. errInfo.add(columnName + "值不能大于999999");
  514. break;
  515. }
  516. break;
  517. case "enabledMark":
  518. if (StringUtil.isEmpty(value)) {
  519. errInfo.add(columnName + "不能为空");
  520. break;
  521. }
  522. if ("启用".equals(value)) {
  523. realMap.put("enabledMark", 1);
  524. } else if ("禁用".equals(value)) {
  525. realMap.put("enabledMark", 0);
  526. } else {
  527. errInfo.add(columnName + "值不正确");
  528. }
  529. break;
  530. default:
  531. break;
  532. }
  533. }
  534. if (errInfo.length() == 0) {
  535. RoleEntity roleEntity = JsonUtil.getJsonToBean(realMap, RoleEntity.class);
  536. roleEntity.setCreatorTime(new Date());
  537. roleEntity.setId(RandomUtil.uuId());
  538. addList.add(roleEntity);
  539. if (organizeIdList != null && !organizeIdList.isEmpty() && roleEntity.getGlobalMark() == 0) {
  540. addOrganizeIdMap.put(roleEntity.getId(), organizeIdList);
  541. }
  542. } else {
  543. eachMap.put("errorsInfo", errInfo.toString());
  544. failList.add(eachMap);
  545. }
  546. }
  547. }
  548. /**
  549. * 导出Excel
  550. *
  551. * @return
  552. */
  553. @Operation(summary = "导出Excel")
  554. @SaCheckPermission("permission.role")
  555. @GetMapping("/ExportData")
  556. public ActionResult ExportData(RolePagination pagination) throws IOException {
  557. if (StringUtil.isEmpty(pagination.getSelectKey())) {
  558. return ActionResult.fail(MsgCode.IMP011.get());
  559. }
  560. List<RoleEntity> list = roleService.getList(pagination);
  561. // todo 导出数据
  562. List<Map<String, Object>> realList = new ArrayList<>();
  563. String[] keys = !StringUtil.isEmpty(pagination.getSelectKey()) ? pagination.getSelectKey() : new String[0];
  564. RoleColumnMap posColumnMap = new RoleColumnMap();
  565. String excelName = posColumnMap.getExcelName();
  566. List<ExcelColumnAttr> models = posColumnMap.getFieldsModel(false);
  567. Map<String, String> keyMap = posColumnMap.getColumnByType(null);
  568. Map<String, String[]> optionMap = new HashMap<>();
  569. ExcelModel excelModel = ExcelModel.builder().selectKey(Arrays.asList(keys)).models(models).optionMap(optionMap).build();
  570. DownloadVO vo = ExcelTool.creatModelExcel(FileTypeConstant.TEMPORARY, excelName, keyMap, realList, excelModel);
  571. return ActionResult.success(vo);
  572. }
  573. private CheckResult checkOrganizes(String organizeNames, Map<String, Object> allOrgsTreeName) {
  574. String[] organizeNameArray = organizeNames.split(",");
  575. List<String> errorOrganizeNameList = new ArrayList<>();
  576. List<List<String>> successOrganizeIdList = new ArrayList<>();
  577. for (String organizeName : organizeNameArray) {
  578. String organizeId = null;
  579. boolean find = false;
  580. for (String key : allOrgsTreeName.keySet()) {
  581. Object o = allOrgsTreeName.get(key);
  582. if (organizeName.equals(o.toString())) {
  583. find = true;
  584. successOrganizeIdList.add(Arrays.asList(key.split(",")));
  585. break;
  586. }
  587. }
  588. if (!find) {
  589. errorOrganizeNameList.add(organizeName);
  590. }
  591. }
  592. if (errorOrganizeNameList.isEmpty()) {
  593. return new CheckResult(true, null, successOrganizeIdList);
  594. } else if (organizeNameArray.length == 1) {
  595. return new CheckResult(false, "找不到该所属组织", null);
  596. } else {
  597. return new CheckResult(false, "找不到该所属组织(" + String.join("、", errorOrganizeNameList) + ")", null);
  598. }
  599. }
  600. /**
  601. * 获取下拉框
  602. *
  603. * @return
  604. */
  605. private Map<String, String[]> getOptionMap() {
  606. Map<String, String[]> optionMap = new HashMap<>();
  607. //角色类型
  608. optionMap.put("globalMark", new String[]{"全局", "组织"});
  609. optionMap.put("enabledMark", new String[]{"启用", "禁用"});
  610. return optionMap;
  611. }
  612. }