SmartAuthorGroupController.java 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. package com.template.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
  5. import com.sun.org.apache.bcel.internal.generic.NEW;
  6. import com.template.annotation.DESRespondSecret;
  7. import com.template.annotation.PassToken;
  8. import com.template.api.SmartAuthorGroupControllerAPI;
  9. import com.template.common.utils.AesUtils;
  10. import com.template.common.utils.TimeExchange2;
  11. import com.template.common.utils.TreeRecordsUtil;
  12. import com.template.common.utils.UUIDUtil;
  13. import com.template.model.pojo.*;
  14. import com.template.model.result.CommonResult;
  15. import com.template.model.weixin.AuthorAndGroup;
  16. import com.template.model.weixin.AuthorAndGroup2;
  17. import com.template.model.weixin.AuthorListGroup;
  18. import com.template.model.weixin.userAuthor;
  19. import com.template.services.*;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.web.bind.annotation.RestController;
  22. import java.text.ParseException;
  23. import java.util.*;
  24. import java.util.stream.Collectors;
  25. /**
  26. * <p>
  27. * 前端控制器
  28. * </p>
  29. *
  30. * @author ceshi
  31. * @since 2023-12-04
  32. */
  33. @RestController
  34. //返回参数加密注解
  35. @DESRespondSecret
  36. public class SmartAuthorGroupController implements SmartAuthorGroupControllerAPI {
  37. @Autowired
  38. private SmartAuthorGroupService smartAuthorGroupService;
  39. @Autowired
  40. public SmartAuthorityService smartAuthorityService;
  41. @Autowired
  42. public SmartUserService smartUserService;
  43. @Autowired
  44. public SmartDepartmentService smartDepartmentService;
  45. @Autowired
  46. public SmartMenuService smartMenuService;
  47. /**
  48. * 新增权限组
  49. *
  50. * @param
  51. * @param
  52. * @return
  53. */
  54. @Override
  55. @PassToken
  56. @DESRespondSecret(validated = true)
  57. public CommonResult insertSmartAuthorGroup(JSONObject jsonObject, Integer samePower) throws ParseException {
  58. if (samePower == null) {
  59. return CommonResult.fail("samePower不能为空");
  60. }
  61. // 所有管理员权限一样
  62. if (samePower == 1) {
  63. AuthorAndGroup authorAndGroup = JSONObject.parseObject(jsonObject.toString(), AuthorAndGroup.class);
  64. SmartAuthorGroup sa = authorAndGroup.getSmartAuthorGroup();
  65. sa.setId((int) UUIDUtil.generateID());
  66. String[] userId = sa.getUserId().split(",");
  67. // 相关判断
  68. QueryWrapper<SmartAuthorGroup> queryWrapper = new QueryWrapper<>();
  69. queryWrapper.eq("deleted", "0");
  70. queryWrapper.eq("parent_id", sa.getParentId());
  71. queryWrapper.eq("name", sa.getName());
  72. List<SmartAuthorGroup> querySmartGroup = smartAuthorGroupService.getAuthorGroupByKey(queryWrapper);
  73. if (querySmartGroup.size() > 0) {
  74. return CommonResult.fail("该管理员已存在,请勿重复添加");
  75. }
  76. QueryWrapper<SmartAuthority> queryWrapper2 = new QueryWrapper<>();
  77. List<String> typeList = Arrays.asList(userId);
  78. queryWrapper2.in("user_id", typeList);
  79. queryWrapper2.eq("deleted", "0");
  80. List<SmartAuthority> querySmart = smartAuthorityService.getAuthorByKey(queryWrapper2);
  81. if (querySmart.size() > 0) {
  82. return CommonResult.fail("该用户已分配其他权限组");
  83. }
  84. Set<String> set = new HashSet<>(typeList);
  85. if (typeList.size() != set.size()) {
  86. return CommonResult.fail("请勿选择重复用户");
  87. }
  88. for (int i = 0; i < userId.length; i++) {
  89. SmartAuthority smartAuthority = new SmartAuthority();
  90. smartAuthority.setUserId(Integer.parseInt(userId[i]));
  91. smartAuthority.setGroupId(sa.getId());
  92. smartAuthority.setDepartmentView(authorAndGroup.getDepartment_view());
  93. smartAuthority.setDepartmentManage(authorAndGroup.getDepartment_manage());
  94. smartAuthority.setCreateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  95. smartAuthority.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  96. smartAuthority.setCreateUser("admin");
  97. smartAuthority.setUpdateUser("admin");
  98. smartAuthority.setDeleted(0);
  99. smartAuthorityService.insertSmartAuthority(smartAuthority);
  100. }
  101. sa.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  102. sa.setCreateUser("admin");
  103. sa.setUpdateUser("admin");
  104. sa.setDeleted(0);
  105. int result = smartAuthorGroupService.insertSmartAuthorGroup(sa);
  106. return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
  107. }
  108. // 每个管理员权限不一样
  109. if (samePower == 2) {
  110. AuthorAndGroup2 authorAndGroup2 = JSONObject.parseObject(jsonObject.toString(), AuthorAndGroup2.class);
  111. SmartAuthorGroup sa = authorAndGroup2.getSmartAuthorGroup();
  112. sa.setId((int) UUIDUtil.generateID());
  113. List<userAuthor> userAuthors = authorAndGroup2.getUserAuthors();
  114. // 相关查询
  115. QueryWrapper<SmartAuthorGroup> queryWrapper = new QueryWrapper<>();
  116. queryWrapper.eq("deleted", 0);
  117. queryWrapper.eq("parent_id", sa.getParentId());
  118. queryWrapper.eq("name", sa.getName());
  119. List<SmartAuthorGroup> querySmartGroup = smartAuthorGroupService.getAuthorGroupByKey(queryWrapper);
  120. if (querySmartGroup.size() > 0) {
  121. return CommonResult.fail("该管理员已存在,请勿重复添加");
  122. }
  123. List<Integer> uid = userAuthors.stream().map(userAuthor::getUserId).collect(Collectors.toList());
  124. QueryWrapper<SmartAuthority> queryWrapper2 = new QueryWrapper<>();
  125. queryWrapper2.in("user_id", uid);
  126. queryWrapper2.eq("deleted", 0);
  127. List<SmartAuthority> querySmart = smartAuthorityService.getAuthorByKey(queryWrapper2);
  128. if (querySmart.size() > 0) {
  129. return CommonResult.fail("该用户已分配其他权限组");
  130. }
  131. Set<Integer> set = new HashSet<>(uid);
  132. if (uid.size() != set.size()) {
  133. return CommonResult.fail("请勿选择重复用户");
  134. }
  135. for (int i = 0; i < userAuthors.size(); i++) {
  136. SmartAuthority smartAuthority = new SmartAuthority();
  137. smartAuthority.setUserId(userAuthors.get(i).getUserId());
  138. smartAuthority.setGroupId(sa.getId());
  139. smartAuthority.setDepartmentView(userAuthors.get(i).getDepartment_view());
  140. smartAuthority.setDepartmentManage(userAuthors.get(i).getDepartment_manage());
  141. smartAuthority.setCreateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  142. smartAuthority.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  143. smartAuthority.setCreateUser("admin");
  144. smartAuthority.setUpdateUser("admin");
  145. smartAuthority.setDeleted(0);
  146. smartAuthorityService.insertSmartAuthority(smartAuthority);
  147. }
  148. sa.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  149. sa.setCreateUser("admin");
  150. sa.setUpdateUser("admin");
  151. sa.setDeleted(0);
  152. int result = smartAuthorGroupService.insertSmartAuthorGroup(sa);
  153. return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
  154. }
  155. return CommonResult.errorMsg("参数格式错误");
  156. }
  157. /**
  158. * 更新权限组
  159. *
  160. * @param
  161. * @param
  162. * @return
  163. */
  164. @Override
  165. @PassToken
  166. @DESRespondSecret(validated = true)
  167. public CommonResult updateSmartAuthorGroup(JSONObject jsonObject, Integer samePower) throws ParseException {
  168. if (samePower == null) {
  169. return CommonResult.errorMsg("samePower不能为空");
  170. }
  171. // 所有管理员权限一样
  172. if (samePower == 1) {
  173. AuthorAndGroup authorAndGroup = JSONObject.parseObject(jsonObject.toString(), AuthorAndGroup.class);
  174. SmartAuthorGroup sa = authorAndGroup.getSmartAuthorGroup();
  175. String[] userId = sa.getUserId().split(",");
  176. // 相关判断
  177. QueryWrapper<SmartAuthorGroup> queryWrapperA = new QueryWrapper<>();
  178. queryWrapperA.eq("deleted", 0);
  179. queryWrapperA.eq("id", sa.getId());
  180. List<SmartAuthorGroup> querySmartGroupA = smartAuthorGroupService.getAuthorGroupByKey(queryWrapperA);
  181. if (querySmartGroupA.size() <= 0) {
  182. return CommonResult.fail("该条信息已删除");
  183. }
  184. QueryWrapper<SmartAuthorGroup> queryWrapperB = new QueryWrapper<>();
  185. queryWrapperB.eq("deleted", 0);
  186. queryWrapperB.eq("id", sa.getParentId());
  187. List<SmartAuthorGroup> querySmartGroupB = smartAuthorGroupService.getAuthorGroupByKey(queryWrapperB);
  188. if (querySmartGroupB.size() <= 0 && sa.getParentId() != 0) {
  189. return CommonResult.fail("父ID不存在");
  190. }
  191. QueryWrapper<SmartAuthorGroup> queryWrapper = new QueryWrapper<>();
  192. queryWrapper.eq("deleted", 0);
  193. queryWrapper.eq("parent_id", sa.getParentId());
  194. queryWrapper.eq("name", sa.getName());
  195. queryWrapper.ne("id", sa.getId());
  196. List<SmartAuthorGroup> querySmartGroup = smartAuthorGroupService.getAuthorGroupByKey(queryWrapper);
  197. if (querySmartGroup.size() > 0) {
  198. return CommonResult.fail("该管理员已存在,请勿重复修改");
  199. }
  200. QueryWrapper<SmartAuthority> queryWrapper2 = new QueryWrapper<>();
  201. List<String> typeList = Arrays.asList(userId);
  202. queryWrapper2.in("user_id", typeList);
  203. queryWrapper2.eq("deleted", 0);
  204. queryWrapper2.ne("group_id", sa.getId());
  205. List<SmartAuthority> querySmart = smartAuthorityService.getAuthorByKey(queryWrapper2);
  206. if (querySmart.size() > 0) {
  207. return CommonResult.fail("该用户已分配其他权限组");
  208. }
  209. Set<String> set = new HashSet<>(typeList);
  210. if (typeList.size() != set.size()) {
  211. return CommonResult.fail("请勿选择重复用户");
  212. }
  213. String[] old_userId = querySmartGroupA.get(0).getUserId().split(",");
  214. for (int i = 0; i < old_userId.length; i++) {
  215. if (!typeList.contains(old_userId[i])) {
  216. QueryWrapper<SmartAuthority> queryWrapperC = new QueryWrapper<>();
  217. queryWrapperC.eq("deleted", 0);
  218. queryWrapperC.eq("user_id", Integer.parseInt(old_userId[i]));
  219. List<SmartAuthority> smartAuthorityC = smartAuthorityService.getAuthorByKey(queryWrapperC);
  220. if (smartAuthorityC.size() > 0) {
  221. SmartAuthority smartAuthority = smartAuthorityC.get(0);
  222. smartAuthority.setDeleted(1);
  223. System.out.println(")))" + smartAuthority.getId());
  224. smartAuthorityService.updateSmartAuthority(smartAuthority);
  225. }
  226. }
  227. }
  228. for (int i = 0; i < userId.length; i++) {
  229. QueryWrapper<SmartAuthority> queryWrapper3 = new QueryWrapper<>();
  230. queryWrapper3.eq("deleted", 0);
  231. queryWrapper3.eq("user_id", Integer.parseInt(userId[i]));
  232. List<SmartAuthority> smartAuthoritys = smartAuthorityService.getAuthorByKey(queryWrapper3);
  233. if (smartAuthoritys.size() > 0) {
  234. SmartAuthority smartAuthority = smartAuthoritys.get(0);
  235. smartAuthority.setGroupId(sa.getId());
  236. smartAuthority.setDepartmentView(authorAndGroup.getDepartment_view());
  237. smartAuthority.setDepartmentManage(authorAndGroup.getDepartment_manage());
  238. smartAuthority.setCreateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  239. smartAuthority.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  240. smartAuthority.setCreateUser("admin");
  241. smartAuthority.setUpdateUser("admin");
  242. smartAuthority.setDeleted(0);
  243. smartAuthorityService.updateSmartAuthority(smartAuthority);
  244. } else {
  245. SmartAuthority smartAuthority = new SmartAuthority();
  246. smartAuthority.setUserId(Integer.parseInt(userId[i]));
  247. smartAuthority.setGroupId(sa.getId());
  248. smartAuthority.setDepartmentView(authorAndGroup.getDepartment_view());
  249. smartAuthority.setDepartmentManage(authorAndGroup.getDepartment_manage());
  250. smartAuthority.setCreateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  251. smartAuthority.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  252. smartAuthority.setCreateUser("admin");
  253. smartAuthority.setUpdateUser("admin");
  254. smartAuthority.setDeleted(0);
  255. smartAuthorityService.insertSmartAuthority(smartAuthority);
  256. }
  257. }
  258. int result = smartAuthorGroupService.updateSmartAuthorGroup(sa);
  259. return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
  260. }
  261. // 每个管理员权限不一样
  262. if (samePower == 2) {
  263. AuthorAndGroup2 authorAndGroup2 = JSONObject.parseObject(jsonObject.toString(), AuthorAndGroup2.class);
  264. SmartAuthorGroup sa = authorAndGroup2.getSmartAuthorGroup();
  265. List<userAuthor> userAuthors = authorAndGroup2.getUserAuthors();
  266. // 相关查询
  267. QueryWrapper<SmartAuthorGroup> queryWrapperA = new QueryWrapper<>();
  268. queryWrapperA.eq("deleted", 0);
  269. queryWrapperA.eq("id", sa.getId());
  270. List<SmartAuthorGroup> querySmartGroupA = smartAuthorGroupService.getAuthorGroupByKey(queryWrapperA);
  271. if (querySmartGroupA.size() <= 0) {
  272. return CommonResult.fail("该条信息已删除");
  273. }
  274. QueryWrapper<SmartAuthorGroup> queryWrapperB = new QueryWrapper<>();
  275. queryWrapperB.eq("deleted", 0);
  276. queryWrapperB.eq("id", sa.getParentId());
  277. List<SmartAuthorGroup> querySmartGroupB = smartAuthorGroupService.getAuthorGroupByKey(queryWrapperB);
  278. if (querySmartGroupB.size() <= 0 && sa.getParentId() != 0) {
  279. return CommonResult.fail("父ID不存在");
  280. }
  281. QueryWrapper<SmartAuthorGroup> queryWrapper = new QueryWrapper<>();
  282. queryWrapper.eq("deleted", 0);
  283. queryWrapper.eq("parent_id", sa.getParentId());
  284. queryWrapper.eq("name", sa.getName());
  285. queryWrapper.ne("id", sa.getId());
  286. List<SmartAuthorGroup> querySmartGroup = smartAuthorGroupService.getAuthorGroupByKey(queryWrapper);
  287. if (querySmartGroup.size() > 0) {
  288. return CommonResult.fail("该管理员已存在,请勿重复添加");
  289. }
  290. List<Integer> uid = userAuthors.stream().map(userAuthor::getUserId).collect(Collectors.toList());
  291. QueryWrapper<SmartAuthority> queryWrapper2 = new QueryWrapper<>();
  292. queryWrapper2.in("user_id", uid);
  293. queryWrapper2.eq("deleted", 0);
  294. queryWrapper2.ne("group_id", sa.getId());
  295. List<SmartAuthority> querySmart = smartAuthorityService.getAuthorByKey(queryWrapper2);
  296. if (querySmart.size() > 0) {
  297. return CommonResult.fail("该用户已分配其他权限组");
  298. }
  299. Set<Integer> set = new HashSet<>(uid);
  300. if (uid.size() != set.size()) {
  301. return CommonResult.fail("请勿选择重复用户");
  302. }
  303. String[] old_userId = querySmartGroupA.get(0).getUserId().split(",");
  304. for (int i = 0; i < old_userId.length; i++) {
  305. if (!uid.contains(Integer.parseInt(old_userId[i]))) {
  306. QueryWrapper<SmartAuthority> queryWrapperC = new QueryWrapper<>();
  307. queryWrapperC.eq("deleted", 0);
  308. queryWrapperC.eq("user_id", Integer.parseInt(old_userId[i]));
  309. List<SmartAuthority> smartAuthorityC = smartAuthorityService.getAuthorByKey(queryWrapperC);
  310. if (smartAuthorityC.size() > 0) {
  311. SmartAuthority smartAuthority = smartAuthorityC.get(0);
  312. smartAuthority.setDeleted(1);
  313. smartAuthorityService.updateSmartAuthority(smartAuthority);
  314. }
  315. }
  316. }
  317. String userid = "";
  318. for (int i = 0; i < userAuthors.size(); i++) {
  319. if (i == userAuthors.size() - 1) {
  320. userid += userAuthors.get(i).getUserId();
  321. } else {
  322. userid += userAuthors.get(i).getUserId() + ",";
  323. }
  324. QueryWrapper<SmartAuthority> queryWrapper3 = new QueryWrapper<>();
  325. queryWrapper3.eq("deleted", 0);
  326. queryWrapper3.eq("user_id", userAuthors.get(i).getUserId());
  327. List<SmartAuthority> smartAuthoritys = smartAuthorityService.getAuthorByKey(queryWrapper3);
  328. if (smartAuthoritys.size() > 0) {
  329. SmartAuthority smartAuthority = smartAuthoritys.get(0);
  330. smartAuthority.setUserId(userAuthors.get(i).getUserId());
  331. smartAuthority.setGroupId(sa.getId());
  332. smartAuthority.setDepartmentView(userAuthors.get(i).getDepartment_view());
  333. smartAuthority.setDepartmentManage(userAuthors.get(i).getDepartment_manage());
  334. smartAuthority.setCreateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  335. smartAuthority.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  336. smartAuthority.setCreateUser("admin");
  337. smartAuthority.setUpdateUser("admin");
  338. smartAuthority.setDeleted(userAuthors.get(i).getDeleted());
  339. smartAuthorityService.updateSmartAuthority(smartAuthority);
  340. } else {
  341. SmartAuthority smartAuthority = new SmartAuthority();
  342. smartAuthority.setUserId(userAuthors.get(i).getUserId());
  343. smartAuthority.setGroupId(sa.getId());
  344. smartAuthority.setDepartmentView(userAuthors.get(i).getDepartment_view());
  345. smartAuthority.setDepartmentManage(userAuthors.get(i).getDepartment_manage());
  346. smartAuthority.setCreateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  347. smartAuthority.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  348. smartAuthority.setCreateUser("admin");
  349. smartAuthority.setUpdateUser("admin");
  350. smartAuthority.setDeleted(0);
  351. smartAuthorityService.insertSmartAuthority(smartAuthority);
  352. }
  353. }
  354. sa.setUserId(userid);
  355. int result = smartAuthorGroupService.updateSmartAuthorGroup(sa);
  356. return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
  357. }
  358. return CommonResult.errorMsg("参数格式错误");
  359. }
  360. /**
  361. * 新增权限组
  362. *
  363. * @param
  364. * @param
  365. * @return
  366. */
  367. @Override
  368. @PassToken
  369. @DESRespondSecret(validated = true)
  370. public CommonResult addSmartAuthorGroup(JSONObject jsonObject) throws ParseException {
  371. AuthorAndGroup authorAndGroup = JSONObject.parseObject(jsonObject.toString(), AuthorAndGroup.class);
  372. SmartAuthorGroup sa = authorAndGroup.getSmartAuthorGroup();
  373. sa.setId((int) UUIDUtil.generateID());
  374. // 相关判断
  375. QueryWrapper<SmartAuthorGroup> queryWrapper = new QueryWrapper<>();
  376. queryWrapper.eq("deleted", "0");
  377. queryWrapper.eq("parent_id", sa.getParentId());
  378. queryWrapper.eq("name", sa.getName());
  379. List<SmartAuthorGroup> querySmartGroup = smartAuthorGroupService.getAuthorGroupByKey(queryWrapper);
  380. if (querySmartGroup.size() > 0) {
  381. return CommonResult.fail("该管理员已存在,请勿重复添加");
  382. }
  383. sa.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  384. sa.setCreateUser("admin");
  385. sa.setUpdateUser("admin");
  386. sa.setDeleted(0);
  387. int result = smartAuthorGroupService.insertSmartAuthorGroup(sa);
  388. return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
  389. }
  390. /**
  391. * 删除权限组
  392. *
  393. * @param
  394. * @param
  395. * @return
  396. */
  397. @Override
  398. @PassToken
  399. @DESRespondSecret(validated = true)
  400. public CommonResult delSmartAuthorGroup(Integer id) {
  401. QueryWrapper<SmartAuthorGroup> queryWrapper1 = new QueryWrapper<>();
  402. queryWrapper1.eq("parent_id", id);
  403. queryWrapper1.eq("deleted", 0);
  404. List<SmartAuthorGroup> list1 = smartAuthorGroupService.getAuthorGroupByKey(queryWrapper1);
  405. SmartAuthorGroup rns = new SmartAuthorGroup();
  406. rns.setId(id);
  407. rns.setDeleted(1);
  408. int result = smartAuthorGroupService.updateSmartAuthorGroup(rns);
  409. SmartAuthority smartAuthority = new SmartAuthority();
  410. smartAuthority.setDeleted(1);
  411. QueryWrapper<SmartAuthority> queryWrapper2 = new QueryWrapper<>();
  412. queryWrapper2.eq("group_id", id);
  413. boolean m = smartAuthorityService.update(smartAuthority, queryWrapper2);
  414. SmartAuthorGroup rns2 = new SmartAuthorGroup();
  415. rns2.setDeleted(1);
  416. QueryWrapper<SmartAuthorGroup> queryWrapper3 = new QueryWrapper<>();
  417. queryWrapper3.eq("parent_id", id);
  418. boolean result2 = smartAuthorGroupService.update(rns2, queryWrapper3);
  419. SmartAuthority smartAuthority2 = new SmartAuthority();
  420. smartAuthority2.setDeleted(1);
  421. QueryWrapper<SmartAuthority> queryWrapper4 = new QueryWrapper<>();
  422. List<Integer> gid = list1.stream().map(SmartAuthorGroup::getId).collect(Collectors.toList());
  423. queryWrapper4.in("group_id", gid);
  424. queryWrapper4.eq("deleted",0);
  425. boolean m2 = smartAuthorityService.update(smartAuthority2,queryWrapper4);
  426. return result > 0 && result2 && m && m2 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
  427. }
  428. /**
  429. * 操作管理员
  430. *
  431. * @param
  432. * @param
  433. * @return
  434. */
  435. @Override
  436. @PassToken
  437. @DESRespondSecret(validated = true)
  438. public CommonResult operateSmartAuthorGroupUser(Integer id, String userId) throws ParseException {
  439. if (id==null){
  440. return CommonResult.fail("id不能为空");
  441. }
  442. if (userId==null){
  443. userId="";
  444. }
  445. SmartAuthorGroup sa = smartAuthorGroupService.getSmartById(id);
  446. if (sa==null){
  447. return CommonResult.fail("当前权限组不存在");
  448. }
  449. if (sa.getUserId()==null){
  450. sa.setUserId("");
  451. }
  452. String[] newUserId = userId.split(",");
  453. List<String> userList = Arrays.asList(newUserId);
  454. Set<String> set = new HashSet<>(userList);
  455. if (userList.size() != set.size()) {
  456. return CommonResult.fail("请勿选择重复用户");
  457. }
  458. QueryWrapper<SmartAuthority> queryWrapperA = new QueryWrapper<>();
  459. List<String> typeList = Arrays.asList(newUserId);
  460. queryWrapperA.in("user_id", typeList);
  461. queryWrapperA.eq("deleted", 0);
  462. queryWrapperA.ne("group_id", sa.getId());
  463. List<SmartAuthority> querySmart = smartAuthorityService.getAuthorByKey(queryWrapperA);
  464. if (querySmart.size() > 0) {
  465. return CommonResult.fail("该用户已分配其他权限组");
  466. }
  467. QueryWrapper<SmartAuthority> queryWrapperA2 = new QueryWrapper<>();
  468. queryWrapperA2.eq("deleted", 0);
  469. queryWrapperA2.eq("group_id", sa.getId());
  470. List<SmartAuthority> oldUser = smartAuthorityService.getAuthorByKey(queryWrapperA2);
  471. List<Integer> oldUserId = oldUser.stream().map(SmartAuthority::getUserId).collect(Collectors.toList());
  472. QueryWrapper<SmartUser> queryWrapperB = new QueryWrapper<>();
  473. List<String> users = Arrays.asList(newUserId);
  474. queryWrapperB.in("id", users);
  475. queryWrapperB.eq("deleted", 0);
  476. List<SmartUser> ulist = smartUserService.list(queryWrapperB);
  477. if (!userId.equals("") && (ulist.isEmpty() || ulist.size()!=newUserId.length)){
  478. return CommonResult.fail("有用户已删除");
  479. }
  480. if (!oldUserId.isEmpty()){
  481. List<Integer> userIds = new ArrayList<>();
  482. for (int i = 0; i < oldUserId.size(); i++) {
  483. System.out.println("-----"+oldUserId.get(i));
  484. if (!userList.contains(String.valueOf(oldUserId.get(i)))) {
  485. userIds.add(oldUserId.get(i));
  486. }
  487. }
  488. if(userIds != null && userIds.size() > 0){
  489. smartAuthorityService.deleteAuthorityByUsergroup(userIds, id);
  490. }
  491. }
  492. for (int i = 0; i < newUserId.length; i++) {
  493. if (!newUserId[i].equals("")){
  494. QueryWrapper<SmartAuthority> queryWrapper2 = new QueryWrapper<>();
  495. queryWrapper2.eq("deleted", 0);
  496. queryWrapper2.eq("user_id", Integer.parseInt(newUserId[i]));
  497. List<SmartAuthority> smartAuthoritys = smartAuthorityService.getAuthorByKey(queryWrapper2);
  498. if (smartAuthoritys.size() <= 0) {
  499. SmartAuthority smartAuthority = new SmartAuthority();
  500. smartAuthority.setUserId(Integer.parseInt(newUserId[i]));
  501. smartAuthority.setGroupId(sa.getId());
  502. smartAuthority.setDepartmentView("");
  503. smartAuthority.setDepartmentManage("");
  504. smartAuthority.setCreateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  505. smartAuthority.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(), "yyyy-MM-dd HH:mm:ss"));
  506. smartAuthority.setCreateUser("admin");
  507. smartAuthority.setUpdateUser("admin");
  508. smartAuthority.setDeleted(0);
  509. smartAuthorityService.insertSmartAuthority(smartAuthority);
  510. }
  511. }
  512. }
  513. sa.setUserId(userId);
  514. int result = smartAuthorGroupService.updateSmartAuthorGroup(sa);
  515. return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
  516. }
  517. /**
  518. * 操作菜单权限
  519. *
  520. * @param
  521. * @param
  522. * @return
  523. */
  524. @Override
  525. @PassToken
  526. @DESRespondSecret(validated = true)
  527. public CommonResult operateSmartAuthorGroupApply(Integer id, String applyId) {
  528. if (id==null){
  529. return CommonResult.fail("id不能为空");
  530. }
  531. if (applyId==null){
  532. applyId="";
  533. }
  534. SmartAuthorGroup sa = smartAuthorGroupService.getSmartById(id);
  535. if (sa==null){
  536. return CommonResult.fail("当前权限组不存在");
  537. }
  538. sa.setApplyId(applyId);
  539. int result = smartAuthorGroupService.updateSmartAuthorGroup(sa);
  540. return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
  541. }
  542. @PassToken
  543. @Override
  544. @DESRespondSecret(validated = false)
  545. public CommonResult getSmartAuthorGroupManager(String groupId) {
  546. List<SmartAuthorGroupManager> list = smartAuthorGroupService.getSmartAuthorGroupManager(groupId);
  547. return CommonResult.ok(list);
  548. }
  549. @PassToken
  550. @Override
  551. public CommonResult operateSmartAuthorDepartment(JSONObject jsonObject) {
  552. Integer id = jsonObject.getInteger("id");
  553. String departmentView = jsonObject.getString("departmentView");
  554. String departmentManage = jsonObject.getString("departmentManage");
  555. if (id==null){
  556. return CommonResult.fail("请传入ID");
  557. }
  558. SmartAuthority smartAuthority = smartAuthorityService.getSmartById(id);
  559. if (departmentView!=null ){
  560. smartAuthority.setDepartmentView(departmentView);
  561. }
  562. if (departmentManage!=null){
  563. smartAuthority.setDepartmentManage(departmentManage);
  564. }
  565. smartAuthorityService.updateSmartAuthority(smartAuthority);
  566. return CommonResult.ok("修改成功");
  567. }
  568. @PassToken
  569. @Override
  570. @DESRespondSecret(validated = false)
  571. public CommonResult getSmartAuthorGroupMenu(Integer id) {
  572. SmartAuthorGroup smartAuthorGroup = smartAuthorGroupService.getSmartById(id);
  573. if (smartAuthorGroup==null || smartAuthorGroup.getDeleted()==1){
  574. return CommonResult.fail("当前权限组已删除");
  575. }
  576. String[] applyIds = smartAuthorGroup.getApplyId().split(",");
  577. QueryWrapper<SmartMenu> queryWrapper2 = new QueryWrapper<>();
  578. queryWrapper2.eq("deleted", 0);
  579. List<String> applyId_ = Arrays.asList(applyIds);
  580. queryWrapper2.in("id", applyId_);
  581. List<SmartMenu> list2 = smartMenuService.list(queryWrapper2);
  582. return CommonResult.ok(list2);
  583. }
  584. @PassToken
  585. @Override
  586. public CommonResult updateSmartAuthorGroupName(JSONObject jsonObject) {
  587. Integer id = jsonObject.getInteger("id");
  588. String name = jsonObject.getString("name");
  589. if (id==null || name==null){
  590. return CommonResult.fail("请传入参数");
  591. }
  592. SmartAuthorGroup smartAuthorGroup = smartAuthorGroupService.getSmartById(id);
  593. if (smartAuthorGroup==null || smartAuthorGroup.getDeleted()==1){
  594. return CommonResult.fail("该权限组已删除");
  595. }
  596. smartAuthorGroup.setName(name);
  597. smartAuthorGroupService.updateSmartAuthorGroup(smartAuthorGroup);
  598. return CommonResult.ok("修改成功");
  599. }
  600. @PassToken
  601. @Override
  602. @DESRespondSecret(validated = false)
  603. public CommonResult queryUserAuthor(String userId,String userHead) {
  604. if (userId==null || userId.equals("")){
  605. userId = AesUtils.decrypt(userHead);
  606. }
  607. String[] userids = userId.split(",");
  608. QueryWrapper<SmartAuthority> queryWrapper1 = new QueryWrapper<>();
  609. queryWrapper1.eq("deleted", 0);
  610. queryWrapper1.in("user_id", Arrays.asList(userids));
  611. List<SmartAuthority> userAuthor = smartAuthorityService.getAuthorByKey(queryWrapper1);
  612. if (userAuthor.size() <= 0) {
  613. return CommonResult.ok(new ArrayList<>());
  614. }
  615. List<JSONObject> allList = new ArrayList<>();
  616. for (int i = 0; i < userAuthor.size(); i++) {
  617. SmartAuthority smartAuthority = userAuthor.get(i);
  618. JSONObject jsonObject = new JSONObject();
  619. jsonObject.put("userId", smartAuthority.getUserId());
  620. Integer groupId = smartAuthority.getGroupId();
  621. SmartAuthorGroup smartAuthorGroup = smartAuthorGroupService.getSmartById(groupId);
  622. QueryWrapper<SmartDepartment> queryWrapper2 = new QueryWrapper<>();
  623. queryWrapper2.eq("deleted", 0);
  624. List<String> viewDepart = Arrays.asList(smartAuthority.getDepartmentView().split(","));
  625. queryWrapper2.in("id", viewDepart);
  626. List<SmartDepartment> list1 = smartDepartmentService.list(queryWrapper2);
  627. QueryWrapper<SmartDepartment> queryWrapper3 = new QueryWrapper<>();
  628. queryWrapper3.eq("deleted", 0);
  629. List<String> manageDepart = Arrays.asList(smartAuthority.getDepartmentManage().split(","));
  630. queryWrapper3.in("id", manageDepart);
  631. List<SmartDepartment> list2 = smartDepartmentService.list(queryWrapper3);
  632. jsonObject.put("departmentViewAuthor", list1);
  633. jsonObject.put("departmentManageAuthor", list2);
  634. if (smartAuthorGroup != null && smartAuthorGroup.getDeleted() == 0) {
  635. QueryWrapper<SmartMenu> queryWrapper4 = new QueryWrapper<>();
  636. queryWrapper4.eq("deleted", 0);
  637. List<String> menuList = Arrays.asList(smartAuthorGroup.getApplyId().split(","));
  638. queryWrapper4.in("id", menuList);
  639. List<SmartMenu> list3 = smartMenuService.list(queryWrapper4);
  640. jsonObject.put("treAuthor", list3);
  641. } else {
  642. jsonObject.put("treAuthor", new ArrayList<>());
  643. }
  644. allList.add(jsonObject);
  645. }
  646. return CommonResult.ok(allList);
  647. }
  648. /**
  649. * 查看权限列表
  650. *
  651. * @return
  652. */
  653. @Override
  654. @PassToken
  655. @DESRespondSecret(validated = true)
  656. public CommonResult querySmartAuthorGroup(Integer authorGroupId) {
  657. List<SmartAuthorGroup> smartAuthorGroups = smartAuthorGroupService.getAuthorGroupList();
  658. List<AuthorListGroup> authorListGroupList = null;
  659. if (smartAuthorGroups.size() > 0) {
  660. authorListGroupList = smartAuthorGroupService.queryCommentTreeRecords(authorGroupId, smartAuthorGroups);
  661. }
  662. return CommonResult.ok(authorListGroupList);
  663. }
  664. @Override
  665. @DESRespondSecret(validated = true)
  666. public CommonResult deleteSmartAuthorGroupById(int id) {
  667. SmartAuthorGroup data = smartAuthorGroupService.getSmartById(id);
  668. if (data == null) {
  669. return CommonResult.fail("当前数据不存在,删除失败!");
  670. }
  671. int result = smartAuthorGroupService.deleteSmartAuthorGroupById(id);
  672. return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
  673. }
  674. @Override
  675. @DESRespondSecret(validated = true)
  676. public CommonResult getClassById(String userHead) {
  677. String userId="";
  678. try {
  679. userId = AesUtils.decrypt(userHead);
  680. } catch (Exception e) {
  681. return CommonResult.fail("传入的userHead有误");
  682. }
  683. SmartAuthorGroup smartAuthorGroup= smartAuthorGroupService.getByUserId(userId);
  684. if (ObjectUtils.isEmpty(smartAuthorGroup)) {
  685. return CommonResult.fail("该用户无权限");
  686. }
  687. Integer parentId = smartAuthorGroup.getParentId();
  688. if (parentId==0) {
  689. JSONObject jsonObject = new JSONObject();
  690. jsonObject.put("schoolClass",0);
  691. return CommonResult.ok(jsonObject);
  692. }
  693. SmartUser smartUser = smartUserService.getById(userId);
  694. Integer schoolClass = smartUser.getSchoolClass();
  695. JSONObject jsonObject = new JSONObject();
  696. jsonObject.put("schoolClass",schoolClass);
  697. return CommonResult.ok(jsonObject);
  698. }
  699. }