liding
2025-04-10 a3e0493b2f597e8c49ee80a0c7ab92bc30dc60a3
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
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.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.dto.StructureTestObjectDto;
import com.ruoyi.basic.mapper.*;
import com.ruoyi.basic.service.*;
import com.ruoyi.common.utils.QueryWrappers;
import com.ruoyi.basic.dto.ProductDTO1;
import com.ruoyi.basic.pojo.*;
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 StructureTestObjectMapper structureTestObjectMapper;
 
    private ProductPartMapper productPartMapper;
 
 
    private StandardProductListService standardProductListService;
 
 
    private StandardTreeMapper standardTreeMapper;
 
 
    private StructureItemParameterService structureItemParameterService;
 
 
    private WorkShopMapper workShopMapper;
 
    @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) {
        // 删除零件绑定
        productPartMapper.delete(Wrappers.<ProductPart>lambdaQuery()
                .eq(ProductPart::getProductId, id));
 
        return productMapper.deleteById(id);
    }
 
    @Override
    public int updateWorkShop(Integer productId, Integer workShopId, String name) {
        return productMapper.update(null, Wrappers.<Product>lambdaUpdate()
                .set(Product::getWorkShopId, workShopId)
                .set(Product::getWorkShopName, name)
                .eq(Product::getId, productId));
    }
 
    @Override
    public int updateObjectWorkShop(StructureTestObjectDto structureTestObjectDto) {
        return structureTestObjectMapper.update(null, Wrappers.<StructureTestObject>lambdaUpdate()
                .set(StructureTestObject::getWorkShopId, structureTestObjectDto.getWorkShopId())
                .set(StructureTestObject::getWorkShopName, structureTestObjectDto.getWorkShopName())
                .eq(StructureTestObject::getId, structureTestObjectDto.getId()));
    }
}