李林
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
/*
 *    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.quality.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.chinaztt.mes.basic.entity.Part;
import com.chinaztt.mes.basic.mapper.PartMapper;
import com.chinaztt.mes.common.numgen.NumberGenerator;
import com.chinaztt.mes.quality.dto.ReportDTO;
import com.chinaztt.mes.quality.dto.ReportItemsDTO;
import com.chinaztt.mes.quality.entity.*;
import com.chinaztt.mes.quality.mapper.*;
import com.chinaztt.mes.quality.service.TestReportService;
import com.chinaztt.ztt.common.security.service.ZttUser;
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.transaction.interceptor.TransactionAspectSupport;
 
import java.time.LocalDateTime;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
/**
 * 检测汇报
 *
 * @author liuth
 * @date 2020-10-21 09:04:30
 */
@Service
@AllArgsConstructor
public class TestReportServiceImpl extends ServiceImpl<TestReportMapper, TestReport> implements TestReportService {
    public static final Pattern QUOTATION_MARK_PATTERN = Pattern.compile("\"(.*?)\"");
    private NumberGenerator<TestReport> numberGenerator;
    private ReportItemsMapper reportItemsMapper;
    private TaskMapper taskMapper;
    private TemplateMapper templateMapper;
    private ItemsMapper itemsMapper;
    private PartMapper partMapper;
    private TestReportProcessingMapper testReportProcessingMapper;
 
    /**
     * 关联删除
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean fullDelete(Long id) {
        baseMapper.deleteById(id);
        //删除检测汇报对应的检测项信息
        reportItemsMapper.delete(Wrappers.<ReportItems>lambdaQuery().eq(ReportItems::getTestReportId, id));
        return true;
 
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean fullSave(ReportDTO reportDTO) {
        try {
            ZttUser currentUser = SecurityUtils.getUser();
            reportDTO.setTestUser(currentUser.getUsername());
            reportDTO.setOrderNo(numberGenerator.generateNumberWithPrefix(TestReport.DIGIT, TestReport.PREFIX, TestReport::getOrderNo));
            reportDTO.setTestTime(LocalDateTime.now());
            baseMapper.insert(reportDTO);
 
            if (CollectionUtil.isNotEmpty(reportDTO.getReportItems())) {
                System.out.println(reportDTO.getReportItems());
                reportDTO.getReportItems().forEach(a -> {
                    a.setTestReportId(reportDTO.getId());
                    reportItemsMapper.insert(a);
                });
            }
        } catch (Exception e) {
            log.error("创建检测汇报失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
 
    }
 
    @Override
    public ReportDTO getFullById(Long id) {
        ReportDTO reportDTO = new ReportDTO();
        //当不为空时获取零件检测信息
        if (id != 0L) {
            // 1.获取检测汇报基本信息
            reportDTO = baseMapper.selectDtoById(id);
            //获取相关汇报检测项信息
            List<ReportItems> reportItemsList = reportItemsMapper.selectList(Wrappers.<ReportItems>lambdaQuery().eq(ReportItems::getTestReportId, reportDTO.getId()));
            if (CollectionUtil.isNotEmpty(reportItemsList)) {
                reportDTO.setReportItems(reportItemsList);
            }
            //2.获取相关待检测任务信息
            List<Task> taskList = taskMapper.selectList(Wrappers.<Task>lambdaQuery().eq(Task::getId, reportDTO.getTestTaskId()));
            if (CollectionUtil.isNotEmpty(taskList)) {
                reportDTO.setTasks(taskList);
            }
            // 3.获取相关检测模板信息
            List<Template> templateList = templateMapper.selectList(Wrappers.<Template>lambdaQuery().eq(Template::getId, taskList.get(0).getTemplateId()));
            if (CollectionUtil.isNotEmpty(templateList)) {
                reportDTO.setTemplates(templateList);
            }
        }
        return reportDTO;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean fullUpdate(ReportDTO reportDTO) {
        try {
            Set<Long> reportItemsIds = new HashSet();
            //更新主表
            baseMapper.updateById(reportDTO);
            //更新汇报检验项关系表
            if (CollectionUtil.isNotEmpty(reportDTO.getReportItems())) {
                reportDTO.getReportItems().forEach(a -> {
                    if (a.getId() == null || a.getId() == 0) {
                        a.setTestReportId(reportDTO.getId());
                        reportItemsMapper.insert(a);
                    } else {
                        reportItemsMapper.updateById(a);
                    }
                    reportItemsIds.add(a.getId());
                });
            }
 
            LambdaQueryWrapper<ReportItems> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ReportItems::getTestReportId, reportDTO.getId());
            if (CollectionUtil.isNotEmpty(reportItemsIds)) {
                wrapper.notIn(ReportItems::getId, reportItemsIds);
            }
            reportItemsMapper.delete(wrapper);
        } catch (Exception e) {
            log.error("更新检测汇报失败", e);
            throw new RuntimeException("更新检测汇报失败");
        }
        return true;
    }
 
    @Override
    public IPage<List<TestReport>> getReportPage(Page page, QueryWrapper<TestReport> ew) {
        return baseMapper.getReportPage(page, ew);
    }
 
    @Override
    public Map<String, Object> getReportAndItemsPage(Page page, Long reportId, String templateName) {
        //通过汇报结果id 获取检测项名集合
        List<ReportItems> reportItemsList = reportItemsMapper.selectList(Wrappers.<ReportItems>lambdaQuery().eq(ReportItems::getTestReportId, reportId));
        List<String> nameList = reportItemsList.stream().map(ReportItems::getTestItems).collect(Collectors.toList());
        System.out.println(nameList);
        return baseMapper.getReportAndItemsPage(page, nameList, templateName);
    }
 
    @Override
    public IPage<List<ReportDTO>> getTestResultPage(Page page, QueryWrapper<TestReport> gen) {
        return baseMapper.getTestResultPage(page, gen);
    }
 
    @Override
    public List<ReportItemsDTO> getTestResult(TestReport report) {
        Part part = partMapper.selectOne(Wrappers.<Part>lambdaQuery().eq(Part::getPartNo, report.getPartNo()));
        //获取列名
        List<ReportItemsDTO> reports = baseMapper.getTestResult(part.getId());
        //拼接crosstab所需要的验证数据
        ReportItemsDTO newReport = baseMapper.getTest(part.getId());
        //行转列
        List<JSONObject> reportList = baseMapper.getTestValue(part.getId(), newReport, report);
        if (CollectionUtil.isNotEmpty(reportList)) {
            reports.get(0).setReportList(reportList);
        }
        return reports;
    }
 
    @Override
    public IPage<List<ReportDTO>> getTestReportProcessingPage(Page page, QueryWrapper<TestReport> gen) {
        return baseMapper.getTestReportProcessingPage(page, gen);
    }
 
    @Override
    public boolean fullReportUpdate(ReportDTO reportDTO) {
        TestReportProcessing newTestReportProcessing = testReportProcessingMapper.selectOne(Wrappers.<TestReportProcessing>lambdaQuery().eq(TestReportProcessing::getQualityTestReportId, reportDTO.getId()));
        TestReportProcessing testReportProcessing = new TestReportProcessing();
        testReportProcessing.setNumber(reportDTO.getNumber());
        testReportProcessing.setQualityTestReportId(reportDTO.getId());
        testReportProcessing.setRemarks(reportDTO.getRemarks());
        testReportProcessing.setType(reportDTO.getType());
        if (newTestReportProcessing == null) {
            testReportProcessingMapper.insert(testReportProcessing);
        } else {
            testReportProcessing.setId(newTestReportProcessing.getId());
            testReportProcessingMapper.updateById(testReportProcessing);
        }
        return true;
    }
 
    @Override
    public ReportDTO getReportById(Long id) {
        ReportDTO reportDTO = new ReportDTO();
        if (id != 0L) {
            reportDTO = baseMapper.getReportById(id);
        }
        return reportDTO;
    }
 
    @Override
    public Map<String, List<List<String>>> getTestReportResult(TestReport report) {
        Map<String, List<List<String>>> result = new HashMap<>(2);
        //获取列名
        Part part = partMapper.selectOne(Wrappers.<Part>lambdaQuery().eq(Part::getPartNo, report.getPartNo()));
        ReportItemsDTO newReport = baseMapper.getTest(part.getId());
        List<List<String>> head = new ArrayList<>();
        List<String> headList = new ArrayList<>();
        String str = newReport.getTestItems();
        Matcher m = QUOTATION_MARK_PATTERN.matcher(str);
        while (m.find()) {
            String headValue = m.group().replace("\"", "");
            // 过滤分组用的id字段
            if (headValue.contains("id") || headValue.contains("Id")) {
                continue;
            }
            String label = com.chinaztt.mes.quality.excel.TestReport.getValue(headValue);
            headList.add(headValue);
            List<String> headName = new ArrayList<>();
            if (StringUtils.isNotBlank(label)) {
                headName.add(label);
            } else {
                headName.add(headValue);
            }
            head.add(headName);
        }
        //获取数据
        List<JSONObject> list = baseMapper.getTestValue(part.getId(), newReport, report);
        List<List<String>> data = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(list)) {
            for (JSONObject json : list) {
                List<String> dataValue = new ArrayList<>();
                for (String headName : headList) {
                    dataValue.add(json.getString(headName));
                }
                data.add(dataValue);
            }
        }
        result.put("data", data);
        result.put("head", head);
        return result;
    }
}