HightlightTemplateService.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package cn.cslg.pas.service.business;
  2. import cn.cslg.pas.common.dto.HightlightTemplateDTO;
  3. import cn.cslg.pas.common.model.cronModel.Personnel;
  4. import cn.cslg.pas.common.model.cronModel.PersonnelVO;
  5. import cn.cslg.pas.common.utils.CacheUtils;
  6. import cn.cslg.pas.common.utils.JsonUtils;
  7. import cn.cslg.pas.common.utils.LoginUtils;
  8. import cn.cslg.pas.common.utils.StringUtils;
  9. import cn.cslg.pas.common.vo.HightlightTemplateVO;
  10. import cn.cslg.pas.common.vo.business.ProjectTaskVO;
  11. import cn.cslg.pas.domain.business.HightlightTemplate;
  12. import cn.cslg.pas.exception.XiaoShiException;
  13. import cn.cslg.pas.mapper.HightlightTemplateMapper;
  14. import cn.cslg.pas.service.permissions.PermissionService;
  15. import cn.cslg.pas.service.query.Query;
  16. import com.alibaba.fastjson.JSONObject;
  17. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  18. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.springframework.beans.BeanUtils;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.scheduling.annotation.Async;
  23. import org.springframework.stereotype.Service;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. /**
  28. * @Author xiexiang
  29. * @Date 2024/1/12
  30. */
  31. @Slf4j
  32. @Service
  33. public class HightlightTemplateService extends ServiceImpl<HightlightTemplateMapper, HightlightTemplate> {
  34. @Autowired
  35. private LoginUtils loginUtils;
  36. @Autowired
  37. private CacheUtils cacheUtils;
  38. @Autowired
  39. private PermissionService permissionService;
  40. /**
  41. * 添加or更新
  42. * @param hightlightTemplateDTO
  43. * @return
  44. */
  45. public HightlightTemplate saveOrUpdate(HightlightTemplateDTO hightlightTemplateDTO){
  46. if (hightlightTemplateDTO == null) {
  47. throw new XiaoShiException("入参为空");
  48. }
  49. PersonnelVO personnelVO = new PersonnelVO();
  50. try {
  51. personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  52. } catch (Exception e) {
  53. throw new XiaoShiException("未查询到当前登陆人");
  54. }
  55. Integer id = hightlightTemplateDTO.getId();
  56. HightlightTemplate hightlightTemplate = new HightlightTemplate();
  57. if (id != null) {
  58. //update
  59. hightlightTemplate = this.getById(id);
  60. BeanUtils.copyProperties(hightlightTemplateDTO, hightlightTemplate);
  61. hightlightTemplate.setConfig(JsonUtils.objectToJson(hightlightTemplateDTO.getConfigs()));
  62. hightlightTemplate.updateById();
  63. } else {
  64. String name = hightlightTemplateDTO.getName();
  65. if (name == null || StringUtils.isEmpty(name)) {
  66. name = "新模板";
  67. hightlightTemplateDTO.setName(name);
  68. }
  69. BeanUtils.copyProperties(hightlightTemplateDTO, hightlightTemplate);
  70. this.setConfigs(hightlightTemplate);
  71. if (hightlightTemplate.getCreateId() == null) {
  72. hightlightTemplate.setCreateId(personnelVO.getId());
  73. }
  74. hightlightTemplate.insert();
  75. }
  76. return hightlightTemplate;
  77. }
  78. /**
  79. * 查询
  80. * @param projectId
  81. * @return
  82. * @throws IOException
  83. */
  84. public List<HightlightTemplateVO> getHightlight(Integer projectId) throws IOException {
  85. if (projectId == null) {
  86. throw new XiaoShiException("入参为空");
  87. }
  88. PersonnelVO personnelVO = new PersonnelVO();
  89. try {
  90. personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  91. } catch (Exception e) {
  92. throw new XiaoShiException("未查询到当前登陆人");
  93. }
  94. List<HightlightTemplateVO> hightlightTemplateVOS = new ArrayList<>();
  95. LambdaQueryWrapper<HightlightTemplate> queryWrapper = new LambdaQueryWrapper<>();
  96. queryWrapper.eq(HightlightTemplate::getProjectId, projectId)
  97. .eq(HightlightTemplate::getCreateId, personnelVO.getId());
  98. List<HightlightTemplate> hightlightTemplates = this.list(queryWrapper);
  99. if (!hightlightTemplates.isEmpty()) {
  100. hightlightTemplates.forEach(item -> {
  101. HightlightTemplateVO hightlightTemplateVO = new HightlightTemplateVO();
  102. BeanUtils.copyProperties(item, hightlightTemplateVO);
  103. hightlightTemplateVOS.add(hightlightTemplateVO);
  104. });
  105. } else {
  106. HightlightTemplateVO hightlightTemplateVO = new HightlightTemplateVO();
  107. HightlightTemplateDTO hightlightDTO = new HightlightTemplateDTO();
  108. hightlightDTO.setName("默认模板");
  109. hightlightDTO.setIfEnable(true);
  110. hightlightDTO.setProjectId(projectId);
  111. hightlightDTO.setCreateId(personnelVO.getId());
  112. hightlightDTO.setIfEnable(true);
  113. hightlightDTO.setIfDefault(true);
  114. Integer id = this.saveOrUpdate(hightlightDTO).getId();
  115. HightlightTemplate hightlightTemplate = this.getById(id);
  116. BeanUtils.copyProperties(hightlightTemplate, hightlightTemplateVO);
  117. hightlightTemplateVOS.add(hightlightTemplateVO);
  118. }
  119. this.loadHightlightVOS(hightlightTemplateVOS);
  120. return hightlightTemplateVOS;
  121. }
  122. public void loadHightlightVOS(List<HightlightTemplateVO> hightlightTemplateVOS) throws IOException {
  123. List<String> createIds = new ArrayList<>();
  124. hightlightTemplateVOS.forEach(item -> {
  125. if (item.getCreateId() != null) {
  126. createIds.add(item.getCreateId());
  127. }
  128. });
  129. List<Personnel> personnels = new ArrayList<>();
  130. //查询发起人名称
  131. if (createIds.size() != 0) {
  132. String res = permissionService.getPersonnelByIdsFromPCS(createIds);
  133. JSONObject jsonObject = JSONObject.parseObject(res);
  134. personnels = JSONObject.parseArray(jsonObject.getString("data"), Personnel.class);
  135. }
  136. //装载信息
  137. for (HightlightTemplateVO hightlightTemplateVO : hightlightTemplateVOS) {
  138. //装载人员信息
  139. Personnel personnel = personnels.stream().filter(item -> item.getId().equals(hightlightTemplateVO.getCreateId())).findFirst().orElse(null);
  140. if (personnel != null) {
  141. hightlightTemplateVO.setCreateName(personnel.getPersonnelName());
  142. }
  143. hightlightTemplateVO.setConfigs(JsonUtils.jsonToList(hightlightTemplateVO.getConfig(), HightlightTemplate.Config.class));
  144. hightlightTemplateVO.setConfig(null);
  145. }
  146. }
  147. public List<Integer> deleteHightlight(List<Integer> ids) {
  148. if (!ids.isEmpty()) {
  149. this.removeBatchByIds(ids);
  150. }
  151. return ids;
  152. }
  153. public void setConfigs(HightlightTemplate hightlightTemplate) {
  154. List<HightlightTemplate.Config> configs = new ArrayList<>();
  155. List<String> colors = new ArrayList<String>() {{
  156. add("#5470c6");
  157. add("#91cc75");
  158. add("#fac858");
  159. add("#ee6666");
  160. add("#73c0de");
  161. }};
  162. for (String color : colors) {
  163. HightlightTemplate.Config config = new HightlightTemplate.Config();
  164. config.setKeywords("");
  165. config.setColor(color);
  166. configs.add(config);
  167. }
  168. hightlightTemplate.setConfig(JsonUtils.objectToJson(configs));
  169. hightlightTemplate.setConfigs(configs);
  170. }
  171. }