SysPlantController.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * Copyright (c) 2016-2019 人人开源 All rights reserved.
  3. * <p>
  4. * https://www.renren.io
  5. * <p>
  6. * 版权所有,侵权必究!
  7. */
  8. package io.renren.modules.sys.controller;
  9. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  10. import io.renren.common.utils.Constant;
  11. import io.renren.common.utils.R;
  12. import io.renren.modules.sys.entity.SysPlantEntity;
  13. import io.renren.modules.sys.entity.SysUserPlantEntity;
  14. import io.renren.modules.sys.service.SysPlantService;
  15. import io.renren.modules.sys.service.SysUserPlantService;
  16. import org.apache.shiro.authz.annotation.RequiresPermissions;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.web.bind.annotation.PathVariable;
  19. import org.springframework.web.bind.annotation.RequestBody;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RestController;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. /**
  26. * 装置管理
  27. *
  28. * @author Mark 735032128@qq.com
  29. */
  30. @RestController
  31. @RequestMapping("/sys/plant")
  32. public class SysPlantController extends AbstractController {
  33. @Autowired
  34. private SysPlantService sysPlantService;
  35. @Autowired
  36. private SysUserPlantService sysUserPlantService;
  37. /**
  38. * 列表
  39. */
  40. @RequestMapping("/list")
  41. @RequiresPermissions("sys:plant:list")
  42. public List<SysPlantEntity> list() {
  43. List<SysPlantEntity> plantList = sysPlantService.queryList(new HashMap<String, Object>());
  44. return plantList;
  45. }
  46. /**
  47. * 用户列表
  48. */
  49. @RequestMapping("/mylist")
  50. public List<SysPlantEntity> mylist() {
  51. List<SysUserPlantEntity> userPlantList = sysUserPlantService.list(new QueryWrapper<SysUserPlantEntity>()
  52. .eq("user_id" ,getUserId()));
  53. List<SysPlantEntity> plantList = new ArrayList<>();
  54. for (SysUserPlantEntity s : userPlantList
  55. ) {
  56. SysPlantEntity sysPlantEntity = sysPlantService.getById(s.getPlantId());
  57. if (sysPlantEntity != null)
  58. plantList.add(sysPlantEntity);
  59. }
  60. return plantList;
  61. }
  62. /**
  63. * 选择装置(添加、修改菜单)
  64. */
  65. @RequestMapping("/select")
  66. @RequiresPermissions("sys:plant:select")
  67. public R select() {
  68. List<SysPlantEntity> plantList = sysPlantService.queryList(new HashMap<String, Object>());
  69. //添加一级装置
  70. if (getUserId() == Constant.SUPER_ADMIN) {
  71. SysPlantEntity root = new SysPlantEntity();
  72. root.setPlantId(0L);
  73. root.setName("一级装置");
  74. root.setParentId(-1L);
  75. root.setOpen(true);
  76. plantList.add(root);
  77. }
  78. return R.ok().put("plantList", plantList);
  79. }
  80. /**
  81. * 上级装置Id(管理员则为0)
  82. */
  83. @RequestMapping("/info")
  84. @RequiresPermissions("sys:plant:list")
  85. public R info() {
  86. long plantId = 0;
  87. if (getUserId() != Constant.SUPER_ADMIN) {
  88. List<SysPlantEntity> plantList = sysPlantService.queryList(new HashMap<String, Object>());
  89. Long parentId = null;
  90. for (SysPlantEntity sysPlantEntity : plantList) {
  91. if (parentId == null) {
  92. parentId = sysPlantEntity.getParentId();
  93. continue;
  94. }
  95. if (parentId > sysPlantEntity.getParentId().longValue()) {
  96. parentId = sysPlantEntity.getParentId();
  97. }
  98. }
  99. plantId = parentId;
  100. }
  101. return R.ok().put("plantId", plantId);
  102. }
  103. /**
  104. * 信息
  105. */
  106. @RequestMapping("/info/{plantId}")
  107. @RequiresPermissions("sys:plant:info")
  108. public R info(@PathVariable("plantId") Long plantId) {
  109. SysPlantEntity plant = sysPlantService.getById(plantId);
  110. return R.ok().put("plant", plant);
  111. }
  112. /**
  113. * 保存
  114. */
  115. @RequestMapping("/save")
  116. @RequiresPermissions("sys:plant:save")
  117. public R save(@RequestBody SysPlantEntity plant) {
  118. sysPlantService.save(plant);
  119. return R.ok();
  120. }
  121. /**
  122. * 修改
  123. */
  124. @RequestMapping("/update")
  125. @RequiresPermissions("sys:plant:update")
  126. public R update(@RequestBody SysPlantEntity plant) {
  127. sysPlantService.updateById(plant);
  128. return R.ok();
  129. }
  130. /**
  131. * 删除
  132. */
  133. @RequestMapping("/delete/{plantId}")
  134. @RequiresPermissions("sys:plant:delete")
  135. public R delete(@PathVariable("plantId") long plantId) {
  136. //判断是否有子装置
  137. List<Long> plantList = sysPlantService.queryPlantIdList(plantId);
  138. if (plantList.size() > 0) {
  139. return R.error("请先删除子装置");
  140. }
  141. sysPlantService.removeById(plantId);
  142. return R.ok();
  143. }
  144. }