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
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.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.dto.*;
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.IProductModelService;
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-19
 */
@Service
@AllArgsConstructor
public class ProductModelServiceImpl extends ServiceImpl<ProductModelMapper, ProductModel> implements IProductModelService {
 
    private final ProductMapper productMapper;
    private final SalesLedgerProductMapper salesLedgerProductMapper;
    private ProductModelMapper productModelMapper;
 
    @Override
    public int addOrEditProductModel(ProductModelDto productModelDto) {
        if(StringUtils.isEmpty(productModelDto.getProductName())){
            throw new RuntimeException("产品名称不能为空");
        }
        Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>()
                .eq(Product::getProductName, productModelDto.getProductName()));
        if (productModelDto.getId() == null) {
            if(product == null){
                product = new Product();
                product.setProductName(productModelDto.getProductName());
                productMapper.insert(product);
            }
            ProductModel productModel = new ProductModel();
            BeanUtils.copyProperties(productModelDto,productModel);
            productModel.setProductId(product.getId());
            return productModelMapper.insert(productModel);
        } else {
            if(product != null){
                product.setProductName(productModelDto.getProductName());
                productMapper.updateById(product);
            }
 
            return productModelMapper.updateById(productModelDto);
        }
    }
 
 
 
    @Override
    public int delProductModel(Long[] ids) {
        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new QueryWrapper<SalesLedgerProduct>()
                .lambda().in(SalesLedgerProduct::getProductModelId, ids));
        if (salesLedgerProducts != null && salesLedgerProducts.size() > 0) {
 
            throw new RuntimeException("已经存在该产品的销售台账和采购台账");
        }
        return productModelMapper.deleteBatchIds(Arrays.asList(ids));
    }
 
    @Override
    public List<ProductModel> selectModelList(ProductDto productDto) {
        LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductModel::getProductId, productDto.getId());
        return productModelMapper.selectList(queryWrapper);
    }
 
    /**
     * 根据id查询产品规格分页查询
     * @param page
     * @param productDto
     * @return
     */
    @Override
    public IPage<ProductModel> modelListPage(Page page, ProductDto productDto) {
        LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductModel::getProductId, productDto.getId());
        return productModelMapper.selectPage(page, queryWrapper);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean importProduct(MultipartFile file) {
        try {
            ExcelUtil<ProductModelExcelCopyDto> productModelExcelUtil = new ExcelUtil<>(ProductModelExcelCopyDto.class);
            List<ProductModelExcelCopyDto> productModelList = productModelExcelUtil.importExcel(file.getInputStream());
            List<Product> productList = productMapper.selectList(new LambdaQueryWrapper<Product>()
                    .isNull(Product::getParentId));
            if(CollectionUtils.isEmpty(productList)) {
                throw new RuntimeException("请先添加父级产品");
            }
            if(CollectionUtils.isNotEmpty(productModelList)){
                // 2. 按产品名称分组
                Map<String, List<ProductModelExcelCopyDto>> groupedByProductName = productModelList.stream()
                        .collect(Collectors.groupingBy(ProductModelExcelCopyDto::getProductName));
                for (Map.Entry<String, List<ProductModelExcelCopyDto>> entry : groupedByProductName.entrySet()) {
                    // 根据名称查询是否存在产品
                    Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>()
                            .eq(Product::getProductName, entry.getKey())
                            .last("limit 1"));
                    if(product == null){
                        product = new Product();
                        product.setProductName(entry.getKey());
                        product.setParentId(null); // 父节点ID
                        // 2. 插入当前节点,MyBatis会自动回填product的id属性
                        productMapper.insert(product);
                    }
                    Product finalProduct = product;
                    entry.getValue().forEach(productModelExcelDto -> {
                        // 根据图纸编号查询
                        ProductModel productModel = productModelMapper.selectOne(new LambdaQueryWrapper<ProductModel>()
                                .eq(ProductModel::getModel, productModelExcelDto.getModel())
                                .last("limit 1"));
                        if(productModel == null){
                            productModel = new ProductModel();
                            BeanUtils.copyProperties(productModelExcelDto,productModel);
                            if(productModelExcelDto.getProductType() == null) {
                                productModel.setProductType(1);
                            }
                            productModel.setProductId(finalProduct.getId());
                            productModelMapper.insert(productModel);
                        }else{
                            BeanUtils.copyProperties(productModelExcelDto,productModel);
                            productModel.setProductId(finalProduct.getId());
                            productModelMapper.updateById(productModel);
                        }
                    });
                }
            }
//            List<ProductModelExcelDto> productModelExcelDtos = OrderUtils.buildTree(productModelList);
//            if(CollectionUtils.isNotEmpty(productModelExcelDtos)){
//                recursiveSaveProduct(productModelExcelDtos.get(0),null);
//            }
            return true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    /**
     * 递归导入树形产品数据
     * @param excelDto Excel解析后的树形节点
     * @param parentId 父节点ID(顶级节点传null/0)
     */
    @Transactional(rollbackFor = Exception.class) // 事务保证,失败则回滚
    public void recursiveSaveProduct(ProductModelExcelDto excelDto, Long parentId) {
        // 1. 构建当前节点的Product实体
        Product product = new Product();
        product.setProductName(excelDto.getProductName());
        product.setParentId(parentId); // 父节点ID
 
        // 2. 插入当前节点,MyBatis会自动回填product的id属性
        productMapper.insert(product);
        Long currentId = product.getId(); // 获取当前节点主键
 
        // 3. 递归处理子节点
        if (excelDto.getChildren() != null && !excelDto.getChildren().isEmpty()) {
            for (ProductModelExcelDto childDto : excelDto.getChildren()) {
                recursiveSaveProduct(childDto, currentId); // 父ID为当前节点ID
            }
        }
 
        // 4. (可选)处理items数据(如果需要插入到关联表)
         if (excelDto.getItems() != null && !excelDto.getItems().isEmpty()) {
             for (ProductModelExcelItemDto item : excelDto.getItems()) {
                 // 插入item关联数据,关联currentId
                 // 构建ProductModel实体
                 ProductModel productModel = new ProductModel();
                 productModel.setProductId(currentId); // 关联当前产品节点ID
                 productModel.setModel(item.getModel()); // 从ItemDTO获取型号
                 productModel.setUnit(item.getUnit());   // 从ItemDTO获取单位
                 productModel.setDrawingNumber(item.getDrawingNumber()); // 图纸编号
                 productModel.setProductType(item.getProductType());
                 // 插入product_model表
                 productModelMapper.insert(productModel);
             }
         }
    }
}