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<Long> createWorkOrder(@Valid @RequestBody MesProWorkOrderSaveReqVO createReqVO) {
|
return success(workOrderService.createWorkOrder(createReqVO));
|
}
|
|
@PutMapping("/update")
|
@Operation(summary = "更新生产工单")
|
@PreAuthorize("@ss.hasPermission('mes:pro-work-order:update')")
|
public CommonResult<Boolean> 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<Boolean> 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<MesProWorkOrderRespVO> 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<PageResult<MesProWorkOrderRespVO>> getWorkOrderPage(@Valid MesProWorkOrderPageReqVO pageReqVO) {
|
PageResult<MesProWorkOrderDO> 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<PageResult<MesProWorkOrderRespVO>> getWorkOrderSchedulePage(@Valid MesProWorkOrderPageReqVO pageReqVO) {
|
PageResult<MesProWorkOrderDO> 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<MesProWorkOrderDO> list = workOrderService.getWorkOrderPage(pageReqVO).getList();
|
List<MesProWorkOrderRespVO> 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<Boolean> 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<Boolean> 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<Boolean> 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<MesProWorkOrderProgressRespVO> 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<Boolean> checkAutoFinish(@PathVariable("id") Long id) {
|
return success(workOrderService.checkAndAutoFinishWorkOrder(id));
|
}
|
|
// ==================== 拼接 VO ====================
|
|
private List<MesProWorkOrderRespVO> buildWorkOrderRespVOList(List<MesProWorkOrderDO> list) {
|
if (CollUtil.isEmpty(list)) {
|
return Collections.emptyList();
|
}
|
// 1. 批量获取关联数据
|
Set<Long> workOrderIds = convertSet(list, MesProWorkOrderDO::getId);
|
Map<Long, MesMdItemDO> itemMap = itemService.getItemMap(
|
convertSet(list, MesProWorkOrderDO::getProductId));
|
Map<Long, CrmCustomerRespDTO> customerMap = customerApi.getCustomerMap(
|
convertSet(list, MesProWorkOrderDO::getClientId));
|
Map<Long, SrmSupplierRespDTO> supplierMap = srmSupplierApi.getSupplierList(
|
convertSet(list, MesProWorkOrderDO::getVendorId)).getCheckedData().stream()
|
.collect(java.util.stream.Collectors.toMap(SrmSupplierRespDTO::getId, s -> s));
|
// 单位从产品关联的 item 获得
|
Map<Long, MesMdUnitMeasureDO> unitMeasureMap = unitMeasureService.getUnitMeasureMap(
|
convertSet(itemMap.values(), MesMdItemDO::getUnitMeasureId));
|
Map<Long, MesProWorkOrderDO> parentMap = workOrderService.getWorkOrderMap(
|
convertSet(list, MesProWorkOrderDO::getParentId));
|
|
// 获取所有关联的工单工序和任务
|
List<MesProWorkOrderProcessDO> allProcesses = workOrderProcessService.getWorkOrderProcessListByWorkOrderIds(workOrderIds);
|
List<MesProTaskDO> allTasks = taskService.getTaskListByWorkOrderIds(workOrderIds);
|
|
Set<Long> processIds = convertSet(allProcesses, MesProWorkOrderProcessDO::getProcessId);
|
Map<Long, MesProProcessDO> processMap = processService.getProcessMap(processIds);
|
|
Map<Long, List<MesProWorkOrderProcessDO>> woProcessMap = convertMultiMap(allProcesses, MesProWorkOrderProcessDO::getWorkOrderId);
|
Map<Long, List<MesProTaskDO>> 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<MesProWorkOrderProcessDO> processes = woProcessMap.getOrDefault(vo.getId(), Collections.emptyList());
|
List<MesProTaskDO> tasks = woTaskMap.getOrDefault(vo.getId(), Collections.emptyList());
|
|
List<MesProWorkOrderProcessProgressVO> progressList = new ArrayList<>();
|
boolean allCompleted = !processes.isEmpty();
|
boolean anyStarted = false;
|
|
Map<Long, List<MesProTaskDO>> processTaskMap = convertMultiMap(tasks, MesProTaskDO::getProcessId);
|
|
for (MesProWorkOrderProcessDO process : processes) {
|
List<MesProTaskDO> 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()); // 待生产
|
}
|
});
|
}
|
|
}
|