云
4 小时以前 c215f6c20445b973ff730dcdf4206426992da81d
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package cn.iocoder.yudao.module.mes.service.qc.oqc;
 
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.oqc.MesQcOqcDO;
import cn.iocoder.yudao.module.mes.controller.admin.qc.oqc.vo.MesQcOqcRetestReqVO;
import cn.iocoder.yudao.module.mes.dal.mysql.qc.oqc.MesQcOqcMapper;
import cn.iocoder.yudao.module.mes.enums.MesBizTypeConstants;
import cn.iocoder.yudao.module.mes.enums.qc.MesQcStatusEnum;
import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemService;
import cn.iocoder.yudao.module.mes.service.qc.defectrecord.MesQcDefectRecordService;
import cn.iocoder.yudao.module.mes.service.qc.template.MesQcTemplateItemService;
import cn.iocoder.yudao.module.mes.service.wm.productsales.MesWmProductSalesLineService;
import cn.iocoder.yudao.module.mes.service.wm.productsales.MesWmProductSalesService;
import cn.iocoder.yudao.module.mes.service.qc.indicatorresult.MesQcIndicatorResultService;
import cn.iocoder.yudao.module.mes.service.qc.ncr.MesQcNcrService;
import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi;
import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
 
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
 
/**
 * {@link MesQcOqcServiceImpl} 的单元测试类
 *
 * @author 超级管理员
 */
@Import(MesQcOqcServiceImpl.class)
public class MesQcOqcServiceImplTest extends BaseDbUnitTest {
 
    @Resource
    private MesQcOqcServiceImpl oqcService;
 
    @Resource
    private MesQcOqcMapper oqcMapper;
 
    @MockitoBean
    private MesWmProductSalesLineService productSalesLineService;
    @MockitoBean
    private MesWmProductSalesService productSalesService;
    @MockitoBean
    private MesQcOqcLineService oqcLineService;
    @MockitoBean
    private MesMdItemService itemService;
    @MockitoBean
    private MesQcTemplateItemService templateItemService;
    @MockitoBean
    private MesQcDefectRecordService defectRecordService;
    @MockitoBean
    private AdminUserApi adminUserApi;
    @MockitoBean
    private MesQcIndicatorResultService indicatorResultService;
    @MockitoBean
    private MesQcNcrService ncrService;
    @MockitoBean
    private MdmItemApi mdmItemApi;
    @MockitoBean
    private CrmCustomerApi customerApi;
 
    @Test
    public void testFinishOqc_writeBack_productSales() {
        // 准备数据:插入一条草稿状态的 OQC 单,来源为销售出库单
        Long sourceLineId = randomLongId();
        Integer checkResult = 1;
        MesQcOqcDO oqc = randomPojo(MesQcOqcDO.class, o -> {
            o.setStatus(MesQcStatusEnum.DRAFT.getStatus());
            o.setSourceDocType(MesBizTypeConstants.WM_PRODUCT_SALES);
            o.setSourceLineId(sourceLineId);
            o.setCheckResult(checkResult);
            o.setUnqualifiedQuantity(java.math.BigDecimal.ZERO);
            o.setQualifiedQuantity(java.math.BigDecimal.TEN);
            o.setCheckQuantity(java.math.BigDecimal.TEN);
        });
        oqcMapper.insert(oqc);
 
        // 调用
        oqcService.finishOqc(oqc.getId());
 
        // 断言:验证状态更新为已完成
        MesQcOqcDO updatedOqc = oqcMapper.selectById(oqc.getId());
        assertEquals(MesQcStatusEnum.FINISHED.getStatus(), updatedOqc.getStatus());
        assertEquals(20, updatedOqc.getWorkflowStatus());
        // 初检完成不放行;销售部和公司质检均审批后才回写。
        verify(productSalesLineService, never()).updateProductSalesLineWhenOqcFinish(anyLong(), anyLong(), anyInt());
        oqcService.salesApprove(oqc.getId(), true, "销售部同意");
        oqcService.companyApprove(oqc.getId(), true, "公司质检同意");
        verify(productSalesLineService).updateProductSalesLineWhenOqcFinish(
                eq(sourceLineId), eq(oqc.getId()), eq(checkResult));
    }
 
    @Test
    public void testFailedInitialRetestPassOnlyReleasesAfterBothApprovals() {
        Long sourceLineId = randomLongId();
        MesQcOqcDO oqc = randomPojo(MesQcOqcDO.class, o -> {
            o.setStatus(MesQcStatusEnum.DRAFT.getStatus());
            o.setSourceDocType(MesBizTypeConstants.WM_PRODUCT_SALES);
            o.setSourceLineId(sourceLineId);
            o.setOutQuantity(java.math.BigDecimal.valueOf(100));
            o.setCheckQuantity(java.math.BigDecimal.TEN);
            o.setQualifiedQuantity(java.math.BigDecimal.valueOf(9));
            o.setUnqualifiedQuantity(java.math.BigDecimal.ONE);
            o.setCheckResult(2);
        });
        oqcMapper.insert(oqc);
        oqcService.finishOqc(oqc.getId());
        assertEquals(10, oqcMapper.selectById(oqc.getId()).getWorkflowStatus());
        verifyNoInteractions(ncrService);
        verify(productSalesLineService, never()).updateProductSalesLineWhenOqcFinish(anyLong(), anyLong(), anyInt());
 
        MesQcOqcRetestReqVO retest = new MesQcOqcRetestReqVO();
        retest.setId(oqc.getId());
        retest.setCheckQuantity(java.math.BigDecimal.TEN);
        retest.setQualifiedQuantity(java.math.BigDecimal.TEN);
        retest.setUnqualifiedQuantity(java.math.BigDecimal.ZERO);
        retest.setCheckResult(1);
        oqcService.retestOqc(retest);
        assertEquals(20, oqcMapper.selectById(oqc.getId()).getWorkflowStatus());
        oqcService.salesApprove(oqc.getId(), true, "销售部通过");
        assertEquals(30, oqcMapper.selectById(oqc.getId()).getWorkflowStatus());
        verify(productSalesLineService, never()).updateProductSalesLineWhenOqcFinish(anyLong(), anyLong(), anyInt());
        oqcService.companyApprove(oqc.getId(), true, "公司质检通过");
        assertEquals(40, oqcMapper.selectById(oqc.getId()).getWorkflowStatus());
        verify(productSalesLineService).updateProductSalesLineWhenOqcFinish(sourceLineId, oqc.getId(), 1);
        verifyNoInteractions(ncrService);
    }
 
    @Test
    public void testFailedRetestCreatesNcrOnlyAfterCompanyApproval() {
        Long sourceLineId = randomLongId();
        MesQcOqcDO oqc = randomPojo(MesQcOqcDO.class, o -> {
            o.setStatus(MesQcStatusEnum.DRAFT.getStatus());
            o.setSourceDocType(MesBizTypeConstants.WM_PRODUCT_SALES);
            o.setSourceLineId(sourceLineId);
            o.setOutQuantity(java.math.BigDecimal.valueOf(100));
            o.setCheckQuantity(java.math.BigDecimal.TEN);
            o.setQualifiedQuantity(java.math.BigDecimal.valueOf(9));
            o.setUnqualifiedQuantity(java.math.BigDecimal.ONE);
            o.setCheckResult(2);
        });
        oqcMapper.insert(oqc);
        oqcService.finishOqc(oqc.getId());
        MesQcOqcRetestReqVO retest = new MesQcOqcRetestReqVO();
        retest.setId(oqc.getId());
        retest.setCheckQuantity(java.math.BigDecimal.TEN);
        retest.setQualifiedQuantity(java.math.BigDecimal.valueOf(8));
        retest.setUnqualifiedQuantity(java.math.BigDecimal.valueOf(2));
        retest.setCheckResult(2);
        retest.setOpinion("复检仍不合格");
        oqcService.retestOqc(retest);
        assertEquals(50, oqcMapper.selectById(oqc.getId()).getWorkflowStatus());
        verifyNoInteractions(ncrService);
        oqcService.salesApprove(oqc.getId(), true, "销售部确认不合格");
        assertEquals(60, oqcMapper.selectById(oqc.getId()).getWorkflowStatus());
        verifyNoInteractions(ncrService);
        oqcService.companyApprove(oqc.getId(), true, "公司质检确认不合格");
        assertEquals(70, oqcMapper.selectById(oqc.getId()).getWorkflowStatus());
        verify(ncrService).createNcr(any());
        verify(productSalesLineService).updateProductSalesLineWhenOqcFinish(sourceLineId, oqc.getId(), 2);
    }
 
    @Test
    public void testFinishOqc_writeBack_noSourceDoc() {
        // 准备数据:sourceDocType 为空,不需要回写
        MesQcOqcDO oqc = randomPojo(MesQcOqcDO.class, o -> {
            o.setStatus(MesQcStatusEnum.DRAFT.getStatus());
            o.setCheckResult(1);
            o.setUnqualifiedQuantity(java.math.BigDecimal.ZERO);
            o.setQualifiedQuantity(java.math.BigDecimal.TEN);
            o.setCheckQuantity(java.math.BigDecimal.TEN);
            o.setSourceDocType(null);
            o.setSourceLineId(null);
        });
        oqcMapper.insert(oqc);
 
        // 调用
        oqcService.finishOqc(oqc.getId());
 
        // 断言:验证状态更新为已完成
        MesQcOqcDO updatedOqc = oqcMapper.selectById(oqc.getId());
        assertEquals(MesQcStatusEnum.FINISHED.getStatus(), updatedOqc.getStatus());
        // 断言:不应该调用任何回写
        verify(productSalesLineService, never()).updateProductSalesLineWhenOqcFinish(
                anyLong(), anyLong(), anyInt());
    }
 
    @Test
    public void testFinishOqc_writeBack_sourceDocTypeDirtyData() {
        // 准备数据:sourceDocType 非空但 sourceLineId 为空,应该抛异常
        MesQcOqcDO oqc = randomPojo(MesQcOqcDO.class, o -> {
            o.setStatus(MesQcStatusEnum.DRAFT.getStatus());
            o.setCheckResult(1);
            o.setUnqualifiedQuantity(java.math.BigDecimal.ZERO);
            o.setQualifiedQuantity(java.math.BigDecimal.TEN);
            o.setCheckQuantity(java.math.BigDecimal.TEN);
            o.setSourceDocType(MesBizTypeConstants.WM_PRODUCT_SALES);
            o.setSourceLineId(null); // 脏数据
        });
        oqcMapper.insert(oqc);
 
        oqcService.finishOqc(oqc.getId());
        oqcService.salesApprove(oqc.getId(), true, "销售部同意");
        assertThrows(IllegalArgumentException.class, () -> oqcService.companyApprove(oqc.getId(), true, "公司质检同意"));
    }
 
    @Test
    public void testFinishOqc_writeBack_unknownSourceDocType() {
        // 准备数据:sourceDocType 无法识别
        MesQcOqcDO oqc = randomPojo(MesQcOqcDO.class, o -> {
            o.setStatus(MesQcStatusEnum.DRAFT.getStatus());
            o.setCheckResult(1);
            o.setUnqualifiedQuantity(java.math.BigDecimal.ZERO);
            o.setQualifiedQuantity(java.math.BigDecimal.TEN);
            o.setCheckQuantity(java.math.BigDecimal.TEN);
            o.setSourceDocType(99999); // 无法识别的类型
            o.setSourceLineId(randomLongId());
        });
        oqcMapper.insert(oqc);
 
        oqcService.finishOqc(oqc.getId());
        oqcService.salesApprove(oqc.getId(), true, "销售部同意");
        assertThrows(IllegalArgumentException.class, () -> oqcService.companyApprove(oqc.getId(), true, "公司质检同意"));
    }
 
    @Test
    public void testFinishOqc_notExists() {
        // 准备参数
        Long oqcId = randomLongId();
 
        // 调用,并断言异常
        assertServiceException(() -> oqcService.finishOqc(oqcId), QC_OQC_NOT_EXISTS);
    }
 
    @Test
    public void testFinishOqc_statusNotDraft() {
        // 准备参数:状态为已完成
        MesQcOqcDO oqc = randomPojo(MesQcOqcDO.class, o -> {
            o.setStatus(MesQcStatusEnum.FINISHED.getStatus());
        });
        oqcMapper.insert(oqc);
 
        // 调用,并断言异常
        assertServiceException(() -> oqcService.finishOqc(oqc.getId()), QC_OQC_NOT_PREPARE);
    }
 
    @Test
    public void testFinishOqc_checkResultEmpty() {
        // 准备数据:checkResult 为空,应该报错
        MesQcOqcDO oqc = randomPojo(MesQcOqcDO.class, o -> {
            o.setStatus(MesQcStatusEnum.DRAFT.getStatus());
            o.setCheckResult(null); // 检测结论为空
            o.setSourceDocType(null);
        });
        oqcMapper.insert(oqc);
 
        // 调用,应该抛异常(检测结论必填)
        assertServiceException(() -> oqcService.finishOqc(oqc.getId()), QC_OQC_CHECK_RESULT_EMPTY);
    }
 
    @Test
    public void testDeleteOqc_success() {
        // 准备数据:草稿状态
        MesQcOqcDO oqc = randomPojo(MesQcOqcDO.class, o -> {
            o.setStatus(MesQcStatusEnum.DRAFT.getStatus());
        });
        oqcMapper.insert(oqc);
 
        // 调用
        oqcService.deleteOqc(oqc.getId());
 
        // 断言:已删除
        assertNull(oqcMapper.selectById(oqc.getId()));
    }
 
    @Test
    public void testDeleteOqc_notExists() {
        assertServiceException(() -> oqcService.deleteOqc(randomLongId()), QC_OQC_NOT_EXISTS);
    }
 
    @Test
    public void testDeleteOqc_statusNotDraft() {
        MesQcOqcDO oqc = randomPojo(MesQcOqcDO.class, o -> {
            o.setStatus(MesQcStatusEnum.FINISHED.getStatus());
        });
        oqcMapper.insert(oqc);
 
        assertServiceException(() -> oqcService.deleteOqc(oqc.getId()), QC_OQC_NOT_PREPARE);
    }
 
}