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
package com.ruoyi.basic.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.dto.ProductDto;
import com.ruoyi.basic.dto.ProductTreeDto;
import com.ruoyi.basic.mapper.ProductMapper;
import com.ruoyi.basic.mapper.ProductModelMapper;
import com.ruoyi.basic.pojo.Product;
import com.ruoyi.basic.pojo.ProductModel;
import com.ruoyi.basic.service.IProductService;
import com.ruoyi.basic.vo.ProductModelVo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
@Service
@AllArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
 
    private ProductMapper productMapper;
 
    private ProductModelMapper productModelMapper;
 
    @Override
    public List<ProductTreeDto> selectProductList(ProductDto productDto) {
        // 查询根节点(parentId 为 null)
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(Product::getParentId);
 
        // 如果有产品名称条件,添加到查询中
        if (productDto.getProductName() != null && !productDto.getProductName().isEmpty()) {
            queryWrapper.like(Product::getProductName, productDto.getProductName());
        }
 
        // 查询根节点列表
        List<Product> rootProducts = productMapper.selectList(queryWrapper);
 
        // 转换为树节点并递归构建子树
        List<ProductTreeDto> tree = new ArrayList<>();
        for (Product product : rootProducts) {
            ProductTreeDto node = convertToTreeDto(product);
            node.setChildren(buildChildrenNodes(product.getId()));
            tree.add(node);
        }
        return tree;
    }
 
    @Override
    public IPage<ProductModelVo> listPageProductModel(Page<ProductModelVo> page, ProductModel productModel) {
        return productModelMapper.listPageProductModel(page, productModel);
    }
 
 
    // 递归构建子节点
    private List<ProductTreeDto> buildChildrenNodes(Long parentId) {
        // 查询当前父节点的子节点
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getParentId, parentId);
        List<Product> childProducts = productMapper.selectList(queryWrapper);
 
        // 转换子节点并递归构建它们的子树
        List<ProductTreeDto> children = new ArrayList<>();
        for (Product child : childProducts) {
            ProductTreeDto childNode = convertToTreeDto(child);
            childNode.setChildren(buildChildrenNodes(child.getId()));
            children.add(childNode);
        }
 
        return children;
    }
 
    // 将 Product 转换为 ProductTreeDto
    private ProductTreeDto convertToTreeDto(Product product) {
        ProductTreeDto dto = new ProductTreeDto();
        BeanUtils.copyProperties(product, dto);
        dto.setLabel(product.getProductName()); // 设置 label 为产品名称
        dto.setChildren(new ArrayList<>());
        return dto;
    }
 
    @Override
    public int addOrEditProduct(ProductDto productDto) {
        String productName = StringUtils.trim(productDto.getProductName());
        if (StringUtils.isEmpty(productName)) {
            throw new IllegalArgumentException("产品名称不能为空");
        }
        // 父节点为空表示新增一级大类
        if (ObjectUtils.isEmpty(productDto.getParentId())) {
            if (productDto.getId() != null) {
                throw new IllegalArgumentException("请选择父节点");
            }
            productDto.setParentId(null);
        }
        productDto.setProductName(productName);
        checkProductNameUnique(productDto.getParentId(), productName, productDto.getId());
        if (productDto.getId() == null) {
            // 新增产品逻辑
            // 检查父节点是否存在(一级大类无父节点)
            if (productDto.getParentId() != null) {
                Product parent = productMapper.selectById(productDto.getParentId());
                if (parent == null) {
                    throw new IllegalArgumentException("父节点不存在,无法添加子产品");
                }
            }
            return productMapper.insert(productDto);
        } else {
            // 编辑产品逻辑
            // 检查产品是否存在(可选,根据业务需求)
            Product existingProduct = productMapper.selectById(productDto.getId());
            if (existingProduct == null) {
                throw new IllegalArgumentException("要编辑的产品不存在");
            }
            return productMapper.updateById(productDto);
        }
    }
 
    private void checkProductNameUnique(Long parentId, String productName, Long currentId) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        if (parentId == null) {
            queryWrapper.isNull(Product::getParentId);
        } else {
            queryWrapper.eq(Product::getParentId, parentId);
        }
        queryWrapper.eq(Product::getProductName, productName)
                .ne(currentId != null, Product::getId, currentId)
                .last("limit 1");
        Product duplicateProduct = productMapper.selectOne(queryWrapper);
        if (duplicateProduct != null) {
            throw new IllegalArgumentException("对应的" + productName + "已经存在");
        }
    }
 
    @Override
    public int delProductByIds(Long[] ids) {
        // 存在子类时不允许删除
        LambdaQueryWrapper<Product> childWrapper = new LambdaQueryWrapper<>();
        childWrapper.in(Product::getParentId, ids).last("limit 1");
        Product childProduct = productMapper.selectOne(childWrapper);
        if (childProduct != null) {
            throw new IllegalArgumentException("该大类下存在子类,不能删除");
        }
 
        // 大类下存在规格型号数据时不允许删除
        for (Product product : productMapper.selectBatchIds(Arrays.asList(ids))) {
            if (product.getParentId() != null) {
                continue;
            }
            LambdaQueryWrapper<ProductModel> modelWrapper = new LambdaQueryWrapper<>();
            modelWrapper.eq(ProductModel::getProductId, product.getId()).last("limit 1");
            if (productModelMapper.selectOne(modelWrapper) != null) {
                throw new IllegalArgumentException("该大类下存在数据,不能删除");
            }
        }
 
        // 1. 删除子表 product_model 中关联的数据
        LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ProductModel::getProductId, ids);
        productModelMapper.delete(queryWrapper);
 
        // 2. 删除主表 product 数据
        int deleteCount = productMapper.deleteBatchIds(Arrays.asList(ids));
 
        return deleteCount;
    }
 
    @Override
    public ProductModel ensureProductHierarchy(String productName, String material, String modelName, String unit) {
        productName = StringUtils.trim(productName);
        material = StringUtils.trim(material);
        modelName = StringUtils.trim(modelName);
        if (StringUtils.isEmpty(productName) || StringUtils.isEmpty(material) || StringUtils.isEmpty(modelName)) {
            return null;
        }
 
        // 品名:一级大类(parentId 为空)
        LambdaQueryWrapper<Product> categoryWrapper = new LambdaQueryWrapper<>();
        categoryWrapper.isNull(Product::getParentId)
                .eq(Product::getProductName, productName)
                .last("limit 1");
        Product category = productMapper.selectOne(categoryWrapper);
        if (category == null) {
            category = new Product();
            category.setProductName(productName);
            productMapper.insert(category);
        }
 
        // 材质:大类下的子类
        LambdaQueryWrapper<Product> materialWrapper = new LambdaQueryWrapper<>();
        materialWrapper.eq(Product::getParentId, category.getId())
                .eq(Product::getProductName, material)
                .last("limit 1");
        Product materialProduct = productMapper.selectOne(materialWrapper);
        if (materialProduct == null) {
            materialProduct = new Product();
            materialProduct.setParentId(category.getId());
            materialProduct.setProductName(material);
            productMapper.insert(materialProduct);
        }
 
        // 规格型号:子类下的规格
        LambdaQueryWrapper<ProductModel> modelWrapper = new LambdaQueryWrapper<>();
        modelWrapper.eq(ProductModel::getProductId, materialProduct.getId())
                .eq(ProductModel::getModel, modelName)
                .last("limit 1");
        ProductModel productModel = productModelMapper.selectOne(modelWrapper);
        if (productModel == null) {
            productModel = new ProductModel();
            productModel.setProductId(materialProduct.getId());
            productModel.setModel(modelName);
            productModel.setUnit(unit);
            productModelMapper.insert(productModel);
        }
        return productModel;
    }
 
}