| | |
| | | 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; |
| | |
| | | */ |
| | | |
| | | @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(); |
| | | WorkflowMain mainRelated = getById(main.getRelatedId()); |
| | | List<WorkflowDevice> deviceListRelatedInDB = deviceService.getByMainId(main.getRelatedId()); |
| | | List<WorkflowDevice> deviceList = main.getDeviceList(); |
| | | if(main.getRelatedId() != null) { |
| | | if (main.getQuantity() > mainRelated.getQuantityUnprocessed()) { |
| | | return new Response().setII(1, "入库数量不能大于维修申请关联单据的未处理数量"); |
| | | //遍历deviceList,如果deviceList内的对象的quantity值大于 deviceListRelatedInDB内对象列表中的对象所有字段相同的记录的quantityUnprocessed值,则返回错误 |
| | | for (int i = 0; i < deviceList.size(); i++) { |
| | | WorkflowDevice device = deviceList.get(i); |
| | | for (int j = 0; j < deviceListRelatedInDB.size(); j++) { |
| | | WorkflowDevice deviceInDB = deviceListRelatedInDB.get(j); |
| | | if (device.getName().equals(deviceInDB.getName()) |
| | | && device.getModel().equals(deviceInDB.getModel()) |
| | | && device.getVersion().equals(deviceInDB.getVersion()) |
| | | && device.getBrand().equals(deviceInDB.getBrand()) |
| | | && device.getType().equals(deviceInDB.getType()) |
| | | && device.getSupplier().equals(deviceInDB.getSupplier()) |
| | | ) { |
| | | if (device.getQuantity() > deviceInDB.getQuantityUnprocessed()) { |
| | | return new Response().setII(1, "入库数量不能大于维修申请关联单据的未处理数量"); |
| | | }else{ |
| | | //当前设备的校验结束,进入下一个设备校验 |
| | | break; |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | | //主表插入 |
| | |
| | | main.setOrderId(orderId); |
| | | main.setTitle(title); |
| | | main.setCreateUserId(user.getId()); |
| | | main.setCreateUserName(user.getName()); |
| | | main.setCreateTime(now); |
| | | main.setBeginTime(now); |
| | | main.setStatus(mainStatus); |
| | | main.setProcessStage(process.getStage()); |
| | | if(main.getType() == WorkflowTypeEnum.DEVICE_REPAIR.getType().intValue()){ //维修申请单,需填入 |
| | | main.setQuantityUnprocessed(main.getQuantity()); |
| | | for (int i = 0; i < deviceList.size(); i++) { |
| | | 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); |
| | | //主表关联的物料插入 |
| | | List<WorkflowDevice> deviceList = main.getDeviceList(); |
| | | for (WorkflowDevice device : deviceList) { |
| | | device.setMainId(main.getId()); |
| | | } |
| | |
| | | linkService.add(link); |
| | | |
| | | //如果存在关联单据id,处理关联单据及自动生成新单据 |
| | | checkRelatedAndDone(main, now, mainRelated, user); |
| | | checkRelatedAndDone(main, now, deviceListRelatedInDB, user); |
| | | return new Response().setII(1,"提交完成"); |
| | | } |
| | | |
| | | private void checkRelatedAndDone(WorkflowMain main, Date now, WorkflowMain mainRelated, User user) { |
| | | private void checkRelatedAndDone(WorkflowMain main, Date now, List<WorkflowDevice> deviceListRelatedInDB, User user) { |
| | | if(main.getRelatedId() != null){ |
| | | //判断是申请的入库,还是报废. |
| | | if (main.getQuantity() < mainRelated.getQuantityUnprocessed()){ //说明需要自动生成入库或者报废单据 |
| | | //申请的数量<关联单据的未处理数量,则说明有剩余. 需要自动生成对立工单 |
| | | List<WorkflowDevice> deviceList = main.getDeviceList(); |
| | | //计算deviceListn内quantity属性值的和 |
| | | int sumQuantity = 0; |
| | | for (WorkflowDevice device : deviceList) { |
| | | sumQuantity += device.getQuantity(); |
| | | } |
| | | //计算deviceListRelatedInDBn内quantityUnprocessed属性值的和 |
| | | int sumQuantityUnprocessedInDB = 0; |
| | | for (WorkflowDevice device : deviceListRelatedInDB) { |
| | | sumQuantityUnprocessedInDB += device.getQuantityUnprocessed(); |
| | | } |
| | | if (sumQuantity < sumQuantityUnprocessedInDB){ //说明需要自动生成入库或者报废单据 |
| | | //需要自动生成报废的申请 |
| | | WorkflowMain mainAuto = new WorkflowMain(); |
| | | WorkflowTypeEnum typeEnumAuto; |
| | |
| | | mainAuto.setCreateUserId(user.getId()); |
| | | mainAuto.setCreateTime(now); |
| | | mainAuto.setBeginTime(now); |
| | | mainAuto.setQuantity(mainRelated.getQuantityUnprocessed()- main.getQuantity()); |
| | | mainAuto.setProcessStage(processAuto.getStage()); |
| | | mainAuto.setStatus(statusAuto); |
| | | mainAuto.setRelatedId(main.getRelatedId()); |
| | | //整理主表的设备附表 |
| | | List<WorkflowDevice> deviceListAuto = new ArrayList<>(); |
| | | //遍历deviceListRelatedInDB,减去deviceList中申请的数量 |
| | | for (WorkflowDevice deviceInDB : deviceListRelatedInDB) { |
| | | boolean deviceExists = false; |
| | | for (WorkflowDevice device : deviceList) { |
| | | if (device.getName().equals(deviceInDB.getName()) |
| | | && device.getModel().equals(deviceInDB.getModel()) |
| | | && device.getVersion().equals(deviceInDB.getVersion()) |
| | | && device.getBrand().equals(deviceInDB.getBrand()) |
| | | && device.getType().equals(deviceInDB.getType()) |
| | | && device.getSupplier().equals(deviceInDB.getSupplier()) |
| | | ){ |
| | | deviceExists = true; |
| | | if (deviceInDB.getQuantityUnprocessed()-device.getQuantity() > 0){ |
| | | WorkflowDevice deviceAuto = new WorkflowDevice(); |
| | | deviceAuto.setName(deviceInDB.getName()); |
| | | deviceAuto.setModel(deviceInDB.getModel()); |
| | | deviceAuto.setVersion(deviceInDB.getVersion()); |
| | | deviceAuto.setQuantity(deviceInDB.getQuantityUnprocessed()-device.getQuantity()); |
| | | deviceAuto.setBrand(deviceInDB.getBrand()); |
| | | deviceAuto.setType(deviceInDB.getType()); |
| | | deviceAuto.setSupplier(deviceInDB.getSupplier()); |
| | | |
| | | deviceListAuto.add(deviceAuto); |
| | | }else{ |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | if (!deviceExists){ |
| | | WorkflowDevice deviceAuto = new WorkflowDevice(); |
| | | deviceAuto.setName(deviceInDB.getName()); |
| | | deviceAuto.setModel(deviceInDB.getModel()); |
| | | deviceAuto.setVersion(deviceInDB.getVersion()); |
| | | deviceAuto.setQuantity(deviceInDB.getQuantityUnprocessed()); |
| | | deviceAuto.setBrand(deviceInDB.getBrand()); |
| | | deviceAuto.setType(deviceInDB.getType()); |
| | | deviceAuto.setSupplier(deviceInDB.getSupplier()); |
| | | |
| | | deviceListAuto.add(deviceAuto); |
| | | } |
| | | } |
| | | |
| | | if(main.getType() == WorkflowTypeEnum.DEVICE_IN.getType().intValue()) { //申请的入库 |
| | | mainAuto.setTaskDesc("提交了维修后的入库申请,系统自动生成剩余数量的报废申请"); |
| | |
| | | mainAuto.setTitle(titleAuto); |
| | | mainAuto.setType(typeEnumAuto.getType()); |
| | | add(mainAuto); |
| | | deviceListAuto.forEach(device -> { |
| | | device.setMainId(mainAuto.getId()); |
| | | }); |
| | | //插入主表的设备附表 |
| | | deviceService.addBatch(deviceListAuto); |
| | | |
| | | //自动节点生成 |
| | | WorkflowLink linkAuto = new WorkflowLink(); |
| | |
| | | }else{ |
| | | relatedMain.setEndReason("用户提交维修申请后的报废申请,维修流程结束"); |
| | | } |
| | | relatedMain.setQuantityUnprocessed(0); |
| | | updateById(relatedMain); |
| | | //更新关联单据的设备剩余未处理数量为0 |
| | | deviceService.setQuantityUnprocessedZero(main.getRelatedId()); |
| | | } |
| | | } |
| | | |
| | |
| | | mapper.update((WorkflowMain) ActionUtil.objeNull,update); |
| | | } |
| | | |
| | | public void resetRepairStatus(Integer relatedId, Integer quantity) { |
| | | public void resetRepairStatus(Integer relatedId) { |
| | | UpdateWrapper<WorkflowMain> update = Wrappers.update(); |
| | | update.set("status",WorkflowEnum.MAIN_STATUS_WAIT_FOR_DEALING.getValue()) |
| | | .set("end_time",null) |
| | | .set("end_reason",null) |
| | | .set("quantity_unprocessed",quantity) |
| | | .eq("id",relatedId); |
| | | mapper.update((WorkflowMain) ActionUtil.objeNull,update); |
| | | } |