| | |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pro.batch.MesProBatchDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pro.batch.MesProBatchWorkerDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pro.pallet.MesProPalletDO; |
| | | import cn.iocoder.yudao.module.mes.framework.qrcode.MesProQrLabelBuilder; |
| | | import cn.iocoder.yudao.module.mes.framework.qrcode.QrCodeImageUtils; |
| | | import cn.iocoder.yudao.module.mes.framework.qrcode.QrLabelInfo; |
| | | import cn.iocoder.yudao.module.mes.service.mdm.MesMdmItemService; |
| | | import cn.iocoder.yudao.module.mes.service.pro.bagcode.MesProBagCodeService; |
| | | import cn.iocoder.yudao.module.mes.service.pro.batch.MesProBatchService; |
| | | import cn.iocoder.yudao.module.mes.service.pro.pallet.MesProPalletService; |
| | | import cn.iocoder.yudao.module.mes.service.pro.qrexport.MesProQrLabelAssembler; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| | | import io.swagger.v3.oas.annotations.Operation; |
| | |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; |
| | | import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PRO_BATCH_NOT_EXISTS; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PRO_QR_EXPORT_TOO_MANY; |
| | | |
| | | @Tag(name = "管理后台 - MES 生产批次") |
| | | @RestController |
| | | @RequestMapping("/mes/pro/batch") |
| | | @Validated |
| | | public class MesProBatchController { |
| | | |
| | | /** |
| | | * 批次追溯响应内嵌的袋码列表上限 |
| | | * <p> |
| | | * 这是响应体积的保护,与二维码导出无关:袋码总量由 bagTotal 返回,列表本身只作展示用途。 |
| | | * 导出路径不再有数量上限。 |
| | | */ |
| | | private static final int MAX_TRACE_BAG_LIST_SIZE = 2000; |
| | | |
| | | @Resource |
| | | private MesProBatchService batchService; |
| | |
| | | private MesProPalletService palletService; |
| | | @Resource |
| | | private MesProBagCodeService bagCodeService; |
| | | @Resource |
| | | private MesProQrLabelAssembler labelAssembler; |
| | | |
| | | @PostMapping("/create") |
| | | @Operation(summary = "创建生产批次(一次录入业务信息,自动生成批次号)") |
| | |
| | | @Parameter(name = "code", description = "批次号(二维码内容)", required = true, example = "PC20260818L01001") |
| | | @PreAuthorize("@ss.hasPermission('mes:pro-batch:query')") |
| | | public CommonResult<MesProBatchTraceRespVO> getBatchByCode(@RequestParam("code") String code) { |
| | | MesProBatchDO batch = batchService.getBatchByCode(code); |
| | | MesProBatchDO batch = batchService.getBatchByScanCode(code); |
| | | if (batch == null) { |
| | | throw exception(PRO_BATCH_NOT_EXISTS); |
| | | } |
| | |
| | | throw exception(PRO_BATCH_NOT_EXISTS); |
| | | } |
| | | List<MesProPalletDO> pallets = palletService.getPalletListByBatchId(batchId); |
| | | if (pallets.size() + 1 > QrCodeImageUtils.MAX_QR_EXPORT) { |
| | | throw exception(PRO_QR_EXPORT_TOO_MANY); |
| | | } |
| | | String qrCode = StrUtil.blankToDefault(batch.getCode(), batch.getTempCode()); |
| | | QrCodeImageUtils.writeZip(response, "批次二维码-" + qrCode, buildBatchQrLabels(batch, pallets)); |
| | | QrCodeImageUtils.writeZip(response, "批次二维码-" + qrCode, labelAssembler.buildBatchLabels(batch, pallets)); |
| | | } |
| | | |
| | | // ==================== 拼接 VO ==================== |
| | | |
| | | @PostMapping("/generate-business-code") |
| | | @Operation(summary = "补生成已有生产批次业务码,已有码值不变") |
| | | @PreAuthorize("@ss.hasPermission('mes:pro-batch:create')") |
| | | public CommonResult<Integer> generateBusinessCodes(@Valid @RequestBody |
| | | cn.iocoder.yudao.module.mes.controller.admin.pro.vo.MesProBusinessCodeGenerateReqVO reqVO) { |
| | | return success(batchService.generateBusinessCodes(reqVO.getIds())); |
| | | } |
| | | |
| | | @GetMapping("/export-business-qr") |
| | | @Operation(summary = "导出批次及所属托盘业务二维码,内容为纯业务码值") |
| | | @PreAuthorize("@ss.hasPermission('mes:pro-batch:export')") |
| | | @ApiAccessLog(operateType = EXPORT) |
| | | public void exportBusinessQr(@RequestParam("batchId") Long batchId, |
| | | HttpServletResponse response) throws IOException { |
| | | MesProBatchDO batch = batchService.getBatch(batchId); |
| | | if (batch == null) throw exception(PRO_BATCH_NOT_EXISTS); |
| | | if (cn.iocoder.yudao.module.mes.enums.pro.MesProBatchStatusEnum.CANCELED.getStatus().equals(batch.getStatus())) |
| | | throw new cn.iocoder.yudao.framework.common.exception.ServiceException(400, "已作废批次不可导出业务二维码"); |
| | | List<MesProPalletDO> pallets = palletService.getPalletListByBatchId(batchId).stream() |
| | | .filter(record -> !cn.iocoder.yudao.module.mes.enums.pro.MesProPalletStatusEnum.CANCELED.getStatus().equals(record.getStatus())).toList(); |
| | | QrCodeImageUtils.writeZip(response, "批次业务二维码-" + batch.getBusinessCode(), |
| | | labelAssembler.buildBatchBusinessLabels(batch, pallets)); |
| | | } |
| | | |
| | | private List<MesProBatchRespVO> buildBatchRespVOList(List<MesProBatchDO> list) { |
| | | if (CollUtil.isEmpty(list)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | // 1. 获得关联数据(责任人、品种物料) |
| | | // 1. 获得关联数据(责任人);品种已下沉到袋码数据包,批次列表不再展示 |
| | | Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap( |
| | | convertSet(list, MesProBatchDO::getOwnerUserId)); |
| | | Map<Long, MesMdmItemDO> itemMap = mdmItemService.getItemMap( |
| | | convertSet(list, MesProBatchDO::getItemId)); |
| | | // 2. 构建结果 |
| | | Map<Long, MesMdmItemDO> itemMap = mdmItemService.getItemMap(convertSet(list, MesProBatchDO::getItemId)); |
| | | return BeanUtils.toBean(list, MesProBatchRespVO.class, vo -> { |
| | | MapUtils.findAndThen(userMap, vo.getOwnerUserId(), user -> |
| | | vo.setOwnerUserName(user.getNickname())); |
| | | MapUtils.findAndThen(itemMap, vo.getItemId(), item -> { |
| | | vo.setItemCode(item.getCode()); |
| | | vo.setItemName(item.getName()); |
| | | vo.setItemSpecification(item.getSpecification()); |
| | | }); |
| | | MapUtils.findAndThen(userMap, vo.getOwnerUserId(), user -> vo.setOwnerUserName(user.getNickname())); |
| | | MapUtils.findAndThen(itemMap, vo.getItemId(), item -> { vo.setItemCode(item.getCode()); vo.setItemName(item.getName()); }); |
| | | }); |
| | | } |
| | | |
| | |
| | | respVO.setWorkers(buildWorkerRespVOList(batchService.getBatchWorkerListByBatchId(batch.getId()))); |
| | | // 托盘列表 |
| | | respVO.setPallets(BeanUtils.toBean(palletService.getPalletListByBatchId(batch.getId()), MesProPalletRespVO.class)); |
| | | // 袋码列表(封顶 2000,bagTotal 记录总数) |
| | | // 袋码列表(封顶 MAX_TRACE_BAG_LIST_SIZE,bagTotal 记录总数) |
| | | List<MesProBagCodeDO> bags = bagCodeService.getBagCodeListByBatchId(batch.getId()); |
| | | respVO.setPendingBagQuantity((int) bags.stream().filter(b -> !Integer.valueOf(30).equals(b.getStatus()) && b.getInboundEventId() == null).count()); |
| | | respVO.setBagTotal(bags.size()); |
| | | if (bags.size() > QrCodeImageUtils.MAX_QR_EXPORT) { |
| | | bags = bags.subList(0, QrCodeImageUtils.MAX_QR_EXPORT); |
| | | if (bags.size() > MAX_TRACE_BAG_LIST_SIZE) { |
| | | bags = bags.subList(0, MAX_TRACE_BAG_LIST_SIZE); |
| | | } |
| | | respVO.setBags(buildBagCodeRespVOList(bags)); |
| | | return respVO; |
| | |
| | | return BeanUtils.toBean(list, MesProBagCodeRespVO.class, vo -> |
| | | MapUtils.findAndThen(batchMap, vo.getBatchId(), batch -> |
| | | vo.setLineCode(batch.getLineCode()).setLineName(batch.getLineName()))); |
| | | } |
| | | |
| | | private List<QrLabelInfo> buildBatchQrLabels(MesProBatchDO batch, List<MesProPalletDO> pallets) { |
| | | // 获得关联数据(品种、责任人)——批次与托盘同属一批,共用同一份 |
| | | Map<Long, MesMdmItemDO> itemMap = mdmItemService.getItemMap( |
| | | convertSet(Collections.singletonList(batch), MesProBatchDO::getItemId)); |
| | | Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap( |
| | | convertSet(Collections.singletonList(batch), MesProBatchDO::getOwnerUserId)); |
| | | // 构建标签:批次图 + 全部托盘图 |
| | | List<QrLabelInfo> labels = new ArrayList<>(pallets.size() + 1); |
| | | labels.add(MesProQrLabelBuilder.buildBatchLabel(batch, itemMap, userMap)); |
| | | for (MesProPalletDO pallet : pallets) { |
| | | labels.add(MesProQrLabelBuilder.buildPalletLabel(pallet, batch, itemMap, userMap)); |
| | | } |
| | | return labels; |
| | | } |
| | | |
| | | } |