2026-06-29 940c8481d2fdcf51341db4ccd6fd6fcc2d43debb
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
package cn.iocoder.yudao.module.mes.service.cal.plan;
 
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.mes.controller.admin.cal.plan.vo.MesCalPlanSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.cal.plan.MesCalPlanDO;
import cn.iocoder.yudao.module.mes.dal.mysql.cal.plan.MesCalPlanMapper;
import cn.iocoder.yudao.module.mes.enums.cal.MesCalPlanStatusEnum;
import cn.iocoder.yudao.module.mes.enums.cal.MesCalShiftMethodEnum;
import cn.iocoder.yudao.module.mes.enums.cal.MesCalShiftTypeEnum;
import cn.iocoder.yudao.module.mes.service.cal.team.MesCalTeamShiftService;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
 
import jakarta.annotation.Resource;
 
import java.time.LocalDateTime;
 
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
 
/**
 * {@link MesCalPlanServiceImpl} 的单元测试
 *
 * @author 芋道源码
 */
@Import(MesCalPlanServiceImpl.class)
public class MesCalPlanServiceImplTest extends BaseDbUnitTest {
 
    @Resource
    private MesCalPlanServiceImpl planService;
 
    @Resource
    private MesCalPlanMapper planMapper;
 
    @MockitoBean
    private MesCalPlanShiftService planShiftService;
    @MockitoBean
    private MesCalPlanTeamService planTeamService;
    @MockitoBean
    private MesCalTeamShiftService teamShiftService;
 
    @Test
    public void testCreatePlan_singleShift_addDefaultShift() {
        // 准备参数
        MesCalPlanSaveReqVO reqVO = new MesCalPlanSaveReqVO();
        reqVO.setCode("PLAN-SINGLE-001");
        reqVO.setName("单白班默认班次");
        reqVO.setCalendarType(1);
        reqVO.setStartDate(LocalDateTime.of(2026, 4, 1, 0, 0));
        reqVO.setEndDate(LocalDateTime.of(2026, 4, 30, 0, 0));
        reqVO.setShiftType(MesCalShiftTypeEnum.SINGLE.getType());
        reqVO.setShiftMethod(MesCalShiftMethodEnum.DAY.getMethod());
        reqVO.setShiftCount(1);
 
        // 调用
        Long planId = planService.createPlan(reqVO);
 
        // 断言 1:计划状态为草稿
        MesCalPlanDO plan = planMapper.selectById(planId);
        assertEquals(MesCalPlanStatusEnum.PREPARE.getStatus(), plan.getStatus());
        // 断言 2:自动生成默认班次
        verify(planShiftService).addDefaultPlanShift(planId, MesCalShiftTypeEnum.SINGLE.getType());
    }
 
    @Test
    public void testConfirmPlan_syncGenerateTeamShiftRecords() {
        // mock 数据:插入一条草稿状态的排班计划
        MesCalPlanDO plan = randomPojo(MesCalPlanDO.class, o -> {
            o.setCalendarType(1);
            o.setStartDate(LocalDateTime.of(2026, 4, 1, 0, 0));
            o.setEndDate(LocalDateTime.of(2026, 4, 3, 0, 0));
            o.setStatus(MesCalPlanStatusEnum.PREPARE.getStatus());
            o.setShiftType(MesCalShiftTypeEnum.SINGLE.getType());
            o.setShiftMethod(MesCalShiftMethodEnum.DAY.getMethod());
            o.setShiftCount(1);
        });
        planMapper.insert(plan);
        // mock 方法:班组数量满足要求
        when(planTeamService.getPlanTeamCountByPlanId(plan.getId())).thenReturn(1L);
 
        // 调用
        planService.confirmPlan(plan.getId());
 
        // 断言 1:计划状态更新为已确认
        MesCalPlanDO updatePlan = planMapper.selectById(plan.getId());
        assertEquals(MesCalPlanStatusEnum.CONFIRMED.getStatus(), updatePlan.getStatus());
        // 断言 2:触发生成班组排班记录
        verify(teamShiftService).generateTeamShiftRecords(plan.getId());
    }
 
    @Test
    public void testDeletePlan_notCascadeDeleteTeamShift() {
        // mock 数据:插入一条草稿状态的排班计划
        MesCalPlanDO plan = randomPojo(MesCalPlanDO.class, o -> {
            o.setCalendarType(1);
            o.setStartDate(LocalDateTime.of(2026, 4, 1, 0, 0));
            o.setEndDate(LocalDateTime.of(2026, 4, 3, 0, 0));
            o.setStatus(MesCalPlanStatusEnum.PREPARE.getStatus());
            o.setShiftType(MesCalShiftTypeEnum.SINGLE.getType());
            o.setShiftMethod(MesCalShiftMethodEnum.DAY.getMethod());
            o.setShiftCount(1);
        });
        planMapper.insert(plan);
 
        // 调用
        planService.deletePlan(plan.getId());
 
        // 断言 1:计划被删除
        assertNull(planMapper.selectById(plan.getId()));
        // 断言 2:级联删除班次和班组关联
        verify(planShiftService).deletePlanShiftByPlanId(plan.getId());
        verify(planTeamService).deleteByPlanId(plan.getId());
        // 断言 3:不级联删除班组排班记录
        verify(teamShiftService, never()).deleteByPlanId(plan.getId());
    }
 
}