package cn.cslg.pas.service.business; import cn.cslg.pas.common.dto.customAnalyse.CustomAnalyseDTO; import cn.cslg.pas.common.dto.customAnalyse.SelectCustomAnalyseDTO; import cn.cslg.pas.common.dto.customAnalyse.SelectCustomAnalyseListDTO; import cn.cslg.pas.common.model.cronModel.PersonnelVO; import cn.cslg.pas.common.utils.CacheUtils; import cn.cslg.pas.common.utils.LoginUtils; import cn.cslg.pas.common.utils.Response; import cn.cslg.pas.common.vo.customAnalyse.CustomAnalyseIdVO; import cn.cslg.pas.common.vo.customAnalyse.CustomAnalyseVO; import cn.cslg.pas.common.vo.customAnalyse.SelectAnalyseVO; import cn.cslg.pas.common.vo.customAnalyse.SelectCustomAnalyseVO; import cn.cslg.pas.domain.business.CustomAnalysisItem; import cn.cslg.pas.exception.UnLoginException; import cn.cslg.pas.mapper.CustomAnalysisItemMapper; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.ObjectUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.stream.Collectors; /** * 自定义分析项目Service层 * * @Author zero * @Date 2024/01/12 */ @Service public class CustomAnalyseService extends ServiceImpl { @Autowired private CacheUtils cacheUtils; @Autowired private LoginUtils loginUtils; @Autowired private CustomAnalysisItemMapper customAnalysisItemMapper; @Autowired private CustomAnalysisItemSchemaService itemSchemaService; @Autowired private CustomAnalysisItemSourceService itemSourceService; @Autowired private CustomAnalysisItemSettingService itemSettingService; public List queryAnalyseGroup(SelectCustomAnalyseVO vo) { //获取登陆人信息 用于设置创建人 PersonnelVO personnelVO = new PersonnelVO(); try { personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); } catch (Exception e) { throw new UnLoginException("未登录"); } SelectAnalyseVO analyseVO = new SelectAnalyseVO(); analyseVO.setProjectId(vo.getProjectId()); analyseVO.setTenantId(personnelVO.getTenantId()); analyseVO.setCreatorId(personnelVO.getId()); List list = customAnalysisItemMapper.queryAnalyseGroup(analyseVO); List returnList = list.stream().filter(item -> item.getParentId().equals(0)).collect(Collectors.toList()); returnList.forEach(item -> { item.setChildren(list.stream().filter(c -> c.getParentId().equals(item.getId())).collect(Collectors.toList())); }); return returnList; } public SelectCustomAnalyseDTO queryAnalyseGroupDetail(CustomAnalyseIdVO vo) { SelectCustomAnalyseDTO dto = new SelectCustomAnalyseDTO(); CustomAnalysisItem item = customAnalysisItemMapper.selectById(vo.getId()); if (item.getParentId() != null && item.getParentId() != 0) { CustomAnalysisItem parentItem = customAnalysisItemMapper.selectById(item.getParentId()); dto.setParentId(item.getParentId()); dto.setParentName(parentItem.getName()); } BeanUtils.copyProperties(item, dto); return dto; } public CustomAnalyseDTO queryAnalyseDetail(CustomAnalyseIdVO vo) { CustomAnalyseDTO dto = new CustomAnalyseDTO(); CustomAnalysisItem item = customAnalysisItemMapper.selectById(vo.getId()); BeanUtils.copyProperties(item, dto); if (item.getType().equals(2)) { dto.setSchema(itemSchemaService.getItemSchemaByUid(item.getUid())); dto.setSetting(itemSettingService.getItemSettingByUid(item.getUid())); dto.setSource(itemSourceService.getItemSourceData(dto)); } else { dto.setSchema(null); dto.setSetting(null); dto.setSource(null); } return dto; } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class) public Integer addAnalyseGroup(CustomAnalyseDTO vo) throws Exception { List items = customAnalysisItemMapper.selectList(new QueryWrapper() .lambda() .eq(CustomAnalysisItem::getName, vo.getName()) .eq(CustomAnalysisItem::getType, vo.getType())); if (items.size() > 1) { throw new Exception("名称不可重复"); } String uid = IdUtil.simpleUUID(); //获取登陆人信息 用于设置创建人 PersonnelVO personnelVO = new PersonnelVO(); try { personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); } catch (Exception e) { throw new UnLoginException("未登录"); } CustomAnalysisItem item = new CustomAnalysisItem(); item.setUid(uid); BeanUtils.copyProperties(vo, item); if (vo.getType() == 1) { item.setParentId(0); } else { item.setParentId(vo.getParentId()); } item.setTenantId(personnelVO.getTenantId()); item.setCreateId(personnelVO.getId()); item.setCreateName(personnelVO.getName()); item.setCreateTime(new Date()); item.setUpdateTime(new Date()); item.insert(); if (item.getType().equals(2)) { itemSettingService.add(vo.getSetting(), uid); itemSchemaService.add(vo.getSchema(), uid); itemSourceService.edit(vo); } return item.getId(); } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class) public Integer editAnalyseGroup(CustomAnalyseDTO vo) throws Exception { List items = customAnalysisItemMapper.selectList(new QueryWrapper() .lambda() .eq(CustomAnalysisItem::getName, vo.getName()) .eq(CustomAnalysisItem::getType, vo.getType())); if (items.size() > 1) { throw new Exception("名称不可重复"); } //获取登陆人信息 用于设置创建人 PersonnelVO personnelVO = new PersonnelVO(); try { personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); } catch (Exception e) { throw new UnLoginException("未登录"); } CustomAnalysisItem item = customAnalysisItemMapper.selectById(vo.getId()); BeanUtils.copyProperties(vo, item); item.setTenantId(personnelVO.getTenantId()); item.setCreateId(personnelVO.getId()); item.setCreateName(personnelVO.getName()); item.setUpdateTime(new Date()); item.updateById(); if (item.getType().equals(2)) { itemSettingService.edit(vo.getSetting(), item.getUid()); itemSchemaService.edit(vo.getSchema(), item.getUid()); itemSourceService.edit(vo); } return item.getId(); } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class) public Integer delAnalyseGroup(CustomAnalyseIdVO vo) throws Exception { CustomAnalysisItem item = customAnalysisItemMapper.selectById(vo.getId()); long count = this.count(new LambdaQueryWrapper() .eq(CustomAnalysisItem::getParentId, vo.getId())); if (count > 0) { throw new Exception("删除失败,请先删除子节点"); } this.removeById(vo.getId()); itemSchemaService.deleteByUid(item.getUid()); itemSettingService.deleteByUid(item.getUid()); itemSourceService.deleteByUid(item.getUid()); return item.getId(); } }