李林
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.chinaztt.mes.quality.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.chinaztt.mes.basic.entity.Part;
import com.chinaztt.mes.basic.mapper.PartMapper;
import com.chinaztt.mes.basic.util.AesUtils;
import com.chinaztt.mes.common.numgen.NumberGenerator;
import com.chinaztt.mes.quality.dto.ApplyDTO;
import com.chinaztt.mes.quality.dto.ApplyPartDTO;
import com.chinaztt.mes.quality.entity.*;
import com.chinaztt.mes.quality.mapper.ApplyMapper;
import com.chinaztt.mes.quality.mapper.ApplyPartMapper;
import com.chinaztt.mes.quality.mapper.ResultMapper;
import com.chinaztt.mes.quality.service.ApplyService;
import com.chinaztt.mes.quality.state.result.constant.ResultStateStringValues;
import com.chinaztt.mes.quality.utils.ResultUtils;
import com.chinaztt.mes.quality.utils.TestApplyRuleUtils;
import com.chinaztt.mes.warehouse.entity.Stock;
import com.chinaztt.mes.warehouse.mapper.StockMapper;
import com.chinaztt.ztt.admin.api.feign.RemoteParamService;
import com.chinaztt.ztt.common.core.constant.SecurityConstants;
import com.chinaztt.ztt.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
 
/**
 * 检测申请表
 *
 * @author cxf
 * @date 2021-04-01 13:34:45
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements ApplyService {
    private NumberGenerator<Apply> applyNumberGenerator;
    private ApplyPartMapper applyPartMapper;
    private ResultMapper resultMapper;
    private StockBean stockBean;
    private ResultUtils resultUtils;
    private static final String STATE = "To be Inspected";
    private TestApplyRuleUtils testApplyRuleUtils;
    private RemoteParamService remoteParamService;
    private StockMapper stockMapper;
    private PartMapper partMapper;
 
    @Override
    public boolean saveDto(ApplyDTO applyDTO) {
        checkApplyNo(applyDTO);
        String ruleApply = remoteParamService.getByKey(Apply.RULE_APPLY, SecurityConstants.FROM_IN).getData();
        //系统参数开关打开&&明细列表不为空&&不是系统规则触发的自动创建成品检
        if(ruleApply.equals("true") && null != applyDTO.getApplyPartList() && !applyDTO.getFinishedProductAutoInsp()){
            testApplyRuleUtils.adjustApplyDTONew(applyDTO);
            if(!applyDTO.getAutoCreateApplyDoor()){
                //不自动报检
                return false;
            }
        }
        //剔除掉相同批次号已申请的对象(工作台批量提交时有两个嵌套循环,第一轮循环是产出列表,第二轮循环是遍历检测结果为null的检测结果列表)
        testApplyRuleUtils.removeAlExistObj(applyDTO.getApplyType(),applyDTO.getApplyPartList());
        baseMapper.insert(applyDTO);
        if (CollectionUtil.isNotEmpty(applyDTO.getApplyPartList())) {
            applyDTO.getApplyPartList().stream().forEach(applyPartDTO -> {
                // 添加申请id
                applyPartDTO.setApplyId(applyDTO.getId());
            });
            saveApplyPartList(applyDTO.getApplyPartList(), applyDTO.getApplyType());
            return true;
        }else{
            //如果是工作台自动报检的则删除空单据,否则判断为手动新增申请单主表
            if (StringUtils.isNotBlank(applyDTO.getRemark()) && applyDTO.getRemark().contains("自动报检")) {
                baseMapper.deleteById(applyDTO);
            }
            return false;//这里返回false可以屏蔽工作台将测结果的申请id更新为null【2022-09-20】
        }
 
    }
 
    @Override
    public boolean saveApplyPartList(List<ApplyPartDTO> applyPartDTOList) {
        if (!CollectionUtils.isEmpty(applyPartDTOList)) {
            Long id = applyPartDTOList.get(0).getApplyId();
            Apply apply = baseMapper.selectById(id);
            saveApplyPartList(applyPartDTOList, apply.getApplyType());
        }
        return true;
    }
 
 
    public boolean saveApplyPartList(List<ApplyPartDTO> applyPartDTOList, String applyType) {
        applyPartDTOList.stream().forEach(applyPartDTO -> {
            // 初始化
            Boolean isQualified = null;
            Boolean isUsed = false;
            applyPartDTO.setIsQualified(null);
            //产出报检就这几个?
            if (Apply.FIRST_APPLY.equals(applyType) || Apply.OUTPUT_APPLY.equals(applyType) || Apply.PRODUCT_APPLY.equals(applyType) || Apply.PROCESS_APPLY.equals(applyType)) {
                // 产出报检:
                // 查检测标准编号(计划)
                List<TestStandard> testStandardList = applyPartMapper.findTestStandardNoBySystemNo(applyPartDTO.getSystemNo());//这里没有根据检测类型取查对应的检测标准
                if (CollectionUtil.isNotEmpty(testStandardList) && testStandardList.size() == 1) {
                    applyPartDTO.setTestStandardNo(testStandardList.get(0).getStandardNo());
                    applyPartDTO.setMoTestStandardId(testStandardList.get(0).getId());
                }
                // 添加显示字段信息,例如批号,零件号,零件描述等
                JSONObject showInfo = applyPartMapper.selectShowInfoBySystemNo(applyPartDTO.getSystemNo());
                if (showInfo != null) {
                    if (StringUtils.isBlank(applyPartDTO.getOutBatchNo())) {
                        applyPartDTO.setLotBatchNo(showInfo.getString("out_batch_no"));//批号
                    } else {
                        applyPartDTO.setLotBatchNo(applyPartDTO.getOutBatchNo());
                    }
                    if (StringUtils.isBlank(applyPartDTO.getPartNo())) {
                        applyPartDTO.setPartNo(showInfo.getString("part_no"));//零件编号
                    }
                    if (StringUtils.isBlank(applyPartDTO.getPartName())) {
                        applyPartDTO.setPartDesc(showInfo.getString("part_name"));//零件描述
                    } else {
                        applyPartDTO.setPartDesc(applyPartDTO.getPartName());
                    }
                    if (applyPartDTO.getPartId() == null) {
                        applyPartDTO.setPartId(showInfo.getLong("part_id"));//零件id
                    }
                    if (applyPartDTO.getCheckLength() == null) {
                        applyPartDTO.setQtyArrived(showInfo.getBigDecimal("product_qty").stripTrailingZeros().toPlainString());
                        applyPartDTO.setTotalLength(new BigDecimal(applyPartDTO.getQtyArrived()));
                    } else {
                        applyPartDTO.setQtyArrived(applyPartDTO.getCheckLength().stripTrailingZeros().toPlainString());
                        applyPartDTO.setTotalLength(new BigDecimal(applyPartDTO.getQtyArrived()));
                    }
                } else {
                    Stock stock = stockMapper.selectById(applyPartDTO.getStockId());
                    if (stock == null) {
                        throw new RuntimeException("不存在该零件的报检信息");
                    }
                    applyPartDTO.setLotBatchNo(stock.getPartBatchNo());
                    Part part = partMapper.selectById(stock.getPartId());
                    applyPartDTO.setPartNo(part.getPartNo());
                    applyPartDTO.setPartDesc(part.getPartName());
                    applyPartDTO.setPartId(part.getId());
                    applyPartDTO.setQtyArrived(stock.getStockQuantity().stripTrailingZeros().toPlainString());
                    applyPartDTO.setTotalLength(stock.getStockQuantity());
                    applyPartDTO.setStockId(stock.getId());
                    applyPartDTO.setSourceLocationId(stock.getLocationId());
                }
            } else if (Apply.LOCAL_APPLY.equals(applyType)) {
                // 本地库存产出报检:
                if (applyPartDTO.getIsUsed() != null) {
                    // 在检测结果登记前是否允许该物料被使用,否:待检测/检测不合格/不允许使用,是:待检测,其他不变
                    if (applyPartDTO.getIsUsed()) {
                        isUsed = null;
                    } else {
                        isQualified = false;
                    }
                }
            }
            applyPartDTO.setUnqualifiedArrived(BigDecimal.ZERO.toString());
            Long operationId = applyPartMapper.selectOperationIdBySystemNo(applyPartDTO.getSystemNo());//获取工序id
            applyPartDTO.setOperationId(operationId);
            applyPartMapper.insert(applyPartDTO);
            // 添加检测结果表
//            resultUtils.addOrUpdateResult(applyPartDTO.getSystemNo(), applyPartDTO.getLotBatchNo(), ResultStateStringValues.UNTESTED,
//                    null, isQualified, isUsed, null, applyPartDTO.getPartId(), null, applyType);
            resultUtils.addOrUpdateResult(applyPartDTO.getSystemNo(), applyPartDTO.getLotBatchNo(), ResultStateStringValues.UNTESTED,
                    null, isQualified, isUsed, null, applyPartDTO.getPartId(), applyPartDTO.getApplyId(), applyType);
        });
        return true;
    }
 
    @Override
    public boolean updateById(Apply apply) {
        checkApplyNo(apply);
        return SqlHelper.retBool(baseMapper.updateById(apply));
    }
 
    private void checkApplyNo(Apply apply) {
 
        if (StringUtils.isBlank(apply.getApplyNo())) {
            // 1. 自动生成编号
            apply.setApplyNo(applyNumberGenerator.generateNumberWithPrefix(Apply.DIGIT, Apply.PREFIX, Apply::getApplyNo));
        } else {
            // 2.判断是否重复
            List<Apply> repeatNos = baseMapper.selectList(Wrappers.<Apply>query().lambda().eq(Apply::getApplyNo, apply.getApplyNo()).ne(Apply::getId, apply.getId()));
            if (CollectionUtil.isNotEmpty(repeatNos)) {
                throw new RuntimeException("编号重复");
            }
        }
    }
 
    @Override
    public boolean deleteById(Long id) {
        applyPartMapper.delete(Wrappers.<ApplyPart>query().lambda().eq(ApplyPart::getApplyId, id));
        return SqlHelper.retBool(baseMapper.deleteById(id));
    }
 
    @Override
    public JSONObject getStock(Apply apply) {
        UUID uuid = UUID.randomUUID();
        String s = UUID.randomUUID().toString();
        JSONObject inAttr = new JSONObject();
        inAttr.put("stateDb", STATE);
        Map<String, Object> stockMap = new HashMap<>(16);
        stockMap.put("contract", stockBean.getRegion());
        try {
            System.out.println(stockBean.getSecretKey());
            System.out.println(stockBean.getCleartextSecretKey());
            if (StringUtils.isNotBlank(stockBean.getSecretKey())) {
                stockMap.put("contractKey", stockBean.getSecretKey());
            } else if (StringUtils.isNotBlank(stockBean.getCleartextSecretKey())) {
                stockMap.put("contractKey", AesUtils.encrypt(stockBean.getCleartextSecretKey()));
            }
        } catch (Exception e) {
            throw new RuntimeException("明文密钥加密失败");
        }
        stockMap.put("userId", SecurityUtils.getUser().getUsername());
        stockMap.put("inAttr", inAttr);
        String result = HttpRequest.get(stockBean.getIp()).form(stockMap).execute().body();
        JSONObject jsonObject1 = JSONObject.parseObject(result);
        return jsonObject1;
    }
}