package cn.cslg.pas.service.business; import cn.cslg.pas.common.dto.HightlightTemplateDTO; import cn.cslg.pas.common.model.cronModel.Personnel; import cn.cslg.pas.common.model.cronModel.PersonnelVO; import cn.cslg.pas.common.utils.CacheUtils; import cn.cslg.pas.common.utils.JsonUtils; import cn.cslg.pas.common.utils.LoginUtils; import cn.cslg.pas.common.utils.StringUtils; import cn.cslg.pas.common.vo.HightlightTemplateVO; import cn.cslg.pas.common.vo.business.ProjectTaskVO; import cn.cslg.pas.domain.business.HightlightTemplate; import cn.cslg.pas.domain.business.LitigationHistory; import cn.cslg.pas.exception.XiaoShiException; import cn.cslg.pas.mapper.HightlightTemplateMapper; import cn.cslg.pas.service.permissions.PermissionService; import cn.cslg.pas.service.query.Query; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * @Author xiexiang * @Date 2024/1/12 */ @Slf4j @Service public class HightlightTemplateService extends ServiceImpl { @Autowired private LoginUtils loginUtils; @Autowired private CacheUtils cacheUtils; @Autowired private PermissionService permissionService; /** * 添加or更新 * @param hightlightTemplateDTO * @return */ public HightlightTemplate saveOrUpdate(HightlightTemplateDTO hightlightTemplateDTO){ if (hightlightTemplateDTO == null) { throw new XiaoShiException("入参为空"); } PersonnelVO personnelVO = new PersonnelVO(); personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); Integer id = hightlightTemplateDTO.getId(); HightlightTemplate hightlightTemplate = new HightlightTemplate(); if (id != null) { //update hightlightTemplate = this.getById(id); Date createTime = hightlightTemplate.getCreateTime(); BeanUtils.copyProperties(hightlightTemplateDTO, hightlightTemplate); hightlightTemplate.setConfig(JsonUtils.objectToJson(hightlightTemplateDTO.getConfigs())); hightlightTemplate.setCreateTime(createTime); hightlightTemplate.updateById(); } else { String name = hightlightTemplateDTO.getName(); if (name == null || StringUtils.isEmpty(name)) { name = "新模板"; hightlightTemplateDTO.setName(name); } BeanUtils.copyProperties(hightlightTemplateDTO, hightlightTemplate); this.setConfigs(hightlightTemplate); if (hightlightTemplate.getCreateId() == null) { hightlightTemplate.setCreateId(personnelVO.getId()); } hightlightTemplate.insert(); } return hightlightTemplate; } /** * 查询 * @param projectId * @return * @throws IOException */ public List getHightlight(Integer projectId) throws IOException { if (projectId == null) { throw new XiaoShiException("入参为空"); } PersonnelVO personnelVO = new PersonnelVO(); personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); List hightlightTemplateVOS = new ArrayList<>(); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(HightlightTemplate::getProjectId, projectId) .eq(HightlightTemplate::getCreateId, personnelVO.getId()) .orderByDesc(HightlightTemplate::getCreateTime); List hightlightTemplates = this.list(queryWrapper); if (!hightlightTemplates.isEmpty()) { hightlightTemplates.forEach(item -> { HightlightTemplateVO hightlightTemplateVO = new HightlightTemplateVO(); BeanUtils.copyProperties(item, hightlightTemplateVO); hightlightTemplateVOS.add(hightlightTemplateVO); }); } else { HightlightTemplateVO hightlightTemplateVO = new HightlightTemplateVO(); HightlightTemplateDTO hightlightDTO = new HightlightTemplateDTO(); hightlightDTO.setName("默认模板"); hightlightDTO.setIfEnable(true); hightlightDTO.setProjectId(projectId); hightlightDTO.setCreateId(personnelVO.getId()); hightlightDTO.setIfEnable(true); hightlightDTO.setIfDefault(true); Integer id = this.saveOrUpdate(hightlightDTO).getId(); HightlightTemplate hightlightTemplate = this.getById(id); BeanUtils.copyProperties(hightlightTemplate, hightlightTemplateVO); hightlightTemplateVOS.add(hightlightTemplateVO); } this.loadHightlightVOS(hightlightTemplateVOS); return hightlightTemplateVOS; } public void loadHightlightVOS(List hightlightTemplateVOS) throws IOException { List createIds = new ArrayList<>(); hightlightTemplateVOS.forEach(item -> { if (item.getCreateId() != null) { createIds.add(item.getCreateId()); } }); List personnels = new ArrayList<>(); //查询发起人名称 if (createIds.size() != 0) { String res = permissionService.getPersonnelByIdsFromPCS(createIds); JSONObject jsonObject = JSONObject.parseObject(res); personnels = JSONObject.parseArray(jsonObject.getString("data"), Personnel.class); } //装载信息 for (HightlightTemplateVO hightlightTemplateVO : hightlightTemplateVOS) { //装载人员信息 Personnel personnel = personnels.stream().filter(item -> item.getId().equals(hightlightTemplateVO.getCreateId())).findFirst().orElse(null); if (personnel != null) { hightlightTemplateVO.setCreateName(personnel.getPersonnelName()); } hightlightTemplateVO.setConfigs(JsonUtils.jsonToList(hightlightTemplateVO.getConfig(), HightlightTemplate.Config.class)); hightlightTemplateVO.setConfig(null); } } public List deleteHightlight(List ids) { if (!ids.isEmpty()) { this.removeBatchByIds(ids); } return ids; } public void setConfigs(HightlightTemplate hightlightTemplate) { List configs = new ArrayList<>(); List colors = new ArrayList() {{ add("#5470c6"); add("#91cc75"); add("#fac858"); add("#ee6666"); add("#73c0de"); }}; for (String color : colors) { HightlightTemplate.Config config = new HightlightTemplate.Config(); config.setKeywords(""); config.setColor(color); configs.add(config); } hightlightTemplate.setConfig(JsonUtils.objectToJson(configs)); hightlightTemplate.setConfigs(configs); } }