xiaoyi
4 天以前 abf1c0a35d85d38239ff5d2ed746a4e4b797c9d1
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
package cn.iocoder.yudao.module.mes.service.qc.outsourcetest;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
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.qc.outsourcetest.vo.MesQcOutsourceTestPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.qc.outsourcetest.vo.MesQcOutsourceTestSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.outsourcetest.MesQcOutsourceTestDO;
import cn.iocoder.yudao.module.mes.dal.mysql.qc.outsourcetest.MesQcOutsourceTestMapper;
import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
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.Collection;
import java.util.Objects;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
 
/**
 * MES 委外检测/试验单 Service 实现类
 *
 * @author xiaoyi
 */
@Service
@Validated
@Slf4j
public class MesQcOutsourceTestServiceImpl implements MesQcOutsourceTestService {
 
    /**
     * 附件关联业务类型
     */
    private static final String ATTACHMENT_BIZ_TYPE = "file";
    /**
     * 附件关联业务记录类型
     */
    private static final String ATTACHMENT_RECORD_TYPE = "mes_qc_outsource_test";
 
    /**
     * 状态:草稿
     */
    private static final Integer STATUS_DRAFT = 0;
    /**
     * 状态:已提交
     */
    private static final Integer STATUS_SUBMITTED = 10;
    /**
     * 状态:已完成
     */
    private static final Integer STATUS_FINISHED = 20;
 
    @Resource
    private MesQcOutsourceTestMapper testMapper;
    @Resource
    private StorageAttachmentApi storageAttachmentApi;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createTest(MesQcOutsourceTestSaveReqVO createReqVO) {
        // 1. 生成单号
        String code = generateCode();
        if (testMapper.selectByCode(code) != null) {
            throw exception(PRO_WORK_ORDER_CODE_DUPLICATE);
        }
        // 2. 插入
        MesQcOutsourceTestDO test = BeanUtils.toBean(createReqVO, MesQcOutsourceTestDO.class, req -> req
                .setCode(code)
                .setStatus(STATUS_DRAFT));
        testMapper.insert(test);
        // 3. 绑定附件
        storageAttachmentApi.bindAttachments(ATTACHMENT_BIZ_TYPE, ATTACHMENT_RECORD_TYPE,
                test.getId(), createReqVO.getBlobIds());
        return test.getId();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTest(MesQcOutsourceTestSaveReqVO updateReqVO) {
        // 1. 校验存在 + 草稿
        MesQcOutsourceTestDO oldTest = validateTestCanModify(updateReqVO.getId());
        // 2. 更新
        MesQcOutsourceTestDO updateObj = BeanUtils.toBean(updateReqVO, MesQcOutsourceTestDO.class, req -> req
                .setCode(oldTest.getCode())
                .setStatus(oldTest.getStatus()));
        testMapper.updateById(updateObj);
        // 3. 重新绑定附件
        storageAttachmentApi.bindAttachments(ATTACHMENT_BIZ_TYPE, ATTACHMENT_RECORD_TYPE,
                updateReqVO.getId(), updateReqVO.getBlobIds());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTest(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        for (Long id : ids) {
            validateTestCanModify(id);
            testMapper.deleteById(id);
        }
    }
 
    @Override
    public MesQcOutsourceTestDO getTest(Long id) {
        return testMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesQcOutsourceTestDO> getTestPage(MesQcOutsourceTestPageReqVO pageReqVO) {
        return testMapper.selectPage(pageReqVO);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitTest(Long id) {
        MesQcOutsourceTestDO test = validateTestExists(id);
        if (!Objects.equals(test.getStatus(), STATUS_DRAFT)) {
            throw exception(PRO_WORK_ORDER_NOT_PREPARE);
        }
        testMapper.updateById(new MesQcOutsourceTestDO().setId(id).setStatus(STATUS_SUBMITTED));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void finishTest(Long id) {
        MesQcOutsourceTestDO test = validateTestExists(id);
        if (!Objects.equals(test.getStatus(), STATUS_SUBMITTED)) {
            throw exception(PRO_WORK_ORDER_NOT_CONFIRMED);
        }
        testMapper.updateById(new MesQcOutsourceTestDO().setId(id).setStatus(STATUS_FINISHED));
    }
 
    private String generateCode() {
        return "WT" + DateUtil.format(java.time.LocalDateTime.now(), "yyyyMMddHHmmss")
                + String.format("%03d", (int) (Math.random() * 1000));
    }
 
    private MesQcOutsourceTestDO validateTestExists(Long id) {
        MesQcOutsourceTestDO test = testMapper.selectById(id);
        if (test == null) {
            throw exception(PRO_WORK_ORDER_NOT_EXISTS);
        }
        return test;
    }
 
    private MesQcOutsourceTestDO validateTestCanModify(Long id) {
        MesQcOutsourceTestDO test = validateTestExists(id);
        if (!Objects.equals(test.getStatus(), STATUS_DRAFT)) {
            throw exception(PRO_WORK_ORDER_NOT_PREPARE);
        }
        return test;
    }
 
}