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 createPlan(@Valid @RequestBody MesProMaintenancePlanSaveReqVO createReqVO) { return success(planService.createPlan(createReqVO)); } @PutMapping("/update") @Operation(summary = "更新电力作业计划") @PreAuthorize("@ss.hasPermission('mes:pro-maintenance-plan:update')") public CommonResult 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 deletePlan(@RequestParam("ids") List 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 getPlan(@RequestParam("id") Long id) { MesProMaintenancePlanDO plan = planService.getPlan(id); if (plan == null) { return success(null); } List planItemList = planService.getPlanItemListByPlanId(id); return success(buildPlanRespVO(plan, planItemList)); } @GetMapping("/page") @Operation(summary = "获得电力作业计划分页") @PreAuthorize("@ss.hasPermission('mes:pro-maintenance-plan:query')") public CommonResult> getPlanPage(@Valid MesProMaintenancePlanPageReqVO pageReqVO) { PageResult pageResult = planService.getPlanPage(pageReqVO); if (CollUtil.isEmpty(pageResult.getList())) { return success(PageResult.empty(pageResult.getTotal())); } List planItemList = planService.getPlanItemListByPlanIds( convertSet(pageResult.getList(), MesProMaintenancePlanDO::getId)); Map> 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 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 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 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 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 generateWorkOrder(@RequestParam("id") Long id) { return success(planService.generateWorkOrder(id)); } private MesProMaintenancePlanRespVO buildPlanRespVO(MesProMaintenancePlanDO plan, List planItems) { MesProMaintenancePlanRespVO vo = BeanUtils.toBean(plan, MesProMaintenancePlanRespVO.class); buildPlanRespVOItems(vo, planItems); return vo; } private void buildPlanRespVOItems(MesProMaintenancePlanRespVO vo, List planItems) { if (CollUtil.isEmpty(planItems)) { vo.setItems(Collections.emptyList()); return; } Map itemMap = itemService.getItemMap(convertSet(planItems, MesProMaintenancePlanItemDO::getItemId)); Map 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)); })); } }