| | |
| | | package com.ruoyi.production.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | |
| | | import com.ruoyi.basic.mapper.ProductModelMapper; |
| | | import com.ruoyi.basic.pojo.Product; |
| | | import com.ruoyi.basic.pojo.ProductModel; |
| | | import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum; |
| | | import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum; |
| | | import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import com.ruoyi.procurementrecord.mapper.ProcurementRecordOutMapper; |
| | | import com.ruoyi.procurementrecord.pojo.ProcurementRecordOut; |
| | | import com.ruoyi.procurementrecord.utils.StockUtils; |
| | | import com.ruoyi.production.controller.ProductWorkOrderController; |
| | | import com.ruoyi.production.dto.ProductStructureDto; |
| | | import com.ruoyi.production.dto.ProductionProductMainDto; |
| | | import com.ruoyi.production.mapper.*; |
| | |
| | | import com.ruoyi.production.service.ProductionProductMainService; |
| | | import com.ruoyi.project.system.domain.SysUser; |
| | | import com.ruoyi.project.system.mapper.SysUserMapper; |
| | | import com.ruoyi.project.system.service.ISysNoticeService; |
| | | import com.ruoyi.quality.mapper.*; |
| | | import com.ruoyi.quality.pojo.*; |
| | | import io.swagger.models.auth.In; |
| | | import com.ruoyi.quality.service.IQualityInspectService; |
| | | import com.ruoyi.stock.support.FinishedProductStockDimensionResolver; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import com.ruoyi.production.mapper.ProductionProductMainMapper; |
| | | import oshi.driver.mac.net.NetStat; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | @AllArgsConstructor |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public class ProductionProductMainServiceImpl extends ServiceImpl<ProductionProductMainMapper, ProductionProductMain> implements ProductionProductMainService { |
| | | |
| | | private final ProcurementRecordOutMapper procurementRecordOutMapper; |
| | | private static final String PROCESS_VOLTAGE_SORT = "电压分选"; |
| | | private static final String PROCESS_OPTICAL_INSPECTION = "光检外观"; |
| | | private static final String PROCESS_PACKAGING = "包装"; |
| | | private static final String INPUT_WEIGHT_PARAMETER = "投入重量/数量"; |
| | | private static final String INPUT_WEIGHT_FIELD = "inputWeight"; |
| | | private static final Object PRODUCT_MAIN_NO_LOCK = new Object(); |
| | | |
| | | private IQualityInspectService qualityInspectService; |
| | | private ProductionProductMainMapper productionProductMainMapper; |
| | | |
| | | private ProductWorkOrderController productWorkOrderController; |
| | | |
| | | private ProductWorkOrderMapper productWorkOrderMapper; |
| | | |
| | | private ProductProcessRouteItemMapper productProcessRouteItemMapper; |
| | | private SysUserMapper userMapper; |
| | | |
| | | private ProductionProductOutputMapper productionProductOutputMapper; |
| | | |
| | | private ProcessRouteItemMapper processRouteItemMapper; |
| | | |
| | | private ProductModelMapper productModelMapper; |
| | | |
| | | private QualityInspectMapper qualityInspectMapper; |
| | | |
| | | private QualityUnqualifiedMapper qualityUnqualifiedMapper; |
| | | private ProductProcessMapper productProcessMapper; |
| | | private ProductProcessRouteMapper productProcessRouteMapper; |
| | | |
| | | private ProductMapper productMapper; |
| | | |
| | | |
| | | private QualityTestStandardParamMapper qualityTestStandardParamMapper; |
| | | private QualityTestStandardBindingMapper qualityTestStandardBindingMapper; |
| | | |
| | | private QualityTestStandardMapper qualityTestStandardMapper; |
| | | private QualityInspectParamMapper qualityInspectParamMapper; |
| | | |
| | | private ProductStructureMapper productStructureMapper; |
| | | |
| | | private ProductionProductInputMapper productionProductInputMapper; |
| | | |
| | | private ProductOrderMapper productOrderMapper; |
| | | |
| | | private SalesLedgerProductionAccountingMapper salesLedgerProductionAccountingMapper; |
| | | |
| | | private StockUtils stockUtils; |
| | | |
| | | private FinishedProductStockDimensionResolver finishedProductStockDimensionResolver; |
| | | private ISysNoticeService sysNoticeService; |
| | | |
| | | @Override |
| | | public IPage<ProductionProductMainDto> listPageProductionProductMainDto(Page page, ProductionProductMainDto productionProductMainDto) { |
| | | return productionProductMainMapper.listPageProductionProductMainDto(page, productionProductMainDto); |
| | | } |
| | | |
| | | // 新增报工,并按照报工主流程处理投入扣库、产出、质检、入库、工单订单进度和核算数据 |
| | | @Override |
| | | public Boolean addProductMain(ProductionProductMainDto dto) { |
| | | SysUser user = userMapper.selectUserById(dto.getUserId()); |
| | | ProductionProductMain productionProductMain = new ProductionProductMain(); |
| | | //当前工艺路线对应的工序详情 |
| | | ProductProcessRouteItem productProcessRouteItem = productProcessRouteItemMapper.selectById(dto.getProductProcessRouteItemId()); |
| | | if (productProcessRouteItem == null) { |
| | | throw new RuntimeException("工艺路线项不存在"); |
| | | // 第一步:先校验报工入参,避免空值和非法数量进入后续流程 |
| | | if (dto == null) { |
| | | throw new ServiceException("报工参数不能为空"); |
| | | } |
| | | //当前具体工序 |
| | | if (dto.getProductProcessRouteItemId() == null) { |
| | | throw new ServiceException("工艺路线工序项不能为空"); |
| | | } |
| | | if (dto.getWorkOrderId() == null) { |
| | | throw new ServiceException("生产工单不能为空"); |
| | | } |
| | | |
| | | BigDecimal reportQty = dto.getQuantity(); |
| | | BigDecimal scrapQty = dto.getScrapQty() == null ? BigDecimal.ZERO : dto.getScrapQty(); |
| | | BigDecimal bomInputQty = dto.getInputWeight(); |
| | | if (bomInputQty == null) { |
| | | bomInputQty = resolveInputWeight(dto.getOtherData()); |
| | | } |
| | | if (reportQty == null || reportQty.compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw new ServiceException("报工数量必须大于0"); |
| | | } |
| | | if (scrapQty.compareTo(BigDecimal.ZERO) < 0) { |
| | | throw new ServiceException("报废数量不能小于0"); |
| | | } |
| | | if (scrapQty.compareTo(reportQty) > 0) { |
| | | throw new ServiceException("报废数量不能大于报工数量"); |
| | | } |
| | | if (bomInputQty == null || bomInputQty.compareTo(BigDecimal.ZERO) < 0) { |
| | | throw new ServiceException("产品结构投入数量不能小于0"); |
| | | } |
| | | |
| | | // 第二步:加载当前工序、工单、工艺路线和订单数据,并校验基础关联关系 |
| | | ProductProcessRouteItem productProcessRouteItem = |
| | | productProcessRouteItemMapper.selectById(dto.getProductProcessRouteItemId()); |
| | | if (productProcessRouteItem == null) { |
| | | throw new ServiceException("工艺路线工序项不存在"); |
| | | } |
| | | |
| | | ProductProcess productProcess = productProcessMapper.selectById(productProcessRouteItem.getProcessId()); |
| | | //工艺路线中当前工序对应的产出规格型号 |
| | | if (productProcess == null) { |
| | | throw new ServiceException("当前工序不存在"); |
| | | } |
| | | |
| | | ProductModel productModel = productModelMapper.selectById(productProcessRouteItem.getProductModelId()); |
| | | //查询该生产订单对应的bom |
| | | ProductProcessRoute productProcessRoute = productProcessRouteMapper.selectById(productProcessRouteItem.getProductRouteId()); |
| | | /*新增报工主表*/ |
| | | //查询最大报工编号 |
| | | String datePrefix = "BG" + LocalDate.now().format(DateTimeFormatter.ofPattern("yyMMdd")); |
| | | QueryWrapper<ProductionProductMain> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.select("MAX(product_no) as maxNo") |
| | | .likeRight("product_no", datePrefix); |
| | | List<Map<String, Object>> resultList = productionProductMainMapper.selectMaps(queryWrapper); |
| | | int sequenceNumber = 1; |
| | | if (resultList != null && !resultList.isEmpty()) { |
| | | Map<String, Object> result = resultList.get(0); |
| | | if (result != null) { |
| | | Object maxNoObj = result.get("maxNo"); |
| | | if (maxNoObj != null) { |
| | | String lastNo = maxNoObj.toString(); |
| | | System.out.println("lastNo: " + lastNo); |
| | | if (productModel == null) { |
| | | throw new ServiceException("当前工序对应的产品型号不存在"); |
| | | } |
| | | |
| | | ProductProcessRoute productProcessRoute = |
| | | productProcessRouteMapper.selectById(productProcessRouteItem.getProductRouteId()); |
| | | if (productProcessRoute == null) { |
| | | throw new ServiceException("工艺路线不存在"); |
| | | } |
| | | |
| | | ProductWorkOrder productWorkOrder = productWorkOrderMapper.selectById(dto.getWorkOrderId()); |
| | | if (productWorkOrder == null) { |
| | | throw new ServiceException("生产工单不存在"); |
| | | } |
| | | if (!Objects.equals(productWorkOrder.getProductProcessRouteItemId(), productProcessRouteItem.getId())) { |
| | | throw new ServiceException("生产工单与当前工序不匹配"); |
| | | } |
| | | |
| | | ProductOrder productOrder = productOrderMapper.selectById(productWorkOrder.getProductOrderId()); |
| | | if (productOrder == null) { |
| | | throw new ServiceException("生产订单不存在"); |
| | | } |
| | | |
| | | // 第三步:校验工序流转逻辑,必须是同一订单的上一道工序已报工且前序无未解除隔离记录 |
| | | Integer currentDragSort = productProcessRouteItem.getDragSort(); |
| | | if (currentDragSort != null && currentDragSort > 1) { |
| | | boolean isPreviousReported = productionProductMainMapper.checkPreviousProcessReported( |
| | | productWorkOrder.getId(), |
| | | currentDragSort |
| | | ); |
| | | if (!isPreviousReported) { |
| | | throw new ServiceException("上一道工序尚未报工,当前工序不能报工"); |
| | | } |
| | | |
| | | ProductProcessRouteItem previousRouteItem = productProcessRouteItemMapper.selectOne( |
| | | Wrappers.<ProductProcessRouteItem>lambdaQuery() |
| | | .eq(ProductProcessRouteItem::getProductRouteId, productProcessRouteItem.getProductRouteId()) |
| | | .eq(ProductProcessRouteItem::getDragSort, currentDragSort - 1) |
| | | ); |
| | | if (previousRouteItem == null) { |
| | | throw new ServiceException("上一道工序不存在"); |
| | | } |
| | | |
| | | ProductWorkOrder previousWorkOrder = productWorkOrderMapper.selectOne( |
| | | Wrappers.<ProductWorkOrder>lambdaQuery() |
| | | .eq(ProductWorkOrder::getProductOrderId, productOrder.getId()) |
| | | .eq(ProductWorkOrder::getProductProcessRouteItemId, previousRouteItem.getId()) |
| | | ); |
| | | if (previousWorkOrder == null) { |
| | | throw new ServiceException("上一道工序工单不存在"); |
| | | } |
| | | |
| | | BigDecimal currentReportedQty = BigDecimal.ZERO; |
| | | List<ProductionProductMain> currentMainList = productionProductMainMapper.selectList( |
| | | Wrappers.<ProductionProductMain>lambdaQuery() |
| | | .eq(ProductionProductMain::getWorkOrderId, productWorkOrder.getId()) |
| | | ); |
| | | if (CollectionUtils.isNotEmpty(currentMainList)) { |
| | | List<Long> currentMainIds = currentMainList.stream().map(ProductionProductMain::getId).collect(Collectors.toList()); |
| | | List<ProductionProductOutput> currentOutputList = productionProductOutputMapper.selectList( |
| | | Wrappers.<ProductionProductOutput>lambdaQuery() |
| | | .in(ProductionProductOutput::getProductMainId, currentMainIds) |
| | | ); |
| | | currentReportedQty = currentOutputList.stream() |
| | | .map(ProductionProductOutput::getQuantity) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | } |
| | | |
| | | BigDecimal previousCompleteQty = |
| | | previousWorkOrder.getCompleteQuantity() == null ? BigDecimal.ZERO : previousWorkOrder.getCompleteQuantity(); |
| | | if (currentReportedQty.add(reportQty).compareTo(previousCompleteQty) > 0) { |
| | | throw new ServiceException("本次报工数量超过上道工序可流转数量"); |
| | | } |
| | | |
| | | List<ProductProcessRouteItem> previousItems = productProcessRouteItemMapper.selectList( |
| | | Wrappers.<ProductProcessRouteItem>lambdaQuery() |
| | | .eq(ProductProcessRouteItem::getProductRouteId, productProcessRouteItem.getProductRouteId()) |
| | | .lt(ProductProcessRouteItem::getDragSort, currentDragSort) |
| | | ); |
| | | |
| | | if (CollectionUtils.isNotEmpty(previousItems)) { |
| | | List<String> previousProcessNames = new ArrayList<>(); |
| | | for (ProductProcessRouteItem item : previousItems) { |
| | | ProductProcess process = productProcessMapper.selectById(item.getProcessId()); |
| | | if (process != null && StringUtils.isNotBlank(process.getName())) { |
| | | previousProcessNames.add(process.getName().trim()); |
| | | } |
| | | } |
| | | |
| | | if (CollectionUtils.isNotEmpty(previousProcessNames)) { |
| | | List<QualityUnqualified> unqualifiedList = |
| | | qualityUnqualifiedMapper.selectUnqualifiedByProductOrderAndProcessNames(productOrder.getId(), previousProcessNames); |
| | | if (CollectionUtils.isNotEmpty(unqualifiedList)) { |
| | | throw new ServiceException("前序工序存在隔离记录,当前工序不能报工"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 第四步:控制当前工单总报工量不能超过计划量 |
| | | BigDecimal currentWorkOrderReportedQty = BigDecimal.ZERO; |
| | | List<ProductionProductMain> workOrderMainList = productionProductMainMapper.selectList( |
| | | Wrappers.<ProductionProductMain>lambdaQuery() |
| | | .eq(ProductionProductMain::getWorkOrderId, productWorkOrder.getId()) |
| | | ); |
| | | if (CollectionUtils.isNotEmpty(workOrderMainList)) { |
| | | List<Long> workOrderMainIds = workOrderMainList.stream().map(ProductionProductMain::getId).collect(Collectors.toList()); |
| | | List<ProductionProductOutput> workOrderOutputList = productionProductOutputMapper.selectList( |
| | | Wrappers.<ProductionProductOutput>lambdaQuery() |
| | | .in(ProductionProductOutput::getProductMainId, workOrderMainIds) |
| | | ); |
| | | currentWorkOrderReportedQty = workOrderOutputList.stream() |
| | | .map(ProductionProductOutput::getQuantity) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | } |
| | | if (productWorkOrder.getPlanQuantity() != null |
| | | && currentWorkOrderReportedQty.add(reportQty).compareTo(productWorkOrder.getPlanQuantity()) > 0) { |
| | | throw new ServiceException("本次报工数量超过工单可报数量"); |
| | | } |
| | | |
| | | // 第五步:生成报工单号并确定报工人信息 |
| | | String productNo; |
| | | synchronized (PRODUCT_MAIN_NO_LOCK) { |
| | | String datePrefix = "BG" + LocalDate.now().format(DateTimeFormatter.ofPattern("yyMMdd")); |
| | | QueryWrapper<ProductionProductMain> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.select("MAX(product_no) as maxNo") |
| | | .likeRight("product_no", datePrefix); |
| | | List<Map<String, Object>> resultList = productionProductMainMapper.selectMaps(queryWrapper); |
| | | |
| | | int sequenceNumber = 1; |
| | | if (CollectionUtils.isNotEmpty(resultList)) { |
| | | Map<String, Object> result = resultList.get(0); |
| | | if (result != null && result.get("maxNo") != null) { |
| | | String lastNo = result.get("maxNo").toString(); |
| | | if (lastNo.startsWith(datePrefix)) { |
| | | try { |
| | | String seqStr = lastNo.substring(datePrefix.length()); |
| | | sequenceNumber = Integer.parseInt(seqStr) + 1; |
| | | sequenceNumber = Integer.parseInt(lastNo.substring(datePrefix.length())) + 1; |
| | | } catch (NumberFormatException e) { |
| | | sequenceNumber = 1; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | productNo = String.format("%s%03d", datePrefix, sequenceNumber); |
| | | } |
| | | String productNo = String.format("%s%03d", datePrefix, sequenceNumber); |
| | | |
| | | Long userId = dto.getUserId(); |
| | | String userName = dto.getUserName(); |
| | | if (userId == null) { |
| | | userId = SecurityUtils.getLoginUser().getUserId(); |
| | | userName = SecurityUtils.getLoginUser().getNickName(); |
| | | } else if (StringUtils.isBlank(userName)) { |
| | | SysUser user = userMapper.selectUserById(userId); |
| | | userName = user == null ? null : user.getNickName(); |
| | | } |
| | | |
| | | // 第六步:先写报工主表 |
| | | ProductionProductMain productionProductMain = new ProductionProductMain(); |
| | | productionProductMain.setProductNo(productNo); |
| | | productionProductMain.setUserId(dto.getUserId()); |
| | | productionProductMain.setUserName(dto.getUserName()); |
| | | productionProductMain.setUserId(userId); |
| | | productionProductMain.setUserName(userName); |
| | | productionProductMain.setProductProcessRouteItemId(dto.getProductProcessRouteItemId()); |
| | | productionProductMain.setWorkOrderId(dto.getWorkOrderId()); |
| | | productionProductMain.setStatus(0); |
| | | productionProductMainMapper.insert(productionProductMain); |
| | | /*新增报工投入表*/ |
| | | List<ProductStructureDto> productStructureDtos = productStructureMapper.listBybomAndProcess(productProcessRoute.getBomId(), productProcess.getId()); |
| | | if (productStructureDtos.size() == 0) { |
| | | //如果该工序没有产品结构的投入品,那这个投入品和产出品是同一个 |
| | | |
| | | // 第七步:根据 BOM 生成投入记录并扣减原料库存 |
| | | List<ProductStructureDto> productStructureDtos = |
| | | productStructureMapper.listBybomAndProcess(productProcessRoute.getBomId(), productProcess.getId()); |
| | | |
| | | if (CollectionUtils.isEmpty(productStructureDtos)) { |
| | | ProductStructureDto productStructureDto = new ProductStructureDto(); |
| | | productStructureDto.setProductModelId(productProcessRouteItem.getProductModelId()); |
| | | productStructureDto.setUnitQuantity(BigDecimal.ONE); |
| | | productStructureDtos.add(productStructureDto); |
| | | } |
| | | |
| | | Set<Long> parentIds = productStructureDtos.stream() |
| | | .map(ProductStructureDto::getParentId) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | Map<Long, ProductStructureDto> parentMap = new HashMap<>(); |
| | | if (CollectionUtils.isNotEmpty(parentIds)) { |
| | | parentMap = productStructureMapper.selectByIds(parentIds) |
| | | .stream() |
| | | .collect(Collectors.toMap(ProductStructureDto::getId, Function.identity(), (a, b) -> a)); |
| | | } |
| | | |
| | | // 第七步-1:投入数量强制取前端传入的 bomInputQty |
| | | BigDecimal inputBaseQty = bomInputQty; |
| | | |
| | | for (ProductStructureDto productStructureDto : productStructureDtos) { |
| | | ProductModel productModel1 = productModelMapper.selectById(productStructureDto.getProductModelId()); |
| | | Product product = productMapper.selectById(productModel1.getProductId()); |
| | | BigDecimal stockQuantity = stockUtils.getStockQuantity(productModel1.getId()).get("stockQuantity"); |
| | | if (!(stockQuantity.compareTo(BigDecimal.ZERO) > 0)) { |
| | | throw new RuntimeException(product.getProductName() + "库存为0"); |
| | | if (productStructureDto.getProductModelId() == null) { |
| | | throw new ServiceException("投入物料产品型号不能为空"); |
| | | } |
| | | if (stockQuantity.compareTo(productStructureDto.getUnitQuantity().multiply(dto.getQuantity())) < 0) { |
| | | throw new RuntimeException(product.getProductName() + "库存不足"); |
| | | |
| | | BigDecimal childQty = productStructureDto.getUnitQuantity(); |
| | | if (childQty == null || childQty.compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw new ServiceException("投入物料用量必须大于0"); |
| | | } |
| | | |
| | | BigDecimal parentQty = BigDecimal.ONE; |
| | | if (productStructureDto.getParentId() != null) { |
| | | ProductStructureDto parent = parentMap.get(productStructureDto.getParentId()); |
| | | if (parent != null) { |
| | | parentQty = parent.getUnitQuantity(); |
| | | } |
| | | } |
| | | |
| | | if (parentQty == null || parentQty.compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw new ServiceException("父级物料用量必须大于0"); |
| | | } |
| | | |
| | | BigDecimal needQty = inputBaseQty; |
| | | |
| | | ProductionProductInput productionProductInput = new ProductionProductInput(); |
| | | productionProductInput.setProductModelId(productStructureDto.getProductModelId()); |
| | | productionProductInput.setQuantity(productStructureDto.getUnitQuantity().multiply(dto.getQuantity())); |
| | | productionProductInput.setQuantity(needQty == null ? BigDecimal.ZERO : needQty); |
| | | productionProductInput.setProductMainId(productionProductMain.getId()); |
| | | productionProductInputMapper.insert(productionProductInput); |
| | | //对应的库存出库 |
| | | DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| | | LocalDate now = LocalDate.now(); |
| | | ProcurementRecordOut procurementRecordOut1 = procurementRecordOutMapper.selectCode(dateFormat.format(now)); |
| | | Long aLong = procurementRecordOut1 == null ? 1L : Long.valueOf(procurementRecordOut1.getCode().split("LS" + dateFormat.format(now))[1]); |
| | | ProcurementRecordOut.ProcurementRecordOutBuilder procurementRecordOut = ProcurementRecordOut.builder() |
| | | .procurementRecordStorageId(0) |
| | | .code("LS" + dateFormat.format(now) + String.format("%03d", aLong + 1)) |
| | | .salesLedgerProductId(productionProductMain.getId())//关联报工产出 |
| | | .inboundBatches(aLong.equals(0L) ? "第1批次" : "第" + (aLong + 1) + "批次") |
| | | .inboundNum(productionProductInput.getQuantity()) |
| | | .type(4) |
| | | .createBy(user.getNickName()) |
| | | .productModelId(productModel1.getId()); |
| | | procurementRecordOutMapper.insert(procurementRecordOut.build()); |
| | | |
| | | stockUtils.substractStock( |
| | | productStructureDto.getProductModelId(), |
| | | needQty, |
| | | StockOutQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_OUT.getCode(), |
| | | productionProductMain.getId(), |
| | | null, |
| | | null |
| | | ); |
| | | } |
| | | /*新增报工产出表*/ |
| | | |
| | | // 第八步:写产出记录并计算本次合格数量 |
| | | ProductionProductOutput productionProductOutput = new ProductionProductOutput(); |
| | | productionProductOutput.setProductMainId(productionProductMain.getId()); |
| | | productionProductOutput.setProductModelId(productProcessRouteItem.getProductModelId()); |
| | | productionProductOutput.setQuantity(dto.getQuantity() != null ? dto.getQuantity() : BigDecimal.ZERO); |
| | | productionProductOutput.setQuantity(reportQty); |
| | | productionProductOutput.setScrapQty(scrapQty); |
| | | productionProductOutput.setOtherData(dto.getOtherData() == null ? "" : dto.getOtherData()); |
| | | productionProductOutputMapper.insert(productionProductOutput); |
| | | //对应的过程检或者出厂检 |
| | | List<ProductProcessRouteItem> productProcessRouteItems = productProcessRouteItemMapper.selectList(Wrappers.<ProductProcessRouteItem>lambdaQuery().eq(ProductProcessRouteItem::getProductRouteId, productProcessRouteItem.getProductRouteId())); |
| | | int inspectType = 1; |
| | | if (productProcessRouteItem.getDragSort()==productProcessRouteItems.size()){ |
| | | //最后一道工序生成出厂检 |
| | | inspectType = 2; |
| | | } |
| | | Product product = productMapper.selectById(productModel.getProductId()); |
| | | QualityInspect qualityInspect = new QualityInspect(); |
| | | qualityInspect.setProductId(product.getId()); |
| | | qualityInspect.setProductName(product.getProductName()); |
| | | qualityInspect.setModel(productModel.getModel()); |
| | | qualityInspect.setUnit(productModel.getUnit()); |
| | | qualityInspect.setQuantity(dto.getQuantity()); |
| | | qualityInspect.setProcess(productProcess.getName()); |
| | | qualityInspect.setInspectState(0); |
| | | qualityInspect.setInspectType(inspectType); |
| | | qualityInspect.setProductMainId(productionProductMain.getId()); |
| | | qualityInspect.setProductModelId(productModel.getId()); |
| | | qualityInspectMapper.insert(qualityInspect); |
| | | List<QualityTestStandardBinding> qualityTestStandardBindings = qualityTestStandardBindingMapper.selectList( |
| | | new LambdaQueryWrapper<QualityTestStandardBinding>() |
| | | .eq(QualityTestStandardBinding::getProductId, product.getId())); |
| | | if (qualityTestStandardBindings.size()>0){ |
| | | qualityTestStandardParamMapper.selectList(Wrappers.<QualityTestStandardParam>lambdaQuery() |
| | | .eq(QualityTestStandardParam::getTestStandardId,qualityTestStandardBindings.get(0).getTestStandardId())) |
| | | .forEach(qualityTestStandardParam -> { |
| | | QualityInspectParam param = new QualityInspectParam(); |
| | | BeanUtils.copyProperties(qualityTestStandardParam, param); |
| | | param.setId(null); |
| | | param.setInspectId(qualityInspect.getId()); |
| | | qualityInspectParamMapper.insert(param); |
| | | }); |
| | | } |
| | | /*更新工单和生产订单*/ |
| | | ProductWorkOrder productWorkOrder = productWorkOrderMapper.selectById(dto.getWorkOrderId()); |
| | | productWorkOrder.setCompleteQuantity(productWorkOrder.getCompleteQuantity().add(dto.getQuantity())); |
| | | if (ObjectUtils.isNull(productWorkOrder.getActualStartTime())){ |
| | | productWorkOrder.setActualStartTime(LocalDate.now());//实际开始时间 |
| | | } |
| | | if (productWorkOrder.getCompleteQuantity().compareTo(productWorkOrder.getPlanQuantity()) == 0){ |
| | | productWorkOrder.setActualEndTime(LocalDate.now());//实际结束时间 |
| | | } |
| | | productWorkOrderMapper.updateById(productWorkOrder); |
| | | //生产订单 |
| | | ProductOrder productOrder = productOrderMapper.selectById(productWorkOrder.getProductOrderId()); |
| | | if (ObjectUtils.isNull(productOrder.getStartTime())){ |
| | | productOrder.setStartTime(LocalDateTime.now());//开始时间 |
| | | } |
| | | if (productProcessRouteItem.getDragSort()==productProcessRouteItems.size()){ |
| | | //如果是最后一道工序报工之后生产订单完成数量+ |
| | | productOrder.setCompleteQuantity(productOrder.getCompleteQuantity().add(dto.getQuantity())); |
| | | if (productOrder.getCompleteQuantity().compareTo(productOrder.getQuantity()) == 0){ |
| | | productOrder.setEndTime(LocalDateTime.now());//结束时间 |
| | | |
| | | BigDecimal qualifiedQty = reportQty.subtract(scrapQty); |
| | | |
| | | List<ProductProcessRouteItem> productProcessRouteItems = productProcessRouteItemMapper.selectList( |
| | | Wrappers.<ProductProcessRouteItem>lambdaQuery() |
| | | .eq(ProductProcessRouteItem::getProductRouteId, productProcessRouteItem.getProductRouteId()) |
| | | ); |
| | | |
| | | Integer maxDragSort = productProcessRouteItems.stream() |
| | | .map(ProductProcessRouteItem::getDragSort) |
| | | .filter(Objects::nonNull) |
| | | .max(Integer::compareTo) |
| | | .orElse(null); |
| | | |
| | | boolean isRouteLastProcess = Objects.equals(productProcessRouteItem.getDragSort(), maxDragSort); |
| | | ReportStockRule reportStockRule = |
| | | resolveReportStockRule(productProcessRouteItem, productProcess, productProcessRouteItems); |
| | | |
| | | // 第九步:如果有合格数量,则根据是否质检决定走质检或直接入库 |
| | | if (qualifiedQty.compareTo(BigDecimal.ZERO) > 0) { |
| | | if (Boolean.TRUE.equals(productProcessRouteItem.getIsQuality())) { |
| | | Product product = productMapper.selectById(productModel.getProductId()); |
| | | if (product == null) { |
| | | throw new ServiceException("质检产品不存在"); |
| | | } |
| | | |
| | | int inspectType = 1; |
| | | String process = productProcess.getName(); |
| | | |
| | | if (reportStockRule.finishedGoodsStockIn) { |
| | | inspectType = 2; |
| | | process = null; |
| | | } |
| | | |
| | | QualityInspect qualityInspect = new QualityInspect(); |
| | | qualityInspect.setProductId(product.getId()); |
| | | qualityInspect.setProductName(product.getProductName()); |
| | | qualityInspect.setModel(productModel.getModel()); |
| | | qualityInspect.setUnit(productModel.getUnit()); |
| | | qualityInspect.setQuantity(reportQty); |
| | | qualityInspect.setProcess(process); |
| | | qualityInspect.setInspectState(0); |
| | | qualityInspect.setInspectType(inspectType); |
| | | qualityInspect.setDefectiveQuantity(scrapQty); |
| | | qualityInspect.setProductMainId(productionProductMain.getId()); |
| | | qualityInspect.setProductModelId(productModel.getId()); |
| | | qualityInspectMapper.insert(qualityInspect); |
| | | |
| | | List<QualityTestStandard> qualityTestStandardList = |
| | | qualityTestStandardMapper.getQualityTestStandardByProductId(product.getId(), inspectType, process); |
| | | |
| | | if (CollectionUtils.isNotEmpty(qualityTestStandardList)) { |
| | | QualityTestStandard qualityTestStandard = qualityTestStandardList.get(0); |
| | | qualityInspect.setTestStandardId(qualityTestStandard.getId()); |
| | | qualityInspectMapper.updateById(qualityInspect); |
| | | |
| | | qualityTestStandardParamMapper.selectList( |
| | | Wrappers.<QualityTestStandardParam>lambdaQuery() |
| | | .eq(QualityTestStandardParam::getTestStandardId, qualityTestStandard.getId()) |
| | | ).forEach(qualityTestStandardParam -> { |
| | | QualityInspectParam param = new QualityInspectParam(); |
| | | BeanUtils.copyProperties(qualityTestStandardParam, param); |
| | | param.setId(null); |
| | | param.setInspectId(qualityInspect.getId()); |
| | | qualityInspectParamMapper.insert(param); |
| | | }); |
| | | } |
| | | } else if (reportStockRule.createStockIn) { |
| | | //成品入库需要电压,工序类别(铜,银) |
| | | if (reportStockRule.finishedGoodsStockIn) { |
| | | String processCategory = |
| | | finishedProductStockDimensionResolver.resolveProcessCategory(productionProductMain.getId()); |
| | | String voltage = |
| | | finishedProductStockDimensionResolver.resolveVoltage(productionProductMain.getId()); |
| | | |
| | | stockUtils.addStock( |
| | | productProcessRouteItem.getProductModelId(), |
| | | qualifiedQty, |
| | | StockInQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_IN.getCode(), |
| | | productionProductMain.getId(), |
| | | processCategory, |
| | | voltage |
| | | ); |
| | | } else { |
| | | stockUtils.addStockNoReview( |
| | | productProcessRouteItem.getProductModelId(), |
| | | qualifiedQty, |
| | | StockInQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_IN.getCode(), |
| | | productionProductMain.getId() |
| | | ); |
| | | } |
| | | } |
| | | |
| | | // 第十步:更新工单、订单完成进度和计件核算数据 |
| | | BigDecimal workOrderCompleteQuantity = |
| | | productWorkOrder.getCompleteQuantity() == null ? BigDecimal.ZERO : productWorkOrder.getCompleteQuantity(); |
| | | productWorkOrder.setCompleteQuantity(workOrderCompleteQuantity.add(qualifiedQty)); |
| | | |
| | | if (ObjectUtils.isNull(productWorkOrder.getActualStartTime())) { |
| | | productWorkOrder.setActualStartTime(LocalDate.now()); |
| | | } |
| | | if (productWorkOrder.getPlanQuantity() != null |
| | | && productWorkOrder.getCompleteQuantity().compareTo(productWorkOrder.getPlanQuantity()) >= 0) { |
| | | productWorkOrder.setActualEndTime(LocalDate.now()); |
| | | } |
| | | productWorkOrderMapper.updateById(productWorkOrder); |
| | | |
| | | if (ObjectUtils.isNull(productOrder.getStartTime())) { |
| | | productOrder.setStartTime(LocalDateTime.now()); |
| | | } |
| | | |
| | | if (isRouteLastProcess) { |
| | | BigDecimal orderCompleteQuantity = |
| | | productOrder.getCompleteQuantity() == null ? BigDecimal.ZERO : productOrder.getCompleteQuantity(); |
| | | productOrder.setCompleteQuantity(orderCompleteQuantity.add(qualifiedQty)); |
| | | |
| | | if (productOrder.getQuantity() != null |
| | | && productOrder.getCompleteQuantity().compareTo(productOrder.getQuantity()) >= 0) { |
| | | productOrder.setEndTime(LocalDateTime.now()); |
| | | } |
| | | } |
| | | productOrderMapper.updateById(productOrder); |
| | | |
| | | SalesLedgerProductionAccounting salesLedgerProductionAccounting = |
| | | SalesLedgerProductionAccounting.builder() |
| | | .productMainId(productionProductMain.getId()) |
| | | .schedulingUserId(userId) |
| | | .schedulingUserName(userName) |
| | | .finishedNum(qualifiedQty) |
| | | .workHours(productProcess.getSalaryQuota()) |
| | | .process(productProcess.getName()) |
| | | .schedulingDate(LocalDate.now()) |
| | | .tenantId(dto.getTenantId()) |
| | | .build(); |
| | | salesLedgerProductionAccountingMapper.insert(salesLedgerProductionAccounting); |
| | | } |
| | | productOrderMapper.updateById(productOrder); |
| | | /*添加生产核算*/ |
| | | SalesLedgerProductionAccounting salesLedgerProductionAccounting = SalesLedgerProductionAccounting.builder() |
| | | .salesLedgerWorkId(productionProductMain.getId()) |
| | | .salesLedgerSchedulingId(0L) |
| | | .salesLedgerId(productOrder.getSalesLedgerId()) |
| | | .salesLedgerProductId(productOrder.getProductModelId()) |
| | | .schedulingUserId(user.getUserId()) |
| | | .schedulingUserName(user.getNickName()) |
| | | .finishedNum(dto.getQuantity() != null ? dto.getQuantity() : BigDecimal.ZERO) |
| | | .workHours(productProcess.getSalaryQuota()) |
| | | .process(productProcess.getName()) |
| | | .schedulingDate(LocalDate.now()) |
| | | .tenantId(dto.getTenantId()) |
| | | .build(); |
| | | salesLedgerProductionAccountingMapper.insert(salesLedgerProductionAccounting); |
| | | |
| | | // 第十一步:统一处理报废数量入不合格库存,避免在主流程中重复入库 |
| | | handleScrapStock(productModel.getId(), scrapQty, isRouteLastProcess, productionProductMain.getId()); |
| | | |
| | | return true; |
| | | } |
| | | |
| | | // 判断当前报工是否需要入库,以及是否按成品入库处理 |
| | | private ReportStockRule resolveReportStockRule(ProductProcessRouteItem currentRouteItem, |
| | | ProductProcess currentProcess, |
| | | List<ProductProcessRouteItem> routeItems) { |
| | | Integer maxDragSort = CollectionUtils.isEmpty(routeItems) |
| | | ? null |
| | | : routeItems.stream() |
| | | .map(ProductProcessRouteItem::getDragSort) |
| | | .filter(Objects::nonNull) |
| | | .max(Integer::compareTo) |
| | | .orElse(null); |
| | | |
| | | boolean isRouteLastProcess = Objects.equals(currentRouteItem.getDragSort(), maxDragSort); |
| | | String currentProcessName = currentProcess == null || currentProcess.getName() == null |
| | | ? "" |
| | | : currentProcess.getName().trim(); |
| | | |
| | | if (PROCESS_VOLTAGE_SORT.equals(currentProcessName)) { |
| | | return new ReportStockRule(false, false); |
| | | } |
| | | |
| | | Set<Long> processIds = CollectionUtils.isEmpty(routeItems) |
| | | ? Collections.emptySet() |
| | | : routeItems.stream() |
| | | .map(ProductProcessRouteItem::getProcessId) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | Map<Long, String> processNameMap = new HashMap<>(); |
| | | if (CollectionUtils.isNotEmpty(processIds)) { |
| | | processNameMap = productProcessMapper.selectBatchIds(processIds) |
| | | .stream() |
| | | .collect(Collectors.toMap( |
| | | ProductProcess::getId, |
| | | process -> process.getName() == null ? "" : process.getName().trim(), |
| | | (a, b) -> a |
| | | )); |
| | | } |
| | | |
| | | Set<String> routeProcessNames = CollectionUtils.isEmpty(routeItems) |
| | | ? Collections.emptySet() |
| | | : routeItems.stream() |
| | | .map(ProductProcessRouteItem::getProcessId) |
| | | .map(processNameMap::get) |
| | | .filter(StringUtils::isNotBlank) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | boolean hasVoltageSort = routeProcessNames.contains(PROCESS_VOLTAGE_SORT); |
| | | boolean hasOpticalInspection = routeProcessNames.contains(PROCESS_OPTICAL_INSPECTION); |
| | | boolean hasPackaging = routeProcessNames.contains(PROCESS_PACKAGING); |
| | | |
| | | if (hasPackaging && PROCESS_PACKAGING.equals(currentProcessName)) { |
| | | return new ReportStockRule(true, true); |
| | | } |
| | | |
| | | if (hasPackaging && PROCESS_OPTICAL_INSPECTION.equals(currentProcessName)) { |
| | | return new ReportStockRule(false, false); |
| | | } |
| | | |
| | | if (!hasPackaging |
| | | && hasVoltageSort |
| | | && hasOpticalInspection |
| | | && PROCESS_OPTICAL_INSPECTION.equals(currentProcessName)) { |
| | | return new ReportStockRule(true, true); |
| | | } |
| | | |
| | | return new ReportStockRule(true, isRouteLastProcess); |
| | | } |
| | | |
| | | // 统一处理报废数量入不合格库存,避免重复入库 |
| | | private void handleScrapStock(Long productModelId, |
| | | BigDecimal scrapQty, |
| | | boolean isRouteLastProcess, |
| | | Long productMainId) { |
| | | if (productModelId == null) { |
| | | throw new ServiceException("报废产品型号不能为空"); |
| | | } |
| | | |
| | | if (scrapQty == null || scrapQty.compareTo(BigDecimal.ZERO) <= 0) { |
| | | return; |
| | | } |
| | | |
| | | if (isRouteLastProcess) { |
| | | stockUtils.addUnStock( |
| | | productModelId, |
| | | scrapQty, |
| | | StockInUnQualifiedRecordTypeEnum.PRODUCTION_SCRAP.getCode(), |
| | | productMainId |
| | | ); |
| | | } else { |
| | | stockUtils.addUnStockNoReview( |
| | | productModelId, |
| | | scrapQty, |
| | | StockInUnQualifiedRecordTypeEnum.PRODUCTION_SCRAP.getCode(), |
| | | productMainId |
| | | ); |
| | | } |
| | | } |
| | | |
| | | // 单次报工对应的入库规则 |
| | | private static final class ReportStockRule { |
| | | private final boolean createStockIn; |
| | | private final boolean finishedGoodsStockIn; |
| | | |
| | | private ReportStockRule(boolean createStockIn, boolean finishedGoodsStockIn) { |
| | | this.createStockIn = createStockIn; |
| | | this.finishedGoodsStockIn = finishedGoodsStockIn; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public Boolean removeProductMain(ProductionProductMainDto dto) { |
| | | ProductionProductMain productionProductMain = productionProductMainMapper.selectById(dto.getId()); |
| | | //该报工对应的工艺路线详情 |
| | | public Boolean removeProductMain(Long id) { |
| | | // 删除报工前先检查是否已经完成不合格处理 |
| | | List<QualityInspect> qualityInspects = qualityInspectMapper.selectList( |
| | | Wrappers.<QualityInspect>lambdaQuery().eq(QualityInspect::getProductMainId, id) |
| | | ); |
| | | if (!qualityInspects.isEmpty()) { |
| | | List<QualityUnqualified> qualityUnqualifieds = qualityUnqualifiedMapper.selectList( |
| | | Wrappers.<QualityUnqualified>lambdaQuery() |
| | | .in(QualityUnqualified::getInspectId, qualityInspects.stream().map(QualityInspect::getId).collect(Collectors.toList())) |
| | | ); |
| | | if (!qualityUnqualifieds.isEmpty() && qualityUnqualifieds.get(0).getInspectState() == 1) { |
| | | throw new ServiceException("该报工已完成不合格处理,不能删除"); |
| | | } |
| | | } |
| | | |
| | | ProductionProductMain productionProductMain = productionProductMainMapper.selectById(id); |
| | | ProductProcessRouteItem productProcessRouteItem = productProcessRouteItemMapper.selectById(productionProductMain.getProductProcessRouteItemId()); |
| | | ProductionProductOutput productionProductOutput = productionProductOutputMapper.selectList(Wrappers.<ProductionProductOutput>lambdaQuery().eq(ProductionProductOutput::getProductMainId, productionProductMain.getId())).get(0); |
| | | /*删除核算*/ |
| | | ProductionProductOutput productionProductOutput = productionProductOutputMapper.selectList( |
| | | Wrappers.<ProductionProductOutput>lambdaQuery().eq(ProductionProductOutput::getProductMainId, productionProductMain.getId()) |
| | | ).get(0); |
| | | |
| | | salesLedgerProductionAccountingMapper.delete( |
| | | new LambdaQueryWrapper<SalesLedgerProductionAccounting>() |
| | | .eq(SalesLedgerProductionAccounting::getSalesLedgerWorkId, productionProductMain.getId()) |
| | | .eq(SalesLedgerProductionAccounting::getProductMainId, productionProductMain.getId()) |
| | | ); |
| | | /*更新工单和生产订单*/ |
| | | |
| | | ProductWorkOrder productWorkOrder = productWorkOrderMapper.selectById(productionProductMain.getWorkOrderId()); |
| | | productWorkOrder.setCompleteQuantity(productWorkOrder.getCompleteQuantity().subtract(productionProductOutput.getQuantity())); |
| | | productWorkOrder.setActualEndTime(null); |
| | | productWorkOrderMapper.updateById(productWorkOrder); |
| | | //判断是否是最后一道工序 |
| | | List<ProductProcessRouteItem> productProcessRouteItems = productProcessRouteItemMapper.selectList(Wrappers.<ProductProcessRouteItem>lambdaQuery().eq(ProductProcessRouteItem::getProductRouteId, productProcessRouteItem.getProductRouteId())); |
| | | if (productProcessRouteItem.getDragSort() == productProcessRouteItems.size()){ |
| | | ProductOrder productOrder = productOrderMapper.selectById(productWorkOrder.getProductOrderId()); |
| | | productOrder.setCompleteQuantity(productOrder.getCompleteQuantity().subtract(productionProductOutput.getQuantity())); |
| | | productOrder.setEndTime(null); |
| | | productOrderMapper.updateById(productOrder); |
| | | if (productWorkOrder != null && productionProductOutput != null) { |
| | | BigDecimal outputQty = productionProductOutput.getQuantity() == null ? BigDecimal.ZERO : productionProductOutput.getQuantity(); |
| | | BigDecimal scrapQty = productionProductOutput.getScrapQty() == null ? BigDecimal.ZERO : productionProductOutput.getScrapQty(); |
| | | BigDecimal completeQty = productWorkOrder.getCompleteQuantity() == null ? BigDecimal.ZERO : productWorkOrder.getCompleteQuantity(); |
| | | |
| | | BigDecimal validQuantity = outputQty.subtract(scrapQty); |
| | | productWorkOrder.setCompleteQuantity(completeQty.subtract(validQuantity)); |
| | | productWorkOrder.setActualEndTime(null); |
| | | productWorkOrderMapper.updateById(productWorkOrder); |
| | | } else { |
| | | throw new ServiceException("操作失败:工单信息或产出记录不存在"); |
| | | } |
| | | /*删除产出*/ |
| | | //删除质检 |
| | | |
| | | List<ProductProcessRouteItem> productProcessRouteItems = productProcessRouteItemMapper.selectList( |
| | | Wrappers.<ProductProcessRouteItem>lambdaQuery().eq(ProductProcessRouteItem::getProductRouteId, productProcessRouteItem.getProductRouteId()) |
| | | ); |
| | | if (productProcessRouteItem.getDragSort() != null |
| | | && productProcessRouteItems != null |
| | | && productProcessRouteItem.getDragSort() == productProcessRouteItems.size()) { |
| | | ProductOrder productOrder = productOrderMapper.selectById(productWorkOrder.getProductOrderId()); |
| | | if (productOrder != null) { |
| | | BigDecimal orderCompleteQty = productOrder.getCompleteQuantity() == null ? BigDecimal.ZERO : productOrder.getCompleteQuantity(); |
| | | BigDecimal totalQty = productionProductOutput.getQuantity() != null ? productionProductOutput.getQuantity() : BigDecimal.ZERO; |
| | | BigDecimal scrapQty = productionProductOutput.getScrapQty() != null ? productionProductOutput.getScrapQty() : BigDecimal.ZERO; |
| | | BigDecimal actualQualifiedQty = totalQty.subtract(scrapQty); |
| | | BigDecimal newCompleteQty = orderCompleteQty.subtract(actualQualifiedQty); |
| | | productOrder.setCompleteQuantity(newCompleteQty.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : newCompleteQty); |
| | | productOrder.setEndTime(null); |
| | | productOrderMapper.updateById(productOrder); |
| | | } else { |
| | | throw new ServiceException("关联的生产订单不存在"); |
| | | } |
| | | } |
| | | |
| | | qualityInspectMapper.selectList( |
| | | new LambdaQueryWrapper<QualityInspect>() |
| | | .eq(QualityInspect::getProductMainId, productionProductMain.getId()) |
| | | ).forEach(q -> { |
| | | qualityInspectParamMapper.delete( |
| | | new LambdaQueryWrapper<QualityInspectParam>() |
| | | .eq(QualityInspectParam::getInspectId, q.getId())); |
| | | .eq(QualityInspectParam::getInspectId, q.getId()) |
| | | ); |
| | | qualityInspectMapper.deleteById(q.getId()); |
| | | stockUtils.deleteStockInRecord(q.getId(), StockInQualifiedRecordTypeEnum.QUALITYINSPECT_STOCK_IN.getCode()); |
| | | }); |
| | | // 删除产出记录 |
| | | productionProductOutputMapper.delete(new LambdaQueryWrapper<ProductionProductOutput>() |
| | | .eq(ProductionProductOutput::getProductMainId, productionProductMain.getId())); |
| | | /*删除投入*/ |
| | | procurementRecordOutMapper.delete(new LambdaQueryWrapper<ProcurementRecordOut>() |
| | | .eq(ProcurementRecordOut::getSalesLedgerProductId, productionProductMain.getId())); |
| | | productionProductInputMapper.delete(new LambdaQueryWrapper<ProductionProductInput>() |
| | | .eq(ProductionProductInput::getProductMainId, productionProductMain.getId())); |
| | | // 删除主表 |
| | | |
| | | productionProductOutputMapper.delete( |
| | | new LambdaQueryWrapper<ProductionProductOutput>() |
| | | .eq(ProductionProductOutput::getProductMainId, productionProductMain.getId()) |
| | | ); |
| | | productionProductInputMapper.delete( |
| | | new LambdaQueryWrapper<ProductionProductInput>() |
| | | .eq(ProductionProductInput::getProductMainId, productionProductMain.getId()) |
| | | ); |
| | | stockUtils.deleteStockInRecord(productionProductMain.getId(), StockInUnQualifiedRecordTypeEnum.PRODUCTION_SCRAP.getCode()); |
| | | stockUtils.deleteStockInRecord(productionProductMain.getId(), StockInQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_IN.getCode()); |
| | | stockUtils.deleteStockOutRecord(productionProductMain.getId(), StockOutQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_OUT.getCode()); |
| | | productionProductMainMapper.deleteById(productionProductMain.getId()); |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public ArrayList<Long> listMain(List<Long> idList) { |
| | | return productionProductMainMapper.listMain(idList); |
| | | } |
| | | |
| | | @Override |
| | | public List<ProductionProductMainDto> getByProductWorkOrderId(Long productWorkOrderId) { |
| | | List<ProductionProductMainDto> productionProductMainDtos = productionProductMainMapper.getByProductWorkOrderId(productWorkOrderId); |
| | | if (productionProductMainDtos == null || productionProductMainDtos.isEmpty()) { |
| | | return productionProductMainDtos; |
| | | } |
| | | |
| | | List<Long> productMainIds = productionProductMainDtos.stream() |
| | | .map(ProductionProductMainDto::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | List<QualityInspect> qualityInspects = qualityInspectMapper.selectList( |
| | | Wrappers.<QualityInspect>lambdaQuery() |
| | | .in(QualityInspect::getProductMainId, productMainIds) |
| | | ); |
| | | |
| | | if (!qualityInspects.isEmpty()) { |
| | | List<Long> inspectIds = qualityInspects.stream() |
| | | .map(QualityInspect::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | List<QualityUnqualified> qualityUnqualifieds = qualityUnqualifiedMapper.selectList( |
| | | Wrappers.<QualityUnqualified>lambdaQuery() |
| | | .in(QualityUnqualified::getInspectId, inspectIds) |
| | | ); |
| | | |
| | | Map<Long, QualityUnqualified> inspectIdToUnqualifiedMap = qualityUnqualifieds.stream() |
| | | .collect(Collectors.toMap(QualityUnqualified::getInspectId, q -> q, (q1, q2) -> q1)); |
| | | |
| | | Map<Long, QualityInspect> productMainIdToInspectMap = qualityInspects.stream() |
| | | .collect(Collectors.toMap(QualityInspect::getProductMainId, q -> q, (q1, q2) -> q1)); |
| | | |
| | | productionProductMainDtos.forEach(p -> { |
| | | QualityInspect qualityInspect = productMainIdToInspectMap.get(p.getId()); |
| | | if (qualityInspect != null) { |
| | | p.setDefectiveQuantity(qualityInspect.getDefectiveQuantity()); |
| | | p.setQualifiedQty(p.getQuantity().subtract(p.getScrapQty()).subtract(p.getDefectiveQuantity())); |
| | | QualityUnqualified qualityUnqualified = inspectIdToUnqualifiedMap.get(qualityInspect.getId()); |
| | | if (qualityUnqualified != null) { |
| | | p.setDealResult(qualityUnqualified.getDealResult() == null ? "" : qualityUnqualified.getDealResult()); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | return productionProductMainDtos; |
| | | } |
| | | |
| | | private BigDecimal resolveInputWeight(String otherData) { |
| | | if (StringUtils.isBlank(otherData)) { |
| | | return null; |
| | | } |
| | | Object parsed; |
| | | try { |
| | | parsed = JSON.parse(otherData); |
| | | } catch (Exception ex) { |
| | | throw new ServiceException("报工参数格式错误,无法解析投入重量/数量"); |
| | | } |
| | | String inputWeight = StringUtils.trim(findParameterValue(parsed, INPUT_WEIGHT_PARAMETER)); |
| | | if (StringUtils.isBlank(inputWeight)) { |
| | | inputWeight = StringUtils.trim(findFieldValue(parsed, INPUT_WEIGHT_FIELD)); |
| | | } |
| | | if (StringUtils.isBlank(inputWeight)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return new BigDecimal(inputWeight); |
| | | } catch (NumberFormatException ex) { |
| | | throw new ServiceException("报工参数中的投入重量/数量格式错误"); |
| | | } |
| | | } |
| | | |
| | | private String findParameterValue(Object node, String parameterItem) { |
| | | if (node instanceof JSONArray) { |
| | | JSONArray array = (JSONArray) node; |
| | | for (Object item : array) { |
| | | String value = findParameterValue(item, parameterItem); |
| | | if (StringUtils.isNotBlank(value)) { |
| | | return value; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | if (node instanceof JSONObject) { |
| | | JSONObject object = (JSONObject) node; |
| | | if (parameterItem.equals(StringUtils.trim(object.getString("parameterItem")))) { |
| | | String value = StringUtils.trim(object.getString("value")); |
| | | if (StringUtils.isNotBlank(value)) { |
| | | return value; |
| | | } |
| | | } |
| | | for (Object value : object.values()) { |
| | | String matched = findParameterValue(value, parameterItem); |
| | | if (StringUtils.isNotBlank(matched)) { |
| | | return matched; |
| | | } |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private String findFieldValue(Object node, String fieldName) { |
| | | if (node instanceof JSONArray) { |
| | | JSONArray array = (JSONArray) node; |
| | | for (Object item : array) { |
| | | String value = findFieldValue(item, fieldName); |
| | | if (StringUtils.isNotBlank(value)) { |
| | | return value; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | if (node instanceof JSONObject) { |
| | | JSONObject object = (JSONObject) node; |
| | | Object fieldValue = object.get(fieldName); |
| | | if (fieldValue != null) { |
| | | String value = StringUtils.trim(String.valueOf(fieldValue)); |
| | | if (StringUtils.isNotBlank(value)) { |
| | | return value; |
| | | } |
| | | } |
| | | for (Object value : object.values()) { |
| | | String matched = findFieldValue(value, fieldName); |
| | | if (StringUtils.isNotBlank(matched)) { |
| | | return matched; |
| | | } |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | } |