package com.example.xiaoshiweixinback.service; 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.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.xiaoshiweixinback.business.common.base.Records; import com.example.xiaoshiweixinback.business.common.base.SystemFile; import com.example.xiaoshiweixinback.business.exception.BusinessException; import com.example.xiaoshiweixinback.business.utils.BatchNoUtil; import com.example.xiaoshiweixinback.business.utils.CacheUtil; import com.example.xiaoshiweixinback.business.utils.LoginUtils; import com.example.xiaoshiweixinback.business.utils.StringUtils; import com.example.xiaoshiweixinback.domain.*; import com.example.xiaoshiweixinback.entity.dto.ticket.*; import com.example.xiaoshiweixinback.entity.vo.*; import com.example.xiaoshiweixinback.entity.vo.ticket.TicketDetailVO; import com.example.xiaoshiweixinback.mapper.TicketMapper; import com.example.xiaoshiweixinback.service.common.FileManagerService; import com.example.xiaoshiweixinback.service.common.NoticeService; import lombok.RequiredArgsConstructor; import org.joda.time.DateTime; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.stream.Collectors; /** * @author admin * @description 针对表【ticket(工单)】的数据库操作Service实现 * @createDate 2024-03-29 16:27:51 */ @Service @RequiredArgsConstructor public class TicketService extends ServiceImpl { private final CacheUtil cacheUtil; private final TicketFillInService ticketFillInService; private final TicketRightsProtectionService ticketRightsProtectionService; private final TicketLitigationRespondingService ticketLitigationRespondingService; private final TicketPatentApplyService ticketPatentApplyService; private final AssoTicketFileService assoTicketFileService; private final TicketFlowService ticketFlowService; private final FileManagerService fileManagerService; private final NoticeService noticeService; /** * 新增or更新工单 * * @param ticketDTO * @return */ @Transactional(rollbackFor = Exception.class) public Integer addorUpdateTicket(TicketDTO ticketDTO) { Integer reId = null; Ticket ticket = new Ticket(); if (ticketDTO.getId() == null) { //工单基本信息入库 BeanUtils.copyProperties(ticketDTO, ticket); PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken()); ticket.setCreateId(personnelVO.getUuid()); String num = BatchNoUtil.getBatchNo(); ticket.setNum(num); ticket.setTicketProgress(1); ticket.insert(); Integer ticketId = ticket.getId(); //根据工单的不同类型 信息入库 Integer ticketType = ticketDTO.getTicketType(); switch (ticketType) { case 1: //填写工单 TicketFillInAddDTO ticketFillInAddDTO = ticketDTO.getTicketFillInAddDTO(); ticketFillInAddDTO.setTicketId(ticketId); reId = ticketFillInService.add(ticketFillInAddDTO); break; case 2: //知识产权维权工单 TicketRightsProtectionAddDTO protectionAddDTO = ticketDTO.getProtectionAddDTO(); protectionAddDTO.setTicketId(ticketId); reId = ticketRightsProtectionService.add(protectionAddDTO); break; case 3: //知识产权应诉工单 TicketLitigationRespondingAddDTO respondingAddDTO = ticketDTO.getRespondingAddDTO(); respondingAddDTO.setTicketId(ticketId); reId = ticketLitigationRespondingService.add(respondingAddDTO); break; case 4: //我要申请专利工单 TicketPatentApplyAddDTO ticketPatentApplyAddDTO = ticketDTO.getTicketPatentApplyAddDTO(); ticketPatentApplyAddDTO.setTicketId(ticketId); reId = ticketPatentApplyService.add(ticketPatentApplyAddDTO); break; default: reId = -1; break; } //处理工单的附件 List fileGuids = ticketDTO.getFileGuids(); if (fileGuids != null && !fileGuids.isEmpty()) { List assoTicketFiles = new ArrayList<>(); for (String fileGuid : fileGuids) { AssoTicketFile assoTicketFile = new AssoTicketFile(); assoTicketFile.setTicketId(ticketId); assoTicketFile.setFileGuid(fileGuid); assoTicketFiles.add(assoTicketFile); } if (!assoTicketFiles.isEmpty()) { assoTicketFileService.saveBatch(assoTicketFiles); } } //添加工单流程记录 ticketFlowService.addTicketFlow(ticket, "创建工单", false, null, 1); //通知管理员 noticeService.noticeAdminToHandle(ticket.getNum()); } else { //TODO 未装载 ticket = this.getById(ticket.getId()); ticket.updateById(); } return reId; } @Transactional(rollbackFor = Exception.class) public Records queryTickets(TicketQueryDTO ticketQueryDTO) { String num = ticketQueryDTO.getNum(); Integer ticketType = ticketQueryDTO.getTicketType(); Integer process = ticketQueryDTO.getProcess(); Integer pageNum = ticketQueryDTO.getCurrent(); Integer pageSize = ticketQueryDTO.getSize(); String contactPerson = ticketQueryDTO.getContactPerson(); Date createTime = ticketQueryDTO.getCreateTime(); Page page = new Page<>(pageNum, pageSize); QueryWrapper queryWrapper = new QueryWrapper<>(); if (ticketType != null) { queryWrapper.lambda().eq(Ticket::getTicketType, ticketType); } if (num != null) { queryWrapper.lambda().like(Ticket::getNum, num); } if (process != null) { queryWrapper.lambda().eq(Ticket::getTicketProgress, process); } if (contactPerson != null) { queryWrapper.lambda().like(Ticket::getContactPerson, contactPerson); } PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken()); Integer personType = personnelVO.getPersonType(); if (personType != null && personType.equals(1)) { queryWrapper.lambda().eq(Ticket::getCreateId, personnelVO.getUuid()); } if (createTime != null) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String date = format.format(createTime); queryWrapper.eq("DATE_FORMAT(create_time,'%Y-%m-%d')", date); } queryWrapper.orderBy(true, false, "create_time"); IPage ticketPage = this.page(page, queryWrapper); List tickets = ticketPage.getRecords(); List ticketVOS = this.loadTicketVO(tickets); Records records = new Records(); records.setData(ticketVOS); records.setCurrent((long) pageNum); records.setSize((long) pageSize); records.setTotal(ticketPage.getTotal()); return records; } public List loadTicketVO(List tickets) { List ticketVOS = new ArrayList<>(); if (!tickets.isEmpty()) { List createIds = new ArrayList<>(); tickets.forEach(item -> { if (item.getCreateId() != null) { createIds.add(item.getCreateId()); } }); for (Ticket ticket : tickets) { TicketVO ticketVO = new TicketVO(); BeanUtils.copyProperties(ticket, ticketVO); Integer ticketId = ticket.getId(); //附图 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(AssoTicketFile::getTicketId, ticketId) .eq(AssoTicketFile::getType,0); List assoTicketFiles = assoTicketFileService.list(queryWrapper); if (assoTicketFiles != null && !assoTicketFiles.isEmpty()) { List fileGuids = assoTicketFiles.stream().map(AssoTicketFile::getFileGuid).collect(Collectors.toList()); List systemFiles = fileManagerService.getSystemFileByGuids(fileGuids); ticketVO.setSystemFiles(systemFiles); } Integer ticketType = ticket.getTicketType(); switch (ticketType) { case 1: TicketFillInVO ticketFillInVO = new TicketFillInVO(); LambdaQueryWrapper fillInWrapper = new LambdaQueryWrapper<>(); fillInWrapper.eq(TicketFillIn::getTicketId, ticketId); TicketFillIn ticketFillIn = ticketFillInService.getOne(fillInWrapper, false); if (ticketFillIn != null) { BeanUtils.copyProperties(ticketFillIn, ticketFillInVO); String patentNoArray = ticketFillIn.getPatentNo(); if (patentNoArray != null && StringUtils.isNotEmpty(patentNoArray)) { String[] stringArray = patentNoArray.split(","); List patentNos = new ArrayList<>(Arrays.asList(stringArray)); ticketFillInVO.setPatentNos(patentNos); } } ticketVO.setTicketFillInVO(ticketFillInVO); break; case 2: TicketRightsProtectionVO rightsProtectionVO = new TicketRightsProtectionVO(); LambdaQueryWrapper rightsProtectionWrapper = new LambdaQueryWrapper<>(); rightsProtectionWrapper.eq(TicketRightsProtection::getTicketId, ticketId); TicketRightsProtection ticketRightsProtection = ticketRightsProtectionService.getOne(rightsProtectionWrapper, false); if (ticketRightsProtection != null) { String rightProof = ticketRightsProtection.getRightProof(); if(rightProof!=null){ String[] stringArray = rightProof.split(","); List proofGuids = new ArrayList<>(Arrays.asList(stringArray)); List systemFiles = new ArrayList<>(); if (proofGuids.size() != 0) { try { String res = fileManagerService.getSystemFileFromFMS(proofGuids); systemFiles = JSONObject.parseArray(res, SystemFile.class); if (systemFiles == null) { systemFiles = new ArrayList<>(); } } catch (Exception e) { } rightsProtectionVO.setProofGuids(proofGuids); rightsProtectionVO.setProofFileList(systemFiles); } } BeanUtils.copyProperties(ticketRightsProtection, rightsProtectionVO); } ticketVO.setTicketRightsProtectionVO(rightsProtectionVO); break; case 3: TicketLitigationRespondingVO litigationRespondingVO = new TicketLitigationRespondingVO(); LambdaQueryWrapper respondingWrapper = new LambdaQueryWrapper<>(); respondingWrapper.eq(TicketLitigationResponding::getTicketId, ticketId); TicketLitigationResponding ticketLitigationResponding = ticketLitigationRespondingService.getOne(respondingWrapper, false); if (ticketLitigationResponding != null) { BeanUtils.copyProperties(ticketLitigationResponding, litigationRespondingVO); } ticketVO.setTicketLitigationRespondingVO(litigationRespondingVO); break; case 4: TicketPatentApplyVO patentApplyVO = new TicketPatentApplyVO(); LambdaQueryWrapper patentApplyWrapper = new LambdaQueryWrapper<>(); patentApplyWrapper.eq(TicketPatentApply::getTicketId, ticketId); TicketPatentApply ticketPatentApply = ticketPatentApplyService.getOne(patentApplyWrapper, false); if (ticketPatentApply != null) { BeanUtils.copyProperties(ticketPatentApply, patentApplyVO); } ticketVO.setTicketPatentApplyVO(patentApplyVO); break; default: break; } ticketVOS.add(ticketVO); } } return ticketVOS; } /** * 更新工单状态 * * @param ticketProcessUpDTO * @return */ @Transactional(rollbackFor = Exception.class) public Integer updateProcess(TicketProcessUpDTO ticketProcessUpDTO) { Integer id = ticketProcessUpDTO.getId(); Integer process = ticketProcessUpDTO.getProcess(); Ticket ticket = this.getById(id); Boolean flag = this.ifHavePermission(ticket.getCreateId()); if (!flag) { return 0; } ticket.setTicketProgress(process); ticket.updateById(); if (process.equals(4)) { ticketFlowService.addTicketFlow(ticket, "工单已确认", false, null, 4,ticket.getCreateId()); } if(process.equals(5)) { ticketFlowService.addTicketFlow(ticket, "工单已取消", false, null, 5,ticket.getCreateId()); } return ticket.getId(); } /** * 更新工单状态 * * @return */ @Transactional(rollbackFor = Exception.class) public Integer updateProcess(String uuid, Integer process) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Ticket::getNum, uuid); Ticket ticket = this.getOne(queryWrapper, false); ticket.setTicketProgress(process); ticket.updateById(); ticketFlowService.addTicketFlow(ticket, "工单支付成功", false, null, 3,ticket.getCreateId()); return ticket.getId(); } /** * 更新价格 * * @param ticketPriceUpDTO * @return */ @Transactional(rollbackFor = Exception.class) public Integer updatePrice(TicketPriceUpDTO ticketPriceUpDTO) { Integer id = ticketPriceUpDTO.getId(); Double price = ticketPriceUpDTO.getPrice(); Ticket ticket = this.getById(id); Boolean flag = this.ifHavePermission(null); if (!flag) { return 0; } ticket.setPrice(price); ticket.updateById(); return ticket.getId(); } private Boolean ifHavePermission(String id) { PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken()); personnelVO.getPersonType(); if (personnelVO.getPersonType().equals(0) || (id != null && personnelVO.getUuid().equals(id))) { return true; } return false; } public Ticket getTicketByUUid(String uuid) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Ticket::getNum, uuid); Ticket ticket = this.getOne(queryWrapper); return ticket; } @Transactional(rollbackFor = Exception.class) public Integer acceptTicket(AcceptTicketDTO acceptTicketDTO) { Integer id = acceptTicketDTO.getId(); Double price = acceptTicketDTO.getPrice(); //根据id查询工单 Ticket ticket = this.getById(id); if (ticket == null) { throw new BusinessException("607", "未查询到工单"); } if (ticket.getTicketProgress() > 2) { throw new BusinessException("607", "此工单不可进行操作"); } ticket.setPrice(price); if (ticket.getTicketProgress() == 2) { ticketFlowService.addTicketFlow(ticket, "修改工单价格为" + price, false, null, 2); } else { ticket.setTicketProgress(2); ticketFlowService.addTicketFlow(ticket, "受理工单并且修改价格为" + price, false, null, 2); } ticket.updateById(); return ticket.getId(); } /** * 上传工单结果 * * @param ticketUploadResultDTO * @return */ @Transactional(rollbackFor = Exception.class) public Integer uploadTicketResult(TicketUploadResultDTO ticketUploadResultDTO) { List fileGuids = new ArrayList<>(); fileGuids = ticketUploadResultDTO.getFileGuids(); String description = ticketUploadResultDTO.getDescription(); Integer id = ticketUploadResultDTO.getId(); //根据id查询工单 Ticket ticket = this.getById(id); if (ticket == null) { throw new BusinessException("607", "未查询到工单"); } if (ticket.getTicketProgress() > 3) { throw new BusinessException("607", "此工单不可进行操作"); } //添加工单流程 TicketFlow ticketFlow = ticketFlowService.addTicketFlow(ticket, "添加工单结果", true, description, 6); //添加结果 assoTicketFileService.addTicketFile(fileGuids, 1, ticket, ticketFlow.getId()); ticket.setTicketProgress(6); ticket.updateById(); return ticket.getId(); } public TicketDetailVO getTicketDetail(Integer ticketId) { TicketDetailVO ticketDetailVO = new TicketDetailVO(); //根据id查询 Ticket ticket = this.getById(ticketId); if (ticket == null) { return ticketDetailVO; } ticketDetailVO = this.loadTicketDetail(ticket); return ticketDetailVO; } public TicketDetailVO loadTicketDetail(Ticket ticket) { TicketDetailVO ticketDetailVO = new TicketDetailVO(); BeanUtils.copyProperties(ticket, ticketDetailVO); Integer ticketId = ticket.getId(); //附图 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(AssoTicketFile::getTicketId, ticketId) .eq(AssoTicketFile::getType, 0); List assoTicketFiles = assoTicketFileService.list(queryWrapper); if (assoTicketFiles != null && !assoTicketFiles.isEmpty()) { List fileGuids = assoTicketFiles.stream().map(AssoTicketFile::getFileGuid).collect(Collectors.toList()); List systemFiles = fileManagerService.getSystemFileByGuids(fileGuids); ticketDetailVO.setSystemFiles(systemFiles); } Integer ticketType = ticket.getTicketType(); switch (ticketType) { case 1: TicketFillInVO ticketFillInVO = ticketFillInService.getVOByTicId(ticketId); ticketDetailVO.setTicketFillInVO(ticketFillInVO); break; case 2: TicketRightsProtectionVO rightsProtectionVO = ticketRightsProtectionService.getVOByTicId(ticketId); ticketDetailVO.setTicketRightsProtectionVO(rightsProtectionVO); break; case 3: TicketLitigationRespondingVO litigationRespondingVO = ticketLitigationRespondingService.getVOByTicId(ticketId); ticketDetailVO.setTicketLitigationRespondingVO(litigationRespondingVO); break; case 4: TicketPatentApplyVO patentApplyVO = ticketPatentApplyService.getVOByTicId(ticketId); ticketDetailVO.setTicketPatentApplyVO(patentApplyVO); break; default: break; } try { TicketFlowVO ticketFlowVO = ticketFlowService.getTicketResult(ticketId); ticketDetailVO.setTicketResult(ticketFlowVO); } catch (Exception e) { } return ticketDetailVO; } }