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
package cn.iocoder.yudao.module.mes.controller.admin.cal.plan;
 
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.mes.controller.admin.cal.plan.vo.MesCalPlanImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.cal.plan.vo.MesCalPlanImportRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.cal.plan.vo.MesCalPlanPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.cal.plan.vo.MesCalPlanRespVO;
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.service.cal.plan.MesCalPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
 
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 
@Tag(name = "管理后台 - MES 排班计划")
@RestController
@RequestMapping("/mes/cal/plan")
@Validated
public class MesCalPlanController {
 
    @Resource
    private MesCalPlanService planService;
 
    @PostMapping("/create")
    @Operation(summary = "创建排班计划")
    @PreAuthorize("@ss.hasPermission('mes:cal-plan:create')")
    public CommonResult<Long> createPlan(@Valid @RequestBody MesCalPlanSaveReqVO createReqVO) {
        return success(planService.createPlan(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新排班计划")
    @PreAuthorize("@ss.hasPermission('mes:cal-plan:update')")
    public CommonResult<Boolean> updatePlan(@Valid @RequestBody MesCalPlanSaveReqVO updateReqVO) {
        planService.updatePlan(updateReqVO);
        return success(true);
    }
 
    @PutMapping("/confirm")
    @Operation(summary = "确认排班计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:cal-plan:update')")
    public CommonResult<Boolean> confirmPlan(@RequestParam("id") Long id) {
        planService.confirmPlan(id);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除排班计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:cal-plan:delete')")
    public CommonResult<Boolean> deletePlan(@RequestParam("id") Long id) {
        planService.deletePlan(id);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得排班计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<MesCalPlanRespVO> getPlan(@RequestParam("id") Long id) {
        MesCalPlanDO plan = planService.getPlan(id);
        return success(BeanUtils.toBean(plan, MesCalPlanRespVO.class));
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得排班计划分页")
    public CommonResult<PageResult<MesCalPlanRespVO>> getPlanPage(@Valid MesCalPlanPageReqVO pageReqVO) {
        PageResult<MesCalPlanDO> pageResult = planService.getPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MesCalPlanRespVO.class));
    }
 
    @GetMapping("/export-excel")
    @Operation(summary = "导出排班计划 Excel")
    @PreAuthorize("@ss.hasPermission('mes:cal-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPlanExcel(@Valid MesCalPlanPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MesCalPlanDO> list = planService.getPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "排班计划.xls", "数据", MesCalPlanRespVO.class,
                BeanUtils.toBean(list, MesCalPlanRespVO.class));
    }
 
    @GetMapping("/get-import-template")
    @Operation(summary = "获得排班计划导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<MesCalPlanImportExcelVO> list = Collections.singletonList(
                MesCalPlanImportExcelVO.builder().code("PLAN-001").name("示例排班计划")
                        .calendarType(1)
                        .startDate(LocalDateTime.of(2026, 1, 1, 0, 0))
                        .endDate(LocalDateTime.of(2026, 12, 31, 0, 0))
                        .shiftType(2)
                        .shiftMethod(1)
                        .shiftCount(7)
                        .excludeWeekend(true)
                        .remark("").build()
        );
        // 输出
        ExcelUtils.writeTemplate(response, "排班计划导入模板.xls", "排班计划列表", MesCalPlanImportExcelVO.class, list);
    }
 
    @PostMapping("/import")
    @Operation(summary = "导入排班计划")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('mes:cal-plan:import')")
    public CommonResult<MesCalPlanImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                             @RequestParam(value = "updateSupport", required = false,
                                                                     defaultValue = "false") Boolean updateSupport) throws Exception {
        List<MesCalPlanImportExcelVO> list = ExcelUtils.read(file, MesCalPlanImportExcelVO.class);
        return success(planService.importPlanList(list, updateSupport));
    }
 
}