diff --git a/ktg-framework/src/main/java/com/ktg/framework/config/SecurityConfig.java b/ktg-framework/src/main/java/com/ktg/framework/config/SecurityConfig.java index 7d19bb5..5d31c2e 100644 --- a/ktg-framework/src/main/java/com/ktg/framework/config/SecurityConfig.java +++ b/ktg-framework/src/main/java/com/ktg/framework/config/SecurityConfig.java @@ -116,6 +116,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter .antMatchers("/druid/**").anonymous() .antMatchers("/websocket/**").anonymous() .antMatchers("/system/autocode/get/**").permitAll() + .antMatchers("/mobile/print/printerconfig/*").permitAll() // 除上面外的所有请求全部需要鉴权认证 .anyRequest().authenticated() .and() diff --git a/ktg-print/src/main/java/com/ktg/print/controller/mobile/PrintMobController.java b/ktg-print/src/main/java/com/ktg/print/controller/mobile/PrintMobController.java new file mode 100644 index 0000000..f49a341 --- /dev/null +++ b/ktg-print/src/main/java/com/ktg/print/controller/mobile/PrintMobController.java @@ -0,0 +1,401 @@ +package com.ktg.print.controller.mobile; + +import cn.hutool.core.collection.CollectionUtil; +import com.ktg.common.constant.UserConstants; +import com.ktg.common.core.controller.BaseController; +import com.ktg.common.core.domain.AjaxResult; +import com.ktg.common.utils.StringUtils; +import com.ktg.mes.dv.domain.DvMachinery; +import com.ktg.mes.dv.service.IDvMachineryService; +import com.ktg.mes.md.domain.MdItem; +import com.ktg.mes.md.service.IMdItemService; +import com.ktg.mes.pro.domain.ProCard; +import com.ktg.mes.pro.domain.ProRouteProcess; +import com.ktg.mes.pro.domain.ProRouteProduct; +import com.ktg.mes.pro.mapper.ProRouteMapper; +import com.ktg.mes.pro.mapper.ProRouteProductMapper; +import com.ktg.mes.pro.service.IProCardService; +import com.ktg.mes.pro.service.IProRouteProcessService; +import com.ktg.mes.wm.domain.WmBarcode; +import com.ktg.mes.wm.service.IWmBarcodeService; +import com.ktg.print.domain.PrintBarcodeModel; +import com.ktg.print.domain.PrintPrinterConfig; +import com.ktg.print.protocol.PrintMessageProto; +import com.ktg.print.server.PrintClientInfoMessageHandler; +import com.ktg.print.server.PrintServerDefaultHandler; +import com.ktg.print.service.IPrintPrinterConfigService; +import io.netty.channel.Channel; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.CollectionUtils; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.net.SocketAddress; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +/** + * @author yinjinlu + * @description + * @date 2024/11/25 + */ +@Api("标签打印机查询接口") +@RestController +@RequestMapping("/mobile/print/barcodePrint") +public class PrintMobController extends BaseController { + + @Autowired + private IPrintPrinterConfigService printPrinterConfigService; + @Autowired + private IMdItemService iMdItemService; + @Autowired + private IWmBarcodeService wmBarcodeService; + @Autowired + private IDvMachineryService iDvMachineryService; + @Autowired + private IProCardService iProCardService; + @Autowired + private ProRouteProductMapper proRouteProductMapper; + @Autowired + private ProRouteMapper proRouteMapper; + @Autowired + private IProRouteProcessService iProRouteProcessService; + + /** + * 条码打印公共接口 + * + * @param printBarcodeModel + * @return + */ + @ApiOperation("移动端标签打印接口") + @PostMapping("/printing") + public AjaxResult printBarcodeLabel(@RequestBody PrintBarcodeModel printBarcodeModel) { + String printerCode = printBarcodeModel.getPrinterCode(); + String businessType = printBarcodeModel.getBusinessType(); + Map params = printBarcodeModel.getParams(); + Long businessId = printBarcodeModel.getBusinessId(); + String businessCode = printBarcodeModel.getBusinessCode(); + PrintPrinterConfig printPrinterConfig = printPrinterConfigService.selectPrintPrinterConfigByPrinterCode(printerCode); + if (printPrinterConfig == null) { + return AjaxResult.error("打印机:" + printerCode + "不存在"); + } + String ip = printPrinterConfig.getPrinterIp(); + String printPort = printPrinterConfig.getPrinterPort().toString(); + String printName = printPrinterConfig.getPrinterName(); + PrintMessageProto.Printer.DataType dataType = PrintMessageProto.Printer.DataType.IQC_PrintMessage; + //根据打印机编码id获取打印机你信息 + String clientIp = printPrinterConfig.getClientIp(); + if (StringUtils.isEmpty(clientIp)) { + return AjaxResult.error("请检查打印机客户端信息配置!"); + } + PrintMessageProto.Printer msg = null; + PrintMessageProto.Printer.PrintInfo printInfo = PrintMessageProto.Printer.PrintInfo.newBuilder().setIp(ip).setCode(printerCode).setName(printName).setPort(printPort).build(); + + switch (businessType) { + case UserConstants.BARCODE_TYPE_ITEM: + //物料标签打印 + //封装模板数据 + MdItem item = null; + if (Optional.ofNullable(businessId).isPresent()) { + item = iMdItemService.selectMdItemById(businessId); + if (item == null) { + return AjaxResult.error("条码内容数据为空!" + "(" + businessId + ")"); + } + } else if (StringUtils.isNotEmpty(businessCode)) { + MdItem itemParam = new MdItem(); + itemParam.setItemCode(businessCode); + List itemList = iMdItemService.selectMdItemList(itemParam); + if (CollectionUtils.isEmpty(itemList)) { + return AjaxResult.error("条码内容数据为空!" + "(" + businessCode + ")"); + } + item = itemList.get(0); + } else { + return AjaxResult.error("缺少业务参数!"); + } + //二维码信息查询 + WmBarcode bacode = new WmBarcode(); + bacode.setBussinessId(item.getItemId()); + bacode.setBussinessCode(item.getItemCode()); + bacode.setBarcodeType(UserConstants.BARCODE_TYPE_ITEM); + List wmBarcodes = wmBarcodeService.selectWmBarcodeList(bacode); + if (CollectionUtils.isEmpty(wmBarcodes)) { + return AjaxResult.error("未查询到二维码信息!" + "(" + businessCode + ")"); + } + WmBarcode wmBarcode = wmBarcodes.get(0); + String materialCode = item.getItemCode();//物料编码 + String materialName = item.getItemName();//物料名称 + String specificationAndModel = item.getSpecification();//物料规格类型 + + String param = wmBarcode.getBarcodeUrl();//物料二维码 + dataType = PrintMessageProto.Printer.DataType.Material_Products; + PrintMessageProto.Printer.MaterialProducts materialProducts = PrintMessageProto.Printer.MaterialProducts.newBuilder().setMaterialCode(materialCode).setMaterialName(materialName).setSpecificationAndModel(specificationAndModel).setParam(param).build(); + // 构造对应的消息对象 + msg = PrintMessageProto.Printer.newBuilder().setMaterialProducts(materialProducts).setDataType(dataType).setPrintInfo(printInfo).build(); + break; + case UserConstants.BARCODE_TYPE_PACKAGE: +// String materialCode = params.get("materialCode"); +// String materialName = params.get("materialName"); +// String specificationAndModel = params.get("specificationAndModel"); +// String param = params.get("param"); +// PrintMessageProto.Printer.DataType dataType = PrintMessageProto.Printer.DataType.Material_Products; +// PrintMessageProto.Printer.MaterialProducts materialProducts = PrintMessageProto.Printer.MaterialProducts.newBuilder().setMaterialCode(materialCode).setMaterialName(materialName).setSpecificationAndModel(specificationAndModel).setParam(param).build(); +// // 构造对应的消息对象 +// msg = PrintMessageProto.Printer.newBuilder().setMaterialProducts(materialProducts).setDataType(dataType).setPrintInfo(printInfo).build(); +// break; + case UserConstants.BARCODE_TYPE_STOCK: + //仓库标签打印 + //封装模板数据 +// String warehouseCode = params.get("warehouseCode"); +// String warehouseName = params.get("warehouseName"); +// String personInCharge = params.get("personInCharge"); +// dataType = PrintMessageProto.Printer.DataType.Warehouse_; +// PrintMessageProto.Printer.Warehouse warehouse = PrintMessageProto.Printer.Warehouse.newBuilder().setWarehouseCode(warehouseCode).setWarehouseName(warehouseName).setPersonInCharge(personInCharge).build(); +// // 构造对应的消息对象 +// msg = PrintMessageProto.Printer.newBuilder().setWarehouse(warehouse).setDataType(dataType).setPrintInfo(printInfo).build(); +// break; + case UserConstants.BARCODE_TYPE_MACHINERY: + //设备标签打印 + //封装模板数据 + DvMachinery dvMachinery = null; + if (Optional.ofNullable(businessId).isPresent()) { + dvMachinery = iDvMachineryService.selectDvMachineryByMachineryId(businessId); + if (dvMachinery == null) { + return AjaxResult.error("条码内容数据为空!" + "(" + businessId + ")"); + } + } else if (StringUtils.isNotEmpty(businessCode)) { + DvMachinery mParam = new DvMachinery(); + mParam.setMachineryCode(businessCode); + List mList = iDvMachineryService.selectDvMachineryList(mParam); + if (CollectionUtils.isEmpty(mList)) { + return AjaxResult.error("条码内容数据为空!" + "(" + businessCode + ")"); + } + dvMachinery = mList.get(0); + } else { + return AjaxResult.error("缺少业务参数!"); + } + //二维码信息查询 + WmBarcode mcode = new WmBarcode(); + mcode.setBussinessId(dvMachinery.getMachineryId()); + mcode.setBussinessCode(dvMachinery.getMachineryCode()); + mcode.setBarcodeType(UserConstants.BARCODE_TYPE_MACHINERY); + List mBarcodes = wmBarcodeService.selectWmBarcodeList(mcode); + if (CollectionUtils.isEmpty(mBarcodes)) { + return AjaxResult.error("未查询到二维码信息!" + "(" + businessCode + ")"); + } + dataType = PrintMessageProto.Printer.DataType.Equipment_; + PrintMessageProto.Printer.Equipment equipment = PrintMessageProto.Printer.Equipment.newBuilder(). + setEquipmentCode(dvMachinery.getMachineryCode()). + setEquipmentName(dvMachinery.getMachineryName()). + setSpecificationAndModel(dvMachinery.getMachinerySpec()). + setParam(mBarcodes.get(0).getBarcodeUrl()).build(); + // 构造对应的消息对象 + msg = PrintMessageProto.Printer.newBuilder().setEquipment(equipment).setDataType(dataType).setPrintInfo(printInfo).build(); + break; + case UserConstants.BARCODE_TYPE_WORKSTATION: + //工作站标签打印 + //封装模板数据 + String workstationCode = params.get("workstationCode"); + String workstationName = params.get("workstationName"); + String belongingProcess = params.get("belongingProcess"); + String param2 = params.get("param"); + dataType = PrintMessageProto.Printer.DataType.Workstation_; + PrintMessageProto.Printer.Workstation workstation = PrintMessageProto.Printer.Workstation.newBuilder().setWorkstationCode(workstationCode).setWorkstationName(workstationName).setBelongingProcess(belongingProcess).setParam(param2).build(); + // 构造对应的消息对象 + msg = PrintMessageProto.Printer.newBuilder().setWorkstation(workstation).setDataType(dataType).setPrintInfo(printInfo).build(); + break; + case UserConstants.BARCODE_TYPE_PROCARD: + //流转卡标签打印 + //封装模板数据 + ProCard proCard = null; + if (Optional.ofNullable(businessId).isPresent()) { + proCard = iProCardService.selectProCardByCardId(businessId); + if (proCard == null) { + return AjaxResult.error("条码内容数据为空!" + "(" + businessId + ")"); + } + } else if (StringUtils.isNotEmpty(businessCode)) { + ProCard pcParam = new ProCard(); + pcParam.setCardCode(businessCode); + List mList = iProCardService.selectProCardList(pcParam); + if (CollectionUtils.isEmpty(mList)) { + return AjaxResult.error("条码内容数据为空!" + "(" + businessCode + ")"); + } + proCard = mList.get(0); + } else { + return AjaxResult.error("缺少业务参数!"); + } + //二维码信息查询 + WmBarcode pcBarcode = new WmBarcode(); + pcBarcode.setBussinessId(proCard.getCardId()); + pcBarcode.setBussinessCode(proCard.getCardCode()); + pcBarcode.setBarcodeType(UserConstants.BARCODE_TYPE_PROCARD); + List pcBarcodes = wmBarcodeService.selectWmBarcodeList(pcBarcode); + if (CollectionUtils.isEmpty(pcBarcodes)) { + return AjaxResult.error("未查询到二维码信息!" + "(" + businessCode + ")"); + } + Long routeId = -1L, processId = -1L; + ProRouteProduct proRouteProduct = new ProRouteProduct(); + proRouteProduct.setItemId(proCard.getItemId()); + List products = proRouteProductMapper.selectProRouteProductList(proRouteProduct); + if (CollectionUtil.isNotEmpty(products)) { + products = products.stream().filter(i -> proRouteMapper.selectProRouteByRouteId(i.getRouteId()).getEnableFlag().equals(UserConstants.YES)).collect(Collectors.toList()); + if (CollectionUtil.isNotEmpty(products)) { + routeId = products.get(0).getRouteId(); + } + } + ProRouteProcess proRouteProcess = new ProRouteProcess(); + proRouteProcess.setRouteId(routeId); + String processingProcedure = ""; + List proList = iProRouteProcessService.selectProRouteProcessList(proRouteProcess); + for (ProRouteProcess process : proList) { + processingProcedure = processingProcedure + "," + process.getProcessName(); + } + dataType = PrintMessageProto.Printer.DataType.Printing_OfCirculation; + PrintMessageProto.Printer.PrintingOfCirculation printingOfCirculation = PrintMessageProto.Printer.PrintingOfCirculation.newBuilder(). + setWorkOrderNumber(proCard.getWorkorderCode()). + setMaterialCode(proCard.getItemCode()). + setMaterialName(proCard.getItemName()). + setSpecificationAndModel(proCard.getSpecification() == null ? "" : proCard.getSpecification()). + setProcessingProcedure(processingProcedure). + setParam(pcBarcodes.get(0).getBarcodeUrl()).build(); + // 构造对应的消息对象 + msg = PrintMessageProto.Printer.newBuilder().setPrintingOfCirculation(printingOfCirculation).setDataType(dataType).setPrintInfo(printInfo).build(); + break; + case UserConstants.BARCODE_TYPE_WAREHOUSE: + // 仓库标签打印 + //封装模板数据 + String warehouseCode = params.get("warehouseCode"); + String warehouseName = params.get("warehouseName"); + String personInCharge = params.get("personInCharge"); + String param3 = params.get("param"); + dataType = PrintMessageProto.Printer.DataType.Warehouse_; + PrintMessageProto.Printer.Warehouse warehouse = PrintMessageProto.Printer.Warehouse.newBuilder().setWarehouseCode(warehouseCode).setWarehouseName(warehouseName).setPersonInCharge(personInCharge).setParam(param3).build(); + // 构造对应的消息对象 + msg = PrintMessageProto.Printer.newBuilder().setWarehouse(warehouse).setDataType(dataType).setPrintInfo(printInfo).build(); + break; + case UserConstants.BARCODE_TYPE_STORAGELOCATION: + // 库区标签打印 + //封装模板数据 + String warehouseLocationCode = params.get("warehouseLocationCode"); + String warehouseLocationName = params.get("warehouseLocationName"); + String position = params.get("position"); + String param4 = params.get("param"); + dataType = PrintMessageProto.Printer.DataType.Warehouse_Location; + PrintMessageProto.Printer.WarehouseLocation location = PrintMessageProto.Printer.WarehouseLocation.newBuilder().setWarehouseLocationCode(warehouseLocationCode).setWarehouseLocationName(warehouseLocationName).setPosition(position).setParam(param4).build(); + // 构造对应的消息对象 + msg = PrintMessageProto.Printer.newBuilder().setWarehouseLocation(location).setDataType(dataType).setPrintInfo(printInfo).build(); + break; + case UserConstants.BARCODE_TYPE_STORAGEAREA: + // 库位标签打印 + //封装模板数据 + String warehouseAreaCode = params.get("warehouseAreaCode"); + String warehouseAreaName = params.get("warehouseAreaName"); + dataType = PrintMessageProto.Printer.DataType.Warehouse_Area; + String param5 = params.get("param"); + PrintMessageProto.Printer.WarehouseArea warehouseArea = PrintMessageProto.Printer.WarehouseArea.newBuilder().setWarehouseAreaCode(warehouseAreaCode).setWarehouseAreaName(warehouseAreaName).setParam(param5).build(); + // 构造对应的消息对象 + msg = PrintMessageProto.Printer.newBuilder().setWarehouseArea(warehouseArea).setDataType(dataType).setPrintInfo(printInfo).build(); + break; + case UserConstants.BARCODE_TYPE_TRANSORDER: + // 流转单标签打印 + //封装模板数据 + //String sampleCode =params.get("sampleCode"); + //String qcObject =params.get("qcObject"); + //String sampleTime =params.get("sampleTime"); + //String batchCode =params.get("batchCode"); + //PrintMessageProt//o.Printer.IQCPrintMessage iQCPrintMessage = PrintMessageProto.Printer.IQCPrintMessage.newBuilder().setSampleCode(sampleCode).setQcObject(qcObject).setSampleTime(sampleTime).setBatchCode(batchCode).build(); + // 构造对应的消息对象 +// msg = PrintMessageProto.Printer.newBuilder().setIqcPrintMessage(iQCPrintMessage).setDataType(dataType).setPrintInfo(printClientInfoMessage).build(); + break; + case UserConstants.BARCODE_TYPE_CLIENT: + // 客户标签打印 + //封装模板数据 + //String sampleCode =params.get("sampleCode"); + //String qcObject =params.get("qcObject"); + //String sampleTime =params.get("sampleTime"); + //String batchCode =params.get("batchCode"); + //PrintMessageProt//o.Printer.IQCPrintMessage iQCPrintMessage = PrintMessageProto.Printer.IQCPrintMessage.newBuilder().setSampleCode(sampleCode).setQcObject(qcObject).setSampleTime(sampleTime).setBatchCode(batchCode).build(); + // 构造对应的消息对象 +// msg = PrintMessageProto.Printer.newBuilder().setIqcPrintMessage(iQCPrintMessage).setDataType(dataType).setPrintInfo(printClientInfoMessage).build(); + break; + case UserConstants.BARCODE_TYPE_VENDOR: + // 供应商标签打印 + //封装模板数据 + //String sampleCode =params.get("sampleCode"); + //String qcObject =params.get("qcObject"); + //String sampleTime =params.get("sampleTime"); + //String batchCode =params.get("batchCode"); + //PrintMessageProt//o.Printer.IQCPrintMessage iQCPrintMessage = PrintMessageProto.Printer.IQCPrintMessage.newBuilder().setSampleCode(sampleCode).setQcObject(qcObject).setSampleTime(sampleTime).setBatchCode(batchCode).build(); + // 构造对应的消息对象 +// msg = PrintMessageProto.Printer.newBuilder().setIqcPrintMessage(iQCPrintMessage).setDataType(dataType).setPrintInfo(printClientInfoMessage).build(); + break; + case UserConstants.BARCODE_TYPE_WORKSHOP: + // 工作站标签打印 + //封装模板数据 + //String sampleCode =params.get("sampleCode"); + //String qcObject =params.get("qcObject"); + //String sampleTime =params.get("sampleTime"); + //String batchCode =params.get("batchCode"); + //PrintMessageProt//o.Printer.IQCPrintMessage iQCPrintMessage = PrintMessageProto.Printer.IQCPrintMessage.newBuilder().setSampleCode(sampleCode).setQcObject(qcObject).setSampleTime(sampleTime).setBatchCode(batchCode).build(); + // 构造对应的消息对象 +// msg = PrintMessageProto.Printer.newBuilder().setIqcPrintMessage(iQCPrintMessage).setDataType(dataType).setPrintInfo(printClientInfoMessage).build(); + break; + case UserConstants.BARCODE_TYPE_WORKORDER: + // 生产工单标签打印 + //封装模板数据 + //String sampleCode =params.get("sampleCode"); + //String qcObject =params.get("qcObject"); + //String sampleTime =params.get("sampleTime"); + //String batchCode =params.get("batchCode"); + //PrintMessageProt//o.Printer.IQCPrintMessage iQCPrintMessage = PrintMessageProto.Printer.IQCPrintMessage.newBuilder().setSampleCode(sampleCode).setQcObject(qcObject).setSampleTime(sampleTime).setBatchCode(batchCode).build(); + // 构造对应的消息对象 +// msg = PrintMessageProto.Printer.newBuilder().setIqcPrintMessage(iQCPrintMessage).setDataType(dataType).setPrintInfo(printClientInfoMessage).build(); + break; + case UserConstants.BARCODE_TYPE_TOOL: + // 工装夹具标签打印 + //封装模板数据 + //String sampleCode =params.get("sampleCode"); + //String qcObject =params.get("qcObject"); + //String sampleTime =params.get("sampleTime"); + //String batchCode =params.get("batchCode"); + //PrintMessageProt//o.Printer.IQCPrintMessage iQCPrintMessage = PrintMessageProto.Printer.IQCPrintMessage.newBuilder().setSampleCode(sampleCode).setQcObject(qcObject).setSampleTime(sampleTime).setBatchCode(batchCode).build(); + // 构造对应的消息对象 +// msg = PrintMessageProto.Printer.newBuilder().setIqcPrintMessage(iQCPrintMessage).setDataType(dataType).setPrintInfo(printClientInfoMessage).build(); + break; + case UserConstants.BARCODE_TYPE_SN: + // SN标签打印 + //封装模板数据 + //String sampleCode =params.get("sampleCode"); + //String qcObject =params.get("qcObject"); + //String sampleTime =params.get("sampleTime"); + //String batchCode =params.get("batchCode"); + //PrintMessageProt//o.Printer.IQCPrintMessage iQCPrintMessage = PrintMessageProto.Printer.IQCPrintMessage.newBuilder().setSampleCode(sampleCode).setQcObject(qcObject).setSampleTime(sampleTime).setBatchCode(batchCode).build(); + // 构造对应的消息对象 +// msg = PrintMessageProto.Printer.newBuilder().setIqcPrintMessage(iQCPrintMessage).setDataType(dataType).setPrintInfo(printClientInfoMessage).build(); + break; + default: + return AjaxResult.error("打印机不支持当前模板:" + "(" + businessType + ")"); + } + // 获取信道数据并发送消息对象给指定打印机客户端 + //打印机名称和打印机客户端地址映射 一对一关系 + ConcurrentHashMap socketAddress = PrintClientInfoMessageHandler.socketAddressMap; + //根据客户端和通道信息 + ConcurrentHashMap channels = PrintServerDefaultHandler.chanelMap; + if (channels.isEmpty() + || socketAddress.isEmpty() + || socketAddress.get(clientIp) == null + || channels.get(socketAddress.get(clientIp)) == null) { + return AjaxResult.error("打印机客户端连接异常" + "(" + clientIp + ")"); + } + Channel channel = channels.get(socketAddress.get(clientIp)); + channel.writeAndFlush(msg); + return AjaxResult.success("打印成功"); + } + +} diff --git a/ktg-print/src/main/java/com/ktg/print/controller/mobile/PrintPrinterMobileController.java b/ktg-print/src/main/java/com/ktg/print/controller/mobile/PrintPrinterMobileController.java index 2a0fd84..cdb6ad1 100644 --- a/ktg-print/src/main/java/com/ktg/print/controller/mobile/PrintPrinterMobileController.java +++ b/ktg-print/src/main/java/com/ktg/print/controller/mobile/PrintPrinterMobileController.java @@ -33,4 +33,7 @@ public class PrintPrinterMobileController extends BaseController { return AjaxResult.success(list); } + + + }