zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package org.jeecg.modules.lims.testing.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.lims.testing.dto.LimsItemTargetDTO;
import org.jeecg.modules.lims.testing.entity.*;
import org.jeecg.modules.lims.testing.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @Description: lims_testing_standard
 * @Author: jeecg-boot
 * @Date: 2023-04-24
 * @Version: V1.0
 */
@Api(tags = "lims_testing_standard")
@RestController
@RequestMapping("/lims/testing/stand")
@Slf4j
public class LimsTestingStandardController extends JeecgController<LimsTestingStandard, ILimsTestingStandardService> {
    @Autowired
    private ILimsTestingStandardService limsTestingStandardService;
    @Autowired
    private ILimsTestingCategoryService limsTestingCategoryService;
    @Autowired
    private ILimsTestingTypeService limsTestingTypeService;
    @Autowired
    private ILimsTestingItemService limsTestingItemService;
    @Autowired
    private ILimsItemTargetService limsItemTargetService;
    @Autowired
    private ILimsTestingOutlineService outlineService;
 
    /**
     * 分页列表查询
     *
     * @param limsTestingStandard
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "lims_testing_standard-分页列表查询")
    @ApiOperation(value = "lims_testing_standard-分页列表查询", notes = "lims_testing_standard-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(LimsTestingStandard limsTestingStandard,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<LimsTestingStandard> queryWrapper = QueryGenerator.initQueryWrapper(limsTestingStandard, req.getParameterMap());
        Page<LimsTestingStandard> page = new Page<LimsTestingStandard>(pageNo, pageSize);
        IPage<LimsTestingStandard> pageList = limsTestingStandardService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
 
    /**
     * 添加
     *
     * @param limsTestingStandard
     * @return
     */
    @AutoLog(value = "lims_testing_standard-添加")
    @ApiOperation(value = "lims_testing_standard-添加", notes = "lims_testing_standard-添加")
    @PostMapping(value = "/add")
    @Transactional
    public Result<?> add(@RequestBody LimsTestingStandard limsTestingStandard) {
        limsTestingStandardService.save(limsTestingStandard);
 
        //添加时,将所有鉴定大纲项添加为技术标准,后续修改启用禁用字段
        String outlineId = limsTestingStandard.getOutlineId();
        if (StringUtils.isEmpty(outlineId)) return Result.OK("添加成功!");
        addStandItmTarget(limsTestingStandard);
        //
        return Result.OK("添加成功!");
    }
 
    /**
     * 添加技术标准 item
     */
    private  List<LimsItemTarget> addStandItmTarget(LimsTestingStandard limsTestingStandard) {
        //1.根据鉴定大纲outlineId查询cat
        QueryWrapper<LimsTestingCategory> catWrapper = new QueryWrapper<>();
        catWrapper.lambda().eq(LimsTestingCategory::getOutlineId, limsTestingStandard.getOutlineId());
        List<LimsTestingCategory> catList = limsTestingCategoryService.list(catWrapper);
        List<String> catIds = catList.stream().map(item -> item.getId()).collect(Collectors.toList());
        if (catIds.isEmpty()) {
            catIds.add("empty");
        }
        //2.根据cat查询tye
        QueryWrapper<LimsTestingType> typWrapper = new QueryWrapper<>();
        typWrapper.lambda().in(LimsTestingType::getCategoryId, catIds);
        List<LimsTestingType> typList = limsTestingTypeService.list(typWrapper);
        List<String> typIds = typList.stream().map(item -> item.getId()).collect(Collectors.toList());
        if (typIds.isEmpty()) {
            typIds.add("empty");
        }
        //3.根据typ查询父类itm
        QueryWrapper<LimsTestingItem> itmWrapper = new QueryWrapper<>();
        itmWrapper.lambda().in(LimsTestingItem::getTypeId, typIds);
        List<LimsTestingItem> itmList = limsTestingItemService.list(itmWrapper);
        List<String> itmIds = itmList.stream().map(item -> item.getId()).collect(Collectors.toList());
 
        //4.组装insert数据
        List<LimsItemTargetDTO> targetDTOSList = itmList.stream().map(item -> {
            //组装ITM
            LimsItemTargetDTO t = new LimsItemTargetDTO();
            t.setItemId(item.getId());
            t.setItemName(item.getName());
            t.setPid(item.getTypeId());
            t.setEnabled(0);
            t.setIsTech(0);
            t.setStandardId(limsTestingStandard.getId());
            t.setOutlineId(limsTestingStandard.getOutlineId());
            return t;
        }).map(item -> {
            //组装TYP
            List<LimsTestingType> selTypList = typList.stream().filter(i -> i.getId().equals(item.getPid())).collect(Collectors.toList());
            LimsTestingType type = selTypList.get(0);
            item.setTypeId(type.getId());
 
            item.setPpid(type.getCategoryId());
            return item;
        }).map(item -> {
            //组装CAT
            List<LimsTestingCategory> selCatList = catList.stream().filter(i -> i.getId().equals(item.getPpid())).collect(Collectors.toList());
            LimsTestingCategory cat = selCatList.get(0);
            item.setCategoryId(cat.getId());
            return item;
        }).collect(Collectors.toList());
        //dto转entity
        List<LimsItemTarget> list = targetDTOSList.stream().map(item -> {
            LimsItemTarget target = new LimsItemTarget();
            BeanUtils.copyProperties(item, target);
            return target;
        }).collect(Collectors.toList());
 
 
        limsItemTargetService.saveBatch(list);
 
        return list;
    }
 
 
    /**
     * 编辑
     *
     * @param stand
     * @return
     */
    @AutoLog(value = "lims_testing_standard-编辑")
    @ApiOperation(value = "lims_testing_standard-编辑", notes = "lims_testing_standard-编辑")
    @PutMapping(value = "/edit")
    @Transactional
    public Result<?> edit(@RequestBody LimsTestingStandard stand) {
 
        //未发布且修改技术标准大纲时才更新技术标准的大纲内容
        //1.查询大纲是否发生更改
        LimsTestingStandard ostand = limsTestingStandardService.getById(stand.getId());
        if (ostand.getOutlineId() != null && stand.getOutlineId() != null && stand.getStatus() == 0 && !ostand.getOutlineId().equals(stand.getOutlineId())) {
            //2.大纲更改,删除历史生成 item_target 记录
            QueryWrapper<LimsItemTarget> targetQueryWrapper = new QueryWrapper<>();
            targetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, ostand.getId());
            // 旧的指标
            List<LimsItemTarget> oldList = limsItemTargetService.list(targetQueryWrapper);
            limsItemTargetService.remove(targetQueryWrapper);
            //3.根据新大纲添加技术标准
            addStandItmTarget(stand);
            // 新的指标
            List<LimsItemTarget> newList = limsItemTargetService.list(targetQueryWrapper);
            // 拷贝编码或名称相同的指标
            for (LimsItemTarget oItem : oldList) {
                if (oItem.getIsTech() == 1) {
                    newList.add(oItem);
                } else {
                    for (LimsItemTarget item : newList) {
 
                        if ((item.getCode() != null && item.getCode().equals(oItem.getCode()))
                                || (item.getName() != null && item.getName().equals(oItem.getName()))
                                || (item.getItemName() != null && item.getItemName().equals(oItem.getItemName()))) {
 
                            LimsTestingType type = limsTestingTypeService.getById(item.getTypeId());
                            LimsTestingType oType = limsTestingTypeService.getById(oItem.getTypeId());
                            if ((type.getCode() != null && type.getCode().equals(oType.getCode()))
                                    || (type.getName() != null && type.getName().equals(oType.getName()))) {
 
                                LimsTestingCategory cate = limsTestingCategoryService.getById(item.getCategoryId());
                                LimsTestingCategory oCate = limsTestingCategoryService.getById(oItem.getCategoryId());
                                if ((cate.getCode() != null && cate.getCode().equals(oCate.getCode()))
                                        || (cate.getName() != null && cate.getName().equals(oCate.getName()))) {
 
                                    item.setEnabled(oItem.getEnabled());
                                    item.setIsJudge(oItem.getIsJudge());
                                    item.setIsTech(oItem.getIsTech());
                                    item.setMax(oItem.getMax());
                                    item.setTyp(oItem.getTyp());
                                    item.setMin(oItem.getMin());
                                    item.setRemark(oItem.getRemark());
                                    item.setUnit(oItem.getUnit());
                                    item.setValType(oItem.getValType());
                                    item.setVal(oItem.getVal());
                                    item.setJudgment(oItem.getJudgment());
                                }
                            }
                        }
                    }
                }
            }
            limsItemTargetService.saveOrUpdateBatch(newList);
        }
 
        //发布时查询当前技术标准的产品是否存在已发布版本,存在则更新为废止
        if (stand.getStatus() == 1) {
            queryOldStand(stand);
            limsTestingStandardService.updateById(stand);
        } else if (stand.getStatus() == 2) {
            //生成新版时先查询该产品的大纲是否更新,更新需要重新根据新大纲生成技术标准
 
 
            LimsTestingOutline oldOutline = outlineService.getById(stand.getOutlineId());
            //如果大纲是启用状态,则直接拷贝,否则需要根据新大纲生成
            if (oldOutline.getStatus() == 1 && oldOutline.getEnabled() == 1) {
                //生成新版本时需要复制大纲下所有数据
                generateNewVersion(stand);
            } else {
 
                //查询在启用的新大纲
                QueryWrapper<LimsTestingOutline> newOutQuery = new QueryWrapper<>();
                newOutQuery.lambda().eq(LimsTestingOutline::getStatus, 1);
                newOutQuery.lambda().eq(LimsTestingOutline::getEnabled, 1);
                LimsTestingOutline one = outlineService.getOne(newOutQuery);
                LimsTestingStandard newStand = new LimsTestingStandard();
                BeanUtils.copyProperties(stand, newStand);
                newStand.setId(null);
                newStand.setStatus(0);
                newStand.setCreateTime(null);
                newStand.setCreateBy(null);
                newStand.setUpdateBy(null);
                newStand.setUpdateTime(null);
                if (stand.getVer() == null) {
                    stand.setVer(1);
                } else {
                    stand.setVer(stand.getVer() + 1);
                }
                newStand.setOutlineId(one.getId());
                limsTestingStandardService.save(newStand);
                List<LimsItemTarget> newList = addStandItmTarget(newStand);
                //生成新大纲后拷贝已录入指标项
                QueryWrapper<LimsItemTarget> targetQueryWrapper = new QueryWrapper<>();
                targetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, stand.getId());
                List<LimsItemTarget> oldList = limsItemTargetService.list(targetQueryWrapper);
                targetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, newStand.getId());
                for (LimsItemTarget oItem : oldList) {
                        for (LimsItemTarget item : newList) {
 
                            if ((item.getCode() != null && item.getCode().equals(oItem.getCode()))
                                    || (item.getName() != null && item.getName().equals(oItem.getName()))
                                    || (item.getItemName() != null && item.getItemName().equals(oItem.getItemName()))) {
 
                                LimsTestingType type = limsTestingTypeService.getById(item.getTypeId());
                                LimsTestingType oType = limsTestingTypeService.getById(oItem.getTypeId());
                                if ((type.getCode() != null && type.getCode().equals(oType.getCode()))
                                        || (type.getName() != null && type.getName().equals(oType.getName()))) {
 
                                    LimsTestingCategory cate = limsTestingCategoryService.getById(item.getCategoryId());
                                    LimsTestingCategory oCate = limsTestingCategoryService.getById(oItem.getCategoryId());
                                    if ((cate.getCode() != null && cate.getCode().equals(oCate.getCode()))
                                            || (cate.getName() != null && cate.getName().equals(oCate.getName()))) {
 
                                        item.setEnabled(oItem.getEnabled());
                                        item.setIsJudge(oItem.getIsJudge());
                                        item.setIsTech(oItem.getIsTech());
                                        item.setMax(oItem.getMax());
                                        item.setTyp(oItem.getTyp());
                                        item.setMin(oItem.getMin());
                                        item.setRemark(oItem.getRemark());
                                        item.setUnit(oItem.getUnit());
                                        item.setValType(oItem.getValType());
                                        item.setVal(oItem.getVal());
                                        item.setJudgment(oItem.getJudgment());
                                    }
                                }
                            }
                        }
                    }
 
 
                limsItemTargetService.saveOrUpdateBatch(newList);
            }
 
 
            //生成新版本时不更新 技术标准,只执行复制数据,
        } else {
            limsTestingStandardService.updateById(stand);
        }
 
 
        return Result.OK("编辑成功!");
    }
 
    /**
     * 查询产品(已发布)技术标准,并更新为废止状态
     *
     * @param stand
     */
    private void queryOldStand(LimsTestingStandard stand) {
        if (stand.getProdId() == null) return;
        QueryWrapper<LimsTestingStandard> standardQueryWrapper = new QueryWrapper<>();
        standardQueryWrapper.lambda().eq(LimsTestingStandard::getProdId, stand.getProdId());
        //已发布状态
        standardQueryWrapper.lambda().eq(LimsTestingStandard::getStatus, 1);
        List<LimsTestingStandard> list = limsTestingStandardService.list(standardQueryWrapper);
        list.forEach(item -> {
            item.setStatus(2);
            limsTestingStandardService.updateById(item);
        });
    }
 
    /**
     * 生成(新版)技术标准
     *
     * @param stand
     */
    private void generateNewVersion(LimsTestingStandard stand) {
        String id = stand.getId();
        stand.setId(null);
        stand.setStatus(0);
        stand.setCreateTime(null);
        stand.setCreateBy(null);
        stand.setUpdateBy(null);
        stand.setUpdateTime(null);
        if (stand.getVer() == null) {
            stand.setVer(1);
        } else {
            stand.setVer(stand.getVer() + 1);
        }
        limsTestingStandardService.save(stand);
 
 
        QueryWrapper<LimsItemTarget> targetQueryWrapper = new QueryWrapper<>();
        targetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, id);
        List<LimsItemTarget> targetList = limsItemTargetService.list(targetQueryWrapper);
        List<LimsItemTarget> tList = targetList.stream().map(item -> {
            item.setId(null);
            //设置新的id
            item.setStandardId(stand.getId());
            return item;
        }).collect(Collectors.toList());
        limsItemTargetService.saveBatch(tList);
 
    }
 
    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "lims_testing_standard-通过id删除")
    @ApiOperation(value = "lims_testing_standard-通过id删除", notes = "lims_testing_standard-通过id删除")
    @DeleteMapping(value = "/delete")
    @Transactional
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        limsTestingStandardService.removeById(id);
        QueryWrapper<LimsItemTarget> targetQueryWrapper = new QueryWrapper<>();
        targetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, id);
        limsItemTargetService.remove(targetQueryWrapper);
        return Result.OK("删除成功!");
    }
 
    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "lims_testing_standard-批量删除")
    @ApiOperation(value = "lims_testing_standard-批量删除", notes = "lims_testing_standard-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    @Transactional
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        this.limsTestingStandardService.removeByIds(list);
        for (int i = 0; i < list.size(); i++) {
            String id = list.get(i);
            QueryWrapper<LimsItemTarget> targetQueryWrapper = new QueryWrapper<>();
            targetQueryWrapper.lambda().eq(LimsItemTarget::getStandardId, id);
            limsItemTargetService.remove(targetQueryWrapper);
        }
        return Result.OK("批量删除成功!");
    }
 
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "lims_testing_standard-通过id查询")
    @ApiOperation(value = "lims_testing_standard-通过id查询", notes = "lims_testing_standard-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        LimsTestingStandard limsTestingStandard = limsTestingStandardService.getById(id);
        if (limsTestingStandard == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(limsTestingStandard);
    }
 
    /**
     * 导出excel
     *
     * @param request
     * @param limsTestingStandard
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LimsTestingStandard limsTestingStandard) {
        return super.exportXls(request, limsTestingStandard, LimsTestingStandard.class, "lims_testing_standard");
    }
 
    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LimsTestingStandard.class);
    }
 
}