CustomAnalysisItemService.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package cn.cslg.pas.service;
  2. import cn.cslg.pas.common.core.base.EStatus;
  3. import cn.cslg.pas.common.utils.Response;
  4. import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
  5. import cn.cslg.pas.common.utils.StringUtils;
  6. import cn.cslg.pas.domain.CustomAnalysisItem;
  7. import cn.cslg.pas.mapper.CustomAnalysisItemMapper;
  8. import cn.cslg.pas.common.utils.CacheUtils;
  9. import cn.dev33.satoken.stp.StpUtil;
  10. import cn.hutool.core.util.IdUtil;
  11. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  12. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  13. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  14. import lombok.RequiredArgsConstructor;
  15. import org.springframework.context.annotation.Lazy;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import java.util.*;
  19. import java.util.stream.Collectors;
  20. /**
  21. * <p>
  22. * 自定义分析项目 服务实现类
  23. * </p>
  24. *
  25. * @author 王岩
  26. * @since 2021-12-23
  27. */
  28. @Service
  29. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  30. public class CustomAnalysisItemService extends ServiceImpl<CustomAnalysisItemMapper, CustomAnalysisItem> {
  31. private final CacheUtils cacheUtils;
  32. private final CustomAnalysisItemSettingService customAnalysisItemSettingService;
  33. private final CustomAnalysisItemSchemaService customAnalysisItemSchemaService;
  34. private final CustomAnalysisItemSourceService customAnalysisItemSourceService;
  35. private final LoginUtils loginUtils;
  36. public List<CustomAnalysisItem> getTreeList(Integer projectId) {
  37. LambdaQueryWrapper<CustomAnalysisItem> queryWrapper = new LambdaQueryWrapper<>();
  38. queryWrapper.and(Wrapper -> Wrapper.eq(CustomAnalysisItem::getProjectId, projectId).or().eq(CustomAnalysisItem::getPermissions, 2));
  39. queryWrapper.orderByAsc(CustomAnalysisItem::getSort);
  40. List<CustomAnalysisItem> list = this.list(queryWrapper);
  41. list.forEach(item -> {
  42. item.setSetting(null);
  43. item.setSchema(null);
  44. item.setSource(null);
  45. });
  46. return this.buildTree2(list);
  47. }
  48. public CustomAnalysisItem getItemByUid(String uid) {
  49. CustomAnalysisItem customAnalysisItem = this.getOne(Wrappers.<CustomAnalysisItem>lambdaQuery().eq(CustomAnalysisItem::getUid, uid));
  50. if (customAnalysisItem.getType().equals(1)) {
  51. customAnalysisItem.setSetting(customAnalysisItemSettingService.getItemSettingByUid(customAnalysisItem.getUid()));
  52. customAnalysisItem.setSchema(customAnalysisItemSchemaService.getItemSchemaByUid(customAnalysisItem.getUid()));
  53. customAnalysisItem.setSource(customAnalysisItemSourceService.getItemSourceData(customAnalysisItem));
  54. } else {
  55. customAnalysisItem.setSetting(null);
  56. customAnalysisItem.setSchema(null);
  57. customAnalysisItem.setSource(null);
  58. }
  59. return customAnalysisItem;
  60. }
  61. public Integer addGroupItem(CustomAnalysisItem customAnalysisItem) {
  62. CustomAnalysisItem group = new CustomAnalysisItem();
  63. group.setUid(IdUtil.simpleUUID());
  64. group.setName(customAnalysisItem.getParentName());
  65. group.setParentId(0);
  66. group.setType(0);
  67. group.setProjectId(customAnalysisItem.getProjectId());
  68. group.setPermissions(2);
  69. group.setSort((int) this.count(Wrappers.<CustomAnalysisItem>lambdaQuery().eq(CustomAnalysisItem::getParentId, 0)) + 1);
  70. group.setCreateBy(loginUtils.getId());
  71. group.insert();
  72. return group.getId();
  73. }
  74. public Map<String, Object> getOptionReturnData(CustomAnalysisItem customAnalysisItem) {
  75. Map<String, Object> map = new HashMap<>();
  76. map.put("uid", customAnalysisItem.getUid());
  77. map.put("name", customAnalysisItem.getName());
  78. map.put("parentId", customAnalysisItem.getParentId());
  79. return map;
  80. }
  81. @Transactional(rollbackFor = Exception.class)
  82. public String add(CustomAnalysisItem customAnalysisItem) {
  83. if (this.checkNameUnique(customAnalysisItem)) {
  84. return Response.error("名称重复");
  85. }
  86. String uid = IdUtil.simpleUUID();
  87. if (StringUtils.isNotEmpty(customAnalysisItem.getParentName())) {
  88. customAnalysisItem.setParentId(this.addGroupItem(customAnalysisItem));
  89. }
  90. customAnalysisItem.setUid(uid);
  91. customAnalysisItem.setCreateBy(loginUtils.getId());
  92. customAnalysisItem.insert();
  93. if (customAnalysisItem.getType().equals(1)) {
  94. customAnalysisItemSettingService.add(customAnalysisItem.getSetting(), uid);
  95. customAnalysisItemSchemaService.add(customAnalysisItem.getSchema(), uid);
  96. customAnalysisItemSourceService.edit(customAnalysisItem);
  97. }
  98. return Response.success(this.getOptionReturnData(customAnalysisItem));
  99. }
  100. @Transactional(rollbackFor = Exception.class)
  101. public String edit(CustomAnalysisItem customAnalysisItem) {
  102. if (this.checkNameUnique(customAnalysisItem)) {
  103. return Response.error("名称重复");
  104. }
  105. CustomAnalysisItem temp = baseMapper.selectById(customAnalysisItem.getId());
  106. if (StringUtils.isNotEmpty(customAnalysisItem.getParentName())) {
  107. customAnalysisItem.setParentId(this.addGroupItem(customAnalysisItem));
  108. }
  109. temp.setName(customAnalysisItem.getName());
  110. temp.setParentId(customAnalysisItem.getParentId());
  111. temp.setSort(customAnalysisItem.getSort());
  112. temp.setRemark(customAnalysisItem.getRemark());
  113. temp.setType(customAnalysisItem.getType());
  114. temp.setPermissions(customAnalysisItem.getPermissions());
  115. temp.setSchema(customAnalysisItem.getSchema());
  116. temp.setSetting(customAnalysisItem.getSetting());
  117. temp.setSource(customAnalysisItem.getSource());
  118. temp.setProjectId(customAnalysisItem.getProjectId());
  119. temp.updateById();
  120. if (temp.getType().equals(1)) {
  121. customAnalysisItemSettingService.edit(temp.getSetting(), temp.getUid());
  122. customAnalysisItemSchemaService.edit(temp.getSchema(), temp.getUid());
  123. customAnalysisItemSourceService.edit(temp);
  124. }
  125. return Response.success(this.getOptionReturnData(temp));
  126. }
  127. @Transactional
  128. public String delete(Integer id) {
  129. CustomAnalysisItem customAnalysisItem = baseMapper.selectById(id);
  130. if (this.list(Wrappers.<CustomAnalysisItem>lambdaQuery().eq(CustomAnalysisItem::getParentId, id)).size() != 0) {
  131. return Response.error("删除失败,请先删除子节点");
  132. }
  133. this.removeById(id);
  134. customAnalysisItemSchemaService.deleteByUid(customAnalysisItem.getUid());
  135. customAnalysisItemSettingService.deleteByUid(customAnalysisItem.getUid());
  136. customAnalysisItemSourceService.deleteByUid(customAnalysisItem.getUid());
  137. return Response.success(true);
  138. }
  139. public List<CustomAnalysisItem> buildTree2(List<CustomAnalysisItem> items) {
  140. List<CustomAnalysisItem> returnList = items.stream().filter(item -> item.getParentId().equals(0)).collect(Collectors.toList());
  141. returnList.forEach(item -> {
  142. item.setChildren(items.stream().filter(c -> c.getParentId().equals(item.getId())).collect(Collectors.toList()));
  143. });
  144. return returnList;
  145. }
  146. public List<CustomAnalysisItem> buildTree(List<CustomAnalysisItem> items) {
  147. List<CustomAnalysisItem> returnList = new ArrayList<>();
  148. List<Integer> tempList = new ArrayList<>();
  149. for (CustomAnalysisItem option : items) {
  150. tempList.add(option.getId());
  151. }
  152. for (Iterator<CustomAnalysisItem> iterator = items.iterator(); iterator.hasNext(); ) {
  153. CustomAnalysisItem item = (CustomAnalysisItem) iterator.next();
  154. if (!tempList.contains(item.getParentId())) {
  155. recursionFn(items, item);
  156. returnList.add(item);
  157. }
  158. }
  159. if (returnList.isEmpty()) {
  160. returnList = items;
  161. }
  162. return returnList;
  163. }
  164. private void recursionFn(List<CustomAnalysisItem> list, CustomAnalysisItem t) {
  165. List<CustomAnalysisItem> childList = getChildList(list, t);
  166. t.setChildren(childList);
  167. for (CustomAnalysisItem tChild : childList) {
  168. if (hasChild(list, tChild)) {
  169. recursionFn(list, tChild);
  170. }
  171. }
  172. }
  173. private List<CustomAnalysisItem> getChildList(List<CustomAnalysisItem> list, CustomAnalysisItem t) {
  174. List<CustomAnalysisItem> tlist = new ArrayList<>();
  175. Iterator<CustomAnalysisItem> it = list.iterator();
  176. while (it.hasNext()) {
  177. CustomAnalysisItem n = (CustomAnalysisItem) it.next();
  178. if (n.getParentId().equals(t.getId())) {
  179. tlist.add(n);
  180. }
  181. }
  182. return tlist;
  183. }
  184. private boolean hasChild(List<CustomAnalysisItem> list, CustomAnalysisItem t) {
  185. return getChildList(list, t).size() > 0 ? true : false;
  186. }
  187. public Boolean checkNameUnique(CustomAnalysisItem customAnalysisItem) {
  188. Integer id = StringUtils.isNull(customAnalysisItem.getId()) ? -1 : customAnalysisItem.getId();
  189. CustomAnalysisItem info = this.getOne(Wrappers.<CustomAnalysisItem>lambdaQuery().eq(CustomAnalysisItem::getParentId, customAnalysisItem.getParentId()).eq(CustomAnalysisItem::getName, customAnalysisItem.getName()).eq(CustomAnalysisItem::getType, customAnalysisItem.getType()).last("limit 1"));
  190. return StringUtils.isNotNull(info) && !info.getId().equals(id);
  191. }
  192. }