package cn.iocoder.yudao.module.mes.controller.admin.pro.workorder; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.ListUtil; 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.collection.MapUtils; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi; import cn.iocoder.yudao.module.crm.api.customer.dto.CrmCustomerRespDTO; import cn.iocoder.yudao.module.mes.controller.admin.pro.workorder.vo.*; 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.process.MesProProcessDO; import cn.iocoder.yudao.module.mes.dal.dataobject.pro.task.MesProTaskDO; import cn.iocoder.yudao.module.mes.dal.dataobject.pro.workorder.MesProWorkOrderDO; import cn.iocoder.yudao.module.mes.dal.dataobject.pro.workorder.MesProWorkOrderProcessDO; import cn.iocoder.yudao.module.mes.enums.pro.MesProWorkOrderProductionStatusEnum; 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.process.MesProProcessService; import cn.iocoder.yudao.module.mes.service.pro.task.MesProTaskService; import cn.iocoder.yudao.module.mes.service.pro.workorder.MesProWorkOrderProcessService; import cn.iocoder.yudao.module.mes.service.pro.workorder.MesProWorkOrderService; import cn.iocoder.yudao.module.srm.api.supplier.SrmSupplierApi; import cn.iocoder.yudao.module.srm.api.supplier.dto.SrmSupplierRespDTO; 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.math.BigDecimal; import java.util.*; 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/work-order") @Validated public class MesProWorkOrderController { @Resource private MesProWorkOrderService workOrderService; @Resource private MesMdItemService itemService; @Resource private CrmCustomerApi customerApi; @Resource private SrmSupplierApi srmSupplierApi; @Resource private MesMdUnitMeasureService unitMeasureService; @Resource private MesProWorkOrderProcessService workOrderProcessService; @Resource private MesProTaskService taskService; @Resource private MesProProcessService processService; @PostMapping("/create") @Operation(summary = "创建生产工单") @PreAuthorize("@ss.hasPermission('mes:pro-work-order:create')") public CommonResult createWorkOrder(@Valid @RequestBody MesProWorkOrderSaveReqVO createReqVO) { return success(workOrderService.createWorkOrder(createReqVO)); } @PutMapping("/update") @Operation(summary = "更新生产工单") @PreAuthorize("@ss.hasPermission('mes:pro-work-order:update')") public CommonResult updateWorkOrder(@Valid @RequestBody MesProWorkOrderSaveReqVO updateReqVO) { workOrderService.updateWorkOrder(updateReqVO); return success(true); } @DeleteMapping("/delete") @Operation(summary = "删除生产工单") @Parameter(name = "id", description = "编号", required = true) @PreAuthorize("@ss.hasPermission('mes:pro-work-order:delete')") public CommonResult deleteWorkOrder(@RequestParam("id") Long id) { workOrderService.deleteWorkOrder(id); return success(true); } @GetMapping("/get") @Operation(summary = "获得生产工单") @Parameter(name = "id", description = "编号", required = true, example = "1024") @PreAuthorize("@ss.hasPermission('mes:pro-work-order:query')") public CommonResult getWorkOrder(@RequestParam("id") Long id) { MesProWorkOrderDO workOrder = workOrderService.getWorkOrder(id); if (workOrder == null) { return success(null); } return success(buildWorkOrderRespVOList(ListUtil.of(workOrder)).get(0)); } @GetMapping("/page") @Operation(summary = "获得生产工单分页") @PreAuthorize("@ss.hasPermission('mes:pro-work-order:query')") public CommonResult> getWorkOrderPage(@Valid MesProWorkOrderPageReqVO pageReqVO) { PageResult pageResult = workOrderService.getWorkOrderPage(pageReqVO); return success(new PageResult<>(buildWorkOrderRespVOList(pageResult.getList()), pageResult.getTotal())); } @GetMapping("/schedule-page") @Operation(summary = "获得生产工单调度状态分页") @PreAuthorize("@ss.hasPermission('mes:pro-work-order:query')") public CommonResult> getWorkOrderSchedulePage(@Valid MesProWorkOrderPageReqVO pageReqVO) { PageResult pageResult = workOrderService.getWorkOrderSchedulePage(pageReqVO); return success(new PageResult<>(buildWorkOrderRespVOList(pageResult.getList()), pageResult.getTotal())); } @GetMapping("/export-excel") @Operation(summary = "导出生产工单 Excel") @PreAuthorize("@ss.hasPermission('mes:pro-work-order:export')") @ApiAccessLog(operateType = EXPORT) public void exportWorkOrderExcel(@Valid MesProWorkOrderPageReqVO pageReqVO, HttpServletResponse response) throws IOException { pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE); List list = workOrderService.getWorkOrderPage(pageReqVO).getList(); List voList = buildWorkOrderRespVOList(list); ExcelUtils.write(response, "生产工单.xls", "数据", MesProWorkOrderRespVO.class, voList); } @PutMapping("/confirm") @Operation(summary = "确认工单") @Parameter(name = "id", description = "编号", required = true) @PreAuthorize("@ss.hasPermission('mes:pro-work-order:update')") public CommonResult confirmWorkOrder(@RequestParam("id") Long id) { workOrderService.confirmWorkOrder(id); return success(true); } @PutMapping("/finish") @Operation(summary = "完成工单") @Parameter(name = "id", description = "编号", required = true) @PreAuthorize("@ss.hasPermission('mes:pro-work-order:update')") public CommonResult finishWorkOrder(@RequestParam("id") Long id) { workOrderService.finishWorkOrder(id); return success(true); } @PutMapping("/cancel") @Operation(summary = "取消工单") @Parameter(name = "id", description = "编号", required = true) @PreAuthorize("@ss.hasPermission('mes:pro-work-order:update')") public CommonResult cancelWorkOrder(@RequestParam("id") Long id) { workOrderService.cancelWorkOrder(id); return success(true); } @GetMapping("/progress/{id}") @Operation(summary = "获取工单进度详情") @Parameter(name = "id", description = "编号", required = true) @PreAuthorize("@ss.hasPermission('mes:pro-work-order:query')") public CommonResult getWorkOrderProgress(@PathVariable("id") Long id) { return success(workOrderService.getWorkOrderProgress(id)); } @PostMapping("/check-auto-finish/{id}") @Operation(summary = "检查并自动完成工单") @Parameter(name = "id", description = "编号", required = true) @PreAuthorize("@ss.hasPermission('mes:pro-work-order:update')") public CommonResult checkAutoFinish(@PathVariable("id") Long id) { return success(workOrderService.checkAndAutoFinishWorkOrder(id)); } // ==================== 拼接 VO ==================== private List buildWorkOrderRespVOList(List list) { if (CollUtil.isEmpty(list)) { return Collections.emptyList(); } // 1. 批量获取关联数据 Set workOrderIds = convertSet(list, MesProWorkOrderDO::getId); Map itemMap = itemService.getItemMap( convertSet(list, MesProWorkOrderDO::getProductId)); Map customerMap = customerApi.getCustomerMap( convertSet(list, MesProWorkOrderDO::getClientId)); Map supplierMap = srmSupplierApi.getSupplierList( convertSet(list, MesProWorkOrderDO::getVendorId)).getCheckedData().stream() .collect(java.util.stream.Collectors.toMap(SrmSupplierRespDTO::getId, s -> s)); // 单位从产品关联的 item 获得 Map unitMeasureMap = unitMeasureService.getUnitMeasureMap( convertSet(itemMap.values(), MesMdItemDO::getUnitMeasureId)); Map parentMap = workOrderService.getWorkOrderMap( convertSet(list, MesProWorkOrderDO::getParentId)); // 获取所有关联的工单工序和任务 List allProcesses = workOrderProcessService.getWorkOrderProcessListByWorkOrderIds(workOrderIds); List allTasks = taskService.getTaskListByWorkOrderIds(workOrderIds); Set processIds = convertSet(allProcesses, MesProWorkOrderProcessDO::getProcessId); Map processMap = processService.getProcessMap(processIds); Map> woProcessMap = convertMultiMap(allProcesses, MesProWorkOrderProcessDO::getWorkOrderId); Map> woTaskMap = convertMultiMap(allTasks, MesProTaskDO::getWorkOrderId); // 2. 拼接 VO(单位名称从产品关联的 item 获得) return BeanUtils.toBean(list, MesProWorkOrderRespVO.class, vo -> { MapUtils.findAndThen(itemMap, vo.getProductId(), item -> { vo.setProductName(item.getName()).setProductCode(item.getCode()) .setProductSpecification(item.getSpecification()); MapUtils.findAndThen(unitMeasureMap, item.getUnitMeasureId(), unitMeasure -> vo.setUnitMeasureName(unitMeasure.getName())); }); MapUtils.findAndThen(customerMap, vo.getClientId(), customer -> vo.setClientName(customer.getName())); MapUtils.findAndThen(supplierMap, vo.getVendorId(), supplier -> vo.setVendorName(supplier.getName())); MapUtils.findAndThen(parentMap, vo.getParentId(), parent -> vo.setParentCode(parent.getCode())); // 计算完工进度 (quantityProduced / quantity * 100) BigDecimal produced = vo.getQuantityProduced() != null ? vo.getQuantityProduced() : BigDecimal.ZERO; if (vo.getQuantity() != null && vo.getQuantity().compareTo(BigDecimal.ZERO) > 0) { vo.setCompletionProgress(produced.multiply(new BigDecimal("100")).divide(vo.getQuantity(), 2, java.math.RoundingMode.HALF_UP)); } else { vo.setCompletionProgress(produced.compareTo(BigDecimal.ZERO) > 0 ? new BigDecimal("100.00") : BigDecimal.ZERO); } // 计算进度和状态 List processes = woProcessMap.getOrDefault(vo.getId(), Collections.emptyList()); List tasks = woTaskMap.getOrDefault(vo.getId(), Collections.emptyList()); List progressList = new ArrayList<>(); boolean allCompleted = !processes.isEmpty(); boolean anyStarted = false; Map> processTaskMap = convertMultiMap(tasks, MesProTaskDO::getProcessId); for (MesProWorkOrderProcessDO process : processes) { List pTasks = processTaskMap.getOrDefault(process.getProcessId(), Collections.emptyList()); BigDecimal totalQty = pTasks.stream().map(t -> t.getQuantity() != null ? t.getQuantity() : BigDecimal.ZERO).reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal totalProduced = pTasks.stream().map(t -> t.getProducedQuantity() != null ? t.getProducedQuantity() : BigDecimal.ZERO).reduce(BigDecimal.ZERO, BigDecimal::add); MesProWorkOrderProcessProgressVO progressVO = new MesProWorkOrderProcessProgressVO(); MesProProcessDO processDO = processMap.get(process.getProcessId()); progressVO.setProcessName(processDO != null ? processDO.getName() : ""); if (totalQty.compareTo(BigDecimal.ZERO) > 0) { progressVO.setProgress(totalProduced.multiply(new BigDecimal("100")).divide(totalQty, 2, java.math.RoundingMode.HALF_UP)); } else { progressVO.setProgress(totalProduced.compareTo(BigDecimal.ZERO) > 0 ? new BigDecimal("100.00") : BigDecimal.ZERO); } progressList.add(progressVO); if (totalProduced.compareTo(BigDecimal.ZERO) > 0) { anyStarted = true; } if (totalQty.compareTo(BigDecimal.ZERO) == 0 || totalProduced.compareTo(totalQty) < 0) { allCompleted = false; } } vo.setProcessProgressList(progressList); if (processes.isEmpty()) { vo.setProductionStatus(MesProWorkOrderProductionStatusEnum.WAITING.getStatus()); // 待生产 } else if (allCompleted) { vo.setProductionStatus(MesProWorkOrderProductionStatusEnum.FINISHED.getStatus()); // 生产完成 } else if (anyStarted) { vo.setProductionStatus(MesProWorkOrderProductionStatusEnum.PRODUCING.getStatus()); // 生产中 } else { vo.setProductionStatus(MesProWorkOrderProductionStatusEnum.WAITING.getStatus()); // 待生产 } }); } }