package cn.cslg.pas.service.dify; import cn.cslg.pas.common.model.cronModel.SystemFile; import cn.cslg.pas.common.model.dify.confessionSession.*; import cn.cslg.pas.common.utils.CacheUtils; import cn.cslg.pas.common.utils.FormatUtil; import cn.cslg.pas.common.utils.LoginUtils; import cn.cslg.pas.domain.dify.AssoConfessionSessionFile; import cn.cslg.pas.exception.ExceptionEnum; import cn.cslg.pas.exception.XiaoShiException; import cn.cslg.pas.mapper.dify.AssoConfessionSessionFileMapper; import cn.cslg.pas.service.common.FileManagerService; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import cn.cslg.pas.domain.dify.ConfessionSession; import cn.cslg.pas.mapper.dify.ConfessionSessionMapper; import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.stream.Collectors; /** * @author admin * @description 针对表【confession_session(交底书会话记录)】的数据库操作Service实现 * @createDate 2025-04-16 15:48:57 */ @Service @RequiredArgsConstructor public class ConfessionSessionService extends ServiceImpl { private final CacheUtils cacheUtils; private final LoginUtils loginUtils; private final FileManagerService fileManagerService; private final AssoConfessionSessionFileMapper assoConfessionSessionFileMapper; public Integer addConfessionSession(AddConfessionSessionDTO addConfessionSessionDTO) { String guid = addConfessionSessionDTO.getFileGuid(); String name = ""; try { if (guid != null && !guid.trim().equals("")) { String res = fileManagerService.getSystemFileFromFMS(Arrays.asList(guid)); List systemFiles = JSONObject.parseArray(res, SystemFile.class); if (systemFiles != null && !systemFiles.isEmpty()) { SystemFile systemFile = systemFiles.get(0); name = systemFile.getOriginalName(); } } } catch (Exception e) { } if (name != null && !name.isEmpty()) { name = name.split("\\.")[0]; } ConfessionSession confessionSession = new ConfessionSession(); confessionSession.setGuid(guid); confessionSession.setConversationName(name); String id = loginUtils.getId().toString(); confessionSession.setCreateId(id); confessionSession.setType(addConfessionSessionDTO.getType()); confessionSession.insert(); return confessionSession.getId(); } public Integer updateConfessionSession(UpdateConfessionSessionDTO updateConfessionSessionDTO) { Integer id = updateConfessionSessionDTO.getConfessionSessionId(); ConfessionSession confessionSession = this.getById(id); if (confessionSession == null) { throw new XiaoShiException(ExceptionEnum.BUSINESS_CHECK, "未查询到交底书会话记录"); } BeanUtils.copyProperties(updateConfessionSessionDTO, confessionSession, FormatUtil.getNullPropertyNames(updateConfessionSessionDTO)); confessionSession.updateById(); return confessionSession.getId(); } public List queryConfessionSession(QueryConfessionSessionDTO queryConfessionSessionDTO) { Integer userId = loginUtils.getId(); Integer confessionSessionId = queryConfessionSessionDTO.getConfessionSessionId(); Integer type = queryConfessionSessionDTO.getType(); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(ConfessionSession::getCreateId, userId) .orderByDesc(ConfessionSession::getCreateTime); if (confessionSessionId != null) { queryWrapper.eq(ConfessionSession::getId, confessionSessionId); } if (type != null) { queryWrapper.eq(ConfessionSession::getType, type); } List confessionSessions = this.list(queryWrapper); List queryConfessionSessionVOS = new ArrayList<>(); if (confessionSessions == null || confessionSessions.size() == 0) { return queryConfessionSessionVOS; } List fileGuids = confessionSessions.stream().filter(item -> item.getGuid() != null).map(ConfessionSession::getGuid).collect(Collectors.toList()); List systemFiles = new ArrayList<>(); try { if (fileGuids != null && fileGuids.size() > 0) { String res = fileManagerService.getSystemFileFromFMS(fileGuids); systemFiles = JSONObject.parseArray(res, SystemFile.class); } } catch (Exception e) { e.printStackTrace(); } List confessionSessionIds = confessionSessions.stream().map(ConfessionSession::getId).collect(Collectors.toList()); List sessionFileDTOS = this.queryAssoConfessionSessionFileDTOS(confessionSessionIds); for (ConfessionSession confessionSession : confessionSessions) { QueryConfessionSessionVO queryConfessionSessionVO = new QueryConfessionSessionVO(); BeanUtils.copyProperties(confessionSession, queryConfessionSessionVO); String guid = confessionSession.getGuid(); if (systemFiles != null) { SystemFile systemFile = systemFiles.stream().filter(item -> item.getGuid().equals(guid)).findFirst().orElse(null); queryConfessionSessionVO.setSystemFile(systemFile); } if (!CollectionUtils.isEmpty(sessionFileDTOS)) { QueryAssoConfessionSessionFileDTO fileDTO = sessionFileDTOS.stream().filter(i -> i.getConfessionSessionId().equals(confessionSession.getId())).findFirst().orElse(null); if (fileDTO != null) { queryConfessionSessionVO.setAssoSystemFile(fileDTO.getSystemFile()); } } queryConfessionSessionVOS.add(queryConfessionSessionVO); } return queryConfessionSessionVOS; } public List queryAssoConfessionSessionFileDTOS(List list) { //装载会话关联文件信息 List assoConfessionSessionFileDTOS = new ArrayList<>(); if (!CollectionUtils.isEmpty(list)) { List assoConfessionSessionFiles = assoConfessionSessionFileMapper.selectList(new LambdaQueryWrapper() .in(AssoConfessionSessionFile::getConfessionSessionId, list)); if (!CollectionUtils.isEmpty(assoConfessionSessionFiles)) { List fileGuids = assoConfessionSessionFiles.stream().map(AssoConfessionSessionFile::getGuid) .filter(StringUtils::isNotEmpty).collect(Collectors.toList()); List systemFiles = new ArrayList<>(); try { if (!CollectionUtils.isEmpty(fileGuids)) { String res = fileManagerService.getSystemFileFromFMS(fileGuids); systemFiles = JSONObject.parseArray(res, SystemFile.class); } } catch (Exception e) { e.printStackTrace(); } for (AssoConfessionSessionFile sessionFile : assoConfessionSessionFiles) { QueryAssoConfessionSessionFileDTO fileDTO = new QueryAssoConfessionSessionFileDTO(); BeanUtils.copyProperties(sessionFile, fileDTO); fileDTO.setAssoConfessionSessionFileId(sessionFile.getId()); if (!CollectionUtils.isEmpty(systemFiles)) { SystemFile systemFile = systemFiles.stream().filter(item -> item.getGuid().equals(sessionFile.getGuid())).findFirst().orElse(null); fileDTO.setSystemFile(systemFile); } assoConfessionSessionFileDTOS.add(fileDTO); } } } return assoConfessionSessionFileDTOS; } public List deleteConfessionSessions(DeleteConfessionSessionDTO deleteConfessionSessionDTO) { List ids = deleteConfessionSessionDTO.getConfessionSessionIds(); this.removeBatchByIds(ids); return ids; } public Integer addSession(AddConfessionSessionDTO addConfessionSessionDTO) { ConfessionSession confessionSession = new ConfessionSession(); confessionSession.setContent(addConfessionSessionDTO.getContent()); confessionSession.setConversationId(addConfessionSessionDTO.getConversationId()); confessionSession.setConversationName(addConfessionSessionDTO.getConversationName()); String createId = loginUtils.getId().toString(); confessionSession.setCreateId(createId); confessionSession.setType(addConfessionSessionDTO.getType()); confessionSession.insert(); return confessionSession.getId(); } public Integer addConfessionSessionFile(AddConfessionSessionFileDTO addConfessionSessionFileDTO) { AssoConfessionSessionFile assoConfessionSessionFile = new AssoConfessionSessionFile(); assoConfessionSessionFile.setConfessionSessionId(addConfessionSessionFileDTO.getConfessionSessionId()); assoConfessionSessionFile.setGuid(addConfessionSessionFileDTO.getGuid()); assoConfessionSessionFile.insert(); return assoConfessionSessionFile.getId(); } public ConfessionSession addSessionReDO(AddConfessionSessionDTO addConfessionSessionDTO) { ConfessionSession confessionSession = new ConfessionSession(); confessionSession.setContent(addConfessionSessionDTO.getContent()); confessionSession.setConversationId(addConfessionSessionDTO.getConversationId()); confessionSession.setConversationName(addConfessionSessionDTO.getConversationName()); String createId = loginUtils.getId().toString(); confessionSession.setCreateId(createId); confessionSession.setType(addConfessionSessionDTO.getType()); confessionSession.setCreateTime(new Date()); confessionSession.insert(); return confessionSession; } }