yaowanxin
10 小时以前 995e1f84b4d775a1f7b9d196bc680a58dcaa3c6d
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
package com.ruoyi.basic.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
import com.ruoyi.purchase.pojo.PurchaseLedger;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
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;
    private final SalesLedgerProductMapper salesLedgerProductMapper;
    private final SalesLedgerMapper salesLedgerMapper;
    private final PurchaseLedgerMapper purchaseLedgerMapper;
 
 
    @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;
    }
 
 
 
    // 递归构建子节点
    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) {
        if (productDto.getId() == null) {
            // 新增产品逻辑
            if (productDto.getParentId() == null) {
                // 若未指定父节点,默认为根节点(parentId 设为 null)
                productDto.setParentId(null);
            } else {
                // 检查父节点是否存在(可选,根据业务需求)
                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);
        }
    }
 
    @Override
    public int delProductByIds(Long[] ids) {
        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new QueryWrapper<SalesLedgerProduct>()
                .lambda().in(SalesLedgerProduct::getProductId, ids));
        if (salesLedgerProducts != null && salesLedgerProducts.size() > 0) {
            salesLedgerProducts.forEach(salesLedgerProduct -> {
                Long salesLedgerId = salesLedgerProduct.getSalesLedgerId();
                SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerId);
                if (salesLedger != null) {
                    throw new RuntimeException("已经存在该产品的销售台账");
                }
                PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(salesLedgerId);
                if (purchaseLedger != null) {
                    throw new RuntimeException("已经存在该产品的采购台账");
                }
            });
        }
        // 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;
    }
 
}