src/main/java/com/whyc/controller/DeviceSpareController.java
@@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @@ -48,6 +49,12 @@ return service.addByExcel(file); } @ApiOperation("excel导出") @GetMapping("excelExport") public void excelExport(HttpServletResponse response) { service.excelExport(response); } @PostMapping("update") @ApiOperation("修改") @@ -55,6 +62,12 @@ return service.update(spare); } @PostMapping("andOrChangePicture") @ApiOperation("新增或者替换图片") public Response andOrChangePicture(@RequestParam Integer id,@RequestParam MultipartFile file) throws IOException { return service.andOrChangePicture(id,file); } @PostMapping("delete") @ApiOperation("删除") public Response delete(@RequestParam Integer id) { src/main/java/com/whyc/controller/WorkflowMainController.java
@@ -8,11 +8,15 @@ import com.whyc.service.WorkflowMainService; import com.whyc.util.ActionUtil; import com.whyc.util.CommonUtil; import com.whyc.util.JsonUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.List; import java.util.Map; @RestController @@ -23,10 +27,16 @@ @Autowired private WorkflowMainService service; @PostMapping("submit") /*@PostMapping("submit") @ApiOperation(value = "提交单据", notes = "传参:taskDesc,mainType,mainTypeCN,mainTypeEn,dealRoleId,processLevel") public Response<Integer> submit(@RequestBody WorkflowMain main){ return service.submit(main); }*/ @PostMapping("submit") @ApiOperation(value = "提交单据", notes = "") public Response<Integer> submit(@RequestParam String mainJsonStr, @RequestPart(required = false) List<MultipartFile> fileList, @RequestPart(required = false) List<MultipartFile> picList) throws IOException { WorkflowMain main = JsonUtil.getGson().fromJson(mainJsonStr, WorkflowMain.class); return service.submit(main,fileList,picList); } src/main/java/com/whyc/mapper/DeviceSpareMapper.java
@@ -8,4 +8,6 @@ void outBound(List<DeviceSpare> spareList); void updateQuantityAndPictureBatch(List<DeviceSpare> spareListUpdate); } src/main/java/com/whyc/pojo/web_site/DeviceSpareLog.java
@@ -25,4 +25,13 @@ private Date createTime; public DeviceSpareLog(Integer deviceSpareId, Integer operationType, String operationDetail, Date createTime) { this.deviceSpareId = deviceSpareId; this.operationType = operationType; this.operationDetail = operationDetail; this.createTime = createTime; } public DeviceSpareLog() { } } src/main/java/com/whyc/pojo/web_site/WorkflowMain.java
@@ -7,6 +7,7 @@ import lombok.Data; import lombok.ToString; import org.apache.ibatis.type.Alias; import org.springframework.web.multipart.MultipartFile; import java.util.Date; import java.util.List; @@ -68,16 +69,16 @@ @ApiModelProperty("工单状态") private Integer status; /**附件名称*/ @ApiModelProperty("附件名称") @ApiModelProperty("附件名称,多个用逗号隔开") private String fileName; /**附件地址*/ @ApiModelProperty("附件地址") @ApiModelProperty("附件地址,多个用逗号隔开") private String fileUrl; /**图片名称*/ @ApiModelProperty("图片名称") @ApiModelProperty("图片名称,多个用逗号隔开") private String picName; /**图片地址*/ @ApiModelProperty("图片地址") @ApiModelProperty("图片地址,多个用逗号隔开") private String picUrl; /**归档意见*/ @ApiModelProperty("归档意见") @@ -101,4 +102,5 @@ @TableField(exist = false) private List<WorkflowDevice> deviceList; } src/main/java/com/whyc/schedule/AlarmInspectionSchedule.java
@@ -17,7 +17,7 @@ /** * 有两个定时任务:1.告警产生,加入到巡检实时表,2.告警是否消失,同步到巡检实时表 */ @EnableScheduling //@EnableScheduling @Component public class AlarmInspectionSchedule { src/main/java/com/whyc/service/DeviceSpareLogService.java
@@ -8,6 +8,7 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Date; import java.util.List; @Service @@ -21,6 +22,11 @@ mapper.insert(deviceSpareLog); } public void add(Integer deviceSpareId, Integer operationType, String operationDetail, Date createTime) { DeviceSpareLog deviceSpareLog = new DeviceSpareLog(deviceSpareId, operationType, operationDetail, createTime); mapper.insert(deviceSpareLog); } public Response<List<DeviceSpareLog>> getList(int deviceSpareId) { QueryWrapper<DeviceSpareLog> query = Wrappers.query(); query.eq("device_spare_id",deviceSpareId); src/main/java/com/whyc/service/DeviceSpareService.java
@@ -1,5 +1,8 @@ package com.whyc.service; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.TemplateExportParams; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; @@ -13,17 +16,22 @@ import com.whyc.util.ThreadLocalUtil; import org.apache.commons.lang.StringUtils; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.net.URLEncoder; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; @Service public class DeviceSpareService { @@ -66,10 +74,10 @@ if (file != null && file.size() > 0) { for (int i = 0; i < file.size(); i++) { MultipartFile multipartFile = file.get(i); String fileName = multipartFile.getOriginalFilename(); //String fileName = multipartFile.getOriginalFilename(); //将fileName中可能存在的,去掉 fileName = fileName.replace(",",""); String filePath = fileDirPath + File.separator + timeFormat+"_"+fileName; //fileName = fileName.replace(",",""); String filePath = fileDirPath + File.separator + spare.getName()+"_"+spare.getModel()+"_"+spare.getVersion() + "_" + (i+1) + "_"+ timeFormat+".png"; multipartFile.transferTo(new File(filePath)); String split = "pis_file"+File.separator+"deviceSpare"; @@ -229,13 +237,143 @@ spare.setSupplier(supplier); spareList.add(spare); //第七列为图片 TODO 待处理 /*Cell cell = row.getCell(7); CellType cellType = cell.getCellType(); System.out.println("-");*/ } addBatch(spareList); return null; } //第七列为图片-浮动式图片 //获取绘图对象中的所有图形 XSSFDrawing drawing = (XSSFDrawing) sheet.getDrawingPatriarch(); if (drawing == null) { drawing = (XSSFDrawing) sheet.createDrawingPatriarch(); } List<XSSFShape> shapes = drawing.getShapes(); for (XSSFShape shape : shapes) { if (shape instanceof XSSFPicture) { XSSFPicture pic = (XSSFPicture) shape; XSSFClientAnchor anchor = pic.getClientAnchor(); //获取图片所在的起始行 int rowIndex = anchor.getRow1(); byte[] data = pic.getPictureData().getData(); DeviceSpare spare = spareList.get(rowIndex - 1); //对存储路径进行定义 Date now = new Date(); String timeFormat = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM_DD_HH_MM_SS_UNION, now); String dirMonth = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM, now); String fileDirPath = CommonUtil.getRootFile() + "deviceSpare" + File.separator + dirMonth; File fileDir = new File(fileDirPath); //如果文件夹不存在则创建 if (!fileDir.exists()) { fileDir.mkdirs(); } String filePath = fileDirPath + File.separator + spare.getName()+"_"+spare.getModel()+"_"+spare.getVersion() + "_"+ timeFormat+".png"; // 保存图片到本地 try (FileOutputStream fos = new FileOutputStream(filePath)) { fos.write(data); } String split = "pis_file"+File.separator+"deviceSpare"; String picUrl = File.separator + filePath.substring(filePath.indexOf(split)); spare.setPictureUrl(picUrl); } } //新增列表 List<DeviceSpare> spareListNew = new ArrayList<>(); //更新列表 List<DeviceSpare> spareListUpdate = new ArrayList<>(); //查询库中的所有设备器件 List<DeviceSpare> deviceSpareListInDB = mapper.selectList((Wrapper<DeviceSpare>) CommonUtil.nullObject); for (DeviceSpare spare : spareList){ boolean isExist = false; for (DeviceSpare spareInDB : deviceSpareListInDB){ if (spare.getName().equals(spareInDB.getName()) && spare.getModel().equals(spareInDB.getModel()) && spare.getVersion().equals(spareInDB.getVersion()) && spare.getBrand().equals(spareInDB.getBrand()) && spare.getType().equals(spareInDB.getType()) && spare.getSupplier().equals(spareInDB.getSupplier()) ){ isExist = true; spare.setId(spareInDB.getId()); spare.setQuantity(spareInDB.getQuantity()+spare.getQuantity()); if (spare.getPictureUrl() != null && spareInDB.getPictureUrl()!=null){ spare.setPictureUrl(spareInDB.getPictureUrl()+","+spare.getPictureUrl()); } spareListUpdate.add(spare); } } if(!isExist){ //不存在则新增 spareListNew.add(spare); } } //更新 if(spareListUpdate.size()>0) { updateQuantityAndPictureBatch(spareListUpdate); } //新增 if(spareListNew.size()>0) { addBatch(spareListNew); } return new Response().setII(1,"导入完成"); } private void updateQuantityAndPictureBatch(List<DeviceSpare> spareListUpdate) { mapper.updateQuantityAndPictureBatch(spareListUpdate); } public void excelExport(HttpServletResponse response) { //查询所有的设备器件 List<DeviceSpare> deviceSpareList = mapper.selectList((Wrapper<DeviceSpare>) CommonUtil.nullObject); //获取导出模板地址 ClassPathResource classPathResource = new ClassPathResource("excel_templates/template_device_spare.xlsx"); String path = classPathResource.getPath(); TemplateExportParams templateExportParams1 = new TemplateExportParams(path,true); // 构建导出数据模型 Map<String, Object> map = new HashMap<>(); map.put("deviceSpareList", deviceSpareList); // 假设模板中使用 ${deviceSpareList} 作为变量名 Workbook wb = ExcelExportUtil.exportExcel(templateExportParams1, map); try { LocalDateTime now = LocalDateTime.now(); String fileName = "维修管理器件库存_"+now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))+".xlsx"; response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8")); response.flushBuffer(); wb.write(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } public Response andOrChangePicture(Integer id, MultipartFile file) throws IOException { DeviceSpare spare = mapper.selectById(id); //对存储路径进行定义 Date now = new Date(); String timeFormat = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM_DD_HH_MM_SS_UNION, now); String dirMonth = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM, now); String fileDirPath = CommonUtil.getRootFile() + "deviceSpare" + File.separator + dirMonth; File fileDir = new File(fileDirPath); //如果文件夹不存在则创建 if (!fileDir.exists()) { fileDir.mkdirs(); } String filePath = fileDirPath + File.separator + spare.getName()+"_"+spare.getModel()+"_"+spare.getVersion() + "_"+ timeFormat+".png"; // 保存图片到本地 file.transferTo(new File(filePath)); String split = "pis_file"+File.separator+"deviceSpare"; String picUrl = File.separator + filePath.substring(filePath.indexOf(split)); //更新图片 UpdateWrapper<DeviceSpare> update = Wrappers.update(); update.eq("id",id); update.set("picture_url",picUrl); mapper.update((DeviceSpare) CommonUtil.nullObject,update); //记录变更 deviceSpareLogService.add(id,2,"更换图片",now); return new Response().setII(1,"新增或者替换图片完成"); } } src/main/java/com/whyc/service/WorkflowMainService.java
@@ -20,9 +20,12 @@ 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 javax.annotation.Resource; import javax.servlet.ServletContext; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; @@ -244,7 +247,7 @@ */ @Transactional public Response submit(WorkflowMain main){ public Response submit(WorkflowMain main, List<MultipartFile> fileList, List<MultipartFile> picList) throws IOException { //1.提交到单据审批流程 //如果存在关联单据id,首先校验提交的数量 Date now = new Date(); @@ -299,6 +302,64 @@ deviceList.get(i).setQuantityUnprocessed(deviceList.get(i).getQuantity()); } } //如果存在附件和图片,则存入对应字段 //对存储路径进行定义 String timeFormat = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM_DD_HH_MM_SS_UNION, now); String dirMonth = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM, now); String fileDirPath = CommonUtil.getRootFile() + "workflow" + File.separator + dirMonth; File fileDir = new File(fileDirPath); //如果文件夹不存在则创建 if (!fileDir.exists()) { fileDir.mkdirs(); } StringBuilder fileUrlSb = new StringBuilder(); StringBuilder fileNameSb = new StringBuilder(); if (fileList != null && fileList.size() > 0) { for (int i = 0; i < fileList.size(); i++) { MultipartFile multipartFile = fileList.get(i); String fileName = multipartFile.getOriginalFilename(); //将fileName中可能存在的,去掉 fileName = fileName.replace(",",""); String filePath = fileDirPath + File.separator + timeFormat+"_"+fileName; multipartFile.transferTo(new File(filePath)); String split = "pis_file"+File.separator+"workflow"; String fileUrl = File.separator + filePath.substring(filePath.indexOf(split)); if(i == fileList.size()-1) { fileUrlSb.append(fileUrl); fileNameSb.append(fileName); }else { fileUrlSb.append(fileUrl).append(","); fileNameSb.append(fileName).append(","); } } } main.setFileUrl(fileUrlSb.toString()); main.setFileName(fileNameSb.toString()); StringBuilder picUrlSb = new StringBuilder(); StringBuilder picNameSb = new StringBuilder(); if (picList != null && picList.size() > 0) { for (int i = 0; i < picList.size(); i++) { MultipartFile multipartFile = picList.get(i); String picName = multipartFile.getOriginalFilename(); //将picName中可能存在的,去掉 picName = picName.replace(",",""); String picPath = fileDirPath + File.separator + picName; multipartFile.transferTo(new File(picPath)); String split = "pis_file"+File.separator+"workflow"; String picUrl = File.separator + picPath.substring(picPath.indexOf(split)); if(i == picList.size()-1) { picUrlSb.append(picUrl); picNameSb.append(picName); }else { picUrlSb.append(picUrl).append(","); picNameSb.append(picName).append(","); } } } main.setPicUrl(picUrlSb.toString()); main.setPicName(picNameSb.toString()); add(main); //主表关联的物料插入 for (WorkflowDevice device : deviceList) { src/main/java/com/whyc/util/CommonUtil.java
@@ -3,6 +3,7 @@ import com.whyc.constant.YamlProperties; import com.whyc.pojo.db_user.User; import com.whyc.service.UserLogService; import org.apache.poi.ss.formula.functions.T; import org.apache.shiro.SecurityUtils; import org.apache.shiro.subject.Subject; import org.springframework.beans.factory.annotation.Autowired; @@ -20,6 +21,7 @@ @Component public class CommonUtil { public static Object nullObject = null; private static UserLogService userLogService; @Autowired src/main/resources/excel_templates/template_device_spare.xlsxBinary files differ
src/main/resources/mapper/DeviceSpareMapper.xml
@@ -13,4 +13,10 @@ and supplier = #{item.supplier} </foreach> </update> <update id="updateQuantityAndPictureBatch"> <foreach collection="list" item="item" separator=";"> update web_site.tb_device_spare set quantity = #{item.quantity},picture_url = #{item.pictureUrl} where id = #{item.id} </foreach> </update> </mapper>