liyong
2026-05-21 8e89f95b9db4039f0cb8b4b8dc7974c247366c4c
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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.core.toolkit.CollectionUtils;
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.ProductModelAnticlockwiseDto;
import com.ruoyi.basic.dto.ProductModelDto;
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.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-19
 */
@Service
@AllArgsConstructor
public class ProductModelServiceImpl extends ServiceImpl<ProductModelMapper, ProductModel> implements IProductModelService {
    private static final String PRODUCT_MODEL_LOCK_PREFIX = "product_model_anticlockwise:";
    private static final long LOCK_WAIT_TIMEOUT_SECONDS = 10L;
    private static final long LOCK_EXPIRE_TIME_SECONDS = 30L;
    private static final String FINISHED_PRODUCT_NAME = "\u6210\u54c1";
    private final RedisTemplate redisTemplate;
 
    private final ProductMapper productMapper;
    private final SalesLedgerProductMapper salesLedgerProductMapper;
    private ProductModelMapper productModelMapper;
 
    @Override
    public int addOrEditProductModel(ProductModelDto productModelDto) {
 
        if (productModelDto.getId() == null) {
            ProductModel productModel = new ProductModel();
            BeanUtils.copyProperties(productModelDto, productModel);
            return productModelMapper.insert(productModel);
        } else {
            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());
        queryWrapper.eq(productDto.getCreateUser() != null, ProductModel::getCreateUser, productDto.getCreateUser());
        queryWrapper.eq(productDto.getDeptId() != null, ProductModel::getDeptId, productDto.getDeptId());
        if (ObjectUtils.isNotEmpty(productDto.getDeptIds())) {
            queryWrapper.in( ProductModel::getDeptId, Arrays.asList(productDto.getDeptIds()));
        }
        return productModelMapper.selectList(queryWrapper);
    }
 
    @Override
    public List<ProductModel> selectModelListByProductIds(@NotNull List<Long> ids) {
        return productModelMapper.selectModelListByProductIds(ids);
    }
 
    /**
     * 根据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 AjaxResult importProductModel(MultipartFile file, Integer productId) {
        if (productId == null) {
            return AjaxResult.error("请先选择产品再导入规格型号");
        }
 
        Product product = productMapper.selectById(productId);
        if (product == null) {
            return AjaxResult.error("选择的产品不存在");
        }
 
        try {
            ExcelUtil<ProductModel> productModelExcelUtil = new ExcelUtil<>(ProductModel.class);
            List<ProductModel> productModelList = productModelExcelUtil.importExcel(file.getInputStream());
 
            if (CollectionUtils.isEmpty(productModelList)) {
                return AjaxResult.error("导入数据不能为空");
            }
 
            //  获取当前产品下所有的规格型号名
            List<ProductModel> existingModels = list(new LambdaQueryWrapper<ProductModel>().eq(ProductModel::getProductId, productId));
            Set<String> existingModelNames = existingModels.stream().map(ProductModel::getModel).collect(Collectors.toSet());
 
            List<ProductModel> waitToSaveList = new ArrayList<>();
            int skipCount = 0;
 
            for (int i = 0; i < productModelList.size(); i++) {
                ProductModel item = productModelList.get(i);
                int rowNum = i + 2;
 
                if (StringUtils.isEmpty(item.getModel())) {
                    return AjaxResult.error("第 " + rowNum + " 行导入失败: [规格型号] 不能为空");
                }
                if (StringUtils.isEmpty(item.getUnit())) {
                    return AjaxResult.error("第 " + rowNum + " 行导入失败: [单位] 不能为空");
                }
 
                //  去重,如果已包含该型号,则跳过
                if (existingModelNames.contains(item.getModel())) {
                    skipCount++;
                    continue;
                }
 
                item.setProductId(product.getId());
                waitToSaveList.add(item);
 
                existingModelNames.add(item.getModel());
            }
 
            if (!waitToSaveList.isEmpty()) {
                saveBatch(waitToSaveList);
            }
 
            if (skipCount == 0) {
                return AjaxResult.success(String.format("成功导入 %d 条数据", waitToSaveList.size()));
            } else {
                return AjaxResult.success(String.format("成功导入 %d 条,跳过已存在数据 %d 条", waitToSaveList.size(), skipCount));
            }
        } catch (Exception e) {
            log.error("导入产品规格异常", e);
            throw new ServiceException("导入失败");
        }
    }
 
    //反向新增成品产品,只有销售关联新增的时候调用
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long productModelAnticlockwise(ProductModelAnticlockwiseDto productModelDto) {
        Long deptId = SecurityUtils.getDeptId()[0];
 
        // 如果提供了ID,直接更新
        if (ObjectUtils.isNotEmpty(productModelDto.getId())) {
            return updateProductModelById(productModelDto, deptId);
        }
 
        // 检查是否已存在相同的产品型号
        ProductModel existingModel = productModelMapper.selectOldProductModel(
                productModelDto.getModel(), productModelDto.getProductName()
        );
        if (existingModel != null) {
            return updateExistingProductModel(existingModel, productModelDto, deptId);
        }
 
        // 新增时需要加锁防止并发创建
        String lockKey = buildProductModelLockKey(FINISHED_PRODUCT_NAME,
                productModelDto.getProductName(), productModelDto.getModel());
        String lockValue = UUID.randomUUID().toString();
        acquireLock(lockKey, lockValue);
        try {
            Long productId = getOrCreateProduct(productModelDto.getProductName(), deptId);
 
            ProductModel productModel = new ProductModel();
            productModel.setProductId(productId);
            productModel.setModel(productModelDto.getModel());
            productModel.setUnit(productModelDto.getUnit());
            productModel.setSubUnit(productModelDto.getSubUnit());
            productModel.setDeptId(deptId);
            productModelMapper.insert(productModel);
            return productModel.getId();
        } finally {
            releaseLock(lockKey, lockValue);
        }
    }
 
    private Long updateProductModelById(ProductModelAnticlockwiseDto productModelDto, Long deptId) {
        ProductModel productModel = productModelMapper.selectById(productModelDto.getId());
        if (productModel == null) {
            throw new ServiceException("产品型号不存在");
        }
        return updateExistingProductModel(productModel, productModelDto, deptId);
    }
 
    private Long updateExistingProductModel(ProductModel productModel, ProductModelAnticlockwiseDto dto, Long deptId) {
        productModel.setModel(dto.getModel());
        productModel.setUnit(dto.getUnit());
        productModel.setSubUnit(dto.getSubUnit());
        productModel.setDeptId(deptId);
        productModelMapper.updateById(productModel);
 
        updateProductName(productModel.getProductId(), dto.getProductName(), deptId);
        return productModel.getId();
    }
 
    private Long getOrCreateProduct(String productName, Long deptId) {
        // 获取或创建父产品"成品"
        Product parentProduct = getOrCreateParentProduct(FINISHED_PRODUCT_NAME, deptId);
 
        // 查找或创建具体产品
        Product product = productMapper.selectOne(new QueryWrapper<Product>().lambda()
                .eq(Product::getProductName, productName)
                .eq(Product::getParentId, parentProduct.getId())
                .last("limit 1"));
 
        if (product == null) {
            product = new Product();
            product.setProductName(productName);
            product.setParentId(parentProduct.getId());
            product.setDeptId(deptId);
            productMapper.insert(product);
        }
        return product.getId();
    }
 
    private Product getOrCreateParentProduct(String parentName, Long deptId) {
        Product parentProduct = productMapper.selectOne(new QueryWrapper<Product>().lambda()
                .eq(Product::getProductName, parentName)
                .last("limit 1"));
 
        if (parentProduct == null) {
            parentProduct = new Product();
            parentProduct.setProductName(parentName);
            parentProduct.setDeptId(deptId);
            productMapper.insert(parentProduct);
        }
        return parentProduct;
    }
 
    private void updateProductName(Long productId, String productName, Long deptId) {
        Product product = productMapper.selectById(productId);
        if (product != null) {
            product.setProductName(productName);
            product.setDeptId(deptId);
            productMapper.updateById(product);
        }
    }
 
    private String buildProductModelLockKey(String parentName, String productName, String model) {
        return PRODUCT_MODEL_LOCK_PREFIX + parentName + ":" + productName + ":" + model;
    }
 
    private void acquireLock(String lockKey, String lockValue) {
        long startWaitTime = System.currentTimeMillis();
        while (System.currentTimeMillis() - startWaitTime < LOCK_WAIT_TIMEOUT_SECONDS * 1000) {
            Boolean locked = redisTemplate.opsForValue().setIfAbsent(
                    lockKey, lockValue, LOCK_EXPIRE_TIME_SECONDS, TimeUnit.SECONDS
            );
            if (Boolean.TRUE.equals(locked)) {
                return;
            }
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new ServiceException("Get product model lock interrupted");
            }
        }
        throw new ServiceException("Get product model lock timeout");
    }
 
    private void releaseLock(String lockKey, String lockValue) {
        String luaScript = "if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) else return 0 end";
        redisTemplate.execute(
                new DefaultRedisScript<>(luaScript, Long.class),
                Collections.singletonList(lockKey),
                lockValue
        );
    }
 
}