package com.ruoyi.production.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.basic.mapper.ProductMapper;
|
import com.ruoyi.basic.mapper.ProductModelMapper;
|
import com.ruoyi.basic.pojo.Product;
|
import com.ruoyi.basic.pojo.ProductModel;
|
import com.ruoyi.common.utils.SecurityUtils;
|
import com.ruoyi.common.utils.bean.BeanUtils;
|
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.pojo.*;
|
import com.ruoyi.production.service.ProductionProductMainService;
|
import com.ruoyi.project.system.domain.SysUser;
|
import com.ruoyi.quality.mapper.QualityInspectMapper;
|
import com.ruoyi.quality.mapper.QualityInspectParamMapper;
|
import com.ruoyi.quality.mapper.QualityTestStandardMapper;
|
import com.ruoyi.quality.pojo.QualityInspect;
|
import com.ruoyi.quality.pojo.QualityInspectParam;
|
import com.ruoyi.quality.pojo.QualityTestStandard;
|
import lombok.AllArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import com.ruoyi.production.mapper.ProductionProductMainMapper;
|
|
import java.math.BigDecimal;
|
import java.time.LocalDate;
|
import java.time.format.DateTimeFormatter;
|
import java.util.List;
|
import java.util.Map;
|
|
@Service
|
@AllArgsConstructor
|
public class ProductionProductMainServiceImpl extends ServiceImpl<ProductionProductMainMapper, ProductionProductMain> implements ProductionProductMainService {
|
|
private ProductionProductMainMapper productionProductMainMapper;
|
|
private ProductWorkOrderController productWorkOrderController;
|
|
private ProductWorkOrderMapper productWorkOrderMapper;
|
|
private ProductProcessRouteItemMapper productProcessRouteItemMapper;
|
|
private ProductionProductOutputMapper productionProductOutputMapper;
|
|
private ProcessRouteItemMapper processRouteItemMapper;
|
|
private ProductModelMapper productModelMapper;
|
|
private QualityInspectMapper qualityInspectMapper;
|
|
private ProductProcessMapper productProcessMapper;
|
|
private ProductMapper productMapper;
|
|
private QualityTestStandardMapper qualityTestStandardMapper;
|
|
private QualityInspectParamMapper qualityInspectParamMapper;
|
|
private ProductStructureMapper productStructureMapper;
|
|
private ProductionProductInputMapper productionProductInputMapper;
|
|
private ProductOrderMapper productOrderMapper;
|
|
private SalesLedgerProductionAccountingMapper salesLedgerProductionAccountingMapper;
|
|
|
@Override
|
public IPage<ProductionProductMainDto> listPageProductionProductMainDto(Page page, ProductionProductMainDto productionProductMainDto) {
|
return productionProductMainMapper.listPageProductionProductMainDto(page, productionProductMainDto);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Boolean addProductMain(ProductionProductMainDto dto) {
|
if (dto == null) {
|
throw new RuntimeException("参数不能为空");
|
}
|
|
SysUser user = SecurityUtils.getLoginUser().getUser();
|
|
if (dto.isReportWork()) {
|
// 更新逻辑 - 只更新数量
|
QueryWrapper<ProductionProductOutput> outputWrapper = new QueryWrapper<>();
|
outputWrapper.eq("product_main_id", dto.getProductMainId());
|
|
ProductionProductOutput output = productionProductOutputMapper.selectOne(outputWrapper);
|
if (output == null) {
|
throw new RuntimeException("产出记录不存在");
|
}
|
|
// 查询生产核算记录
|
QueryWrapper<SalesLedgerProductionAccounting> accountingWrapper = new QueryWrapper<>();
|
accountingWrapper.eq("sales_ledger_work_id", dto.getProductMainId());
|
SalesLedgerProductionAccounting accounting = salesLedgerProductionAccountingMapper.selectOne(accountingWrapper);
|
if (accounting == null) {
|
throw new RuntimeException("生产核算记录不存在");
|
}
|
|
// 只更新数量
|
if (dto.getQuantity() != null) {
|
output.setQuantity(dto.getQuantity());
|
productionProductOutputMapper.updateById(output);
|
// 更新生产核算记录
|
accounting.setFinishedNum(dto.getQuantity());
|
salesLedgerProductionAccountingMapper.updateById(accounting);
|
}
|
return true;
|
}
|
|
// 新增逻辑
|
ProductionProductMain productionProductMain = new ProductionProductMain();
|
ProductProcessRouteItem productProcessRouteItem = productProcessRouteItemMapper.selectById(dto.getProductProcessRouteItemId());
|
if (productProcessRouteItem == null) {
|
throw new RuntimeException("工艺路线项不存在");
|
}
|
|
String datePrefix = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
|
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 (lastNo.startsWith(datePrefix)) {
|
try {
|
String seqStr = lastNo.substring(datePrefix.length());
|
sequenceNumber = Integer.parseInt(seqStr) + 1;
|
} catch (NumberFormatException e) {
|
sequenceNumber = 1;
|
}
|
}
|
}
|
}
|
}
|
|
String productNo = String.format("%s%03d", datePrefix, sequenceNumber);
|
productionProductMain.setProductNo(productNo);
|
productionProductMain.setUserId(user.getUserId());
|
productionProductMain.setProductProcessRouteItemId(dto.getProductProcessRouteItemId());
|
productionProductMain.setWorkOrderId(dto.getWorkOrderId());
|
productionProductMain.setStatus(0);
|
|
// 添加报工主表
|
int insert = productionProductMainMapper.insert(productionProductMain);
|
|
//更新工单
|
if (insert > 0) {
|
UpdateWrapper<ProductWorkOrder> wrapper = new UpdateWrapper<>();
|
wrapper.set("report_work", true)
|
.set("quantity", dto.getQuantity())
|
.set("product_main_id", productionProductMain.getId())
|
.eq("id", dto.getWorkOrderId());
|
productWorkOrderMapper.update(null, wrapper);
|
}
|
ProductProcess productProcess = productProcessMapper.selectById(productProcessRouteItem.getProcessId());
|
ProductModel productModel = productProcessRouteItem.getProductModelId() != null ?
|
productModelMapper.selectById(productProcessRouteItem.getProductModelId()) : null;
|
|
if (productModel != null) {
|
Product product = productMapper.selectById(productModel.getProductId());
|
int inspectType = "组装".equals(productProcess.getName()) ? 2 : 1;
|
|
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());
|
qualityInspectMapper.insert(qualityInspect);
|
|
qualityTestStandardMapper.selectList(
|
new LambdaQueryWrapper<QualityTestStandard>()
|
.eq(QualityTestStandard::getProductId, product.getId())
|
).forEach(standard -> {
|
QualityInspectParam param = new QualityInspectParam();
|
BeanUtils.copyProperties(standard, param);
|
param.setId(null);
|
param.setInspectId(qualityInspect.getId());
|
qualityInspectParamMapper.insert(param);
|
});
|
}
|
// 添加投入
|
if (productModel != null) {
|
List<ProductStructureDto> productStructureDtos = productStructureMapper.listByproductModelId(productModel.getId());
|
for (ProductStructureDto productStructureDto : productStructureDtos) {
|
ProductionProductInput productionProductInput = new ProductionProductInput();
|
productionProductInput.setProductModelId(productStructureDto.getProductModelId());
|
productionProductInput.setQuantity(productStructureDto.getUnitQuantity());
|
productionProductInput.setProductMainId(productionProductMain.getId());
|
productionProductInputMapper.insert(productionProductInput);
|
}
|
}
|
|
// 添加产出
|
ProductionProductOutput productionProductOutput = new ProductionProductOutput();
|
productionProductOutput.setProductMainId(productionProductMain.getId());
|
productionProductOutput.setProductModelId(productProcessRouteItem.getProductModelId());
|
productionProductOutput.setQuantity(dto.getQuantity() != null ? dto.getQuantity() : BigDecimal.ZERO);
|
productionProductOutputMapper.insert(productionProductOutput);
|
|
// 获取生产订单
|
ProductWorkOrder productWorkOrder = productWorkOrderMapper.selectById(dto.getWorkOrderId());
|
ProductOrder productOrder = productOrderMapper.selectById(productWorkOrder.getProductOrderId());
|
if (productOrder == null) {
|
throw new RuntimeException("生产订单不存在");
|
}
|
// 添加生产核算
|
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);
|
|
return true;
|
}
|
|
@Override
|
@Transactional
|
public Boolean removeProductMain(ProductionProductMainDto dto) {
|
Long id = dto.getId();
|
|
// 删除质检参数和质检记录
|
qualityInspectMapper.selectList(
|
new LambdaQueryWrapper<QualityInspect>()
|
.eq(QualityInspect::getProductMainId, id)
|
).forEach(q -> {
|
qualityInspectParamMapper.delete(
|
new LambdaQueryWrapper<QualityInspectParam>()
|
.eq(QualityInspectParam::getInspectId, q.getId()));
|
qualityInspectMapper.deleteById(q.getId());
|
});
|
|
// 删除产出记录
|
productionProductOutputMapper.delete(new LambdaQueryWrapper<ProductionProductOutput>()
|
.eq(ProductionProductOutput::getProductMainId, id)
|
);
|
|
// 删除关联的核算数据
|
salesLedgerProductionAccountingMapper.delete(
|
new LambdaQueryWrapper<SalesLedgerProductionAccounting>()
|
.eq(SalesLedgerProductionAccounting::getSalesLedgerWorkId, id)
|
);
|
|
// 删除主表
|
return productionProductMainMapper.deleteById(id) > 0;
|
}
|
}
|