12 changed files with 304 additions and 29 deletions
@ -0,0 +1,90 @@ |
|||
package com.project.logistics.controller; |
|||
|
|||
import com.project.logistics.domain.entity.PrintRecordEntity; |
|||
import com.project.logistics.domain.service.base.PrintMachineService; |
|||
import com.project.logistics.domain.service.base.PrintRecordService; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.*; |
|||
|
|||
import java.util.Map; |
|||
|
|||
@Slf4j |
|||
@RestController |
|||
@RequestMapping("/api/logistics/print") |
|||
public class PrintController { |
|||
|
|||
@Autowired |
|||
private PrintRecordService printRecordService; |
|||
|
|||
|
|||
@Autowired |
|||
private PrintMachineService printMachineService; |
|||
/** |
|||
* 1. 询问接口:我这台机器是否打过这张单? |
|||
* @return "true" 代表没打过,允许打印;"false" 代表已打过。 |
|||
*/ |
|||
@GetMapping("/check") |
|||
public String check(@RequestParam("orderNo") String orderNo, |
|||
@RequestParam("machineId") String machineId) { |
|||
|
|||
// 只查询打印记录表
|
|||
boolean alreadyPrinted = printRecordService.lambdaQuery() |
|||
.eq(PrintRecordEntity::getOrderNo, orderNo) |
|||
.eq(PrintRecordEntity::getMachineId, machineId) |
|||
.eq(PrintRecordEntity::getPrintStatus, 1) |
|||
.count() > 0; |
|||
|
|||
if (alreadyPrinted) { |
|||
return "false"; |
|||
} |
|||
|
|||
log.info(">>> 机器 {} 请求打印新单据: {}", machineId, orderNo); |
|||
return "true"; |
|||
} |
|||
|
|||
@PostMapping("/heartbeat") |
|||
public Map<String, Object> heartbeat(@RequestBody Map<String, Object> params) { |
|||
String machineId = (String) params.get("machineId"); |
|||
String status = (String) params.get("status"); |
|||
|
|||
// 更新机器状态和时间
|
|||
printMachineService.updateHeartbeat(machineId, status); |
|||
|
|||
return Map.of("code", 200, "msg", "pong"); |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 2. 反馈接口:打印成功,请记录。 |
|||
*/ |
|||
@PostMapping("/callback") |
|||
public Map<String, Object> callback(@RequestBody Map<String, Object> params) { |
|||
String orderNo = (String) params.get("orderNo"); |
|||
String machineId = (String) params.get("machineId"); |
|||
Integer printStatus = (Integer) params.get("printStatus"); |
|||
|
|||
// 记录或更新打印日志
|
|||
PrintRecordEntity record = printRecordService.lambdaQuery() |
|||
.eq(PrintRecordEntity::getOrderNo, orderNo) |
|||
.eq(PrintRecordEntity::getMachineId, machineId) |
|||
.one(); |
|||
|
|||
if (record == null) { |
|||
record = new PrintRecordEntity(); |
|||
record.setOrderNo(orderNo); |
|||
record.setMachineId(machineId); |
|||
} |
|||
|
|||
record.setPrintStatus(printStatus); |
|||
printRecordService.saveOrUpdate(record); |
|||
|
|||
if (printStatus == 1) { |
|||
log.info(">>> 单据 {} 物理打印成功 (机器: {})", orderNo, machineId); |
|||
} else { |
|||
log.warn(">>> 单据 {} 物理打印失败 (机器: {})", orderNo, machineId); |
|||
} |
|||
|
|||
return Map.of("code", 200, "msg", "received"); |
|||
} |
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
package com.project.logistics.domain.entity; |
|||
|
|||
import com.baomidou.mybatisplus.annotation.TableField; |
|||
import com.baomidou.mybatisplus.annotation.TableName; |
|||
import com.project.base.domain.entity.BaseEntity; |
|||
import jakarta.persistence.*; |
|||
import lombok.Data; |
|||
import lombok.EqualsAndHashCode; |
|||
import org.hibernate.annotations.Comment; |
|||
import java.time.LocalDateTime; |
|||
|
|||
@Data |
|||
@EqualsAndHashCode(callSuper = true) |
|||
@Entity |
|||
@Table(name = "auto_print_machine", indexes = { |
|||
@Index(name = "idx_machine_id", columnList = "machine_id", unique = true) |
|||
}) |
|||
@TableName("auto_print_machine") |
|||
@Comment("打印机在线状态表") |
|||
public class PrintMachineEntity extends BaseEntity { |
|||
|
|||
@Id |
|||
@Column(name = "id", columnDefinition = "bigint(20) comment '主键ID'") |
|||
private Long id; |
|||
|
|||
@Comment("机器标识 (MAC地址或PC名)") |
|||
@Column(name = "machine_id", length = 100, nullable = false) |
|||
@TableField("machine_id") |
|||
private String machineId; |
|||
|
|||
@Comment("最后心跳时间") |
|||
@Column(name = "last_heartbeat_time") |
|||
@TableField("last_heartbeat_time") |
|||
private LocalDateTime lastHeartbeatTime; |
|||
|
|||
@Comment("在线状态 (ONLINE/OFFLINE)") |
|||
@Column(name = "status", length = 20) |
|||
@TableField("status") |
|||
private String status; |
|||
|
|||
@Comment("客户端版本号") |
|||
@Column(name = "client_version", length = 20) |
|||
@TableField("client_version") |
|||
private String clientVersion; |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
package com.project.logistics.domain.entity; |
|||
|
|||
import com.baomidou.mybatisplus.annotation.TableField; |
|||
import com.baomidou.mybatisplus.annotation.TableName; |
|||
import com.project.base.domain.entity.BaseEntity; |
|||
import jakarta.persistence.*; |
|||
import lombok.Data; |
|||
import lombok.EqualsAndHashCode; |
|||
import org.hibernate.annotations.Comment; |
|||
|
|||
@Data |
|||
@EqualsAndHashCode(callSuper = true) |
|||
@Entity |
|||
@Table(name = "auto_print_record", indexes = { |
|||
@Index(name = "idx_print_order_no", columnList = "order_no", unique = true) |
|||
}) |
|||
@TableName("auto_print_record") |
|||
@Comment("面单物理打印记录表") |
|||
public class PrintRecordEntity extends BaseEntity { |
|||
|
|||
@Id |
|||
@Column(name = "id", columnDefinition = "bigint(20) comment '主键ID'") |
|||
private Long id; |
|||
|
|||
@Comment("业务单号 (U9单号)") |
|||
@Column(name = "order_no", length = 64, nullable = false) |
|||
@TableField("order_no") |
|||
private String orderNo; |
|||
|
|||
@Comment("执行打印的机器标识 (MAC地址或PC名)") |
|||
@Column(name = "machine_id", length = 100) |
|||
@TableField("machine_id") |
|||
private String machineId; |
|||
|
|||
@Comment("打印时的文件MD5 (校验用)") |
|||
@Column(name = "file_md5", length = 64) |
|||
@TableField("file_md5") |
|||
private String fileMd5; |
|||
|
|||
@Comment("打印状态 (1:成功)") |
|||
@Column(name = "status") |
|||
@TableField("status") |
|||
private Integer printStatus; |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
package com.project.logistics.domain.service.base; |
|||
|
|||
import com.baomidou.mybatisplus.extension.service.IService; |
|||
import com.project.logistics.domain.entity.PrintMachineEntity; |
|||
|
|||
public interface PrintMachineService extends IService<PrintMachineEntity> { |
|||
void updateHeartbeat(String machineId, String status); |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
package com.project.logistics.domain.service.base; |
|||
|
|||
import com.baomidou.mybatisplus.extension.service.IService; |
|||
import com.project.logistics.domain.entity.PrintRecordEntity; |
|||
|
|||
public interface PrintRecordService extends IService<PrintRecordEntity> { |
|||
|
|||
boolean isPrintedByThisMachine(String orderNo, String machineId); |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
package com.project.logistics.domain.service.base.impl; |
|||
|
|||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|||
import com.project.logistics.domain.entity.PrintMachineEntity; |
|||
import com.project.logistics.domain.service.base.PrintMachineService; |
|||
import com.project.logistics.mapper.PrintMachineMapper; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
|
|||
import java.time.LocalDateTime; |
|||
|
|||
@Service |
|||
@Slf4j |
|||
public class PrintMachineServiceImpl extends ServiceImpl<PrintMachineMapper, PrintMachineEntity> |
|||
implements PrintMachineService { |
|||
|
|||
@Override |
|||
@Transactional(rollbackFor = Exception.class) |
|||
public void updateHeartbeat(String machineId, String status) { |
|||
// 使用 MyBatis-Plus 的 LambdaQuery 检查是否存在记录
|
|||
PrintMachineEntity machine = this.lambdaQuery() |
|||
.eq(PrintMachineEntity::getMachineId, machineId) |
|||
.one(); |
|||
|
|||
if (machine == null) { |
|||
machine = new PrintMachineEntity(); |
|||
machine.setMachineId(machineId); |
|||
} |
|||
|
|||
machine.setStatus(status); |
|||
machine.setLastHeartbeatTime(LocalDateTime.now()); |
|||
|
|||
// 保存或更新
|
|||
this.saveOrUpdate(machine); |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
package com.project.logistics.domain.service.base.impl; |
|||
|
|||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|||
import com.project.logistics.domain.entity.PrintRecordEntity; |
|||
import com.project.logistics.domain.service.base.PrintRecordService; |
|||
import com.project.logistics.mapper.PrintRecordMapper; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
|
|||
@Service |
|||
public class PrintRecordServiceImpl extends ServiceImpl<PrintRecordMapper, PrintRecordEntity> |
|||
implements PrintRecordService { |
|||
|
|||
@Override |
|||
public boolean isPrintedByThisMachine(String orderNo, String machineId) { |
|||
return this.lambdaQuery() |
|||
.eq(PrintRecordEntity::getOrderNo, orderNo) |
|||
.eq(PrintRecordEntity::getMachineId, machineId) |
|||
.eq(PrintRecordEntity::getPrintStatus, 1) |
|||
.count() > 0; } |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
package com.project.logistics.mapper; |
|||
|
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|||
import com.project.logistics.domain.entity.PrintMachineEntity; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
|
|||
@Mapper |
|||
public interface PrintMachineMapper extends BaseMapper<PrintMachineEntity> { |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
package com.project.logistics.mapper; |
|||
|
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|||
import com.project.logistics.domain.entity.PrintRecordEntity; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
|
|||
@Mapper |
|||
public interface PrintRecordMapper extends BaseMapper<PrintRecordEntity> { |
|||
} |
|||
Loading…
Reference in new issue