p-honggang.li
2025-08-29 a027868b328bc8138ee3dd5e496f37f442e8fca4
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
package com.webmanage.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.webmanage.entity.Product;
import com.webmanage.mapper.ProductMapper;
import com.webmanage.service.ProductService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import java.util.List;
 
/**
 * 产品服务实现类
 * 
 * @author webmanage
 * @date 2024-08-07
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
 
    @Override
    public List<Product> getProductList(String productName, String productType, String status, Long providerId) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        if (StringUtils.hasText(productName)) {
            queryWrapper.like(Product::getProductName, productName);
        }
        if (StringUtils.hasText(productType)) {
            queryWrapper.eq(Product::getProductType, productType);
        }
        if (StringUtils.hasText(status)) {
            queryWrapper.eq(Product::getStatus, status);
        }
        if (providerId != null) {
            queryWrapper.eq(Product::getProviderId, providerId);
        }
        
        // 按创建时间倒序排列
        queryWrapper.orderByDesc(Product::getCreatedAt);
        
        return this.list(queryWrapper);
    }
 
    @Override
    public Product getProductById(Long id) {
        return this.getById(id);
    }
 
    @Override
    public boolean createProduct(Product product) {
        // 检查产品编码是否已存在
        if (checkProductCodeExists(product.getProductCode(), null)) {
            throw new RuntimeException("产品编码已存在");
        }
        
        return this.save(product);
    }
 
    @Override
    public boolean updateProduct(Product product) {
        // 检查产品编码是否已存在(排除当前产品)
        if (checkProductCodeExists(product.getProductCode(), product.getId())) {
            throw new RuntimeException("产品编码已存在");
        }
        
        return this.updateById(product);
    }
 
    @Override
    public boolean deleteProduct(Long id) {
        return this.removeById(id);
    }
 
    @Override
    public boolean updateProductStatus(Long id, String status) {
        LambdaUpdateWrapper<Product> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Product::getId, id)
                    .set(Product::getStatus, status);
        
        return this.update(updateWrapper);
    }
 
    @Override
    public boolean updateProductAuditStatus(Long id, String auditStatus) {
        LambdaUpdateWrapper<Product> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Product::getId, id)
                    .set(Product::getAuditStatus, auditStatus);
        
        return this.update(updateWrapper);
    }
 
    @Override
    public boolean checkProductCodeExists(String productCode, Long excludeId) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getProductCode, productCode);
        
        if (excludeId != null) {
            queryWrapper.ne(Product::getId, excludeId);
        }
        
        return this.count(queryWrapper) > 0;
    }
}