xiaoyi
3 天以前 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package cn.iocoder.yudao.module.mes.controller.admin.pro.maintenanceplan;
 
import cn.hutool.core.collection.CollUtil;
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.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.mes.controller.admin.pro.maintenanceplan.vo.MesProMaintenancePlanPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.pro.maintenanceplan.vo.MesProMaintenancePlanRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.pro.maintenanceplan.vo.MesProMaintenancePlanSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.unitmeasure.MesMdUnitMeasureDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.maintenanceplan.MesProMaintenancePlanDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.maintenanceplan.MesProMaintenancePlanItemDO;
import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemService;
import cn.iocoder.yudao.module.mes.service.md.unitmeasure.MesMdUnitMeasureService;
import cn.iocoder.yudao.module.mes.service.pro.maintenanceplan.MesProMaintenancePlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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 java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMultiMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
 
@Tag(name = "管理后台 - MES 电力作业计划")
@RestController
@RequestMapping("/mes/pro/maintenance-plan")
@Validated
public class MesProMaintenancePlanController {
 
    @Resource
    private MesProMaintenancePlanService planService;
    @Resource
    private MesMdItemService itemService;
    @Resource
    private MesMdUnitMeasureService unitMeasureService;
 
    @PostMapping("/create")
    @Operation(summary = "创建电力作业计划")
    @PreAuthorize("@ss.hasPermission('mes:pro-maintenance-plan:create')")
    public CommonResult<Long> createPlan(@Valid @RequestBody MesProMaintenancePlanSaveReqVO createReqVO) {
        return success(planService.createPlan(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新电力作业计划")
    @PreAuthorize("@ss.hasPermission('mes:pro-maintenance-plan:update')")
    public CommonResult<Boolean> updatePlan(@Valid @RequestBody MesProMaintenancePlanSaveReqVO updateReqVO) {
        planService.updatePlan(updateReqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除电力作业计划")
    @Parameter(name = "ids", description = "编号数组", required = true)
    @PreAuthorize("@ss.hasPermission('mes:pro-maintenance-plan:delete')")
    public CommonResult<Boolean> deletePlan(@RequestParam("ids") List<Long> ids) {
        planService.deletePlan(ids);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得电力作业计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:pro-maintenance-plan:query')")
    public CommonResult<MesProMaintenancePlanRespVO> getPlan(@RequestParam("id") Long id) {
        MesProMaintenancePlanDO plan = planService.getPlan(id);
        if (plan == null) {
            return success(null);
        }
        List<MesProMaintenancePlanItemDO> planItemList = planService.getPlanItemListByPlanId(id);
        return success(buildPlanRespVO(plan, planItemList));
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得电力作业计划分页")
    @PreAuthorize("@ss.hasPermission('mes:pro-maintenance-plan:query')")
    public CommonResult<PageResult<MesProMaintenancePlanRespVO>> getPlanPage(@Valid MesProMaintenancePlanPageReqVO pageReqVO) {
        PageResult<MesProMaintenancePlanDO> pageResult = planService.getPlanPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty(pageResult.getTotal()));
        }
        List<MesProMaintenancePlanItemDO> planItemList = planService.getPlanItemListByPlanIds(
                convertSet(pageResult.getList(), MesProMaintenancePlanDO::getId));
        Map<Long, List<MesProMaintenancePlanItemDO>> planItemMap = convertMultiMap(planItemList, MesProMaintenancePlanItemDO::getPlanId);
        return success(BeanUtils.toBean(pageResult, MesProMaintenancePlanRespVO.class,
                plan -> buildPlanRespVOItems(plan, planItemMap.get(plan.getId()))));
    }
 
    @GetMapping("/export-excel")
    @Operation(summary = "导出电力作业计划 Excel")
    @PreAuthorize("@ss.hasPermission('mes:pro-maintenance-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPlanExcel(@Valid MesProMaintenancePlanPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MesProMaintenancePlanRespVO> list = planService.getPlanPage(pageReqVO).getList().stream()
                .map(plan -> buildPlanRespVO(plan, planService.getPlanItemListByPlanId(plan.getId())))
                .collect(java.util.stream.Collectors.toList());
        ExcelUtils.write(response, "电力作业计划.xls", "数据", MesProMaintenancePlanRespVO.class, list);
    }
 
    @PutMapping("/issue")
    @Operation(summary = "下发电力作业计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:pro-maintenance-plan:update')")
    public CommonResult<Boolean> issuePlan(@RequestParam("id") Long id) {
        planService.issuePlan(id);
        return success(true);
    }
 
    @PutMapping("/finish")
    @Operation(summary = "完成电力作业计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:pro-maintenance-plan:update')")
    public CommonResult<Boolean> finishPlan(@RequestParam("id") Long id) {
        planService.finishPlan(id);
        return success(true);
    }
 
    @PostMapping("/generate-purchase-plan")
    @Operation(summary = "根据备件需求生成采购计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:pro-maintenance-plan:generate-purchase-plan')")
    public CommonResult<Long> generatePurchasePlan(@RequestParam("id") Long id) {
        return success(planService.generatePurchasePlan(id));
    }
 
    @PostMapping("/generate-work-order")
    @Operation(summary = "生成运维/发电/检修工单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:pro-maintenance-plan:generate-work-order')")
    public CommonResult<Long> generateWorkOrder(@RequestParam("id") Long id) {
        return success(planService.generateWorkOrder(id));
    }
 
    private MesProMaintenancePlanRespVO buildPlanRespVO(MesProMaintenancePlanDO plan,
                                                        List<MesProMaintenancePlanItemDO> planItems) {
        MesProMaintenancePlanRespVO vo = BeanUtils.toBean(plan, MesProMaintenancePlanRespVO.class);
        buildPlanRespVOItems(vo, planItems);
        return vo;
    }
 
    private void buildPlanRespVOItems(MesProMaintenancePlanRespVO vo, List<MesProMaintenancePlanItemDO> planItems) {
        if (CollUtil.isEmpty(planItems)) {
            vo.setItems(Collections.emptyList());
            return;
        }
        Map<Long, MesMdItemDO> itemMap = itemService.getItemMap(convertSet(planItems, MesProMaintenancePlanItemDO::getItemId));
        Map<Long, MesMdUnitMeasureDO> unitMeasureMap = unitMeasureService.getUnitMeasureMap(
                convertSet(itemMap.values(), MesMdItemDO::getUnitMeasureId));
        vo.setItems(BeanUtils.toBean(planItems, MesProMaintenancePlanRespVO.Item.class, item -> {
            MapUtils.findAndThen(itemMap, item.getItemId(), mesItem -> item.setItemCode(mesItem.getCode())
                    .setItemName(mesItem.getName())
                    .setUnitMeasureName(unitMeasureMap.get(mesItem.getUnitMeasureId()) != null
                            ? unitMeasureMap.get(mesItem.getUnitMeasureId()).getName() : null));
        }));
    }
 
}