package cn.cslg.pas.service.business; import cn.cslg.pas.common.dto.business.EventDTO; import cn.cslg.pas.common.dto.business.UpdateEventDTO; import cn.cslg.pas.common.model.cronModel.*; import cn.cslg.pas.common.model.request.*; import cn.cslg.pas.common.utils.CacheUtils; import cn.cslg.pas.common.utils.LoginUtils; import cn.cslg.pas.common.vo.business.EventCountVO; import cn.cslg.pas.common.vo.business.EventVO; import cn.cslg.pas.domain.business.AssoEventFile; import cn.cslg.pas.domain.business.Event; import cn.cslg.pas.domain.business.SystemDict; import cn.cslg.pas.exception.UnLoginException; import cn.cslg.pas.exception.XiaoShiException; import cn.cslg.pas.factorys.businessFactory.Business; import cn.cslg.pas.factorys.reGroupFactory.QueryGroupFactory; import cn.cslg.pas.factorys.reGroupFactory.QueryGroupImp; import cn.cslg.pas.mapper.AssoProjectEventMapper; import cn.cslg.pas.mapper.EventMapper; import cn.cslg.pas.service.common.FileManagerService; import cn.cslg.pas.service.permissions.PermissionService; import cn.cslg.pas.service.query.FormatQueryService; import com.alibaba.fastjson.JSON; 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.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @Service @Slf4j public class EventService extends ServiceImpl implements Business { @Autowired private EventMapper eventMapper; @Autowired private FormatQueryService formatQueryService; @Autowired private FileManagerService fileManagerService; @Autowired private AssoEventFileService assoEventFileService; @Autowired private QueryGroupFactory queryGroupFactory; @Autowired private PermissionService permissionService; @Autowired private SystemDictService systemDictService; @Autowired private AssoProjectEventMapper assoProjectEventMapper; @Autowired private CacheUtils cacheUtils; @Autowired private LoginUtils loginUtils; public String getEvent(String sql) { // eventMapper.getEvent(sql); return ""; } @Override @Transactional(rollbackFor = Exception.class) public Object queryMessage(QueryRequest queryRequest) throws Exception { List sqls = formatQueryService.reSqls(queryRequest,"event"); //根据sql查询事件信息 List eventVOS = eventMapper.getEvent(sqls.get(0),sqls.get(1),sqls.get(2)); //查询总数 Long total = eventMapper.getEventCount(sqls.get(0)); //装载事件信息 this.loadEvent(eventVOS); Records records = new Records(); records.setCurrent(queryRequest.getCurrent()); records.setSize(queryRequest.getSize()); records.setData(eventVOS); records.setTotal(total); return records; } @Override @Transactional(rollbackFor = Exception.class) public Integer addMessage(Object object, List files) { EventDTO eventDTO = (EventDTO) object; //检查事件格式 if (eventDTO.getScenarioId() == null) { throw new XiaoShiException("参数错误"); } //获取登录人信息 PersonnelVO personnelVO = new PersonnelVO(); try { personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); } catch (Exception e) { throw new UnLoginException("未登录"); } //根据名称查询是否重复 eventDTO.setName(eventDTO.getName().trim()); String name = eventDTO.getName(); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Event::getName, name); List events = this.list(queryWrapper); if (events != null && events.size() != 0) { throw new XiaoShiException("名称重复"); } //事件入库 Event event = new Event(); BeanUtils.copyProperties(eventDTO, event); event.setCreateId(personnelVO.getId()); event.setTenantId(personnelVO.getTenantId()); event.insert(); if (files != null && files.size() != 0) { try { List guids = fileManagerService.uploadFileGetGuid(files); List assoEventFiles = new ArrayList<>(); for (String item : guids) { AssoEventFile assoEventFile = new AssoEventFile(); assoEventFile.setEventId(event.getId()); assoEventFile.setFileGuid(item); assoEventFile.setCreateId(personnelVO.getId()); assoEventFiles.add(assoEventFile); } if (assoEventFiles != null && assoEventFiles.size() != 0) { assoEventFileService.saveBatch(assoEventFiles); } } catch (Exception e) { } } return event.getId(); } @Override @Transactional(rollbackFor = Exception.class) public Object deleteMessage(List ids) throws IOException { //根据事件id删除事件和文件关联 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(AssoEventFile::getEventId, ids); List assoEventFiles = assoEventFileService.list(queryWrapper); List guids = assoEventFiles.stream().map(AssoEventFile::getFileGuid).collect(Collectors.toList()); // 根据guid删除文件 if (guids.size() != 0) { fileManagerService.deleteFileFromFMS(guids); } //删除事件和文件关联表 assoEventFiles.remove(queryWrapper); //根据事件id删除事件 this.removeBatchByIds(ids); return ids; } /** * 更新事件接口 * * @param object * @param files * @return */ @Override @Transactional(rollbackFor = Exception.class) public Object updateMessage(Object object, List files) { PersonnelVO personnelVO =cacheUtils.getLoginUser(loginUtils.getId()); UpdateEventDTO updateEventDTO = (UpdateEventDTO) object; //检查事件格式 if (updateEventDTO == null || updateEventDTO.getId() == null) { throw new XiaoShiException("参数错误"); } Event event = this.getById(updateEventDTO.getId()); //根据名称查询是否重复 updateEventDTO.setName(updateEventDTO.getName().trim()); String name = updateEventDTO.getName(); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Event::getName, name); List events = this.list(queryWrapper); if (!updateEventDTO.getName().equals(event.getName()) && events.size() != 0) { throw new XiaoShiException("名称重复"); } BeanUtils.copyProperties(updateEventDTO, event); event.updateById(); // 根据事件Id查询对应的附件Id LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(AssoEventFile::getEventId, updateEventDTO.getId()); List assoReportFiles = assoEventFileService.list(wrapper); List fileGuIds = assoReportFiles.stream().map(AssoEventFile::getFileGuid).collect(Collectors.toList()); // 获得事件更新后的附件Id List updateFilGuId = new ArrayList<>(); if (updateEventDTO.getFileGuids() != null && updateEventDTO.getFileGuids().size() != 0) { updateFilGuId = updateEventDTO.getFileGuids(); } fileGuIds.removeAll(updateFilGuId); //做差获得被删除的文件Id if (fileGuIds.size() != 0) { //根据文件Id删除报事件文件关联表记录 LambdaQueryWrapper deleteWrapper = new LambdaQueryWrapper<>(); deleteWrapper.in(AssoEventFile::getFileGuid, fileGuIds); assoEventFileService.remove(deleteWrapper); //远程删除服务器上文件 try { fileManagerService.deleteFileFromFMS(fileGuIds); } catch (Exception e) { } } //添加文件 if (files != null && files.size() != 0) { try { List guids = fileManagerService.uploadFileGetGuid(files); List assoEventFiles = new ArrayList<>(); guids.forEach(item -> { AssoEventFile assoEventFile = new AssoEventFile(); assoEventFile.setEventId(event.getId()); assoEventFile.setFileGuid(item); assoEventFile.setCreateId(personnelVO.getId()); assoEventFiles.add(assoEventFile); }); if (assoEventFiles != null && assoEventFiles.size() != 0) { assoEventFileService.saveBatch(assoEventFiles); } } catch (Exception e) { } } return event.getId(); } /** * 查询事件分组信息 * * @param groupRequest * @return * @throws Exception */ @Transactional(rollbackFor = Exception.class) public Object getGroup(GroupRequest groupRequest,String tableName) throws Exception { StringRequest stringRequest =new StringRequest(); BeanUtils.copyProperties(groupRequest,stringRequest); List sqls = formatQueryService.reSqls(stringRequest,tableName); //格式化 分组 GroupConfig groupConfig=null; if (groupRequest.getGroupBy() != null) { String json = CommonService.readJsonFile(tableName+".json"); List groupConfigs = JSON.parseArray(json, GroupConfig.class); groupConfig = groupConfigs.stream().filter(item -> groupRequest.getGroupBy().equals(item.getField())).findFirst().orElse(null); if (groupConfig == null) { throw new XiaoShiException("未找到配置"); } } //返回分组数据 QueryGroupImp queryGroupImp = queryGroupFactory.getClass(groupConfig.getGroupClass()); ReGroupDataVO reGroupDataVO = queryGroupImp.getGroup(sqls, tableName, groupConfig.getSqlField()); //装载数据 GroupVO groupVO = new GroupVO(); groupVO.setField(groupRequest.getGroupBy()); groupVO.setValues(reGroupDataVO.getValues()); Records records = new Records(); records.setCurrent(groupRequest.getCurrent()); records.setSize(groupRequest.getSize()); records.setData(groupVO); records.setTotal(reGroupDataVO.getTotal()); return records; } @Override public Object addMessage(Object object) { return null; } @Override public Object updateMessage(Object object) { return null; } /** * 装载事件返回类 * * @param eventVOs */ private void loadEvent(List eventVOs) throws IOException { List createIds = new ArrayList<>(); List clientIds = new ArrayList<>(); List ids = new ArrayList<>(); List eventCountVOS = new ArrayList<>(); //获得所有文件的guid eventVOs.forEach( item -> { if (item.getClientId() != null) { clientIds.add(item.getClientId()); } if (item.getCreateId() != null) { createIds.add(item.getCreateId()); } if (item.getId() != null) { ids.add(item.getId()); } } ); List personnels = new ArrayList<>(); List clients = new ArrayList<>(); List guids = new ArrayList<>(); List systemFiles = new ArrayList<>(); List assoEventFiles = new ArrayList<>(); if (ids.size() != 0) { //根据事件id获得事件文件关联表 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(AssoEventFile::getEventId, ids); assoEventFiles = assoEventFileService.list(queryWrapper); guids = assoEventFiles.stream().map(AssoEventFile::getFileGuid).collect(Collectors.toList()); //根据事件id分组查询事件关联专题库或报告数量 eventCountVOS = assoProjectEventMapper.getEventProjectCount(ids); } //查询创建人名称 if (createIds.size() != 0) { String res = permissionService.getPersonnelByIdsFromPCS(createIds); JSONObject jsonObject = JSONObject.parseObject(res); personnels = JSONObject.parseArray(jsonObject.getString("data"), Personnel.class); } //查询客户名称 if (clientIds.size() != 0) { String res = permissionService.getClientByIdsFromPCS(clientIds); JSONObject jsonObject = JSONObject.parseObject(res); clients = JSONObject.parseArray(jsonObject.getString("data"), Client.class); } //查询文件 if (guids.size() != 0) { String res = fileManagerService.getSystemFileFromFMS(guids); systemFiles = JSONObject.parseArray(res, SystemFile.class); } //查询关联报告或专题库 //查询应用场景 List systemDictList = systemDictService.getSystemDictListByType(Arrays.asList("ENTERPRISE_APPLICATION_SCENARIO")); //查询专题库数量 //装载信息 for (EventVO eventVO : eventVOs) { //装载人员信息 Personnel personnel = personnels.stream().filter(item -> item.getId().equals(Integer.parseInt(eventVO.getCreateId()))).findFirst().orElse(null); if (personnel != null) { eventVO.setCreateName(personnel.getPersonnelName()); } //装载客户信息 Client client = clients.stream().filter(item -> item.getId().equals(eventVO.getClientId())).findFirst().orElse(null); if (client != null) { eventVO.setClientName(client.getName()); } //装载场景 SystemDict systemDict = systemDictList.stream().filter(item -> item.getValue().equals(eventVO.getScenarioId().toString())).findFirst().orElse(null); if (systemDict != null) { eventVO.setScenarioName(systemDict.getLabel()); } //装载文件信息 List assoEventFileTemp = assoEventFiles.stream().filter(item -> item.getEventId().equals(eventVO.getId())).collect(Collectors.toList()); if (assoEventFileTemp.size() != 0) { List guidTemp = assoEventFileTemp.stream().map(AssoEventFile::getFileGuid).collect(Collectors.toList()); if (guidTemp.size() != 0) { List systemFileTemp = systemFiles.stream().filter(item -> guidTemp.contains(item.getGuid())).collect(Collectors.toList()); if (systemFileTemp.size() != 0) { eventVO.setSystemFileList(systemFileTemp); } } } //装载专题库或报告数量 if (eventCountVOS.size() != 0) { //专题库数量 EventCountVO eventCountVO1 = eventCountVOS.stream().filter(item -> item.getEventId().equals(eventVO.getId()) && item.getProjectType().equals(1)).findFirst().orElse(null); //报告数量 EventCountVO eventCountVO2 = eventCountVOS.stream().filter(item -> item.getEventId().equals(eventVO.getId()) && item.getProjectType().equals(2)).findFirst().orElse(null); //设置专题库数量 if (eventCountVO1 != null) { eventVO.setPatentProjectNum(eventCountVO1.getProjectCount()); } else { eventVO.setPatentProjectNum(0); } //设置报告数量 if (eventCountVO2 != null) { eventVO.setReportProjectNum(eventCountVO2.getProjectCount()); } else { eventVO.setReportProjectNum(0); } } else { eventVO.setReportProjectNum(0); eventVO.setPatentProjectNum(0); } } } }