|  |  | 
 |  |  |     private DeviceExtMapper deviceExtMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private UnqualifiedRecordMapper unqualifiedRecordMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private CompanyUserExtMapper companyUserExtMapper; | 
 |  |  |  | 
 |  |  |     @Transactional(rollbackFor = {BusinessException.class,Exception.class}) | 
 |  |  |     @Override | 
 |  |  | 
 |  |  |         } | 
 |  |  |         if(!Constants.equalsInteger(mp.getStatus(),Constants.WORKORDER_STATUS.create) | 
 |  |  |                 && !Constants.equalsInteger(mp.getStatus(),Constants.WORKORDER_STATUS.material) | 
 |  |  |                 && !Constants.equalsInteger(mp.getStatus(),Constants.WORKORDER_STATUS.producing) | 
 |  |  |                 && !Constants.equalsInteger(mp.getStatus(),Constants.WORKORDER_STATUS.check) | 
 |  |  |                 && !Constants.equalsInteger(mp.getStatus(),Constants.WORKORDER_STATUS.done)){ | 
 |  |  |             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,该工单状态已流转,不能进行该操作!"); | 
 |  |  | 
 |  |  |         wrModel.setMaterialId(wStock.getMaterialId()); | 
 |  |  |         wrModel.setDepartId(mp.getDepartId()); | 
 |  |  |         wrModel.setCreateTime(DateUtil.getCurrentDate()); | 
 |  |  |         wrModel.setCreateUser(user.getId()); | 
 |  |  |         wrModel.setCreateUser(mp.getCreateUser()); | 
 |  |  |         wrModel.setMaterialBatch(mp.getBatch()); | 
 |  |  |         wrModel.setType(Constants.WORKORDER_RECORD_TYPE.materail); | 
 |  |  |         wrModel.setFactoryId(mp.getFactoryId()); | 
 |  |  |         wrModel.setProcedureId(mp.getProcedureId()); | 
 |  |  |         wrModel.setBatch(mp.getBatch()); | 
 |  |  |         wrModel.setProcedureId(wStock.getProcedureId()); | 
 |  |  |         wrModel.setBatch(wStock.getBatch()); | 
 |  |  |         wrModel.setUnitId(mp.getUnitId()); | 
 |  |  |         wrModel.setPlanId(mp.getPlanId()); | 
 |  |  |         //TODO 目前使用工装主键存储货位主键 | 
 |  |  | 
 |  |  |         if(mp== null){ | 
 |  |  |             throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "对不起,该记录不存在!"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         Plans plans = plansExtMapper.selectById(mp.getPlanId()); | 
 |  |  |         if(Objects.isNull(plans)){ | 
 |  |  |             throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "对不起,工序计划记录不存在!"); | 
 |  |  |         } | 
 |  |  |         //查询工序计划下 所有工单已报工数量 | 
 |  |  |         QueryWorkorderExtDTO dto = new QueryWorkorderExtDTO(); | 
 |  |  |         dto.setPlanId(mp.getPlanId()); | 
 |  |  |         dto.setDeleted(Constants.ZERO); | 
 |  |  |         dto.setStatus(Constants.WORKORDER_STATUS.baogong); | 
 |  |  |         List<WorkorderExtListVO> workorderList = workorderExtMapper.selectList(dto); | 
 |  |  |         Integer plansQualifiedNum = 0 , plansUnQualifiedNum = 0; | 
 |  |  |         plansQualifiedNum = workorderList.stream().map( workorderExtListVO -> workorderExtListVO.getQualifiedNum()).reduce(Constants.ZERO, Integer::sum); | 
 |  |  |         plansUnQualifiedNum = workorderList.stream().map( workorderExtListVO -> workorderExtListVO.getUnqualifiedNum()).reduce(Constants.ZERO, Integer::sum); | 
 |  |  |         if((BigDecimal.valueOf(plansQualifiedNum).add(BigDecimal.valueOf(plansUnQualifiedNum).add(autoWorkReportDTO.getCreateWorkorderRecordDTO().getUnQualifiedNum()) | 
 |  |  |                 .add(autoWorkReportDTO.getCreateWorkorderRecordDTO().getQualifiedNum()))).compareTo(BigDecimal.valueOf(plans.getNum()))>Constants.ZERO){ | 
 |  |  |             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,报工数量超出工序计划总数!"); | 
 |  |  |         } | 
 |  |  |         Workorder param = new Workorder(); | 
 |  |  |  | 
 |  |  |         param.setPlanId(mp.getPlanId()); | 
 |  |  |         param.setId(autoWorkReportDTO.getCreateWorkorderRecordDTO().getWorkorderId()); | 
 |  |  |         if(Constants.equalsInteger(mp.getPaused(),Constants.ONE)){ | 
 |  |  |             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,工单已暂停!"); | 
 |  |  |         } | 
 |  |  | 
 |  |  |         List<Appliances> updateApplianceProList = new ArrayList<>(); | 
 |  |  |         //待更新库存集合 | 
 |  |  |         List<WStock> stockList = new ArrayList<>(); | 
 |  |  |         //处理产出数据 | 
 |  |  |         WorkorderRecord workorderRecord = this.createWorkorderRecord(autoWorkReportDTO.getCreateWorkorderRecordDTO(),user,null); | 
 |  |  |         //查询投料和报工记录列表 | 
 |  |  |         List<WorkorderRecordExtListVO>  allRecordList = workorderRecordExtMapper.selectAllList(getQueryRecordParam(param)); | 
 |  |  |         if(Objects.isNull(allRecordList)||allRecordList.size()==Constants.ZERO){ | 
 |  |  |             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"未查询投料和报工记录!"); | 
 |  |  |         } | 
 |  |  |         //处理产出数据 | 
 |  |  |         WorkorderRecord workorderRecord = this.createWorkorderRecord(autoWorkReportDTO.getCreateWorkorderRecordDTO(),user); | 
 |  |  |         //检查数据信息 | 
 |  |  | //        caculateCheckRecord(user, mp,allRecordList); | 
 |  |  |         QueryBomDetailExtDTO bdparam = initBomParam(user,mp); | 
 |  |  | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         //更新工单状态为已报工 | 
 |  |  |         mp.setUnqualifiedNum(autoWorkReportDTO.getCreateWorkorderRecordDTO().getUnQualifiedNum().intValue()); | 
 |  |  |         mp.setQualifiedNum(autoWorkReportDTO.getCreateWorkorderRecordDTO().getQualifiedNum().intValue()); | 
 |  |  |         updateOrderInfo(user,mp,Constants.WORKORDER_HISTORY_STATUS.baogong); | 
 |  |  |  | 
 |  |  |         //批量插入出入库明细记录 | 
 |  |  | 
 |  |  |             List<WOutbound> outboundList = new ArrayList<>(); | 
 |  |  |             //如果需要投料的物料集合,在库存中寻找投料来源 | 
 |  |  |             for(WStock model : materialList){ | 
 |  |  |                 List<WStock> useList = allList.stream().filter(wStock -> Constants.equalsInteger(model.getMaterialId(),wStock.getMaterialId())).collect(Collectors.toList()); | 
 |  |  |                 BigDecimal wStockNum = useList.stream().map(s -> s.getNum()).reduce(BigDecimal.ZERO, BigDecimal::add); | 
 |  |  |                 if(wStockNum.compareTo(Constants.formatBigdecimal(model.getNum()))<Constants.ZERO){ | 
 |  |  |                     throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,生产物料不足,无法进行该操作!"); | 
 |  |  |                 } | 
 |  |  |                 for(WStock ts : allList){ | 
 |  |  |  | 
 |  |  |                     if(Constants.equalsInteger(model.getMaterialId(),ts.getMaterialId())){ | 
 |  |  |                         //还需要投料数量 | 
 |  |  |                         BigDecimal actNum = Constants.formatBigdecimal(model.getNum()).subtract(Constants.formatBigdecimal(model.getTNum())); | 
 |  |  | 
 |  |  |                 //如果是产出 | 
 |  |  |                 mp.setHasProduceNum(Constants.formatBigdecimal(tModel.getNum()).intValue()+Constants.formatIntegerNum(mp.getHasProduceNum())); | 
 |  |  |                 //产出合格 | 
 |  |  |                this.createOutDetail(user,outbound,mp,tModel,Constants.ZERO,detailList,stockList); | 
 |  |  |                 this.createOutDetail(user,outbound,mp,tModel,Constants.ZERO,detailList,stockList); | 
 |  |  |                //产出不良 | 
 |  |  |                 this.createOutDetail(user,outbound,mp,tModel,Constants.ONE,detailList,stockList); | 
 |  |  |             } | 
 |  |  | 
 |  |  |         detail.setWarehouseId(outbound.getWarehouseId()); | 
 |  |  |         detail.setLocationId(mp.getFinishWarehouseLocationId()); | 
 |  |  |         detail.setNum(BigDecimal.valueOf(doneType.equals(Constants.ZERO)?tModel.getQualifiedNum():tModel.getUnqualifiedNum())); | 
 |  |  |         if(detail.getNum().compareTo(BigDecimal.ZERO)==Constants.ZERO){ | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         detail.setStatus(Constants.ONE); | 
 |  |  |         detail.setDoneNum(detail.getNum()); | 
 |  |  |         detail.setDoneDate(new Date()); | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     @Transactional(rollbackFor = {BusinessException.class,Exception.class}) | 
 |  |  |     public WorkorderRecord createWorkorderRecord(CreateWorkorderRecordDTO createWorkorderRecordDTO, LoginUserInfo loginUserInfo){ | 
 |  |  |     public WorkorderRecord createWorkorderRecord(CreateWorkorderRecordDTO createWorkorderRecordDTO, LoginUserInfo loginUserInfo ,Integer userId){ | 
 |  |  |         Workorder workorder  = workorderExtMapper.selectById(createWorkorderRecordDTO.getWorkorderId()); | 
 |  |  |         if(Objects.isNull(workorder)){ | 
 |  |  |             throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,未查询到该工单信息!"); | 
 |  |  | 
 |  |  |         //数据存储 | 
 |  |  |         workorderRecord = new WorkorderRecord(); | 
 |  |  |         workorderRecord.setDeleted(Constants.ZERO); | 
 |  |  |         workorderRecord.setCreateUser(loginUserInfo.getId()); | 
 |  |  |         if(!Objects.isNull(userId)){ | 
 |  |  |             CompanyUser companyUser = new CompanyUser(); | 
 |  |  |             companyUser.setUserId(userId); | 
 |  |  |             companyUser.setRootDepartId(loginUserInfo.getRootDepartment().getId()); | 
 |  |  |             companyUser =     companyUserExtMapper.selectOne(new QueryWrapper<>(companyUser).last("limit 1")); | 
 |  |  |             if(Objects.isNull(companyUser)){ | 
 |  |  |                 throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,未查询到生产人员对应企业用户信息!"); | 
 |  |  |             } | 
 |  |  |             workorderRecord.setCreateUser(userId); | 
 |  |  |             workorderRecord.setRootDepartId(loginUserInfo.getRootDepartment().getId()); | 
 |  |  |             workorderRecord.setDepartId(loginUserInfo.getCurComDepartment().getId()); | 
 |  |  |         }else{ | 
 |  |  |             workorderRecord.setCreateUser(loginUserInfo.getId()); | 
 |  |  |             workorderRecord.setRootDepartId(loginUserInfo.getRootDepartment().getId()); | 
 |  |  |             workorderRecord.setDepartId(loginUserInfo.getCurComDepartment().getId()); | 
 |  |  |         } | 
 |  |  |         workorderRecord.setCreateTime(new Date()); | 
 |  |  |         workorderRecord.setNum(num); | 
 |  |  |         workorderRecord.setRootDepartId(loginUserInfo.getRootDepartment().getId()); | 
 |  |  |         workorderRecord.setDepartId(loginUserInfo.getCurComDepartment().getId()); | 
 |  |  |         workorderRecord.setPlanId(workorder.getPlanId()); | 
 |  |  |         workorderRecord.setWorkorderId(createWorkorderRecordDTO.getWorkorderId()); | 
 |  |  |         workorderRecord.setBatch(workorder.getBatch()); | 
 |  |  | 
 |  |  |         workorderRecord.setQualifiedNum(createWorkorderRecordDTO.getQualifiedNum()); | 
 |  |  |         //计算工资信息 | 
 |  |  |         SalaryParam salaryParam = salaryParamMapper.selectOne(new QueryWrapper<SalaryParam>().eq("DELETED",Constants.ZERO).eq("MATERIAL_ID",workorderRecord.getMaterialId()) | 
 |  |  |                 .eq("DEPART_ID",workorderRecord.getFactoryId()).eq("PROCEDURE_ID",workorderRecord.getProcedureId()).last(" limit 1  ")); | 
 |  |  |                 .eq("DEPART_ID",workorderRecord.getFactoryId()).eq("PROCEDURE_ID",workorderRecord.getProcedureId()).eq("TYPE",createWorkorderRecordDTO.getType()).last(" limit 1  ")); | 
 |  |  |         if(!Objects.isNull(salaryParam)){ | 
 |  |  |             workorderRecord.setSalaryPrice(salaryParam.getSalary()); | 
 |  |  |             workorderRecord.setSalaryType(salaryParam.getType()); | 
 |  |  | 
 |  |  |                 workorderRecord.setSalaryNum(salaryParam.getNum()); | 
 |  |  |                 workorderRecord.setSalary(salaryParam.getSalary().multiply(salaryParam.getUnqualified()==Constants.ONE?num:createWorkorderRecordDTO.getQualifiedNum())); | 
 |  |  |             }else{ | 
 |  |  |                 workorderRecord.setDuration(createWorkorderRecordDTO.getDuration()); | 
 |  |  |                 workorderRecord.setSalary(BigDecimal.valueOf(createWorkorderRecordDTO.getDuration()).multiply(salaryParam.getSalary()).divide(new BigDecimal(3600),BigDecimal.ROUND_HALF_UP)); | 
 |  |  |                 if(!Objects.isNull(createWorkorderRecordDTO.getDuration())&&Constants.formatIntegerNum(createWorkorderRecordDTO.getDuration())>Constants.ZERO){ | 
 |  |  |                     workorderRecord.setDuration(createWorkorderRecordDTO.getDuration()); | 
 |  |  |                     workorderRecord.setSalary(BigDecimal.valueOf(createWorkorderRecordDTO.getDuration()).multiply(salaryParam.getSalary()).divide(new BigDecimal(3600),BigDecimal.ROUND_HALF_UP)); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         //插入工单操作记录,并且判断是否更改为工单的状态【生产中】 |