云
2026-10-25 d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea
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
package cn.iocoder.yudao.module.mes.service.pd.reel;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.pd.reel.vo.ProductCtcReelImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.pd.reel.vo.ProductCtcReelImportRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.pd.reel.vo.ProductCtcReelPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.pd.reel.vo.ProductCtcReelSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pd.ctc.ProductCtcDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pd.reel.ProductCtcReelDO;
import cn.iocoder.yudao.module.mes.dal.mysql.pd.reel.ProductCtcReelMapper;
import cn.iocoder.yudao.module.mes.service.pd.ctc.ProductCtcService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PD_CTC_NOT_EXISTS;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PD_CTC_REEL_IMPORT_LIST_IS_EMPTY;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PD_CTC_REEL_NOT_EXISTS;
 
/**
 * MES 换位导线盘具计算 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ProductCtcReelServiceImpl implements ProductCtcReelService {
 
    @Resource
    private ProductCtcReelMapper reelMapper;
 
    @Resource
    @Lazy
    private ProductCtcService ctcService;
 
    @Override
    public Long createReel(ProductCtcReelSaveReqVO createReqVO) {
        // 校验换位导线产品存在
        validateCtcExists(createReqVO.getCtcId());
        // 插入
        ProductCtcReelDO reel = BeanUtils.toBean(createReqVO, ProductCtcReelDO.class);
        reelMapper.insert(reel);
        return reel.getId();
    }
 
    @Override
    public void updateReel(ProductCtcReelSaveReqVO updateReqVO) {
        // 校验存在
        validateReelExists(updateReqVO.getId());
        // 校验换位导线产品存在
        validateCtcExists(updateReqVO.getCtcId());
        // 更新
        ProductCtcReelDO updateObj = BeanUtils.toBean(updateReqVO, ProductCtcReelDO.class);
        reelMapper.updateById(updateObj);
    }
 
    @Override
    public void deleteReel(Long id) {
        // 校验存在
        validateReelExists(id);
        // 删除
        reelMapper.deleteById(id);
    }
 
    @Override
    public ProductCtcReelDO getReel(Long id) {
        return reelMapper.selectById(id);
    }
 
    @Override
    public PageResult<ProductCtcReelDO> getReelPage(ProductCtcReelPageReqVO pageReqVO) {
        return reelMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<ProductCtcReelDO> getReelListByCtcId(Long ctcId) {
        return reelMapper.selectListByCtcId(ctcId);
    }
 
    @Override
    public void deleteByCtcId(Long ctcId) {
        reelMapper.deleteByCtcId(ctcId);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ProductCtcReelImportRespVO importReelList(List<ProductCtcReelImportExcelVO> importList, boolean updateSupport) {
        if (CollUtil.isEmpty(importList)) {
            throw exception(PD_CTC_REEL_IMPORT_LIST_IS_EMPTY);
        }
        ProductCtcReelImportRespVO respVO = ProductCtcReelImportRespVO.builder()
                .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>())
                .failureCodes(new LinkedHashMap<>()).build();
        AtomicInteger index = new AtomicInteger(1);
        importList.forEach(importItem -> {
            int currentIndex = index.getAndIncrement();
            String key = StrUtil.blankToDefault(importItem.getCtcOrderSpec(), "第 " + currentIndex + " 行");
            if (StrUtil.isBlank(importItem.getCtcOrderSpec())) {
                respVO.getFailureCodes().put(key, "订货规格不能为空");
                return;
            }
            ProductCtcDO ctc = ctcService.getCtcByOrderSpec(importItem.getCtcOrderSpec().trim());
            if (ctc == null) {
                respVO.getFailureCodes().put(key, "订货规格[" + importItem.getCtcOrderSpec() + "]不存在");
                return;
            }
            ProductCtcReelSaveReqVO reqVO = BeanUtils.toBean(importItem, ProductCtcReelSaveReqVO.class);
            reqVO.setCtcId(ctc.getId());
            String resultKey = importItem.getCtcOrderSpec() + "/" + StrUtil.blankToDefault(importItem.getReelType(), "无类型");
            ProductCtcReelDO existReel = reelMapper.selectByCtcIdAndReelType(ctc.getId(), importItem.getReelType());
            try {
                if (existReel == null) {
                    reqVO.setId(null);
                    createReel(reqVO);
                    respVO.getCreateCodes().add(resultKey);
                } else if (updateSupport) {
                    reqVO.setId(existReel.getId());
                    updateReel(reqVO);
                    respVO.getUpdateCodes().add(resultKey);
                } else {
                    respVO.getFailureCodes().put(key, "订货规格+盘具类型已存在");
                }
            } catch (ServiceException e) {
                respVO.getFailureCodes().put(key, e.getMessage());
            }
        });
        return respVO;
    }
 
    private void validateCtcExists(Long ctcId) {
        ProductCtcDO ctc = ctcService.getCtc(ctcId);
        if (ctc == null) {
            throw exception(PD_CTC_NOT_EXISTS);
        }
    }
 
    private ProductCtcReelDO validateReelExists(Long id) {
        ProductCtcReelDO reel = reelMapper.selectById(id);
        if (reel == null) {
            throw exception(PD_CTC_REEL_NOT_EXISTS);
        }
        return reel;
    }
 
}