116 分钟以前 beaba1bf3a30053408a860479f9150b02169a7c8
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
package cn.iocoder.yudao.module.mes.service.pro.pallet;
 
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.mes.controller.admin.pro.pallet.vo.MesProPalletAutoPrintRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.pro.pallet.vo.MesProPalletBatchCreateReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.pro.pallet.vo.MesProPalletPageReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.batch.MesProBatchDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.pallet.MesProPalletDO;
import cn.iocoder.yudao.module.mes.dal.mysql.pro.bagcode.MesProBagCodeMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.pro.batch.MesProBatchMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.pro.pallet.MesProPalletMapper;
import cn.iocoder.yudao.module.mes.dal.dataobject.mdm.MesMdmItemDO;
import cn.iocoder.yudao.module.mes.enums.pro.MesProBagTypeEnum;
import cn.iocoder.yudao.module.mes.enums.pro.MesProBatchStatusEnum;
import cn.iocoder.yudao.module.mes.enums.pro.MesProPalletStatusEnum;
import cn.iocoder.yudao.module.mes.service.mdm.MesMdmItemService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
 
/**
 * MES 托盘码 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
@Slf4j
public class MesProPalletServiceImpl implements MesProPalletService {
 
    @Resource
    private MesProPalletMapper palletMapper;
    @Resource
    private MesProBagCodeMapper bagCodeMapper;
    @Resource
    private MesProBatchMapper batchMapper;
    @Resource
    private MesMdmItemService mdmItemService;
    @Resource
    private AdminUserApi adminUserApi;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Long> batchCreatePallet(MesProPalletBatchCreateReqVO createReqVO) {
        // 1. 校验批次存在且处于生产中状态
        MesProBatchDO batch = batchMapper.selectById(createReqVO.getBatchId());
        if (batch == null) {
            throw exception(PRO_BATCH_NOT_EXISTS);
        }
        if (!MesProBatchStatusEnum.PRODUCING.getStatus().equals(batch.getStatus())) {
            throw exception(PRO_BATCH_STATUS_NOT_GENERATE_CODE);
        }
        if (createReqVO.getCount() == null || createReqVO.getCount() <= 0) {
            throw exception(PRO_PALLET_GENERATE_COUNT_INVALID);
        }
 
        // 2. 计算起始序号(包含已逻辑删除记录,保证序号不回退、码值唯一)
        int startNo = palletMapper.selectMaxPalletNo(batch.getId()) + 1;
 
        // 3. 批量生成托盘码:批次号 + -P + 2 位托盘序号
        List<MesProPalletDO> pallets = new ArrayList<>(createReqVO.getCount());
        for (int i = 0; i < createReqVO.getCount(); i++) {
            int palletNo = startNo + i;
            pallets.add(new MesProPalletDO()
                    .setCode(batch.getCode() + "-P" + StrUtil.padPre(String.valueOf(palletNo), 2, '0'))
                    .setBatchId(batch.getId())
                    .setBatchCode(batch.getCode())
                    .setPalletNo(palletNo)
                    .setCapacity(createReqVO.getCapacity())
                    .setBagCount(0)
                    .setAutoGenerated(false)
                    .setStatus(MesProPalletStatusEnum.IN_USE.getStatus())
                    .setRemark(createReqVO.getRemark()));
        }
        palletMapper.insertBatch(pallets);
 
        // 4. 回写批次托盘码数量
        batchMapper.updateById(new MesProBatchDO().setId(batch.getId())
                .setPalletQuantity((batch.getPalletQuantity() == null ? 0 : batch.getPalletQuantity())
                        + createReqVO.getCount()));
        return pallets.stream().map(MesProPalletDO::getId).toList();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MesProPalletAutoPrintRespVO> autoCreatePallets(Long batchId) {
        MesProBatchDO batch = batchMapper.selectByIdForUpdate(batchId);
        if (batch == null) {
            throw exception(PRO_BATCH_NOT_EXISTS);
        }
        if (!MesProBatchStatusEnum.PRODUCING.getStatus().equals(batch.getStatus())) {
            throw exception(PRO_BATCH_STATUS_NOT_GENERATE_CODE);
        }
        MesProBagTypeEnum bagType = MesProBagTypeEnum.valueOfType(batch.getBagType());
        if (bagType == null) {
            throw exception(PRO_BATCH_BAG_TYPE_INVALID);
        }
        List<cn.iocoder.yudao.module.mes.dal.dataobject.pro.bagcode.MesProBagCodeDO> candidates =
                bagCodeMapper.selectUnboundEffectiveListByBatchId(batchId);
        int threshold = bagType.getThreshold();
        int palletCount = candidates.size() / threshold;
        if (palletCount == 0) {
            return Collections.emptyList();
        }
 
        int startNo = palletMapper.selectMaxPalletNo(batchId) + 1;
        List<MesProPalletDO> pallets = new ArrayList<>(palletCount);
        List<List<Long>> bagIdGroups = new ArrayList<>(palletCount);
        for (int i = 0; i < palletCount; i++) {
            int palletNo = startNo + i;
            MesProPalletDO pallet = new MesProPalletDO()
                    .setCode(batch.getCode() + "-P" + StrUtil.padPre(String.valueOf(palletNo), 2, '0'))
                    .setBatchId(batchId)
                    .setBatchCode(batch.getCode())
                    .setPalletNo(palletNo)
                    .setCapacity(threshold)
                    .setBagCount(threshold)
                    .setAutoGenerated(true)
                    .setStatus(MesProPalletStatusEnum.IN_USE.getStatus())
                    .setRemark("按有效袋码阈值自动生成");
            pallets.add(pallet);
            bagIdGroups.add(candidates.subList(i * threshold, (i + 1) * threshold).stream()
                    .map(cn.iocoder.yudao.module.mes.dal.dataobject.pro.bagcode.MesProBagCodeDO::getId).toList());
        }
        palletMapper.insertBatch(pallets);
        for (int i = 0; i < pallets.size(); i++) {
            MesProPalletDO pallet = pallets.get(i);
            bagCodeMapper.bindPalletByIds(bagIdGroups.get(i), pallet.getId(), pallet.getCode());
        }
        batchMapper.updateById(new MesProBatchDO().setId(batchId)
                .setPalletQuantity((batch.getPalletQuantity() == null ? 0 : batch.getPalletQuantity()) + pallets.size()));
        return buildAutoPrintVOs(pallets, batch);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePallet(Long id) {
        // 1. 校验存在
        MesProPalletDO pallet = validatePalletExists(id);
        // 2. 校验未绑定袋码
        if (bagCodeMapper.selectCountByPalletId(id) > 0) {
            throw exception(PRO_PALLET_HAS_BAG_NOT_DELETE);
        }
        // 3. 删除托盘码
        palletMapper.deleteById(id);
        // 4. 回减批次托盘码数量
        MesProBatchDO batch = batchMapper.selectById(pallet.getBatchId());
        if (batch != null && batch.getPalletQuantity() != null && batch.getPalletQuantity() > 0) {
            batchMapper.updateById(new MesProBatchDO().setId(batch.getId())
                    .setPalletQuantity(batch.getPalletQuantity() - 1));
        }
    }
 
    private List<MesProPalletAutoPrintRespVO> buildAutoPrintVOs(List<MesProPalletDO> pallets, MesProBatchDO batch) {
        MesMdmItemDO item = batch.getItemId() == null ? null : mdmItemService.getItem(batch.getItemId());
        AdminUserRespDTO owner = batch.getOwnerUserId() == null ? null : adminUserApi.getUser(batch.getOwnerUserId());
        return pallets.stream().map(pallet -> new MesProPalletAutoPrintRespVO()
                .setId(pallet.getId())
                .setCode(pallet.getCode())
                .setBatchId(pallet.getBatchId())
                .setBatchCode(pallet.getBatchCode())
                .setPalletNo(pallet.getPalletNo())
                .setCopies(3)
                .setCapacity(pallet.getCapacity())
                .setBagCount(pallet.getBagCount())
                .setItemId(batch.getItemId())
                .setItemCode(item == null ? null : item.getCode())
                .setItemName(item == null ? null : item.getName())
                .setItemSpecification(item == null ? null : item.getSpecification())
                .setLineCode(batch.getLineCode())
                .setLineName(batch.getLineName())
                .setProduceDate(batch.getProduceDate())
                .setOwnerUserId(batch.getOwnerUserId())
                .setOwnerUserName(owner == null ? null : owner.getNickname()))
                .toList();
    }
 
    @Override
    public MesProPalletDO getPallet(Long id) {
        return palletMapper.selectById(id);
    }
 
    @Override
    public MesProPalletDO getPalletByCode(String code) {
        return palletMapper.selectByCode(code);
    }
 
    @Override
    public MesProPalletDO validatePalletExists(Long id) {
        MesProPalletDO pallet = palletMapper.selectById(id);
        if (pallet == null) {
            throw exception(PRO_PALLET_NOT_EXISTS);
        }
        return pallet;
    }
 
    @Override
    public PageResult<MesProPalletDO> getPalletPage(MesProPalletPageReqVO pageReqVO) {
        return palletMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<MesProPalletDO> getPalletListByBatchId(Long batchId) {
        return palletMapper.selectListByBatchId(batchId);
    }
 
    @Override
    public void updatePalletBagCount(Long palletId, Integer bagCount) {
        palletMapper.updateById(new MesProPalletDO().setId(palletId).setBagCount(bagCount));
    }
 
}