chenrui
2025-04-09 e2c5017787d7fc5eea2afb8bc84bca0db054ad8c
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
package com.ruoyi.basic.service.impl;
 
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.ruoyi.basic.dto.ProductPartDto;
import com.ruoyi.basic.mapper.ProductPartLogMapper;
import com.ruoyi.basic.pojo.ProductPartLog;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.QueryWrappers;
import com.ruoyi.basic.mapper.ProductPartMapper;
import com.ruoyi.basic.mapper.StructureTestObjectPartMapper;
import com.ruoyi.basic.pojo.ProductPart;
import com.ruoyi.basic.pojo.StructureTestObjectPart;
import com.ruoyi.basic.service.ProductPartService;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
 
@Transactional
@Service
@AllArgsConstructor
public class ProductPartServiceImpl extends ServiceImpl<ProductPartMapper, ProductPart> implements ProductPartService {
 
    private ProductPartMapper productPartMapper;
    private StructureTestObjectPartMapper structureTestObjectPartMapper;
 
    private ProductPartLogMapper productPartLogMapper;
 
 
    @Override
    public IPage<ProductPart> selectByProductId(IPage<ProductPart> page, ProductPart productPart) {
        return productPartMapper.selectListByProductId(page, QueryWrappers.queryWrappers(productPart), productPart.getProductId());
    }
 
    @Override
    public void addProductPart(ProductPart productPart) {
        if (productPart.getProductId() == null) {
            throw new BaseException("缺少产品对象id");
        }
        this.isPartNoExist(productPart.getPartNo(), productPart.getProductId(), null);
        productPart.setReview("待复核");
        productPartMapper.insert(productPart);
    }
 
    @Override
    public int updateProductPartById(ProductPart productPart) {
        if (productPart.getProductId() == null) {
            throw new BaseException("缺少产品对象id");
        }
//        this.isPartNoExist(productPart.getPartNo(), productPart.getProductId(), null);
        if (!"已复核".equals(productPart.getReview())) {
            productPart.setReview("待复核");
        }
        return productPartMapper.updateById(productPart);
    }
 
    @Override
    public List<Map<String, Object>> inspectionItems(String sample, String productName) {
        // 优先处理两个参数均非空的场景
        if (StringUtils.isNotEmpty(productName) && StringUtils.isNotEmpty(sample)) {
            //先差产品,有过有值直接返回,没值再查大类
            List<Map<String, Object>> result = productPartMapper.inspectionItems(productName);
            return result.isEmpty() ? productPartMapper.inspectionItems(sample) : result;
        }
 
        // 其他情况直接选取有效参数(优先productName)或空字符串
        String param = StringUtils.isNotEmpty(productName) ? productName :
                StringUtils.isNotEmpty(sample) ? sample : "";
        return productPartMapper.inspectionItems(param);
 
    }
 
    @Override
    public int productPartReviewById(ProductPartDto productPartDto) {
        int userId = SecurityUtils.getUserId().intValue();
        String username = SecurityUtils.getUsername();
 
        // 定义通用的产品部分对象
        Object productPart = null;
        if ("对象".equals(productPartDto.getType())) {
            productPart = structureTestObjectPartMapper.selectById(productPartDto.getId());
            if (productPart != null && ((StructureTestObjectPart) productPart).getTestObjectId() == null) {
                throw new BaseException("缺少产品对象id");
            }
        } else {
            productPart = productPartMapper.selectById(productPartDto.getId());
            if (productPart != null && ((ProductPart) productPart).getProductId() == null) {
                throw new BaseException("缺少产品对象id");
            }
        }
 
        if (productPart == null) {
            return 0;
        }
 
        // 设置审核状态
        if (productPart instanceof StructureTestObjectPart) {
            ((StructureTestObjectPart) productPart).setReview("已复核");
        } else {
            ((ProductPart) productPart).setReview("已复核");
        }
 
        // 更新产品部分信息
        int num = 0;
        if (productPart instanceof StructureTestObjectPart) {
            num = structureTestObjectPartMapper.updateById((StructureTestObjectPart) productPart);
        } else {
            num = productPartMapper.updateById((ProductPart) productPart);
        }
 
        // 如果更新成功,创建或更新日志
        if (num > 0) {
            createOrUpdateProductPartLog(productPart, username, userId, productPartDto.getType(), productPartDto);
        }
 
        return num;
    }
 
    private void createOrUpdateProductPartLog(Object productPart, String username, int userId, String type, ProductPartDto productPartDto) {
        ProductPartLog productPartLog = new ProductPartLog();
        if (productPart instanceof StructureTestObjectPart) {
            StructureTestObjectPart part = (StructureTestObjectPart) productPart;
            productPartLog.setProductPartId(part.getId());
            productPartLog.setColor(part.getColor());
            productPartLog.setColorCode(part.getColorCode());
            productPartLog.setPartNo(part.getPartNo());
            productPartLog.setInspectionItem(part.getInspectionItem());
            productPartLog.setReview(part.getReview());
        } else {
            ProductPart part = (ProductPart) productPart;
            productPartLog.setProductPartId(part.getId());
            productPartLog.setColor(part.getColor());
            productPartLog.setColorCode(part.getColorCode());
            productPartLog.setPartNo(part.getPartNo());
            productPartLog.setInspectionItem(part.getInspectionItem());
            productPartLog.setReview(part.getReview());
        }
        productPartLog.setOperName(username);
        productPartLog.setOperId(userId);
        productPartLog.setOperTime(LocalDateTime.now());
        productPartLog.setType(type);
 
        // 检查是否存在相同 ProductPartId 的日志记录
        ProductPartLog existingLog = productPartLogMapper.selectOne(new LambdaQueryWrapper<ProductPartLog>().eq(ProductPartLog::getProductPartId, productPartDto.getId()));
        if (existingLog != null) {
            // 如果存在,更新记录
            productPartLog.setId(existingLog.getId()); // 设置更新记录的 ID
            productPartLogMapper.updateById(productPartLog);
        } else {
            // 如果不存在,插入新记录
            productPartLogMapper.insert(productPartLog);
        }
    }
 
 
    @Override
    public IPage<ProductPartLog> productPartLogList(Page page, Integer id, String type) {
        return productPartLogMapper.selectPage(page, Wrappers.<ProductPartLog>lambdaQuery()
                .eq(ProductPartLog::getProductPartId, id)
                .eq(ProductPartLog::getType, type));
    }
 
    // 判断零件号是否存在
    public void isPartNoExist(String partNo, Integer productId, Integer id) {
        // 零件号唯一 但不必填
        if (StringUtils.isNotBlank(partNo)) {
            Long count = productPartMapper.selectCount(new LambdaQueryWrapper<ProductPart>()
//                    .eq(ProductPart::getProductId, productId)
                    .eq(ProductPart::getPartNo, partNo)
                    .ne(id != null, ProductPart::getId, id));
            Long selectCount = structureTestObjectPartMapper.selectCount(Wrappers.<StructureTestObjectPart>lambdaQuery()
                    .eq(StructureTestObjectPart::getPartNo, partNo));
            if (count > 0 || selectCount > 0) {
                throw new BaseException("该零件号已绑定过检验对象");
            }
        } else {
            throw new BaseException("请输入零件号");
        }
    }
}