buhuazhen
4 天以前 03ac57d63d2f196ce92ace269b5681aca1c0467a
src/main/java/com/ruoyi/production/service/impl/ProductionProductMainServiceImpl.java
@@ -42,11 +42,10 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
import static cn.hutool.core.date.LocalDateTimeUtil.between;
@Service
@AllArgsConstructor
@@ -54,6 +53,7 @@
public class ProductionProductMainServiceImpl extends ServiceImpl<ProductionProductMainMapper, ProductionProductMain> implements ProductionProductMainService {
    private final SalesLedgerMapper salesLedgerMapper;
    private final ProductionMachineRecordMapper productionMachineRecordMapper;
    private IQualityInspectService qualityInspectService;
    private ProductionProductMainMapper productionProductMainMapper;
@@ -146,7 +146,7 @@
        productionProductMain.setProductNo(productNo);
        // 修改班组信息 以第一人作为原先setUserId setUserName
        Assert.isTrue(CollUtil.isNotEmpty(dto.getTeamList()),"班组信息不能为空");
        Assert.isTrue(CollUtil.isNotEmpty(dto.getTeamList()), "班组信息不能为空");
        productionProductMain.setTeamIds(dto.getTeamList().stream().map(ProductionProductMainDto.Team::getUserId).map(String::valueOf).collect(Collectors.joining(",")));
        productionProductMain.setTeamNames(dto.getTeamList().stream().map(ProductionProductMainDto.Team::getUserName).collect(Collectors.joining(",")));
@@ -156,21 +156,25 @@
        productionProductMain.setUserName(SecurityUtils.getLoginUser().getNickName());
        productionProductMain.setProductProcessRouteItemId(dto.getProductProcessRouteItemId());
        productionProductMain.setWorkOrderId(dto.getWorkOrderId());
        productionProductMain.setAuditUserId(dto.getAuditUserId());
        productionProductMain.setAuditUserName(dto.getAuditUserName());
//        productionProductMain.setAuditUserId(dto.getAuditUserId());
//        productionProductMain.setAuditUserName(dto.getAuditUserName());
        productionProductMain.setStatus(0);
        if (ObjectUtils.isNotEmpty(dto.getStartTime())&&ObjectUtils.isNotEmpty(dto.getEndTime())) {
        if (ObjectUtils.isNotEmpty(dto.getStartTime()) && ObjectUtils.isNotEmpty(dto.getEndTime())) {
            productionProductMain.setStartTime(dto.getStartTime());
            productionProductMain.setEndTime(dto.getEndTime());
        }else {
        } else {
            productionProductMain.setStartTime(productWorkOrder.getStartProductTime());
            productionProductMain.setEndTime(now);
        }
        productionProductMain.setDeviceId(dto.getDeviceId());
        productionProductMain.setDeviceName(dto.getDeviceName());
        // 审批人为自己 直接通过审批
        productionProductMain.setAuditUserName(SecurityUtils.getUsername());
        productionProductMain.setAuditUserId(SecurityUtils.getUserId());
        productionProductMainMapper.insert(productionProductMain);
        /*新增报工投入表*/
        List<ProductStructureDto> productStructureDtos = productStructureMapper.listBybomAndProcess(productProcessRoute.getBomId(), productProcess.getId());
@@ -182,14 +186,12 @@
            productStructureDtos.add(productStructureDto);
        }
        for (ProductStructureDto productStructureDto : productStructureDtos) {
            ProductionProductInput productionProductInput = new ProductionProductInput();
            productionProductInput.setProductModelId(productStructureDto.getProductModelId());
            productionProductInput.setQuantity(productStructureDto.getUnitQuantity().multiply(dto.getQuantity()));
            productionProductInput.setProductMainId(productionProductMain.getId());
            productionProductInputMapper.insert(productionProductInput);
//            stockUtils.substractStock(productStructureDto.getProductModelId(), productionProductInput.getQuantity(), StockOutQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_OUT.getCode(), productionProductMain.getId());
        }
        /*新增报工产出表*/
        ProductionProductOutput productionProductOutput = new ProductionProductOutput();
@@ -198,6 +200,7 @@
        productionProductOutput.setQuantity(dto.getQuantity() != null ? dto.getQuantity() : BigDecimal.ZERO);
        productionProductOutput.setScrapQty(dto.getScrapQty() != null ? dto.getScrapQty() : BigDecimal.ZERO);
        productionProductOutput.setReplenishQty(dto.getReplenishQty());
        productionProductOutput.setAddQty(dto.getAddQty() != null ? dto.getAddQty() : BigDecimal.ZERO);
        productionProductOutputMapper.insert(productionProductOutput);
        //合格数量=报工数量-报废数量
        BigDecimal productQty = productionProductOutput.getQuantity().subtract(productionProductOutput.getScrapQty());
@@ -209,7 +212,7 @@
            if (ObjectUtils.isNull(productWorkOrder.getActualStartTime())) {
                productWorkOrder.setActualStartTime(LocalDate.now());//实际开始时间
            }
            if (productWorkOrder.getCompleteQuantity().compareTo(productWorkOrder.getPlanQuantity()) == 0) {
            if (productWorkOrder.getCompleteQuantity().compareTo(productWorkOrder.getPlanQuantity()) >= 0) {
                productWorkOrder.setActualEndTime(LocalDate.now());//实际结束时间
            }
            productWorkOrder.setEndProductTime(now);
@@ -219,17 +222,21 @@
            if (ObjectUtils.isNull(productOrder.getStartTime())) {
                productOrder.setStartTime(now);//开始时间
            }
            if (productProcessRouteItem.getDragSort() == productProcessRouteItems.size()) {
            if (productProcessRouteItem.getDragSort() >= productProcessRouteItems.size() -1 ) {
                //如果是最后一道工序报工之后生产订单完成数量+
                productOrder.setCompleteQuantity(productOrder.getCompleteQuantity().add(productQty));
                if (productOrder.getCompleteQuantity().compareTo(productOrder.getQuantity()) == 0) {
                if (productOrder.getCompleteQuantity().compareTo(productOrder.getQuantity()) >= 0) {
                    productOrder.setEndTime(now);//结束时间
                }
            }
            productOrderMapper.updateById(productOrder);
        }
        //nextAddProductMain(productionProductOutput) // 由于需要审核,所以需要拆封下来
        ProductAuditVo productAuditVo = new ProductAuditVo();
        productAuditVo.setId(productionProductMain.getId());
        productAuditVo.setAuditStatus(AuditEnum.AUDIT_SUCCESS);
        productAuditVo.setAuditOpinion("");
        ((ProductionProductMainService) AopContext.currentProxy()).auditProductMain(productAuditVo);
//        nextAddProductMain(productionProductOutput) // 由于需要审核,所以需要拆封下来
        return true;
    }
@@ -239,7 +246,7 @@
        ProductionProductMain productionProductMain = productionProductMainMapper.selectById(productAuditVo.getId());
        // 当前审批人 要与当前登录人为同一人
        if(productionProductMain.getAuditUserId() != -1){
        if (productionProductMain.getAuditUserId() != -1) {
            Assert.isTrue(SecurityUtils.getUserId().equals(productionProductMain.getAuditUserId()), "当前登录用户不是当前审批人");
        }
@@ -312,7 +319,7 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void nextAddProductMain(@NotNull ProductionProductOutput productionProductOutput){
    public void nextAddProductMain(@NotNull ProductionProductOutput productionProductOutput) {
        //合格数量=报工数量-报废数量
        ProductionProductMain productionProductMain = productionProductMainMapper.selectById(productionProductOutput.getProductMainId());
        BigDecimal productQty = productionProductOutput.getQuantity().subtract(productionProductOutput.getScrapQty());
@@ -362,31 +369,49 @@
                                qualityInspectParamMapper.insert(param);
                            });
                }
            }else {
            } else {
                //直接入库
                stockUtils.addStock(productProcessRouteItem.getProductModelId(), productQty, StockInQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_IN.getCode(), productionProductMain.getId(),"-","-","-");
                stockUtils.addStock(productProcessRouteItem.getProductModelId(), productQty, StockInQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_IN.getCode(), productionProductMain.getId(), "-", "-", "-");
            }
            /*添加生产核算        区分工序是计件还是计时*/
            BigDecimal workHours = productProcess.getSalaryQuota();
            SalesLedgerProductionAccounting salesLedgerProductionAccounting = SalesLedgerProductionAccounting.builder()
                    .productMainId(productionProductMain.getId())
                    .schedulingUserId(productionProductMain.getUserId())
                    .schedulingUserName(userMapper.selectUserById(productionProductMain.getUserId()).getNickName())
                    .finishedNum(productQty)
                    .workHours(workHours)
                    .process(productProcess.getName())
                    .schedulingDate(LocalDate.now())
                    .tenantId(productionProductOutput.getTenantId())
                    .build();
            salesLedgerProductionAccountingMapper.insert(salesLedgerProductionAccounting);
            List<ProductionMachineRecord> productionMachineRecords = productionMachineRecordMapper.selectList(Wrappers.<ProductionMachineRecord>lambdaQuery().eq(ProductionMachineRecord::getWorkOrderId, productionProductMain.getWorkOrderId()));
            if (ObjectUtils.isNotEmpty(productionMachineRecords)) {
                for (ProductionMachineRecord productionMachineRecord : productionMachineRecords) {
                    //说明已经添加过了,不添加了
                    if (productionMachineRecord.getReportStatus()) {
                        continue;
                    }
                    for (String s : productionMachineRecord.getOperatorId().split(",")) {
                        Long minutes = 0L;
                        if (productionMachineRecord.getMachineStartTime() != null) {
                            minutes = between(productionMachineRecord.getMachineStartTime(), LocalDateTime.now()).toMinutes();
                        }
                        SalesLedgerProductionAccounting salesLedgerProductionAccounting = SalesLedgerProductionAccounting.builder()
                                .productMainId(productionProductMain.getId())
                                .schedulingUserId(Long.parseLong(s))
                                .schedulingUserName(Optional.of(userMapper.selectUserById(Long.parseLong(s))).orElse(new SysUser()).getNickName())
                                .finishedNum(productQty)
                                .workHours(workHours)
                                .process(productProcess.getName())
                                .schedulingDate(LocalDate.now())
                                .tenantId(productionProductOutput.getTenantId())
                                .deviceId(productionMachineRecord.getMachineId())
                                .workHour(minutes)
                                .build();
                        salesLedgerProductionAccountingMapper.insert(salesLedgerProductionAccounting);
                    }
                    productionMachineRecord.setReportStatus(true);
                    productionMachineRecordMapper.updateById(productionMachineRecord);
                }
            }
        }
        //如果报废数量>0,需要进入报废的库存
        if (ObjectUtils.isNotEmpty(productionProductOutput.getScrapQty())) {
            if (productionProductOutput.getScrapQty().compareTo(BigDecimal.ZERO) > 0) {
                stockUtils.addUnStock(productModel.getId(), productionProductOutput.getScrapQty(), StockInUnQualifiedRecordTypeEnum.PRODUCTION_SCRAP.getCode(), productionProductMain.getId(),"-","-","-");
                stockUtils.addUnStock(productModel.getId(), productionProductOutput.getScrapQty(), StockInUnQualifiedRecordTypeEnum.PRODUCTION_SCRAP.getCode(), productionProductMain.getId(), "-", "-", "-");
            }
        }
    }
@@ -395,10 +420,10 @@
    public Boolean removeProductMain(Long id) {
        //判断该条报工是否不合格处理,如果不合格处理了,则不允许删除
        List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(Wrappers.<QualityInspect>lambdaQuery().eq(QualityInspect::getProductMainId, id));
        if (qualityInspects.size() > 0){
        if (qualityInspects.size() > 0) {
            List<QualityUnqualified> qualityUnqualifieds = qualityUnqualifiedMapper.selectList(Wrappers.<QualityUnqualified>lambdaQuery()
                    .in(QualityUnqualified::getInspectId, qualityInspects.stream().map(QualityInspect::getId).collect(Collectors.toList())));
            if (qualityUnqualifieds.size() > 0 && qualityUnqualifieds.get(0).getInspectState()==1) {
            if (qualityUnqualifieds.size() > 0 && qualityUnqualifieds.get(0).getInspectState() == 1) {
                throw new ServiceException("该条报工已经不合格处理了,不允许删除");
            }
        }
@@ -422,7 +447,7 @@
            BigDecimal validQuantity = outputQty.subtract(scrapQty);
            if(productionProductMain.getAuditStatus() != 2){
            if (productionProductMain.getAuditStatus() != 2) {
                productWorkOrder.setCompleteQuantity(completeQty.subtract(validQuantity).max(BigDecimal.ZERO));
            }
            productWorkOrder.setActualEndTime(null);
@@ -456,7 +481,7 @@
                    new LambdaQueryWrapper<QualityInspectParam>()
                            .eq(QualityInspectParam::getInspectId, q.getId()));
            qualityInspectMapper.deleteById(q.getId());
                stockUtils.deleteStockInRecord(q.getId(), StockInQualifiedRecordTypeEnum.QUALITYINSPECT_STOCK_IN.getCode());
            stockUtils.deleteStockInRecord(q.getId(), StockInQualifiedRecordTypeEnum.QUALITYINSPECT_STOCK_IN.getCode());
        });
        // 删除产出记录