gongchunyi
2026-04-16 763c537040d45334190f470cd5ba056610d3aa36
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
package com.ruoyi.production.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.production.dto.ProductProcessDto;
import com.ruoyi.production.enums.ProductProcessEnum;
import com.ruoyi.production.mapper.ProcessRouteItemMapper;
import com.ruoyi.production.mapper.ProductProcessMapper;
import com.ruoyi.production.mapper.ProductProcessRouteItemMapper;
import com.ruoyi.production.pojo.ProcessRouteItem;
import com.ruoyi.production.pojo.ProductProcess;
import com.ruoyi.production.pojo.ProductProcessRouteItem;
import com.ruoyi.production.service.ProductProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.List;
 
@Slf4j
@Service
public class ProductProcessServiceImpl extends ServiceImpl<ProductProcessMapper, ProductProcess> implements ProductProcessService {
 
    @Autowired
    private ProductProcessMapper productProcessMapper;
 
    @Autowired
    private ProcessRouteItemMapper processRouteItemMapper;
 
    @Autowired
    private ProductProcessRouteItemMapper productProcessRouteItemMapper;
 
    @Override
    public IPage<ProductProcessDto> listPage(Page page, ProductProcessDto productProcessDto) {
        return productProcessMapper.listPage(page, productProcessDto);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ProductProcessDto productProcessDto) {
        if (ObjectUtils.isEmpty(productProcessDto.getName())) {
            throw new ServiceException("部件名称不能为空");
        }
        long count = this.count(Wrappers.<ProductProcess>lambdaQuery().eq(ProductProcess::getName, productProcessDto.getName()));
        if (count > 0) {
            throw new ServiceException("部件名称已存在,不能重复");
        }
 
        if (ObjectUtils.isNotEmpty(productProcessDto.getNo())) {
            long noCount = this.count(Wrappers.<ProductProcess>lambdaQuery().eq(ProductProcess::getNo, productProcessDto.getNo()));
            if (noCount > 0) {
                throw new ServiceException("工序编号已存在,不能重复");
            }
        }
 
        ProductProcess productProcess = new ProductProcess();
        BeanUtils.copyProperties(productProcessDto, productProcess);
        boolean save = productProcessMapper.insert(productProcess) > 0;
        if (save && ObjectUtils.isEmpty(productProcess.getNo())) {
            String no = "GX" + String.format("%08d", productProcess.getId());
            // 注意:这里由于是自动生成的 ID 补全,通常不会重复,但建议 set 之后更新
            productProcess.setNo(no);
            productProcessMapper.updateById(productProcess);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProductProcessDto productProcessDto) {
        if (ObjectUtils.isEmpty(productProcessDto.getName())) {
            throw new ServiceException("部件名称不能为空");
        }
 
        long nameCount = this.count(Wrappers.<ProductProcess>lambdaQuery()
                .eq(ProductProcess::getName, productProcessDto.getName())
                .ne(ProductProcess::getId, productProcessDto.getId()));
        if (nameCount > 0) {
            throw new ServiceException("部件名称已存在,不能重复");
        }
        if (ObjectUtils.isNotEmpty(productProcessDto.getNo())) {
            long noCount = this.count(Wrappers.<ProductProcess>lambdaQuery()
                    .eq(ProductProcess::getNo, productProcessDto.getNo())
                    .ne(ProductProcess::getId, productProcessDto.getId()));
            if (noCount > 0) {
                throw new ServiceException("工序编号已存在,不能重复");
            }
        }
 
        ProductProcess productProcess = new ProductProcess();
        BeanUtils.copyProperties(productProcessDto, productProcess);
        this.updateById(productProcess);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importData(MultipartFile file) {
        try {
            ExcelUtil<ProductProcess> util = new ExcelUtil<>(ProductProcess.class);
            List<ProductProcess> productProcessList = util.importExcel(file.getInputStream());
            if (CollectionUtils.isEmpty(productProcessList)) {
                throw new ServiceException("模板错误或导入数据为空");
            }
 
            for (int i = 0; i < productProcessList.size(); i++) {
                ProductProcess productProcess = productProcessList.get(i);
                int rowNum = i + 2;
 
                if (ObjectUtils.isEmpty(productProcess)) {
                    throw new ServiceException("第" + rowNum + "行数据为空,请使用正确的模板进行导入");
                }
                if (ObjectUtils.isEmpty(productProcess.getName())) {
                    throw new ServiceException("第" + rowNum + "行:部件名称不能为空");
                }
                if (ObjectUtils.isEmpty(productProcess.getProductProcessType())) {
                    throw new ServiceException("第" + rowNum + "行:部件【" + productProcess.getName() + "】的类型不能为空");
                }
                ProductProcessEnum enumByInfo = ProductProcessEnum.getEnumByInfo(productProcess.getProductProcessType());
                if (ObjectUtils.isEmpty(enumByInfo)) {
                    throw new ServiceException("第" + rowNum + "行:部件【" + productProcess.getName() + "】的类型【" + productProcess.getProductProcessType() + "】不存在,请填写正确的类型:加工、刮板冷芯制作、管路组对、罐体连接及调试、测试打压、其他");
                }else {
                    productProcess.setType(enumByInfo.getCode());
                }
            }
 
            saveOrUpdateBatch(productProcessList);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            log.error("部件导入异常:{}", e.getMessage(), e);
            throw new ServiceException("部件导入异常:" + e.getMessage());
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(List<Integer> ids) {
        // 查询是否生产中已经引用了这些工序
        List<ProcessRouteItem> processRouteItems = processRouteItemMapper.selectList(Wrappers.<ProcessRouteItem>lambdaQuery().in(ProcessRouteItem::getProcessId, ids));
        List<ProductProcessRouteItem> productProcessRouteItems = productProcessRouteItemMapper.selectList(Wrappers.<ProductProcessRouteItem>lambdaQuery().in(ProductProcessRouteItem::getProcessId, ids));
        if (!CollectionUtils.isEmpty(processRouteItems) || !CollectionUtils.isEmpty(productProcessRouteItems)) {
            throw new ServiceException("该工序已经被使用,无法删除");
        }
        productProcessMapper.deleteBatchIds(ids);
    }
}