SmartAuthorGroupController.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. package com.template.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.template.annotation.PassToken;
  5. import com.template.api.SmartAuthorGroupControllerAPI;
  6. import com.template.common.utils.TimeExchange2;
  7. import com.template.common.utils.TreeRecordsUtil;
  8. import com.template.common.utils.UUIDUtil;
  9. import com.template.model.pojo.SmartAuthorGroup;
  10. import com.template.model.pojo.SmartAuthority;
  11. import com.template.model.pojo.SmartUser;
  12. import com.template.model.result.CommonResult;
  13. import com.template.model.weixin.AuthorAndGroup;
  14. import com.template.model.weixin.AuthorAndGroup2;
  15. import com.template.model.weixin.AuthorListGroup;
  16. import com.template.model.weixin.userAuthor;
  17. import com.template.services.SmartAuthorGroupService;
  18. import com.template.services.SmartAuthorityService;
  19. import com.template.services.SmartUserService;
  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. public class SmartAuthorGroupController implements SmartAuthorGroupControllerAPI {
  35. @Autowired
  36. private SmartAuthorGroupService smartAuthorGroupService;
  37. @Autowired
  38. public SmartAuthorityService smartAuthorityService;
  39. @Autowired
  40. public SmartUserService smartUserService;
  41. /**
  42. * 新增权限组
  43. * @param
  44. * @param
  45. * @return
  46. */
  47. @Override
  48. @PassToken
  49. public CommonResult insertSmartAuthorGroup(JSONObject jsonObject, Integer samePower) throws ParseException {
  50. if (samePower==null){
  51. return CommonResult.fail("samePower不能为空");
  52. }
  53. // 所有管理员权限一样
  54. if (samePower==1){
  55. AuthorAndGroup authorAndGroup = JSONObject.parseObject(jsonObject.toString(), AuthorAndGroup.class);
  56. SmartAuthorGroup sa = authorAndGroup.getSmartAuthorGroup();
  57. sa.setId((int) UUIDUtil.generateID());
  58. String[] userId = sa.getUserId().split(",");
  59. // 相关判断
  60. QueryWrapper<SmartAuthorGroup> queryWrapper = new QueryWrapper<>();
  61. queryWrapper.eq("deleted", "0");
  62. queryWrapper.eq("parent_id", sa.getParentId());
  63. queryWrapper.eq("name", sa.getName());
  64. List<SmartAuthorGroup> querySmartGroup = smartAuthorGroupService.getAuthorGroupByKey(queryWrapper);
  65. if (querySmartGroup.size()>0){
  66. return CommonResult.fail("该管理员已存在,请勿重复添加");
  67. }
  68. QueryWrapper<SmartAuthority> queryWrapper2 = new QueryWrapper<>();
  69. List<String> typeList = Arrays.asList(userId);
  70. queryWrapper2.in("user_id", typeList);
  71. queryWrapper2.eq("deleted", "0");
  72. List<SmartAuthority> querySmart = smartAuthorityService.getAuthorByKey(queryWrapper2);
  73. if (querySmart.size()>0){
  74. return CommonResult.fail("该用户已分配其他权限组");
  75. }
  76. Set<String> set = new HashSet<>(typeList);
  77. if (typeList.size()!=set.size()){
  78. return CommonResult.fail("请勿选择重复用户");
  79. }
  80. for (int i = 0; i < userId.length; i++) {
  81. SmartAuthority smartAuthority = new SmartAuthority();
  82. smartAuthority.setUserId(Integer.parseInt(userId[i]));
  83. smartAuthority.setGroupId(sa.getId());
  84. smartAuthority.setDepartmentView(authorAndGroup.getDepartment_view());
  85. smartAuthority.setDepartmentManage(authorAndGroup.getDepartment_manage());
  86. smartAuthority.setCreateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(),"yyyy-MM-dd HH:mm:ss"));
  87. smartAuthority.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(),"yyyy-MM-dd HH:mm:ss"));
  88. smartAuthority.setCreateUser("admin");
  89. smartAuthority.setUpdateUser("admin");
  90. smartAuthority.setDeleted(0);
  91. smartAuthorityService.insertSmartAuthority(smartAuthority);
  92. }
  93. sa.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(),"yyyy-MM-dd HH:mm:ss"));
  94. sa.setCreateUser("admin");
  95. sa.setUpdateUser("admin");
  96. sa.setDeleted(0);
  97. int result = smartAuthorGroupService.insertSmartAuthorGroup(sa);
  98. return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
  99. }
  100. // 每个管理员权限不一样
  101. if (samePower==2){
  102. AuthorAndGroup2 authorAndGroup2 = JSONObject.parseObject(jsonObject.toString(), AuthorAndGroup2.class);
  103. SmartAuthorGroup sa = authorAndGroup2.getSmartAuthorGroup();
  104. sa.setId((int) UUIDUtil.generateID());
  105. List<userAuthor> userAuthors = authorAndGroup2.getUserAuthors();
  106. // 相关查询
  107. QueryWrapper<SmartAuthorGroup> queryWrapper = new QueryWrapper<>();
  108. queryWrapper.eq("deleted", 0);
  109. queryWrapper.eq("parent_id", sa.getParentId());
  110. queryWrapper.eq("name", sa.getName());
  111. List<SmartAuthorGroup> querySmartGroup = smartAuthorGroupService.getAuthorGroupByKey(queryWrapper);
  112. if (querySmartGroup.size()>0){
  113. return CommonResult.fail("该管理员已存在,请勿重复添加");
  114. }
  115. List<Integer> uid =userAuthors.stream().map(userAuthor::getUserId).collect(Collectors.toList());
  116. QueryWrapper<SmartAuthority> queryWrapper2 = new QueryWrapper<>();
  117. queryWrapper2.in("user_id", uid);
  118. queryWrapper2.eq("deleted", 0);
  119. List<SmartAuthority> querySmart = smartAuthorityService.getAuthorByKey(queryWrapper2);
  120. if (querySmart.size()>0){
  121. return CommonResult.fail("该用户已分配其他权限组");
  122. }
  123. Set<Integer> set = new HashSet<>(uid);
  124. if (uid.size()!=set.size()){
  125. return CommonResult.fail("请勿选择重复用户");
  126. }
  127. for (int i = 0; i < userAuthors.size(); i++) {
  128. SmartAuthority smartAuthority = new SmartAuthority();
  129. smartAuthority.setUserId(userAuthors.get(i).getUserId());
  130. smartAuthority.setGroupId(sa.getId());
  131. smartAuthority.setDepartmentView(userAuthors.get(i).getDepartment_view());
  132. smartAuthority.setDepartmentManage(userAuthors.get(i).getDepartment_manage());
  133. smartAuthority.setCreateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(),"yyyy-MM-dd HH:mm:ss"));
  134. smartAuthority.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(),"yyyy-MM-dd HH:mm:ss"));
  135. smartAuthority.setCreateUser("admin");
  136. smartAuthority.setUpdateUser("admin");
  137. smartAuthority.setDeleted(0);
  138. smartAuthorityService.insertSmartAuthority(smartAuthority);
  139. }
  140. sa.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(),"yyyy-MM-dd HH:mm:ss"));
  141. sa.setCreateUser("admin");
  142. sa.setUpdateUser("admin");
  143. sa.setDeleted(0);
  144. int result = smartAuthorGroupService.insertSmartAuthorGroup(sa);
  145. return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
  146. }
  147. return CommonResult.errorMsg("参数格式错误");
  148. }
  149. /**
  150. * 更新权限组
  151. * @param
  152. * @param
  153. * @return
  154. */
  155. @Override
  156. @PassToken
  157. public CommonResult updateSmartAuthorGroup(JSONObject jsonObject, Integer samePower) throws ParseException {
  158. if (samePower==null){
  159. return CommonResult.errorMsg("samePower不能为空");
  160. }
  161. // 所有管理员权限一样
  162. if (samePower==1){
  163. AuthorAndGroup authorAndGroup = JSONObject.parseObject(jsonObject.toString(), AuthorAndGroup.class);
  164. SmartAuthorGroup sa = authorAndGroup.getSmartAuthorGroup();
  165. String[] userId = sa.getUserId().split(",");
  166. // 相关判断
  167. QueryWrapper<SmartAuthorGroup> queryWrapperA = new QueryWrapper<>();
  168. queryWrapperA.eq("deleted", 0);
  169. queryWrapperA.eq("id",sa.getId());
  170. List<SmartAuthorGroup> querySmartGroupA = smartAuthorGroupService.getAuthorGroupByKey(queryWrapperA);
  171. if (querySmartGroupA.size()<=0){
  172. return CommonResult.fail("该条信息已删除");
  173. }
  174. QueryWrapper<SmartAuthorGroup> queryWrapperB = new QueryWrapper<>();
  175. queryWrapperB.eq("deleted", 0);
  176. queryWrapperB.eq("id",sa.getParentId());
  177. List<SmartAuthorGroup> querySmartGroupB = smartAuthorGroupService.getAuthorGroupByKey(queryWrapperB);
  178. if (querySmartGroupB.size()<=0 && sa.getParentId()!=0){
  179. return CommonResult.fail("父ID不存在");
  180. }
  181. QueryWrapper<SmartAuthorGroup> queryWrapper = new QueryWrapper<>();
  182. queryWrapper.eq("deleted", 0);
  183. queryWrapper.eq("parent_id", sa.getParentId());
  184. queryWrapper.eq("name", sa.getName());
  185. queryWrapper.ne("id",sa.getId());
  186. List<SmartAuthorGroup> querySmartGroup = smartAuthorGroupService.getAuthorGroupByKey(queryWrapper);
  187. if (querySmartGroup.size()>0){
  188. return CommonResult.fail("该管理员已存在,请勿重复修改");
  189. }
  190. QueryWrapper<SmartAuthority> queryWrapper2 = new QueryWrapper<>();
  191. List<String> typeList = Arrays.asList(userId);
  192. queryWrapper2.in("user_id", typeList);
  193. queryWrapper2.eq("deleted", 0);
  194. queryWrapper2.ne("group_id", sa.getId());
  195. List<SmartAuthority> querySmart = smartAuthorityService.getAuthorByKey(queryWrapper2);
  196. if (querySmart.size()>0){
  197. return CommonResult.fail("该用户已分配其他权限组");
  198. }
  199. Set<String> set = new HashSet<>(typeList);
  200. if (typeList.size()!=set.size()){
  201. return CommonResult.fail("请勿选择重复用户");
  202. }
  203. String[] old_userId = querySmartGroupA.get(0).getUserId().split(",");
  204. for (int i = 0; i < old_userId.length; i++) {
  205. if (!typeList.contains(old_userId[i])){
  206. QueryWrapper<SmartAuthority> queryWrapperC = new QueryWrapper<>();
  207. queryWrapperC.eq("deleted", 0);
  208. queryWrapperC.eq("user_id",Integer.parseInt(old_userId[i]));
  209. List<SmartAuthority> smartAuthorityC = smartAuthorityService.getAuthorByKey(queryWrapperC);
  210. if (smartAuthorityC.size()>0){
  211. SmartAuthority smartAuthority = smartAuthorityC.get(0);
  212. smartAuthority.setDeleted(1);
  213. System.out.println(")))"+smartAuthority.getId());
  214. smartAuthorityService.updateSmartAuthority(smartAuthority);
  215. }
  216. }
  217. }
  218. for (int i = 0; i < userId.length; i++) {
  219. QueryWrapper<SmartAuthority> queryWrapper3 = new QueryWrapper<>();
  220. queryWrapper3.eq("deleted", 0);
  221. queryWrapper3.eq("user_id",Integer.parseInt(userId[i]));
  222. List<SmartAuthority> smartAuthoritys = smartAuthorityService.getAuthorByKey(queryWrapper3);
  223. if (smartAuthoritys.size()>0){
  224. SmartAuthority smartAuthority = smartAuthoritys.get(0);
  225. smartAuthority.setGroupId(sa.getId());
  226. smartAuthority.setDepartmentView(authorAndGroup.getDepartment_view());
  227. smartAuthority.setDepartmentManage(authorAndGroup.getDepartment_manage());
  228. smartAuthority.setCreateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(),"yyyy-MM-dd HH:mm:ss"));
  229. smartAuthority.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(),"yyyy-MM-dd HH:mm:ss"));
  230. smartAuthority.setCreateUser("admin");
  231. smartAuthority.setUpdateUser("admin");
  232. smartAuthority.setDeleted(0);
  233. smartAuthorityService.updateSmartAuthority(smartAuthority);
  234. } else {
  235. SmartAuthority smartAuthority = new SmartAuthority();
  236. smartAuthority.setUserId(Integer.parseInt(userId[i]));
  237. smartAuthority.setGroupId(sa.getId());
  238. smartAuthority.setDepartmentView(authorAndGroup.getDepartment_view());
  239. smartAuthority.setDepartmentManage(authorAndGroup.getDepartment_manage());
  240. smartAuthority.setCreateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(),"yyyy-MM-dd HH:mm:ss"));
  241. smartAuthority.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(),"yyyy-MM-dd HH:mm:ss"));
  242. smartAuthority.setCreateUser("admin");
  243. smartAuthority.setUpdateUser("admin");
  244. smartAuthority.setDeleted(0);
  245. smartAuthorityService.insertSmartAuthority(smartAuthority);
  246. }
  247. }
  248. int result = smartAuthorGroupService.updateSmartAuthorGroup(sa);
  249. return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
  250. }
  251. // 每个管理员权限不一样
  252. if (samePower==2){
  253. AuthorAndGroup2 authorAndGroup2 = JSONObject.parseObject(jsonObject.toString(), AuthorAndGroup2.class);
  254. SmartAuthorGroup sa = authorAndGroup2.getSmartAuthorGroup();
  255. List<userAuthor> userAuthors = authorAndGroup2.getUserAuthors();
  256. // 相关查询
  257. QueryWrapper<SmartAuthorGroup> queryWrapperA = new QueryWrapper<>();
  258. queryWrapperA.eq("deleted", 0);
  259. queryWrapperA.eq("id",sa.getId());
  260. List<SmartAuthorGroup> querySmartGroupA = smartAuthorGroupService.getAuthorGroupByKey(queryWrapperA);
  261. if (querySmartGroupA.size()<=0){
  262. return CommonResult.fail("该条信息已删除");
  263. }
  264. QueryWrapper<SmartAuthorGroup> queryWrapperB = new QueryWrapper<>();
  265. queryWrapperB.eq("deleted", 0);
  266. queryWrapperB.eq("id",sa.getParentId());
  267. List<SmartAuthorGroup> querySmartGroupB = smartAuthorGroupService.getAuthorGroupByKey(queryWrapperB);
  268. if (querySmartGroupB.size()<=0 && sa.getParentId()!=0){
  269. return CommonResult.fail("父ID不存在");
  270. }
  271. QueryWrapper<SmartAuthorGroup> queryWrapper = new QueryWrapper<>();
  272. queryWrapper.eq("deleted", 0);
  273. queryWrapper.eq("parent_id", sa.getParentId());
  274. queryWrapper.eq("name", sa.getName());
  275. queryWrapper.ne("id",sa.getId());
  276. List<SmartAuthorGroup> querySmartGroup = smartAuthorGroupService.getAuthorGroupByKey(queryWrapper);
  277. if (querySmartGroup.size()>0){
  278. return CommonResult.fail("该管理员已存在,请勿重复添加");
  279. }
  280. List<Integer> uid =userAuthors.stream().map(userAuthor::getUserId).collect(Collectors.toList());
  281. QueryWrapper<SmartAuthority> queryWrapper2 = new QueryWrapper<>();
  282. queryWrapper2.in("user_id", uid);
  283. queryWrapper2.eq("deleted", 0);
  284. queryWrapper2.ne("group_id", sa.getId());
  285. List<SmartAuthority> querySmart = smartAuthorityService.getAuthorByKey(queryWrapper2);
  286. if (querySmart.size()>0){
  287. return CommonResult.fail("该用户已分配其他权限组");
  288. }
  289. Set<Integer> set = new HashSet<>(uid);
  290. if (uid.size()!=set.size()){
  291. return CommonResult.fail("请勿选择重复用户");
  292. }
  293. String[] old_userId = querySmartGroupA.get(0).getUserId().split(",");
  294. for (int i = 0; i < old_userId.length; i++) {
  295. if (!uid.contains(Integer.parseInt(old_userId[i]))){
  296. QueryWrapper<SmartAuthority> queryWrapperC = new QueryWrapper<>();
  297. queryWrapperC.eq("deleted", 0);
  298. queryWrapperC.eq("user_id",Integer.parseInt(old_userId[i]));
  299. List<SmartAuthority> smartAuthorityC = smartAuthorityService.getAuthorByKey(queryWrapperC);
  300. if (smartAuthorityC.size()>0){
  301. SmartAuthority smartAuthority = smartAuthorityC.get(0);
  302. smartAuthority.setDeleted(1);
  303. smartAuthorityService.updateSmartAuthority(smartAuthority);
  304. }
  305. }
  306. }
  307. String userid = "";
  308. for (int i = 0; i < userAuthors.size(); i++) {
  309. if (i==userAuthors.size()-1){
  310. userid+=userAuthors.get(i).getUserId();
  311. } else {
  312. userid+=userAuthors.get(i).getUserId()+",";
  313. }
  314. QueryWrapper<SmartAuthority> queryWrapper3 = new QueryWrapper<>();
  315. queryWrapper3.eq("deleted", 0);
  316. queryWrapper3.eq("user_id",userAuthors.get(i).getUserId());
  317. List<SmartAuthority> smartAuthoritys = smartAuthorityService.getAuthorByKey(queryWrapper3);
  318. if (smartAuthoritys.size()>0){
  319. SmartAuthority smartAuthority = smartAuthoritys.get(0);
  320. smartAuthority.setUserId(userAuthors.get(i).getUserId());
  321. smartAuthority.setGroupId(sa.getId());
  322. smartAuthority.setDepartmentView(userAuthors.get(i).getDepartment_view());
  323. smartAuthority.setDepartmentManage(userAuthors.get(i).getDepartment_manage());
  324. smartAuthority.setCreateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(),"yyyy-MM-dd HH:mm:ss"));
  325. smartAuthority.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(),"yyyy-MM-dd HH:mm:ss"));
  326. smartAuthority.setCreateUser("admin");
  327. smartAuthority.setUpdateUser("admin");
  328. smartAuthority.setDeleted(userAuthors.get(i).getDeleted());
  329. smartAuthorityService.updateSmartAuthority(smartAuthority);
  330. } else {
  331. SmartAuthority smartAuthority = new SmartAuthority();
  332. smartAuthority.setUserId(userAuthors.get(i).getUserId());
  333. smartAuthority.setGroupId(sa.getId());
  334. smartAuthority.setDepartmentView(userAuthors.get(i).getDepartment_view());
  335. smartAuthority.setDepartmentManage(userAuthors.get(i).getDepartment_manage());
  336. smartAuthority.setCreateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(),"yyyy-MM-dd HH:mm:ss"));
  337. smartAuthority.setUpdateTime(TimeExchange2.StringToDate(TimeExchange2.getTime(),"yyyy-MM-dd HH:mm:ss"));
  338. smartAuthority.setCreateUser("admin");
  339. smartAuthority.setUpdateUser("admin");
  340. smartAuthority.setDeleted(0);
  341. smartAuthorityService.insertSmartAuthority(smartAuthority);
  342. }
  343. }
  344. sa.setUserId(userid);
  345. int result = smartAuthorGroupService.updateSmartAuthorGroup(sa);
  346. return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
  347. }
  348. return CommonResult.errorMsg("参数格式错误");
  349. }
  350. @PassToken
  351. @Override
  352. public CommonResult queryUserAuthor(String userId){
  353. QueryWrapper<SmartAuthority> queryWrapper1 = new QueryWrapper<>();
  354. queryWrapper1.eq("deleted", 0);
  355. queryWrapper1.eq("user_id", userId);
  356. List<SmartAuthority> userAuthor = smartAuthorityService.getAuthorByKey(queryWrapper1);
  357. if (userAuthor.size()<=0){
  358. return CommonResult.ok(new ArrayList<>());
  359. }
  360. SmartAuthority smartAuthority = userAuthor.get(0);
  361. Integer groupId = smartAuthority.getGroupId();
  362. SmartAuthorGroup smartAuthorGroup = smartAuthorGroupService.getSmartById(groupId);
  363. if (smartAuthorGroup==null){
  364. return CommonResult.ok(new ArrayList<>());
  365. }
  366. JSONObject jsonObject = new JSONObject();
  367. jsonObject.put("departmentViewAuthor", smartAuthority.getDepartmentView());
  368. jsonObject.put("departmentManageAuthor", smartAuthority.getDepartmentManage());
  369. if (smartAuthorGroup.getDeleted()==0){
  370. jsonObject.put("treAuthor", smartAuthorGroup.getApplyId());
  371. }
  372. return CommonResult.ok(jsonObject);
  373. }
  374. /**
  375. * 查看权限列表
  376. * @return
  377. */
  378. @Override
  379. @PassToken
  380. public CommonResult querySmartAuthorGroup(Integer authorGroupId) {
  381. List<SmartAuthorGroup> smartAuthorGroups = smartAuthorGroupService.getAuthorGroupList();
  382. List<AuthorListGroup> authorListGroupList = null;
  383. if (smartAuthorGroups.size()>0) {
  384. authorListGroupList = TreeRecordsUtil.queryCommentTreeRecords(authorGroupId, smartAuthorGroups);
  385. }
  386. return CommonResult.ok(authorListGroupList);
  387. }
  388. @Override
  389. public CommonResult deleteSmartAuthorGroupById(int id) {
  390. SmartAuthorGroup data = smartAuthorGroupService.getSmartById(id);
  391. if(data == null){
  392. return CommonResult.fail("当前数据不存在,删除失败!");
  393. }
  394. int result = smartAuthorGroupService.deleteSmartAuthorGroupById(id);
  395. return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
  396. }
  397. }