gongchunyi
2 天以前 2a4b95fd5718712ab08f63db49702dbbab2a158d
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
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 jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
@Service
@AllArgsConstructor
@Slf4j
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) {
        if (ObjectUtils.isEmpty(productDto.getParentId())) {
            throw new IllegalArgumentException("请选择父节点");
        }
        String productName = StringUtils.trim(productDto.getProductName());
        if (StringUtils.isEmpty(productName)) {
            throw new IllegalArgumentException("产品名称不能为空");
        }
        productDto.setProductName(productName);
        checkProductNameUnique(productDto.getParentId(), productName, productDto.getId());
        if (productDto.getId() == 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<>();
        queryWrapper.eq(Product::getParentId, parentId)
                .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 void exportImportTemplate(HttpServletResponse response) {
        String templatePath = "static/产品导入模板.xlsx";
        try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(templatePath)) {
            if (inputStream == null) {
                throw new FileNotFoundException("模板文件不存在:" + templatePath);
            }
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("产品导入模板.xlsx", "utf-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);
            try (OutputStream outputStream = response.getOutputStream()) {
                byte[] buffer = new byte[1024];
                int len;
                while ((len = inputStream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, len);
                }
                outputStream.flush();
            }
        } catch (IOException e) {
            log.error("导出产品导入模板失败", e);
            try {
                response.getWriter().write("模板导出失败:" + e.getMessage());
            } catch (IOException ex) {
                log.error("响应输出错误", ex);
            }
        }
    }
 
    @Override
    public int delProductByIds(Long[] ids) {
        LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ProductModel::getProductId, ids);
        productModelMapper.delete(queryWrapper);
 
        return productMapper.deleteBatchIds(Arrays.asList(ids));
    }
}