zss
2025-03-11 eeb8d7faa8d25b3ca9fe75ef28f035c49af5b06d
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
package com.ruoyi.basic.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
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.ruoyi.basic.dto.ProductDTO1;
import com.ruoyi.basic.excel.StructureTestObjectData;
import com.ruoyi.basic.mapper.ProductMapper;
import com.ruoyi.basic.mapper.StandardTreeMapper;
import com.ruoyi.basic.mapper.StructureTestObjectMapper;
import com.ruoyi.basic.pojo.*;
import com.ruoyi.basic.service.LaboratoryService;
import com.ruoyi.basic.service.ProductService;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.QueryWrappers;
import com.ruoyi.basic.service.StandardProductListService;
import com.ruoyi.basic.service.StructureItemParameterService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
/**
* @author z1292
* @description 针对表【product(产品表)】的数据库操作Service实现
* @createDate 2024-04-26 01:11:02
*/
@Service
@AllArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService {
 
    private ProductMapper productMapper;
 
    private LaboratoryService laboratoryService;
 
    private StructureTestObjectMapper structureTestObjectMapper;
 
    private StandardProductListService standardProductListService;
 
    private StandardTreeMapper standardTreeMapper;
 
    private StructureItemParameterService structureItemParameterService;
 
    @Override
    public IPage<Product> selectProductListByObjectId(Page page, ProductDTO1 product) {
        String partNo = product.getPartNo();
        product.setPartNo(null);
        return productMapper.selectProductListByObjectId(page, QueryWrappers.queryWrappers(product), partNo);
    }
 
    @Override
    public int addProduct(Product product) {
 
        return productMapper.insert(product);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int upProduct(Product product) {
        // 查询原本的名称
        Product oldProduct = productMapper.selectById(product.getId());
 
        if (!oldProduct.getName().equals(product.getName())) {
            // 修改名称匹配的标准树下的检验项目
            // 查询所有对象+名称的树
            StructureTestObject testObject = structureTestObjectMapper.selectById(oldProduct.getObjectId());
 
            List<StandardProductList> standardProductLists = standardProductListService.list(Wrappers.<StandardProductList>lambdaUpdate()
                    .eq(StandardProductList::getSample, oldProduct.getName())
                    .eq(StandardProductList::getSampleType, testObject.getSpecimenName()));
            if (CollectionUtils.isNotEmpty(standardProductLists)) {
                for (StandardProductList standardProductList : standardProductLists) {
                    // 修改样品名称
                    standardProductList.setSample(product.getName());
                    // 修改树名称
                    // 需要截取第四级, 避免三四级名称一样修改错误
                    String[] trees = standardProductList.getTree().split(" - ");
                    trees[3] = product.getName();
                    List<String> list = CollUtil.newArrayList(trees);
                    String newName = CollUtil.join(list, " - ");
                    standardProductList.setTree(newName);
                }
                standardProductListService.updateBatchById(standardProductLists);
            }
 
            // 修改检验项目参数的检验对象
            // 拼接["object","product"]查询检验项目参数修改绑定的检验对象
            String format = "[\"{}\",\"{}\"]";
            String sampleOld = StrUtil.format(format, testObject.getSpecimenName(), oldProduct.getName());
            List<StructureItemParameter> itemParameterList = structureItemParameterService.list(Wrappers.<StructureItemParameter>lambdaQuery()
                    .like(StructureItemParameter::getSample, sampleOld));
            if (CollectionUtils.isNotEmpty(itemParameterList)) {
                for (StructureItemParameter structureItemParameter : itemParameterList) {
                    // 修改绑定的样品名称
                    String sampleNew = StrUtil.format(format, testObject.getSpecimenName(), product.getName());
                    String sampleUp = structureItemParameter.getSample().replace(sampleOld, sampleNew);
                    structureItemParameter.setSample(sampleUp);
                }
                structureItemParameterService.updateBatchById(itemParameterList);
            }
 
            // 修改树的型号
            standardTreeMapper.update(null, Wrappers.<StandardTree>lambdaUpdate()
                    .eq(StandardTree::getSampleType, testObject.getSpecimenName())
                    .eq(StandardTree::getSample, oldProduct.getName())
                    .set(StandardTree::getSample, product.getName()));
        }
 
        return productMapper.updateById(product);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delProduct(Integer id) {
        return productMapper.deleteById(id);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importPartExcel(List<StructureTestObjectData> list) {
        list.forEach(i -> {
            // 检验对象
            StructureTestObject structureTestObject1 = structureTestObjectMapper.selectOne(Wrappers.<StructureTestObject>lambdaQuery()
                    .eq(StructureTestObject::getSpecimenName, i.getSpecimenName())
                    .eq(StructureTestObject::getSpecimenNameEn, i.getSpecimenNameEn()));
            Laboratory laboratory = laboratoryService.getOne(Wrappers.<Laboratory>lambdaQuery()
                    .eq(Laboratory::getLaboratoryName, i.getLaboratory()));
            if (ObjectUtils.isEmpty(laboratory)) {
                throw new BaseException("未找到该场所:" + i.getLaboratory() + ",请检查是否存在该场所!");
            }
            // 如果为空进行新增
            if(ObjectUtils.isEmpty(structureTestObject1)) {
                StructureTestObject structureTestObject = new StructureTestObject();
                structureTestObject.setLaboratoryId(laboratory.getId());
                structureTestObject.setSpecimenName(i.getSpecimenName());
                structureTestObject.setSpecimenNameEn(i.getSpecimenNameEn());
                structureTestObject.setCode(i.getCode());
                structureTestObjectMapper.insert(structureTestObject);
 
                // 产品
                Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery()
                        .eq(Product::getName, i.getName())
                        .eq(Product::getNameEn, i.getNameEn()));
                if (ObjectUtils.isEmpty(product)){
                    Product product1 = new Product();
                    product1.setName(i.getName());
                    product1.setNameEn(i.getNameEn());
                    product1.setObjectId(structureTestObject.getId());
                    baseMapper.insert(product1);
                }
            } else {
                structureTestObject1.setCode(i.getCode());
                structureTestObject1.setLaboratoryId(laboratory.getId());
                structureTestObjectMapper.updateById(structureTestObject1);
                // 产品
                Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery()
                        .eq(Product::getName, i.getName())
                        .eq(Product::getNameEn, i.getNameEn()));
                if (ObjectUtils.isEmpty(product)){
                    Product product1 = new Product();
                    product1.setName(i.getName());
                    product1.setNameEn(i.getNameEn());
                    product1.setObjectId(structureTestObject1.getId());
                    baseMapper.insert(product1);
                } else {
                    product.setName(i.getName());
                    product.setNameEn(i.getNameEn());
                    product.setObjectId(structureTestObject1.getId());
                    baseMapper.updateById(product);
                }
            }
        });
    }
 
    @Override
    public String getWordQrModel(Integer orderId) {
        return baseMapper.getWordQrModel(orderId);
    }
}