/*
|
* Copyright (c) 2018-2025, ztt All rights reserved.
|
*
|
* Redistribution and use in source and binary forms, with or without
|
* modification, are permitted provided that the following conditions are met:
|
*
|
* Redistributions of source code must retain the above copyright notice,
|
* this list of conditions and the following disclaimer.
|
* Redistributions in binary form must reproduce the above copyright
|
* notice, this list of conditions and the following disclaimer in the
|
* documentation and/or other materials provided with the distribution.
|
* Neither the name of the pig4cloud.com developer nor the names of its
|
* contributors may be used to endorse or promote products derived from
|
* this software without specific prior written permission.
|
* Author: ztt
|
*/
|
package com.chinaztt.mes.production.service.impl;
|
|
import com.alibaba.fastjson.JSON;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.chinaztt.mes.mould.util.MouldUtil;
|
import com.chinaztt.mes.plan.entity.MoRoutingOperation;
|
import com.chinaztt.mes.plan.mapper.MoRoutingOperationMapper;
|
import com.chinaztt.mes.production.dto.MouldRegisterDTO;
|
import com.chinaztt.mes.production.dto.MouldUseRecordDTO;
|
import com.chinaztt.mes.production.dto.mould.InsertLifeDTO;
|
import com.chinaztt.mes.production.entity.*;
|
import com.chinaztt.mes.production.mapper.MouldRegisterMapper;
|
import com.chinaztt.mes.production.mapper.MouldUseRecordMapper;
|
import com.chinaztt.mes.production.mapper.ProductOutputMapper;
|
import com.chinaztt.mes.production.mapper.ProductStepMapper;
|
import com.chinaztt.mes.production.service.MouldUseRecordService;
|
import com.chinaztt.mes.technology.entity.RoutingOperation;
|
import com.chinaztt.mes.technology.mapper.RoutingOperationMapper;
|
import com.chinaztt.ztt.common.security.util.SecurityUtils;
|
import lombok.AllArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.math.BigDecimal;
|
import java.text.SimpleDateFormat;
|
import java.util.Collections;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
/**
|
* 模具使用记录表
|
*
|
* @author cxf
|
* @date 2021-10-25 13:07:10
|
*/
|
@Service
|
@AllArgsConstructor
|
@Transactional(rollbackFor = Exception.class)
|
public class MouldUseRecordServiceImpl extends ServiceImpl<MouldUseRecordMapper, MouldUseRecord> implements MouldUseRecordService {
|
|
private ProductOutputMapper productOutputMapper;
|
private MouldRegisterMapper mouldRegisterMapper;
|
private ProductStepMapper productStepMapper;
|
private MoRoutingOperationMapper routingOperationMapper;
|
|
private MouldUtil mouldUtil;
|
|
@Override
|
public IPage<MouldUseRecordDTO> getMouldUseRecordPage(Page page, QueryWrapper<MouldUseRecordDTO> gen) {
|
return baseMapper.getMouldUseRecordPage(page,gen);
|
}
|
|
@Override
|
public Boolean insertMouldLife(ProductMain productMain) {
|
// 找出登记的模具
|
List<MouldRegisterDTO> registerList = mouldRegisterMapper.selectByWorkstationId(productMain.getWorkstationId());
|
if (CollectionUtils.isEmpty(registerList)) {
|
return false;
|
}
|
for (MouldRegisterDTO register:registerList) {
|
// 调用模具消耗接口参数
|
InsertLifeDTO dto = new InsertLifeDTO();
|
dto.setMouldCode(register.getMouldCode());
|
dto.setSerialNumber(productMain.getProductNo());
|
dto.setProcedureNumber(register.getOperationName());
|
dto.setMachineNumber(register.getWorkstationNo());
|
dto.setBuildMaker(SecurityUtils.getUser().getUsername());
|
dto.setProductSpec(register.getPartNo());
|
dto.setProductionDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
|
|
BigDecimal total = BigDecimal.ZERO;
|
|
// 找出该报工所有产出
|
List<ProductOutput> outputList = productOutputMapper.selectList(Wrappers.<ProductOutput>lambdaQuery()
|
.eq(ProductOutput::getProductMainId, productMain.getId()));
|
|
// 判断模具登记类型
|
// 工序直接按产出计算消耗
|
if (register.getType() == MouldRegister.TYPE_OPERATION) {
|
MoRoutingOperation operation = routingOperationMapper.findByProductMainId(productMain.getId());
|
if (!operation.getOperationId().equals(register.getOperationId())) {
|
return false;
|
}
|
|
total = outputList.stream().map(ProductOutput::getProductQty).reduce(BigDecimal.ZERO, BigDecimal::add);
|
dto.setLifeValue(total.multiply(register.getLifeConversionFactor()));
|
} else {
|
// 找出工单工步数据
|
List<ProductStep> productSteps = productStepMapper.selectList(Wrappers.<ProductStep>lambdaQuery()
|
.eq(ProductStep::getProductMainId, productMain.getId()).eq(ProductStep::getStepId, register.getStepId()));
|
if (productSteps.isEmpty()) {
|
return false;
|
}
|
total = productSteps.stream().map(ProductStep::getNumber).reduce(BigDecimal.ZERO, BigDecimal::add);
|
dto.setLifeValue(total.multiply(register.getLifeConversionFactor()));
|
}
|
|
MouldUseRecord record = new MouldUseRecordDTO();
|
record.setMouldRegisterId(register.getId());
|
record.setProductMainId(productMain.getId());
|
record.setOperationTaskId(productMain.getOperationTaskId());
|
record.setOutput(total);
|
record.setUsageAmount(dto.getLifeValue());
|
record.setProductNo(productMain.getProductNo());
|
record.setStatus(MouldUseRecord.STATE_SUBMITTED);
|
record.setWorkstationId(register.getWorkstationId());
|
if (CollectionUtils.isNotEmpty(outputList)) {
|
record.setBatchNo(outputList.get(0).getOutBatchNo());
|
}
|
record.insert();
|
mouldUtil.insertTempLifeRecord(JSON.toJSONString(dto));
|
}
|
return true;
|
}
|
|
@Override
|
public Boolean clearMouldLife(ProductMain productMain) {
|
// 找出登记的模具
|
List<MouldRegisterDTO> registerList = mouldRegisterMapper.selectByWorkstationId(productMain.getWorkstationId());
|
if (CollectionUtils.isEmpty(registerList)) {
|
return false;
|
}
|
List<MouldUseRecord> recordList = this.list(Wrappers.<MouldUseRecord>lambdaQuery().eq(MouldUseRecord::getProductMainId, productMain.getId()));
|
if (CollectionUtils.isEmpty(recordList)) {
|
return false;
|
}
|
// 清除模具使用记录表
|
List<Long> recordIdList = recordList.stream().map(MouldUseRecord::getId).collect(Collectors.toList());
|
baseMapper.deleteBatchIds(recordIdList);
|
|
// 调用模具消耗接口参数
|
for (MouldRegisterDTO register: registerList) {
|
InsertLifeDTO dto = new InsertLifeDTO();
|
dto.setMouldCode(register.getMouldCode());
|
dto.setSerialNumber(productMain.getProductNo());
|
dto.setProcedureNumber(register.getOperationName());
|
dto.setMachineNumber(register.getWorkstationNo());
|
dto.setBuildMaker(SecurityUtils.getUser().getUsername());
|
dto.setProductSpec(register.getPartNo());
|
dto.setLifeValue(BigDecimal.ZERO);
|
mouldUtil.insertTempLifeRecord(JSON.toJSONString(dto));
|
}
|
return true;
|
}
|
}
|