李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 *    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;
    }
}