package com.om.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollectionUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.om.entity.dto.*; import com.om.entity.po.*; import com.om.entity.vo.*; import com.om.exception.BadReqException; import com.om.exception.BizException; import com.om.mapper.RepairMapper; import com.om.service.*; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.om.utils.*; import org.apache.commons.lang3.StringUtils; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.multipart.support.StandardServletMultipartResolver; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; /** *

* 服务实现类 *

* * @author bmmx * @since 2024-02-18 */ @Service public class RepairServiceImpl extends ServiceImpl implements IRepairService { @Resource private IFaultCodeService faultCodeService; @Resource private IEcuInfoService ecuInfoService; @Resource private IElectronicControlService electronicControlService; @Resource RepairMapper repairMapper; @Resource IUserService userService; @Resource private HuaweiObsUtil huaweiObsUtil; @Resource private HttpServletRequest request; @Resource private ITextPdfUtil pdfUtil; @Resource private HtmlGenerator htmlGenerator; @Override @Transactional public Result AppCreateReport(APPReportCreateDTO dto) { Integer userId = UserContext.getUserId(); User user = userService.getById(userId); // 判断数据、 if (BeanUtil.isEmpty(dto)) { throw new BadReqException("数据为空"); } Repair repair = BeanUtil.copyProperties(dto, Repair.class); List electronicControls = dto.getElectronicControls(); List eleIds = new ArrayList<>(); List electronicControlList = new ArrayList<>(); for (AppElectronicDTO electronicControlDTO : electronicControls) { ElectronicControl electronicControl = BeanUtil.copyProperties(electronicControlDTO, ElectronicControl.class); electronicControlList.add(electronicControl); List ecuInfors = electronicControlDTO.getEcuInfors(); List faultCodes = electronicControlDTO.getFaultCodes(); List ecuInfos = BeanUtil.copyToList(ecuInfors, EcuInfo.class); List faultCodes1 = BeanUtil.copyToList(faultCodes, FaultCode.class); for (EcuInfo ecuInfo : ecuInfos) { ecuInfo.setElectronicControlId(electronicControl.getId()); } for (FaultCode faultCode : faultCodes1) { faultCode.setElectronicControlId(electronicControl.getId()); } eleIds.add(electronicControl.getId()); ecuInfoService.saveBatch(ecuInfos); faultCodeService.saveBatch(faultCodes1); } StringBuffer sb = new StringBuffer(); for (int i = 0; i < eleIds.size(); i++) { sb.append(eleIds); if (i != eleIds.size() - 1) { sb.append(","); } } repair.setElectronicControlIds(sb.toString()); electronicControlService.saveBatch(electronicControlList); repair.setUsername(user.getUsername()); this.save(repair); return Result.ok(); } @Override public Result AppGetPageList(APPRepairQueryPageDTO dto) { Integer userId = UserContext.getUserId(); User user = userService.getById(userId); if (BeanUtil.isEmpty(user)) { throw new BizException("用户不存在"); } //获取数据 Integer pageIndex = dto.getPageIndex(); Integer pageSize = dto.getPageSize(); //分页查询 Page page = this.lambdaQuery() .orderByDesc(Repair::getCreateTime) .eq(Repair::getUsername, user.getUsername()) .page(new Page<>(pageIndex, pageSize)); if (BeanUtil.isEmpty(page)) { throw new BadReqException("请求失败"); } AppRepairQueryPageVO vo = new AppRepairQueryPageVO(); vo.setSize(pageSize); vo.setCurrent(pageIndex); vo.setPages((int) page.getPages()); vo.setTotal((int) page.getTotal()); List records = page.getRecords(); if (CollectionUtil.isEmpty(records)) { records = Collections.emptyList(); } List list = new ArrayList<>(); for (Repair record : records) { DiagnosticReportVO diagnosticReportVO = new DiagnosticReportVO(); diagnosticReportVO.setCreationTime(LocalDateTime.now()); diagnosticReportVO.setDiagnosticReportId(record.getId()); diagnosticReportVO.setDiagnosticReportName(record.getMainTitle()); diagnosticReportVO.setFileName(record.getFileName()); String electronicControlIds = record.getElectronicControlIds(); List eleIds = Arrays.stream(electronicControlIds.split(",")) .map(Integer::parseInt) .collect(Collectors.toList()); for (Integer eleId : eleIds) { List faultCodeList = faultCodeService.lambdaQuery() .eq(FaultCode::getElectronicControlId, eleId) .list(); diagnosticReportVO.setFaultCodesNumber(String.valueOf(faultCodeList.size())); } list.add(diagnosticReportVO); } vo.setRecords(list); return Result.ok(vo); } @Override @Transactional public Result create(RepairDTO repairDTO) { Repair repair = new Repair(); BeanUtil.copyProperties(repairDTO,repair); return Result.ok(); } @Override @Transactional public Result delete(Integer id) { Repair repair = this.getById(id); if (repair == null) { throw new BadReqException("删除错误"); } String electronicControlIds = repair.getElectronicControlIds(); List eleIds = Arrays.stream(electronicControlIds.split(",")) .map(Integer::parseInt) .collect(Collectors.toList()); for (Integer eleId : eleIds) { List faultIds = faultCodeService.lambdaQuery() .eq(FaultCode::getElectronicControlId, eleId) .list().stream().map(c -> c.getId()).collect(Collectors.toList()); List ecuIds = ecuInfoService.lambdaQuery() .eq(EcuInfo::getElectronicControlId, eleId) .list().stream().map(c -> c.getId()).collect(Collectors.toList()); faultCodeService.removeByIds(faultIds); ecuInfoService.removeByIds(ecuIds); } electronicControlService.removeByIds(eleIds); this.removeById(id); return Result.ok(); } @Override public Result getByIds(Integer id) { Repair repair = this.getById(id); if (repair == null) { throw new BadReqException("查询错误"); } String electronicControlIds = repair.getElectronicControlIds(); RepairVO vo = new RepairVO(); List eleIds = Arrays.stream(electronicControlIds.split(",")) .map(Integer::parseInt) .collect(Collectors.toList()); BeanUtil.copyProperties(repair, vo); ReportTitleDto reportTitleDto = new ReportTitleDto(); BeanUtil.copyProperties(repair,reportTitleDto); List electronicControlsList = new ArrayList<>(); int eleCount = 0; int faultCount = 0; for (Integer eleId : eleIds) { ElectronicControls electronicControls = new ElectronicControls(); ElectronicControl electronicControl = electronicControlService.getById(eleId); BeanUtil.copyProperties(electronicControl,reportTitleDto); electronicControls.setElectronicControlName(electronicControl.getElectronicControlName()); List list = ecuInfoService.lambdaQuery() .eq(EcuInfo::getElectronicControlId, eleId) .list(); electronicControls.setEcuInfo(list); List faultCodeList = faultCodeService.lambdaQuery() .eq(FaultCode::getElectronicControlId, eleId) .list(); electronicControls.setFaultCode(faultCodeList); electronicControlsList.add(electronicControls); eleCount++; faultCount += faultCodeList.size(); } vo.setElectronicControls(electronicControlsList); vo.setElectronicControlCount(eleCount); vo.setFaultCodeCount(faultCount); vo.setReportTitleDto(reportTitleDto); return Result.ok(vo); } @Override public Result getPageList(RepairQueryPageDTO dto) { //从dto中获取数据 Integer pageIndex = dto.getPageIndex(); Integer pageSize = dto.getPageSize(); LocalDateTime beginTime = dto.getBeginTime(); LocalDateTime endTime = dto.getEndTime(); String searchNum = dto.getSearchNum(); // String searchVIN = dto.getSearchVIN(); String searchDistributor = dto.getSearchDistributor(); //分页查询 Page page = this.lambdaQuery() .like(StringUtils.isNotBlank(searchNum), Repair::getRepairNum, searchNum) // .like(StringUtils.isNotBlank(searchVIN),Repair::) .like(StringUtils.isNotBlank(searchDistributor), Repair::getDepartmentName, searchDistributor) .ge(beginTime != null, Repair::getCreateTime, beginTime) .le(endTime != null, Repair::getCreateTime, endTime) .page(new Page<>(pageIndex, pageSize)); RepairQueryPageVO vo = new RepairQueryPageVO(); vo.setCurrent((int) page.getCurrent()); vo.setSize((int) page.getSize()); vo.setPages((int) page.getPages()); vo.setTotal((int) page.getTotal()); if (StringUtils.isNotBlank(searchNum) || StringUtils.isNotBlank(searchDistributor) || beginTime != null || endTime != null) { vo.setSearchCount(true); } List records = page.getRecords(); List queryRepairVOList = new ArrayList<>(); for (Repair record : records) { QueryRepairVO queryRepairVO = BeanUtil.copyProperties(record, QueryRepairVO.class); String electronicControlIds = record.getElectronicControlIds(); List eleIds = Arrays.stream(electronicControlIds.split(",")) .map(Integer::parseInt) .collect(Collectors.toList()); List electronicControlList = electronicControlService.listByIds(eleIds); //查询系统数量 queryRepairVO.setSystemCount(electronicControlList.size()); int faultCount = 0; for (ElectronicControl electronicControl : electronicControlList) { List faultCodeList = faultCodeService.lambdaQuery() .eq(electronicControl != null, FaultCode::getElectronicControlId, electronicControl.getId()) .list(); faultCount += faultCodeList.size(); } //查询故障数量 queryRepairVO.setFaultCount(faultCount); queryRepairVOList.add(queryRepairVO); } vo.setRecords(queryRepairVOList); return Result.ok(vo); } @Override public Result reportPdf(Integer id) { //根据id查询报告是否存在 Repair repair = this.getById(id); if (BeanUtil.isEmpty(repair)){ throw new BadReqException("该诊断报告不存在"); } Map baseMap = new HashMap<>(); baseMap.put("车架号",repair.getVin()); baseMap.put("年款",repair.getModelYear()); baseMap.put("里程",repair.getMileage()); baseMap.put("诊断路径",repair.getDiagnosticPath()); baseMap.put("报告编号",repair.getRepairNum()); baseMap.put("SN",repair.getSn()); String username = repair.getUsername(); //根据用户名查询用户 User user = userService.lambdaQuery() .eq(User::getUsername, username) .one(); if (BeanUtil.isEmpty(user)){ throw new BadReqException("该用户不存在"); } Map companyMap = new HashMap<>(); companyMap.put("公司",user.getCompany()); companyMap.put("电话",user.getTel()); companyMap.put("地址",user.getAddress()); List controlListMap = new ArrayList<>(); String electronicControlIds = repair.getElectronicControlIds(); List eleIds = Arrays.stream(electronicControlIds.split(",")) .map(Integer::parseInt) .collect(Collectors.toList()); List electronicControlList = electronicControlService.listByIds(eleIds); for (Integer eleId : eleIds) { ElectronicControl electronicControl = electronicControlService.getById(eleId); List EcuList = ecuInfoService.lambdaQuery() .eq(EcuInfo::getElectronicControlId, eleId) .list(); List FaultList = faultCodeService.lambdaQuery() .eq(FaultCode::getElectronicControlId, eleId) .eq(FaultCode::getStatus,1) .list(); Map faultCodeMap = new HashMap<>(); Map versionMap = new HashMap<>(); for (EcuInfo ecuInfo : EcuList) { versionMap.put(ecuInfo.getStrCaption(),ecuInfo.getStrInformation()); } for (FaultCode faultCode : FaultList) { faultCodeMap.put(faultCode.getFaultCode(),faultCode.getDescription()); } ControlListMap controlListMap1 = new ControlListMap(); controlListMap1.setName(electronicControl.getElectronicControlName()); controlListMap1.setVersionMap(versionMap); controlListMap1.setFaultCodeMap(faultCodeMap); controlListMap.add(controlListMap1); } File file = pdfUtil.MapToPDF(repair.getId(), controlListMap, baseMap, companyMap); if (file==null){ throw new BizException("该pdf文件不存在"); } try { huaweiObsUtil.upload(file, "report"); String uploadUrl1 = huaweiObsUtil.getUploadUrl(file.getName(), "report"); file.delete(); return Result.ok(uploadUrl1); } catch (IOException e) { throw new RuntimeException(e); } } @Override public Result reportH5(Integer id) { //根据id查询报告是否存在 Repair repair = this.getById(id); if (BeanUtil.isEmpty(repair)){ throw new BadReqException("该诊断报告不存在"); } Map baseMap = new HashMap<>(); baseMap.put("车架号",repair.getVin()); baseMap.put("年款",repair.getModelYear()); baseMap.put("里程",repair.getMileage()); baseMap.put("诊断路径",repair.getDiagnosticPath()); baseMap.put("报告编号",repair.getRepairNum()); baseMap.put("SN",repair.getSn()); String username = repair.getUsername(); //根据用户名查询用户 User user = userService.lambdaQuery() .eq(User::getUsername, username) .one(); if (BeanUtil.isEmpty(user)){ throw new BadReqException("该用户不存在"); } Map companyMap = new HashMap<>(); companyMap.put("公司",user.getCompany()); companyMap.put("电话",user.getTel()); companyMap.put("地址",user.getAddress()); List controlListMap = new ArrayList<>(); String electronicControlIds = repair.getElectronicControlIds(); List eleIds = Arrays.stream(electronicControlIds.split(",")) .map(Integer::parseInt) .collect(Collectors.toList()); List electronicControlList = electronicControlService.listByIds(eleIds); for (Integer eleId : eleIds) { ElectronicControl electronicControl = electronicControlService.getById(eleId); List EcuList = ecuInfoService.lambdaQuery() .eq(EcuInfo::getElectronicControlId, eleId) .list(); List FaultList = faultCodeService.lambdaQuery() .eq(FaultCode::getElectronicControlId, eleId) .eq(FaultCode::getStatus,1) .list(); Map faultCodeMap = new HashMap<>(); Map versionMap = new HashMap<>(); for (EcuInfo ecuInfo : EcuList) { versionMap.put(ecuInfo.getStrCaption(),ecuInfo.getStrInformation()); } for (FaultCode faultCode : FaultList) { faultCodeMap.put(faultCode.getFaultCode(),faultCode.getDescription()); } ControlListMap controlListMap1 = new ControlListMap(); controlListMap1.setName(electronicControl.getElectronicControlName()); controlListMap1.setVersionMap(versionMap); controlListMap1.setFaultCodeMap(faultCodeMap); controlListMap.add(controlListMap1); } File file = htmlGenerator.generateHtmlByMap(repair.getId(), controlListMap, baseMap, companyMap); if (file==null){ throw new BizException("该h5文件不存在"); } try { huaweiObsUtil.upload(file,"h5"); String uploadUrl1 = huaweiObsUtil.getUploadUrl(file.getName(), "h5"); file.delete(); return Result.ok(uploadUrl1); } catch (IOException e) { throw new RuntimeException(e); } } @Override public Result updateBase(AppRepairUpdateBaseDTO dto) { Integer id = dto.getId(); String type = dto.getType(); String value = dto.getValue(); //根据id进行查询 Repair repair = this.getById(id); if (BeanUtil.isEmpty(repair)){ throw new BadReqException("该诊断报告不存在"); } if (type.equals("vin")){ repair.setVin(value); }else if (type.equals("year")){ repair.setModelYear(value); } else if (type.equals("mileage")){ repair.setMileage(value); } this.updateById(repair); return Result.ok(); } @Override public Result updateCompany(AppRepairUpdateBaseDTO dto) { Integer id = dto.getId(); String type = dto.getType(); String value = dto.getValue(); //根据id进行查询 Repair repair = this.getById(id); if (BeanUtil.isEmpty(repair)){ throw new BadReqException("该诊断报告不存在"); } String username = repair.getUsername(); //根据username查询用户 User user = userService.lambdaQuery() .eq(User::getUsername, username) .one(); if (BeanUtil.isEmpty(user)){ throw new BadReqException("该用户不存在"); } if (type.equals("company")){ user.setCompany(value); }else if (type.equals("address")){ user.setAddress(value); } else if (type.equals("phone")){ user.setTel(value); } userService.updateById(user); return Result.ok(); } }