diff --git a/agileboot-api/src/main/java/com/agileboot/api/controller/ApprovalApiController.java b/agileboot-api/src/main/java/com/agileboot/api/controller/ApprovalApiController.java index 40d004a..470521f 100644 --- a/agileboot-api/src/main/java/com/agileboot/api/controller/ApprovalApiController.java +++ b/agileboot-api/src/main/java/com/agileboot/api/controller/ApprovalApiController.java @@ -6,6 +6,7 @@ import com.agileboot.common.core.dto.ResponseDTO; import com.agileboot.common.core.page.PageDTO; import com.agileboot.common.exception.ApiException; import com.agileboot.common.exception.error.ErrorCode; +import com.agileboot.domain.cabinet.operation.command.AddCabinetCellOperationCommand; import com.agileboot.domain.shop.approval.ReturnApprovalApplicationService; import com.agileboot.domain.shop.approval.command.AddReturnApprovalCommand; import com.agileboot.domain.shop.approval.command.UpdateReturnApprovalCommand; @@ -198,5 +199,15 @@ public class ApprovalApiController { return ResponseDTO.ok("校验成功"); } - + /** + * 打开审批商品柜门接口 + * @param approvalGoodsCellId 通过审批单商品详情ID + * @return 空响应 + */ + @PostMapping("/openCabinet/{approvalGoodsCellId}") + public ResponseDTO openCabinet(@PathVariable Long approvalGoodsCellId, + @RequestBody AddCabinetCellOperationCommand operationCommand) { + approvalApplicationService.openCabinet(approvalGoodsCellId, operationCommand); + return ResponseDTO.ok(); + } } diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/shop/approval/ReturnApprovalApplicationService.java b/agileboot-domain/src/main/java/com/agileboot/domain/shop/approval/ReturnApprovalApplicationService.java index fd1cc15..8a3d201 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/shop/approval/ReturnApprovalApplicationService.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/shop/approval/ReturnApprovalApplicationService.java @@ -5,14 +5,22 @@ import cn.hutool.json.JSONUtil; import com.agileboot.common.constant.PayApiConstants; import com.agileboot.common.constant.WeixinConstants; import com.agileboot.common.core.page.PageDTO; +import com.agileboot.common.exception.ApiException; +import com.agileboot.common.exception.error.ErrorCode; import com.agileboot.domain.asset.AssetApplicationService; import com.agileboot.domain.cabinet.cell.db.CabinetCellEntity; import com.agileboot.domain.cabinet.cell.db.CabinetCellService; import com.agileboot.domain.cabinet.cell.model.CabinetCellModel; import com.agileboot.domain.cabinet.cell.model.CabinetCellModelFactory; +import com.agileboot.domain.cabinet.mainboard.db.CabinetMainboardEntity; +import com.agileboot.domain.cabinet.mainboard.db.CabinetMainboardService; +import com.agileboot.domain.cabinet.operation.command.AddCabinetCellOperationCommand; +import com.agileboot.domain.cabinet.operation.model.CabinetCellOperationModel; +import com.agileboot.domain.cabinet.operation.model.CabinetCellOperationModelFactory; import com.agileboot.domain.cabinet.smartCabinet.db.SmartCabinetEntity; import com.agileboot.domain.cabinet.smartCabinet.db.SmartCabinetService; import com.agileboot.domain.common.command.BulkOperationCommand; +import com.agileboot.domain.mqtt.MqttService; import com.agileboot.domain.qywx.accessToken.AccessTokenApplicationService; import com.agileboot.domain.qywx.accessToken.db.QyAccessTokenEntity; import com.agileboot.domain.qywx.api.QywxApiUtil; @@ -96,6 +104,9 @@ public class ReturnApprovalApplicationService { private final AssetApplicationService assetApplicationService; private final ApprovalGoodsService approvalGoodsService; private final ApprovalGoodsCellService approvalGoodsCellService; + private final CabinetMainboardService cabinetMainboardService; + private final CabinetCellOperationModelFactory cabinetCellOperationModelFactory; + private final MqttService mqttService; /** * 获取退货审批列表 @@ -619,4 +630,51 @@ public class ReturnApprovalApplicationService { approval.setCodeCheck(1); approval.updateById(); } + + public void openCabinet(Long approvalGoodsCellId, AddCabinetCellOperationCommand operationCommand) { + ApprovalGoodsCellEntity approvalGoodsCell = approvalGoodsCellService.getById(approvalGoodsCellId); + if (approvalGoodsCell == null) { + throw new RuntimeException("无效的审批商品格口关联"); + } + + ApprovalGoodsEntity approvalGoods = approvalGoodsService.getById(approvalGoodsCell.getApprovalGoodsId()); + + // 获取柜子单元和智能柜信息 + CabinetCellEntity cabinetCellEntity = cabinetCellService.getById(approvalGoodsCell.getCellId()); + SmartCabinetEntity smartCabinet = smartCabinetService.getById(cabinetCellEntity.getCabinetId()); + if (null == smartCabinet) { + throw new ApiException(ErrorCode.Client.COMMON_FORBIDDEN_TO_CALL, "柜子不存在"); + } + + CabinetMainboardEntity cabinetMainboard = cabinetMainboardService.getById(cabinetCellEntity.getMainboardId()); + + operationCommand.setCellId(approvalGoodsCell.getCellId()); + operationCommand.setGoodsId(approvalGoods.getGoodsId()); + operationCommand.setGoodsName(approvalGoods.getGoodsName()); + operationCommand.setStatus(1); + CabinetCellOperationModel cellOperationModel = cabinetCellOperationModelFactory.create(); + + // 构造MQTT开柜指令: + // 指令格式:8A + 锁控编号(2位HEX) + 引脚号(2位HEX) + 操作码11 + String mqttDate = "8A"; + mqttDate += String.format("%02X", cabinetMainboard.getLockControlNo()); + mqttDate += String.format("%02X", cabinetCellEntity.getPinNo()); + mqttDate += "11"; + try { + mqttService.publish(mqttDate, smartCabinet.getMqttServerId()); + } catch (Exception e) { + log.error("mqtt publish error", e); + operationCommand.setStatus(2); + throw new ApiException(ErrorCode.Internal.INTERNAL_ERROR, "发送指令失败"); + } finally { + // 记录操作日志 + operationCommand.setCreatorId(0L); + operationCommand.setCreateTime(new Date()); + operationCommand.setUpdaterId(0L); + operationCommand.setUpdateTime(new Date()); + operationCommand.setDeleted(false); + cellOperationModel.loadAddCommand(operationCommand); + cellOperationModel.insert(); + } + } } \ No newline at end of file