123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- package cn.cslg.pas.service.business;
- import cn.cslg.pas.common.dto.business.EventDTO;
- import cn.cslg.pas.common.dto.business.ProjectEventDTO;
- 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.AssoProjectEvent;
- 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<EventMapper, Event> 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;
- @Override
- @Transactional(rollbackFor = Exception.class)
- public Object queryMessage(QueryRequest queryRequest) throws Exception {
- List<String> sqls = formatQueryService.reSqls(queryRequest, "event");
- sqls = this.loadSearchSql(sqls);
- //根据sql查询事件信息
- List<EventVO> 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<MultipartFile> files) {
- //获取登录人信息
- PersonnelVO personnelVO = new PersonnelVO();
- personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
- this.checkParameter(object);
- EventDTO eventDTO = (EventDTO) object;
- //根据名称查询是否重复
- eventDTO.setName(eventDTO.getName().trim());
- //事件入库
- 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<String> guids = fileManagerService.uploadFileGetGuid(files);
- List<AssoEventFile> 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<Integer> ids) throws IOException {
- //根据事件id删除事件和文件关联
- LambdaQueryWrapper<AssoEventFile> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.in(AssoEventFile::getEventId, ids);
- List<AssoEventFile> assoEventFiles = assoEventFileService.list(queryWrapper);
- List<String> 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<MultipartFile> files) {
- PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
- UpdateEventDTO updateEventDTO = (UpdateEventDTO) object;
- //检查事件格式
- if (updateEventDTO == null || updateEventDTO.getId() == null) {
- throw new XiaoShiException("参数错误");
- }
- this.checkParameter(object);
- Event event = this.getById(updateEventDTO.getId());
- //根据名称查询是否重复
- updateEventDTO.setName(updateEventDTO.getName().trim());
- BeanUtils.copyProperties(updateEventDTO, event);
- event.updateById();
- // 根据事件Id查询对应的附件Id
- LambdaQueryWrapper<AssoEventFile> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(AssoEventFile::getEventId, updateEventDTO.getId());
- List<AssoEventFile> assoReportFiles = assoEventFileService.list(wrapper);
- List<String> fileGuIds = assoReportFiles.stream().map(AssoEventFile::getFileGuid).collect(Collectors.toList());
- // 获得事件更新后的附件Id
- List<String> updateFilGuId = new ArrayList<>();
- if (updateEventDTO.getFileGuids() != null && updateEventDTO.getFileGuids().size() != 0) {
- updateFilGuId = updateEventDTO.getFileGuids();
- }
- fileGuIds.removeAll(updateFilGuId);
- //做差获得被删除的文件Id
- if (fileGuIds.size() != 0) {
- //根据文件Id删除报事件文件关联表记录
- LambdaQueryWrapper<AssoEventFile> 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<String> guids = fileManagerService.uploadFileGetGuid(files);
- List<AssoEventFile> 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<String> sqls = formatQueryService.reSqls(stringRequest, tableName);
- sqls = this.loadSearchSql(sqls);
- //格式化 分组
- GroupConfig groupConfig = null;
- if (groupRequest.getGroupBy() != null) {
- String json = CommonService.readJsonFile(tableName + ".json");
- List<GroupConfig> 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());
- String countFiled = "id";
- ReGroupDataVO reGroupDataVO = queryGroupImp.getGroup(sqls, tableName, groupConfig.getSqlField(), countFiled);
- //装载数据
- 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<EventVO> eventVOs) throws IOException {
- List<String> createIds = new ArrayList<>();
- List<Integer> clientIds = new ArrayList<>();
- List<Integer> ids = new ArrayList<>();
- List<EventCountVO> 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<Personnel> personnels = new ArrayList<>();
- List<Client> clients = new ArrayList<>();
- List<String> guids = new ArrayList<>();
- List<SystemFile> systemFiles = new ArrayList<>();
- List<AssoEventFile> assoEventFiles = new ArrayList<>();
- if (ids.size() != 0) {
- //根据事件id获得事件文件关联表
- LambdaQueryWrapper<AssoEventFile> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.in(AssoEventFile::getEventId, ids);
- assoEventFiles = assoEventFileService.list(queryWrapper);
- guids = assoEventFiles.stream().map(AssoEventFile::getFileGuid).collect(Collectors.toList());
- String rootSql = this.loadProjectRootSql();
- // String rootSql =null;
- //根据事件id分组查询事件关联专题库或报告数量
- eventCountVOS = assoProjectEventMapper.getEventProjectCount(ids, rootSql);
- }
- //查询创建人名称
- 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);
- if (res != null && !res.trim().equals("")) {
- systemFiles = JSONObject.parseArray(res, SystemFile.class);
- }
- }
- //查询关联报告或专题库
- //查询应用场景
- List<SystemDict> systemDictList = systemDictService.getSystemDictListByType(Arrays.asList("ENTERPRISE_APPLICATION_SCENARIO"));
- //查询专题库数量
- //装载信息
- for (EventVO eventVO : eventVOs) {
- //装载人员信息
- Personnel personnel = personnels.stream().filter(item -> item.getId().equals(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.setScenarioNames(Arrays.asList(systemDict.getLabel()));
- eventVO.setScenarioIds(Arrays.asList(eventVO.getScenarioId()));
- }
- //装载文件信息
- List<AssoEventFile> assoEventFileTemp = assoEventFiles.stream().filter(item -> item.getEventId().equals(eventVO.getId())).collect(Collectors.toList());
- if (assoEventFileTemp.size() != 0) {
- List<String> guidTemp = assoEventFileTemp.stream().map(AssoEventFile::getFileGuid).collect(Collectors.toList());
- if (guidTemp.size() != 0) {
- List<SystemFile> 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(0)).findFirst().orElse(null);
- //报告数量
- EventCountVO eventCountVO2 = eventCountVOS.stream().filter(item ->
- item.getEventId().equals(eventVO.getId()) && item.getProjectType().equals(1)).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);
- }
- }
- }
- public List<Integer> getEventIdByName(String value, Boolean ifEqual) {
- LambdaQueryWrapper<Event> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.select(Event::getId);
- if (ifEqual != null && ifEqual.equals(true)) {
- queryWrapper.like(Event::getName, value);
- } else {
- queryWrapper.eq(Event::getName, value);
- }
- java.util.function.Function<Object, Integer> f = (o -> Integer.parseInt(o.toString()));
- List<Integer> ids = this.listObjs(queryWrapper, f);
- return ids;
- }
- //装载查询语句
- private List<String> loadSearchSql(List<String> sqls) {
- PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
- Integer tenant_id = personnelVO.getTenantId();
- Integer roleType = personnelVO.getRoleType();
- String rootSql = "";
- if (roleType == null || roleType.equals(2) || roleType.equals(0)) {
- rootSql = "(tenant_id =" + tenant_id + ")";
- }
- if (sqls.get(0) != null && !sqls.get(0).equals("") && !rootSql.equals("")) {
- sqls.set(0, rootSql + " and" + "(" + sqls.get(0) + ")");
- } else if ((sqls.get(0) == null || sqls.get(0).equals("")) && !rootSql.equals("")) {
- sqls.set(0, rootSql);
- }
- return sqls;
- }
- /**
- * 参数检查
- *
- * @param object
- */
- public void checkParameter(Object object) {
- PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
- String name = null;
- Integer id = null;
- Integer scenarioId = null;
- if (object instanceof EventDTO) {
- EventDTO eventDTO = (EventDTO) object;
- eventDTO.getScenarioId();
- name = eventDTO.getName();
- scenarioId = eventDTO.getScenarioId();
- } else if (object instanceof UpdateEventDTO) {
- UpdateEventDTO eventDTO = (UpdateEventDTO) object;
- eventDTO.getScenarioId();
- name = eventDTO.getName();
- scenarioId = eventDTO.getScenarioId();
- id = eventDTO.getId();
- }
- //检验名称是否为空
- if (name == null || name.trim().equals("")) {
- throw new XiaoShiException("请输入名称");
- }
- //检验是否传入应用场景
- if (scenarioId == null) {
- throw new XiaoShiException("请选择应用场景");
- }
- //检验名称是否重复
- LambdaQueryWrapper<Event> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.eq(Event::getName, name)
- .eq(Event::getTenantId, personnelVO.getTenantId());
- if (id != null) {
- queryWrapper.ne(Event::getId, id);
- }
- Event event = this.getOne(queryWrapper, false);
- if (event != null) {
- throw new XiaoShiException("名称重复");
- }
- }
- private String loadProjectRootSql() {
- PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
- String id = personnelVO.getId();
- Integer tenantId = personnelVO.getTenantId();
- Integer roleType = personnelVO.getRoleType();
- String rootSql = null;
- if (roleType == null || roleType.equals(0)) {
- rootSql = "(p.create_id =" + id + " or p.head_id=" + id + " or p.id in (select project_id from asso_project_person where person_id =" + id + ")) ";
- } else if (roleType.equals(2)) {
- rootSql = "(p.tenant_id=" + tenantId + " or p.id in (select project_id from asso_project_person where person_id =" + id + ")) ";
- }
- return rootSql;
- }
- }
|