gongchunyi
7 小时以前 64c5103ed52d6c07a3abaaf8cc62a2b5b7b4d249
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
package com.ruoyi.sales.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.sales.mapper.SalesLedgerProductProcessBindMapper;
import com.ruoyi.sales.pojo.SalesLedgerProductProcess;
import com.ruoyi.sales.mapper.SalesLedgerProductProcessMapper;
import com.ruoyi.sales.pojo.SalesLedgerProductProcessBind;
import com.ruoyi.sales.service.ISalesLedgerProductProcessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
/**
 * <p>
 * 销售产品加工明细 服务实现类
 * </p>
 *
 * @author deslrey
 * @since 2026-03-25
 */
@Service
public class SalesLedgerProductProcessServiceImpl extends ServiceImpl<SalesLedgerProductProcessMapper, SalesLedgerProductProcess> implements ISalesLedgerProductProcessService {
 
    @Autowired
    private SalesLedgerProductProcessBindMapper salesLedgerProductProcessBindMapper;
 
    @Override
    public Page<SalesLedgerProductProcess> salesLedgerProductProcessList(Page<SalesLedgerProductProcess> page, String name) {
        LambdaQueryWrapper<SalesLedgerProductProcess> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name)) {
            wrapper.like(SalesLedgerProductProcess::getProcessName, name);
        }
        return this.page(page, wrapper);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProcess(SalesLedgerProductProcess process) {
        checkDuplicate(process.getProcessName(), null, process.getProcessName());
        this.save(process);
        if (!StringUtils.hasText(process.getCode())) {
            process.setCode("P" + process.getId());
            this.updateById(process);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProcess(SalesLedgerProductProcess process) {
        checkDuplicate(process.getProcessName(), process.getId(), process.getProcessName());
        this.updateById(process);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProcess(Integer id) {
        if (id == null) {
            throw new ServiceException("删除失败,删除数据不能为空");
        }
        SalesLedgerProductProcess salesLedgerProductProcess = baseMapper.selectById(id);
        if (salesLedgerProductProcess == null) {
            throw new ServiceException("删除失败,该额外加工需求不存在");
        }
        //  查看是否被引用
        Long selectedCount = salesLedgerProductProcessBindMapper.selectCount(new LambdaQueryWrapper<SalesLedgerProductProcessBind>().eq(SalesLedgerProductProcessBind::getSalesLedgerProductProcessId, salesLedgerProductProcess.getId()));
        if (selectedCount > 0) {
            throw new ServiceException("删除失败,该额外加工已被引用,禁止删除");
        }
 
        this.removeById(id);
    }
 
    private void checkDuplicate(String processName, Integer excludeId, String code) {
        LambdaQueryWrapper<SalesLedgerProductProcess> wrapper = new LambdaQueryWrapper<>();
 
        wrapper.and(w -> w
                .eq(SalesLedgerProductProcess::getProcessName, processName)
                .or()
                .eq(SalesLedgerProductProcess::getCode, code));
 
        if (excludeId != null) {
            wrapper.ne(SalesLedgerProductProcess::getId, excludeId);
        }
 
        if (this.count(wrapper) > 0) {
            throw new RuntimeException("工序名称或编码已存在");
        }
    }
}