李林
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package com.chinaztt.mes.quality.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.druid.sql.visitor.functions.Trim;
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.StringUtils;
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.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.chinaztt.mes.basic.entity.Param;
import com.chinaztt.mes.basic.service.ParamService;
import com.chinaztt.mes.basic.util.DictUtils;
import com.chinaztt.mes.common.numgen.NumberGenerator;
import com.chinaztt.mes.quality.dto.TestStandardDTO;
import com.chinaztt.mes.quality.entity.ReportSample;
import com.chinaztt.mes.quality.entity.TestStandard;
import com.chinaztt.mes.quality.entity.TestStandardParam;
import com.chinaztt.mes.quality.excel.TestStandardData;
import com.chinaztt.mes.quality.mapper.TestStandardMapper;
import com.chinaztt.mes.quality.mapper.TestStandardParamMapper;
import com.chinaztt.mes.quality.service.ReportSampleService;
import com.chinaztt.mes.quality.service.TestStandardParamService;
import com.chinaztt.mes.quality.service.TestStandardService;
import com.chinaztt.ztt.admin.api.entity.SysDictItem;
import com.chinaztt.ztt.common.core.util.R;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 检测标准
 *
 * @author cxf
 * @date 2021-04-22 09:03:06
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class TestStandardServiceImpl extends ServiceImpl<TestStandardMapper, TestStandard> implements TestStandardService {
    private NumberGenerator<TestStandard> testStandardNumberGenerator;
    private TestStandardParamMapper testStandardParamMapper;
    private TestStandardParamService testStandardParamService;
    private ParamService paramService;
    private ReportSampleService reportSampleService;
    private TestStandardMapper testStandardMapper;
    private DictUtils dictUtils;
    @Override
    public boolean save(TestStandard testStandard) {
        checkStandardNo(testStandard);
        return SqlHelper.retBool(baseMapper.insert(testStandard));
    }
 
    @Override
    public boolean updateById(TestStandard testStandard) {
        checkStandardNo(testStandard);
        return SqlHelper.retBool(baseMapper.updateById(testStandard));
    }
 
    private void checkStandardNo(TestStandard testStandard) {
        if (StringUtils.isBlank(testStandard.getStandardNo())) {
            // 1. 自动生成编号
            testStandard.setStandardNo(testStandardNumberGenerator.generateNumberWithPrefix(TestStandard.DIGIT, TestStandard.PREFIX, TestStandard::getStandardNo));
        } else {
            // 2.判断是否重复
            List<TestStandard> repeatNos = baseMapper.selectList(Wrappers.<TestStandard>query().lambda().eq(TestStandard::getStandardNo, testStandard.getStandardNo()).ne(TestStandard::getId, testStandard.getId()));
            if (CollectionUtil.isNotEmpty(repeatNos)) {
                throw new RuntimeException("编号重复");
            }
        }
    }
    public TestStandard copyTestStandard(TestStandard testStandard){
        TestStandard newTestStandard = new TestStandard();
        if(testStandard!=null){
            newTestStandard.setStandardNo(testStandardNumberGenerator.generateNumberWithPrefix(TestStandard.DIGIT, TestStandard.PREFIX, TestStandard::getStandardNo));
            newTestStandard.setStandardName(testStandard.getStandardName());
            newTestStandard.setActive(true);
            newTestStandard.setRemark(testStandard.getRemark());
            newTestStandard.setState(testStandard.getState());
            newTestStandard.setVersion(testStandard.getVersion());
            baseMapper.insert(newTestStandard);
            //查询检测的参数
            List<TestStandardParam> testStandardParamList = testStandardParamMapper.selectList(Wrappers.<TestStandardParam>lambdaQuery().eq(TestStandardParam::getTestStandardId,testStandard.getId()));
            for (TestStandardParam testStandardParam:testStandardParamList){
                testStandardParam.setTestStandardId(newTestStandard.getId());
                testStandardParamMapper.insert(testStandardParam);
            }
        }
        return newTestStandard;
    }
    @Override
    public R copyTestById(Long id) {
        TestStandard testStandard = baseMapper.selectById(id);
        if (testStandard!=null){
            TestStandard newTestStandard = new TestStandard();
            checkStandardNo(newTestStandard);
            newTestStandard.setStandardName(testStandard.getStandardName());
            newTestStandard.setVersion(testStandard.getVersion());
            newTestStandard.setState(testStandard.getState());
            newTestStandard.setRemark(testStandard.getRemark());
            newTestStandard.setActive(testStandard.getActive());
            newTestStandard.setJudgeFormula(testStandard.getJudgeFormula());
            newTestStandard.setInspectionType(testStandard.getInspectionType());
            newTestStandard.setOperationId(testStandard.getOperationId());
            baseMapper.insert(newTestStandard);
            List<TestStandardParam> testStandardParamList = testStandardParamMapper.selectList(Wrappers.<TestStandardParam>lambdaQuery().eq(TestStandardParam::getTestStandardId,testStandard.getId()));
            if (CollectionUtil.isNotEmpty(testStandardParamList)){
                for (TestStandardParam testStandardParam:testStandardParamList){
                    TestStandardParam newTestStandardParam = new TestStandardParam();
                    newTestStandardParam.setTestStandardId(newTestStandard.getId());
                    newTestStandardParam.setActive(testStandardParam.getActive());
                    newTestStandardParam.setCode(testStandardParam.getCode());
                    newTestStandardParam.setIndex(testStandardParam.getIndex());
                    newTestStandardParam.setParameterItem(testStandardParam.getParameterItem());
                    newTestStandardParam.setReferenceValue(testStandardParam.getReferenceValue());
                    newTestStandardParam.setType(testStandardParam.getType());
                    newTestStandardParam.setUnit(testStandardParam.getUnit());
                    newTestStandardParam.setParamType(testStandardParam.getParamType());
                    newTestStandardParam.setJudgeFormula(testStandardParam.getJudgeFormula());
                    newTestStandardParam.setValueFormula(testStandardParam.getValueFormula());
                    newTestStandardParam.setIsReference(testStandardParam.getIsReference());
                    newTestStandardParam.setWireCore(testStandardParam.getWireCore());
                    testStandardParamMapper.insert(newTestStandardParam);
                }
            }
        }
        return R.ok();
    }
 
    @Override
    public void importExcel(List<TestStandardData> cachedDataList) {
        // 根据标准编号分组
        LinkedHashMap<String, List<TestStandardData>> groupMap = cachedDataList.stream().collect(Collectors.groupingBy(TestStandardData::getStandardNo, LinkedHashMap::new, Collectors.toList()));
 
        List<TestStandardParam> paramList = new ArrayList<>();
        // 循环新增
        for (Map.Entry<String, List<TestStandardData>> dataList : groupMap.entrySet()) {
            TestStandard testStandard = baseMapper.selectOne(Wrappers.<TestStandard>query().lambda().eq(TestStandard::getStandardNo, dataList.getKey()));
            // 如果检测标准不存在则为新增操作
            if (testStandard == null) {
                testStandard = new TestStandard();
                // 找出存在标准名称的主数据
                Optional<TestStandardData> optional = dataList.getValue().stream().filter(e -> e.getStandardName() != null).findFirst();
                if (!optional.isPresent()) {
                    throw new RuntimeException("标准编号为" + dataList.getValue().get(0).getStandardNo() + "不能没有标准名称");
                }
                //根据工序名称获取工序id
                Long operationId = baseMapper.getOperationIdByOperationName(optional.get().getOperationName());
                if(operationId != null){
                    testStandard.setOperationId(operationId);
                }
                testStandard.setStandardNo(optional.get().getStandardNo());
                testStandard.setStandardName(optional.get().getStandardName());
                testStandard.setJudgeFormula(org.springframework.util.StringUtils.trimAllWhitespace(optional.get().getJudgeFormula()));
                if(false){
                    //用户在Excel中维护好直接导入,重复的原始code,系统无法替换
                    if(StringUtils.isNotBlank(testStandard.getJudgeFormula())){
                        if(testStandard.getJudgeFormula().contains("_")){
                            throw new RuntimeException("检测标准主表判断公式 = " + testStandard.getJudgeFormula() + " -> 包含字符 < _ > -> 被系统规则征用");
                        }
                    }
                }
                List<SysDictItem> dict = dictUtils.getDict("apply_report_type");
                Boolean door = false;
                if(CollectionUtils.isNotEmpty(dict)){
                    for(SysDictItem sysDictItem : dict){
                        if(sysDictItem.getLabel().equals(optional.get().getInspectionType())){
                            testStandard.setInspectionType(sysDictItem.getValue());
                            door = true;
                            break;
                        }
                    }
                }else{
                    throw new RuntimeException("数据字典为空 -> 数据字典code = " + "【apply_report_type】");
                }
                //testStandard.setInspectionType(optional.get().getInspectionType());
                if(!door){
                    throw new RuntimeException("检测类型 = " + "【" + optional.get().getInspectionType() + "】在数据字典中不存在");
                }
                baseMapper.insert(testStandard);
            }
            long index = 1;
            for (TestStandardData testStandardData : dataList.getValue()) {
                Param param = paramService.getOne(Wrappers.<Param>lambdaQuery().eq(Param::getCode, testStandardData.getCode()));
                if (param == null) {
                    throw new RuntimeException("参数编号为"+ testStandardData.getCode() +"在参数列表中未找到");
                }
                if(true){
                    if(param.getCode().contains("_")){
                        throw new RuntimeException("参数编号 = " + param.getCode() + " -> 包含字符 < _ > -> 被系统规则征用");
                    }
                    if(testStandardData.getParameterItem().contains("_")){
                        throw new RuntimeException("检测项名称 = " + testStandardData.getParameterItem() + " -> 包含字符 < _ > -> 被系统规则征用");
                    }
                }
                TestStandardParam standardParam = new TestStandardParam();
                //统一在检测标准批准时校正,保证导入时的效率
                if(StringUtils.isNotBlank(testStandardData.getWireCore()) && testStandardData.getWireCore().trim().length() > 0){
                    if(testStandardData.getWireCore().contains("_")){
                        throw new RuntimeException("线芯 = " + testStandardData.getWireCore() + " -> 包含被系统规则征用的字符 = < _ >");
                    }else{
                        standardParam.setWireCore(testStandardData.getWireCore());//线芯
                    }
                }
                standardParam.setCode(param.getCode());
                standardParam.setParameterItem(testStandardData.getParameterItem());
                standardParam.setParameterFormat(param.getParameterFormat());
                standardParam.setType(param.getType());
                standardParam.setUnit(param.getUnit());
                String referenceValue = StringUtils.isNotBlank(testStandardData.getReferenceValue1()) ? testStandardData.getReferenceValue1():"";
                if (StringUtils.isNotBlank(testStandardData.getReferenceValue2())) {
                    referenceValue = referenceValue + "-" + testStandardData.getReferenceValue2();
                }
                standardParam.setReferenceValue(referenceValue);//参考值
                standardParam.setTestStandardId(testStandard.getId());
                standardParam.setIndex(index);
                standardParam.setJudgeFormula(org.springframework.util.StringUtils.trimAllWhitespace(testStandardData.getParamJudgeFormula()));
                standardParam.setValueFormula(org.springframework.util.StringUtils.trimAllWhitespace(testStandardData.getValueFormula()));
                if(StringUtils.isNotBlank(testStandardData.getIsReference()) && testStandardData.getIsReference().equals("是")){
                    standardParam.setIsReference(true);
                }else{
                    standardParam.setIsReference(false);
                }
                if(StringUtils.isNotBlank(testStandardData.getIsCheck()) && testStandardData.getIsCheck().equals("是")){
                    standardParam.setIsCheck(true);
                }else{
                    standardParam.setIsCheck(false);
                }
                standardParam.setParamType(testStandardData.getParamType());
                standardParam.setPosition(testStandardData.getPosition());
                standardParam.setAisle(testStandardData.getAisle());
                paramList.add(standardParam);
                index++;
            }
        }
        testStandardParamService.saveBatch(paramList, paramList.size());
    }
 
    @Override
    public R removeByIds(List<Long> ids) {
        List<Long> deleteIds = new ArrayList<>();
        for (Long id : ids) {
            int count = reportSampleService.count(Wrappers.<ReportSample>lambdaQuery().eq(ReportSample::getIsMoTestStandard, false).eq(ReportSample::getTestStandardId, id));
            if (count == 0) {
                deleteIds.add(id);
            }
        }
        if (deleteIds.size() == 0) {
            return R.failed("所选检测标准已被引用,无法删除");
        }
        this.baseMapper.deleteBatchIds(deleteIds);
        testStandardParamMapper.deleteByStandardIds(deleteIds);
        return R.ok();
    }
 
    @Override
    public TestStandard getByNo(String no) {
        TestStandard testStandard = testStandardMapper.selectOne(Wrappers.<TestStandard>lambdaQuery().eq(TestStandard::getActive, true)
                .eq(TestStandard::getStandardNo, no));
        if (testStandard == null) {
            throw new RuntimeException("根据检测标准编号,未查到基础检测标准,请确认");
        }
        return testStandard;
    }
 
    @Override
    public IPage<List<TestStandardDTO>> pageInfo(Page page, QueryWrapper<TestStandard> gen) {
        return baseMapper.pageInfo(page,gen);
    }
 
    @Override
    public TestStandardDTO getInfoById(Long id) {
        return baseMapper.getInfoById(id);
    }
 
    @Override
    public void  approveTestStandard(TestStandard testStandard){
        baseMapper.updateById(testStandard);
        //保存明细(校正导入的检测标准参数)
        List<TestStandardParam> testStandardParamList = testStandardParamMapper.selectList(Wrappers.<TestStandardParam>lambdaQuery().eq(TestStandardParam::getTestStandardId,testStandard.getId()));
        testStandardParamService.saveList(testStandardParamList);
    }
 
 
}