diff --git a/klp-admin/src/main/java/com/klp/framework/client/SqlServerApiClient.java b/klp-admin/src/main/java/com/klp/framework/client/SqlServerApiClient.java index 405aec7d..d817f335 100644 --- a/klp-admin/src/main/java/com/klp/framework/client/SqlServerApiClient.java +++ b/klp-admin/src/main/java/com/klp/framework/client/SqlServerApiClient.java @@ -428,6 +428,21 @@ public class SqlServerApiClient { return executeSql("oracle", sql.toString(), params); } + public ExecuteSqlResponse queryExcoilByInsdateRange(String startTime, String endTime) { + Map params = new java.util.HashMap<>(); + StringBuilder sql = new StringBuilder("SELECT * FROM JXPLTCM.PLTCM_PDO_EXCOIL WHERE 1=1"); + if (startTime != null && !startTime.trim().isEmpty()) { + sql.append(" AND INSDATE > TO_DATE(:startTime, 'YYYY-MM-DD HH24:MI:SS')"); + params.put("startTime", startTime.trim()); + } + if (endTime != null && !endTime.trim().isEmpty()) { + sql.append(" AND INSDATE <= TO_DATE(:endTime, 'YYYY-MM-DD HH24:MI:SS')"); + params.put("endTime", endTime.trim()); + } + sql.append(" ORDER BY INSDATE ASC"); + return executeSql("oracle", sql.toString(), params); + } + public ExecuteSqlResponse queryExcoilList(int page, int pageSize) { int endRow = page * pageSize; int startRow = endRow - pageSize; diff --git a/klp-admin/src/main/java/com/klp/framework/service/SqlServerApiBusinessService.java b/klp-admin/src/main/java/com/klp/framework/service/SqlServerApiBusinessService.java index bf39a5c6..e5845d22 100644 --- a/klp-admin/src/main/java/com/klp/framework/service/SqlServerApiBusinessService.java +++ b/klp-admin/src/main/java/com/klp/framework/service/SqlServerApiBusinessService.java @@ -161,6 +161,13 @@ public class SqlServerApiBusinessService { return asRowList(client.queryExcoilByTimeRange(startTime, endTime)); } + /** + * 出口卷实绩列表(按数据写入时间),用于增量同步。 + */ + public List> getExcoilByInsdateRange(String startTime, String endTime) { + return asRowList(client.queryExcoilByInsdateRange(startTime, endTime)); + } + /** * 出口卷实绩列表(分页),来自 PLTCM_PDO_EXCOIL。 */ diff --git a/klp-admin/src/main/java/com/klp/framework/sqlserver/SqlServerApiController.java b/klp-admin/src/main/java/com/klp/framework/sqlserver/SqlServerApiController.java index b4b972b5..d2a73c6f 100644 --- a/klp-admin/src/main/java/com/klp/framework/sqlserver/SqlServerApiController.java +++ b/klp-admin/src/main/java/com/klp/framework/sqlserver/SqlServerApiController.java @@ -156,7 +156,7 @@ public class SqlServerApiController { } /** - * 出口卷实绩列表(按时间段),来自 PLTCM_PDO_EXCOIL。 + * 出口卷实绩列表(按下线时间),来自 PLTCM_PDO_EXCOIL。 * startTime / endTime 格式:yyyy-MM-dd HH:mm:ss */ @GetMapping("/excoil/by-time") @@ -166,6 +166,17 @@ public class SqlServerApiController { return R.ok(businessService.getExcoilByTimeRange(startTime, endTime)); } + /** + * 出口卷实绩列表(按数据写入时间),用于增量同步。 + * startTime(exclusive)/ endTime(inclusive),格式:yyyy-MM-dd HH:mm:ss + */ + @GetMapping("/excoil/by-insdate") + public R>> excoilByInsdate( + @RequestParam(required = false) String startTime, + @RequestParam(required = false) String endTime) { + return R.ok(businessService.getExcoilByInsdateRange(startTime, endTime)); + } + /** * 出口卷实绩列表(分页),来自 PLTCM_PDO_EXCOIL。 */ diff --git a/klp-mes/src/main/java/com/klp/mes/excoil/controller/MesExCoilController.java b/klp-mes/src/main/java/com/klp/mes/excoil/controller/MesExCoilController.java new file mode 100644 index 00000000..34493f41 --- /dev/null +++ b/klp-mes/src/main/java/com/klp/mes/excoil/controller/MesExCoilController.java @@ -0,0 +1,112 @@ +package com.klp.mes.excoil.controller; + +import java.util.List; +import java.util.Arrays; +import java.util.Map; + +import lombok.RequiredArgsConstructor; +import javax.servlet.http.HttpServletResponse; +import javax.validation.constraints.*; +import org.springframework.web.bind.annotation.*; +import org.springframework.validation.annotation.Validated; +import com.klp.common.annotation.RepeatSubmit; +import com.klp.common.annotation.Log; +import com.klp.common.core.controller.BaseController; +import com.klp.common.core.domain.PageQuery; +import com.klp.common.core.domain.R; +import com.klp.common.core.validate.AddGroup; +import com.klp.common.core.validate.EditGroup; +import com.klp.common.enums.BusinessType; +import com.klp.common.utils.poi.ExcelUtil; +import com.klp.mes.excoil.domain.vo.MesExCoilVo; +import com.klp.mes.excoil.domain.bo.MesExCoilBo; +import com.klp.mes.excoil.service.IMesExCoilService; +import com.klp.common.core.page.TableDataInfo; + +/** + * 出口卷数据同步 + * + * @author klp + * @date 2026-05-21 + */ +@Validated +@RequiredArgsConstructor +@RestController +@RequestMapping("/excoil/exCoil") +public class MesExCoilController extends BaseController { + + private final IMesExCoilService iMesExCoilService; + + /** + * 查询出口卷数据同步列表 + */ + @GetMapping("/list") + public TableDataInfo list(MesExCoilBo bo, PageQuery pageQuery) { + return iMesExCoilService.queryPageList(bo, pageQuery); + } + + /** + * 导出出口卷数据同步列表 + */ + @Log(title = "出口卷数据同步", businessType = BusinessType.EXPORT) + @PostMapping("/export") + public void export(MesExCoilBo bo, HttpServletResponse response) { + List list = iMesExCoilService.queryList(bo); + ExcelUtil.exportExcel(list, "出口卷数据同步", MesExCoilVo.class, response); + } + + /** + * 获取出口卷数据同步详细信息 + * + * @param exId 主键 + */ + @GetMapping("/{exId}") + public R getInfo(@NotNull(message = "主键不能为空") + @PathVariable Long exId) { + return R.ok(iMesExCoilService.queryById(exId)); + } + + /** + * 新增出口卷数据同步 + */ + @Log(title = "出口卷数据同步", businessType = BusinessType.INSERT) + @RepeatSubmit() + @PostMapping() + public R add(@Validated(AddGroup.class) @RequestBody MesExCoilBo bo) { + return toAjax(iMesExCoilService.insertByBo(bo)); + } + + /** + * 修改出口卷数据同步 + */ + @Log(title = "出口卷数据同步", businessType = BusinessType.UPDATE) + @RepeatSubmit() + @PutMapping() + public R edit(@Validated(EditGroup.class) @RequestBody MesExCoilBo bo) { + return toAjax(iMesExCoilService.updateByBo(bo)); + } + + /** + * 删除出口卷数据同步 + * + * @param exIds 主键串 + */ + @Log(title = "出口卷数据同步", businessType = BusinessType.DELETE) + @DeleteMapping("/{exIds}") + public R remove(@NotEmpty(message = "主键不能为空") + @PathVariable Long[] exIds) { + return toAjax(iMesExCoilService.deleteWithValidByIds(Arrays.asList(exIds), true)); + } + + /** + * 从 L2 系统同步出口卷实绩数据。 + * 首次全量同步,后续增量同步(以 insdate 为锚点)。 + */ + @Log(title = "出口卷数据同步", businessType = BusinessType.OTHER) + @RepeatSubmit(interval = 300000, message = "同步任务已提交,请勿重复操作") + @PostMapping("/sync") + public R> sync() { + Map result = iMesExCoilService.syncExCoilData(); + return R.ok(result); + } +} diff --git a/klp-mes/src/main/java/com/klp/mes/excoil/domain/MesExCoil.java b/klp-mes/src/main/java/com/klp/mes/excoil/domain/MesExCoil.java new file mode 100644 index 00000000..4a0764ad --- /dev/null +++ b/klp-mes/src/main/java/com/klp/mes/excoil/domain/MesExCoil.java @@ -0,0 +1,196 @@ +package com.klp.mes.excoil.domain; + +import com.baomidou.mybatisplus.annotation.*; +import com.klp.common.core.domain.BaseEntity; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.math.BigDecimal; +import java.util.Date; +import com.fasterxml.jackson.annotation.JsonFormat; + +/** + * 出口卷数据同步对象 mes_ex_coil + * + * @author klp + * @date 2026-05-21 + */ +@Data +@EqualsAndHashCode(callSuper = true) +@TableName("mes_ex_coil") +public class MesExCoil extends BaseEntity { + + private static final long serialVersionUID=1L; + + /** + * 主键ID + */ + @TableId(value = "ex_id") + private Long exId; + /** + * 出口卷号(唯一标识) + */ + private String excoilid; + /** + * 入口卷号 + */ + private String encoilid; + /** + * 热卷号 + */ + private String hotCoilid; + /** + * 钢种 + */ + private String grade; + /** + * 订单质量 + */ + private String orderQuality; + /** + * 产品类型 + */ + private String productType; + /** + * 状态 + */ + private String status; + /** + * 班次 + */ + private String shift; + /** + * 包装类型 + */ + private String parkType; + /** + * 切边类型 + */ + private String sideTrim; + /** + * 入口厚度 + */ + private BigDecimal entryThick; + /** + * 入口宽度 + */ + private BigDecimal entryWidth; + /** + * 入口重量 + */ + private BigDecimal entryWeight; + /** + * 使用入口重量 + */ + private BigDecimal usedEntryWeight; + /** + * 出口厚度 + */ + private BigDecimal exitThick; + /** + * 出口宽度 + */ + private BigDecimal exitWidth; + /** + * 出口长度 + */ + private BigDecimal exitLength; + /** + * 出口重量 + */ + private BigDecimal exitWeight; + /** + * 计算出口重量 + */ + private BigDecimal calcExitWeight; + /** + * 实测出口重量 + */ + private BigDecimal measExitWeight; + /** + * 出口正偏差 + */ + private BigDecimal exitPosDev; + /** + * 出口负偏差 + */ + private BigDecimal exitNegDev; + /** + * 内径 + */ + private BigDecimal innerDiameter; + /** + * 外径 + */ + private BigDecimal outerDiameter; + /** + * 头部位置 + */ + private BigDecimal headpos; + /** + * 尾部位置 + */ + private BigDecimal tailpos; + /** + * 质量 + */ + private BigDecimal quality; + /** + * 板形质量 + */ + private BigDecimal shapeQuality; + /** + * 机组 + */ + private String crew; + /** + * 报告标志 + */ + private Long reportFlag; + /** + * 子ID + */ + private Long subid; + /** + * 序号 + */ + private Long rn; + /** + * 上线时间 + */ + private Date onlineDate; + /** + * 开始时间 + */ + private Date startDate; + /** + * 结束时间 + */ + private Date endDate; + /** + * 焊接时间 + */ + private Date weldedDate; + /** + * 数据写入时间(来源系统) + */ + private Date insdate; + /** + * 同步时间 + */ + private Date syncTime; + /** + * 同步备注 + */ + private String comments; + /** + * 备注 + */ + private String remark; + /** + * 删除标识 0正常 2删除 + */ + @TableLogic + private Long delFlag; + +} diff --git a/klp-mes/src/main/java/com/klp/mes/excoil/domain/bo/MesExCoilBo.java b/klp-mes/src/main/java/com/klp/mes/excoil/domain/bo/MesExCoilBo.java new file mode 100644 index 00000000..41e8e205 --- /dev/null +++ b/klp-mes/src/main/java/com/klp/mes/excoil/domain/bo/MesExCoilBo.java @@ -0,0 +1,229 @@ +package com.klp.mes.excoil.domain.bo; + +import com.klp.common.core.domain.BaseEntity; +import lombok.Data; +import lombok.EqualsAndHashCode; +import javax.validation.constraints.*; + +import java.math.BigDecimal; +import java.util.Date; +import com.fasterxml.jackson.annotation.JsonFormat; + +/** + * 出口卷数据同步业务对象 mes_ex_coil + * + * @author klp + * @date 2026-05-21 + */ + +@Data +@EqualsAndHashCode(callSuper = true) +public class MesExCoilBo extends BaseEntity { + + /** + * 主键ID + */ + private Long exId; + + /** + * 出口卷号(唯一标识) + */ + private String excoilid; + + /** + * 入口卷号 + */ + private String encoilid; + + /** + * 热卷号 + */ + private String hotCoilid; + + /** + * 钢种 + */ + private String grade; + + /** + * 订单质量 + */ + private String orderQuality; + + /** + * 产品类型 + */ + private String productType; + + /** + * 状态 + */ + private String status; + + /** + * 班次 + */ + private String shift; + + /** + * 包装类型 + */ + private String parkType; + + /** + * 切边类型 + */ + private String sideTrim; + + /** + * 入口厚度 + */ + private BigDecimal entryThick; + + /** + * 入口宽度 + */ + private BigDecimal entryWidth; + + /** + * 入口重量 + */ + private BigDecimal entryWeight; + + /** + * 使用入口重量 + */ + private BigDecimal usedEntryWeight; + + /** + * 出口厚度 + */ + private BigDecimal exitThick; + + /** + * 出口宽度 + */ + private BigDecimal exitWidth; + + /** + * 出口长度 + */ + private BigDecimal exitLength; + + /** + * 出口重量 + */ + private BigDecimal exitWeight; + + /** + * 计算出口重量 + */ + private BigDecimal calcExitWeight; + + /** + * 实测出口重量 + */ + private BigDecimal measExitWeight; + + /** + * 出口正偏差 + */ + private BigDecimal exitPosDev; + + /** + * 出口负偏差 + */ + private BigDecimal exitNegDev; + + /** + * 内径 + */ + private BigDecimal innerDiameter; + + /** + * 外径 + */ + private BigDecimal outerDiameter; + + /** + * 头部位置 + */ + private BigDecimal headpos; + + /** + * 尾部位置 + */ + private BigDecimal tailpos; + + /** + * 质量 + */ + private BigDecimal quality; + + /** + * 板形质量 + */ + private BigDecimal shapeQuality; + + /** + * 机组 + */ + private String crew; + + /** + * 报告标志 + */ + private Long reportFlag; + + /** + * 子ID + */ + private Long subid; + + /** + * 序号 + */ + private Long rn; + + /** + * 上线时间 + */ + private Date onlineDate; + + /** + * 开始时间 + */ + private Date startDate; + + /** + * 结束时间 + */ + private Date endDate; + + /** + * 焊接时间 + */ + private Date weldedDate; + + /** + * 数据写入时间(来源系统) + */ + private Date insdate; + + /** + * 同步时间 + */ + private Date syncTime; + + /** + * 同步备注 + */ + private String comments; + + /** + * 备注 + */ + private String remark; + + +} diff --git a/klp-mes/src/main/java/com/klp/mes/excoil/domain/vo/MesExCoilVo.java b/klp-mes/src/main/java/com/klp/mes/excoil/domain/vo/MesExCoilVo.java new file mode 100644 index 00000000..9f8b61c3 --- /dev/null +++ b/klp-mes/src/main/java/com/klp/mes/excoil/domain/vo/MesExCoilVo.java @@ -0,0 +1,272 @@ +package com.klp.mes.excoil.domain.vo; + +import java.math.BigDecimal; +import java.util.Date; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; +import com.alibaba.excel.annotation.ExcelProperty; +import com.klp.common.annotation.ExcelDictFormat; +import com.klp.common.convert.ExcelDictConvert; +import lombok.Data; + + +/** + * 出口卷数据同步视图对象 mes_ex_coil + * + * @author klp + * @date 2026-05-21 + */ +@Data +@ExcelIgnoreUnannotated +public class MesExCoilVo { + + private static final long serialVersionUID = 1L; + + /** + * 主键ID + */ + @ExcelProperty(value = "主键ID") + private Long exId; + + /** + * 出口卷号(唯一标识) + */ + @ExcelProperty(value = "出口卷号(唯一标识)") + private String excoilid; + + /** + * 入口卷号 + */ + @ExcelProperty(value = "入口卷号") + private String encoilid; + + /** + * 热卷号 + */ + @ExcelProperty(value = "热卷号") + private String hotCoilid; + + /** + * 钢种 + */ + @ExcelProperty(value = "钢种") + private String grade; + + /** + * 订单质量 + */ + @ExcelProperty(value = "订单质量") + private String orderQuality; + + /** + * 产品类型 + */ + @ExcelProperty(value = "产品类型") + private String productType; + + /** + * 状态 + */ + @ExcelProperty(value = "状态") + private String status; + + /** + * 班次 + */ + @ExcelProperty(value = "班次") + private String shift; + + /** + * 包装类型 + */ + @ExcelProperty(value = "包装类型") + private String parkType; + + /** + * 切边类型 + */ + @ExcelProperty(value = "切边类型") + private String sideTrim; + + /** + * 入口厚度 + */ + @ExcelProperty(value = "入口厚度") + private BigDecimal entryThick; + + /** + * 入口宽度 + */ + @ExcelProperty(value = "入口宽度") + private BigDecimal entryWidth; + + /** + * 入口重量 + */ + @ExcelProperty(value = "入口重量") + private BigDecimal entryWeight; + + /** + * 使用入口重量 + */ + @ExcelProperty(value = "使用入口重量") + private BigDecimal usedEntryWeight; + + /** + * 出口厚度 + */ + @ExcelProperty(value = "出口厚度") + private BigDecimal exitThick; + + /** + * 出口宽度 + */ + @ExcelProperty(value = "出口宽度") + private BigDecimal exitWidth; + + /** + * 出口长度 + */ + @ExcelProperty(value = "出口长度") + private BigDecimal exitLength; + + /** + * 出口重量 + */ + @ExcelProperty(value = "出口重量") + private BigDecimal exitWeight; + + /** + * 计算出口重量 + */ + @ExcelProperty(value = "计算出口重量") + private BigDecimal calcExitWeight; + + /** + * 实测出口重量 + */ + @ExcelProperty(value = "实测出口重量") + private BigDecimal measExitWeight; + + /** + * 出口正偏差 + */ + @ExcelProperty(value = "出口正偏差") + private BigDecimal exitPosDev; + + /** + * 出口负偏差 + */ + @ExcelProperty(value = "出口负偏差") + private BigDecimal exitNegDev; + + /** + * 内径 + */ + @ExcelProperty(value = "内径") + private BigDecimal innerDiameter; + + /** + * 外径 + */ + @ExcelProperty(value = "外径") + private BigDecimal outerDiameter; + + /** + * 头部位置 + */ + @ExcelProperty(value = "头部位置") + private BigDecimal headpos; + + /** + * 尾部位置 + */ + @ExcelProperty(value = "尾部位置") + private BigDecimal tailpos; + + /** + * 质量 + */ + @ExcelProperty(value = "质量") + private BigDecimal quality; + + /** + * 板形质量 + */ + @ExcelProperty(value = "板形质量") + private BigDecimal shapeQuality; + + /** + * 机组 + */ + @ExcelProperty(value = "机组") + private String crew; + + /** + * 报告标志 + */ + @ExcelProperty(value = "报告标志") + private Long reportFlag; + + /** + * 子ID + */ + @ExcelProperty(value = "子ID") + private Long subid; + + /** + * 序号 + */ + @ExcelProperty(value = "序号") + private Long rn; + + /** + * 上线时间 + */ + @ExcelProperty(value = "上线时间") + private Date onlineDate; + + /** + * 开始时间 + */ + @ExcelProperty(value = "开始时间") + private Date startDate; + + /** + * 结束时间 + */ + @ExcelProperty(value = "结束时间") + private Date endDate; + + /** + * 焊接时间 + */ + @ExcelProperty(value = "焊接时间") + private Date weldedDate; + + /** + * 数据写入时间(来源系统) + */ + @ExcelProperty(value = "数据写入时间(来源系统)") + private Date insdate; + + /** + * 同步时间 + */ + @ExcelProperty(value = "同步时间") + private Date syncTime; + + /** + * 同步备注 + */ + @ExcelProperty(value = "同步备注") + private String comments; + + /** + * 备注 + */ + @ExcelProperty(value = "备注") + private String remark; + + +} diff --git a/klp-mes/src/main/java/com/klp/mes/excoil/mapper/MesExCoilMapper.java b/klp-mes/src/main/java/com/klp/mes/excoil/mapper/MesExCoilMapper.java new file mode 100644 index 00000000..640ce386 --- /dev/null +++ b/klp-mes/src/main/java/com/klp/mes/excoil/mapper/MesExCoilMapper.java @@ -0,0 +1,17 @@ +package com.klp.mes.excoil.mapper; + +import com.klp.mes.excoil.domain.MesExCoil; +import com.klp.mes.excoil.domain.vo.MesExCoilVo; +import com.klp.common.core.mapper.BaseMapperPlus; +import java.util.Date; + +/** + * 出口卷数据同步Mapper接口 + * + * @author klp + * @date 2026-05-21 + */ +public interface MesExCoilMapper extends BaseMapperPlus { + + Date getMaxInsdate(); +} diff --git a/klp-mes/src/main/java/com/klp/mes/excoil/service/IMesExCoilService.java b/klp-mes/src/main/java/com/klp/mes/excoil/service/IMesExCoilService.java new file mode 100644 index 00000000..348a4736 --- /dev/null +++ b/klp-mes/src/main/java/com/klp/mes/excoil/service/IMesExCoilService.java @@ -0,0 +1,55 @@ +package com.klp.mes.excoil.service; + +import com.klp.mes.excoil.domain.MesExCoil; +import com.klp.mes.excoil.domain.vo.MesExCoilVo; +import com.klp.mes.excoil.domain.bo.MesExCoilBo; +import com.klp.common.core.page.TableDataInfo; +import com.klp.common.core.domain.PageQuery; + +import java.util.Collection; +import java.util.List; +import java.util.Map; + +/** + * 出口卷数据同步Service接口 + * + * @author klp + * @date 2026-05-21 + */ +public interface IMesExCoilService { + + /** + * 查询出口卷数据同步 + */ + MesExCoilVo queryById(Long exId); + + /** + * 查询出口卷数据同步列表 + */ + TableDataInfo queryPageList(MesExCoilBo bo, PageQuery pageQuery); + + /** + * 查询出口卷数据同步列表 + */ + List queryList(MesExCoilBo bo); + + /** + * 新增出口卷数据同步 + */ + Boolean insertByBo(MesExCoilBo bo); + + /** + * 修改出口卷数据同步 + */ + Boolean updateByBo(MesExCoilBo bo); + + /** + * 校验并批量删除出口卷数据同步信息 + */ + Boolean deleteWithValidByIds(Collection ids, Boolean isValid); + + /** + * 从 L2 系统同步出口卷实绩数据 + */ + Map syncExCoilData(); +} diff --git a/klp-mes/src/main/java/com/klp/mes/excoil/service/impl/MesExCoilServiceImpl.java b/klp-mes/src/main/java/com/klp/mes/excoil/service/impl/MesExCoilServiceImpl.java new file mode 100644 index 00000000..78a15f46 --- /dev/null +++ b/klp-mes/src/main/java/com/klp/mes/excoil/service/impl/MesExCoilServiceImpl.java @@ -0,0 +1,303 @@ +package com.klp.mes.excoil.service.impl; + +import cn.hutool.core.bean.BeanUtil; +import com.klp.common.core.page.TableDataInfo; +import com.klp.common.core.domain.PageQuery; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.klp.common.utils.StringUtils; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; +import com.klp.mes.excoil.domain.bo.MesExCoilBo; +import com.klp.mes.excoil.domain.vo.MesExCoilVo; +import com.klp.mes.excoil.domain.MesExCoil; +import com.klp.mes.excoil.mapper.MesExCoilMapper; +import com.klp.mes.excoil.service.IMesExCoilService; + +import java.math.BigDecimal; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@RequiredArgsConstructor +@Service +public class MesExCoilServiceImpl implements IMesExCoilService { + + private final MesExCoilMapper baseMapper; + + @Value("${server.port:8080}") + private int serverPort; + + private RestTemplate restTemplate; + + private RestTemplate getRestTemplate() { + if (restTemplate == null) { + SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); + factory.setConnectTimeout(5000); + factory.setReadTimeout(600000); + restTemplate = new RestTemplate(factory); + } + return restTemplate; + } + + private String getInternalApiBaseUrl() { + return "http://127.0.0.1:" + serverPort; + } + + @Override + public MesExCoilVo queryById(Long exId){ + return baseMapper.selectVoById(exId); + } + + @Override + public TableDataInfo queryPageList(MesExCoilBo bo, PageQuery pageQuery) { + LambdaQueryWrapper lqw = buildQueryWrapper(bo); + Page result = baseMapper.selectVoPage(pageQuery.build(), lqw); + return TableDataInfo.build(result); + } + + @Override + public List queryList(MesExCoilBo bo) { + LambdaQueryWrapper lqw = buildQueryWrapper(bo); + return baseMapper.selectVoList(lqw); + } + + private LambdaQueryWrapper buildQueryWrapper(MesExCoilBo bo) { + Map params = bo.getParams(); + LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); + lqw.eq(StringUtils.isNotBlank(bo.getExcoilid()), MesExCoil::getExcoilid, bo.getExcoilid()); + lqw.eq(StringUtils.isNotBlank(bo.getEncoilid()), MesExCoil::getEncoilid, bo.getEncoilid()); + lqw.eq(StringUtils.isNotBlank(bo.getHotCoilid()), MesExCoil::getHotCoilid, bo.getHotCoilid()); + lqw.eq(StringUtils.isNotBlank(bo.getGrade()), MesExCoil::getGrade, bo.getGrade()); + lqw.eq(StringUtils.isNotBlank(bo.getOrderQuality()), MesExCoil::getOrderQuality, bo.getOrderQuality()); + lqw.eq(StringUtils.isNotBlank(bo.getProductType()), MesExCoil::getProductType, bo.getProductType()); + lqw.eq(StringUtils.isNotBlank(bo.getStatus()), MesExCoil::getStatus, bo.getStatus()); + lqw.eq(StringUtils.isNotBlank(bo.getShift()), MesExCoil::getShift, bo.getShift()); + lqw.eq(StringUtils.isNotBlank(bo.getParkType()), MesExCoil::getParkType, bo.getParkType()); + lqw.eq(StringUtils.isNotBlank(bo.getSideTrim()), MesExCoil::getSideTrim, bo.getSideTrim()); + lqw.eq(bo.getEntryThick() != null, MesExCoil::getEntryThick, bo.getEntryThick()); + lqw.eq(bo.getEntryWidth() != null, MesExCoil::getEntryWidth, bo.getEntryWidth()); + lqw.eq(bo.getEntryWeight() != null, MesExCoil::getEntryWeight, bo.getEntryWeight()); + lqw.eq(bo.getUsedEntryWeight() != null, MesExCoil::getUsedEntryWeight, bo.getUsedEntryWeight()); + lqw.eq(bo.getExitThick() != null, MesExCoil::getExitThick, bo.getExitThick()); + lqw.eq(bo.getExitWidth() != null, MesExCoil::getExitWidth, bo.getExitWidth()); + lqw.eq(bo.getExitLength() != null, MesExCoil::getExitLength, bo.getExitLength()); + lqw.eq(bo.getExitWeight() != null, MesExCoil::getExitWeight, bo.getExitWeight()); + lqw.eq(bo.getCalcExitWeight() != null, MesExCoil::getCalcExitWeight, bo.getCalcExitWeight()); + lqw.eq(bo.getMeasExitWeight() != null, MesExCoil::getMeasExitWeight, bo.getMeasExitWeight()); + lqw.eq(bo.getExitPosDev() != null, MesExCoil::getExitPosDev, bo.getExitPosDev()); + lqw.eq(bo.getExitNegDev() != null, MesExCoil::getExitNegDev, bo.getExitNegDev()); + lqw.eq(bo.getInnerDiameter() != null, MesExCoil::getInnerDiameter, bo.getInnerDiameter()); + lqw.eq(bo.getOuterDiameter() != null, MesExCoil::getOuterDiameter, bo.getOuterDiameter()); + lqw.eq(bo.getHeadpos() != null, MesExCoil::getHeadpos, bo.getHeadpos()); + lqw.eq(bo.getTailpos() != null, MesExCoil::getTailpos, bo.getTailpos()); + lqw.eq(bo.getQuality() != null, MesExCoil::getQuality, bo.getQuality()); + lqw.eq(bo.getShapeQuality() != null, MesExCoil::getShapeQuality, bo.getShapeQuality()); + lqw.eq(StringUtils.isNotBlank(bo.getCrew()), MesExCoil::getCrew, bo.getCrew()); + lqw.eq(bo.getReportFlag() != null, MesExCoil::getReportFlag, bo.getReportFlag()); + lqw.eq(bo.getSubid() != null, MesExCoil::getSubid, bo.getSubid()); + lqw.eq(bo.getRn() != null, MesExCoil::getRn, bo.getRn()); + lqw.eq(bo.getOnlineDate() != null, MesExCoil::getOnlineDate, bo.getOnlineDate()); + lqw.eq(bo.getStartDate() != null, MesExCoil::getStartDate, bo.getStartDate()); + lqw.eq(bo.getEndDate() != null, MesExCoil::getEndDate, bo.getEndDate()); + lqw.eq(bo.getWeldedDate() != null, MesExCoil::getWeldedDate, bo.getWeldedDate()); + lqw.eq(bo.getInsdate() != null, MesExCoil::getInsdate, bo.getInsdate()); + lqw.eq(bo.getSyncTime() != null, MesExCoil::getSyncTime, bo.getSyncTime()); + lqw.eq(StringUtils.isNotBlank(bo.getComments()), MesExCoil::getComments, bo.getComments()); + return lqw; + } + + @Override + public Boolean insertByBo(MesExCoilBo bo) { + MesExCoil add = BeanUtil.toBean(bo, MesExCoil.class); + validEntityBeforeSave(add); + boolean flag = baseMapper.insert(add) > 0; + if (flag) { + bo.setExId(add.getExId()); + } + return flag; + } + + @Override + public Boolean updateByBo(MesExCoilBo bo) { + MesExCoil update = BeanUtil.toBean(bo, MesExCoil.class); + validEntityBeforeSave(update); + return baseMapper.updateById(update) > 0; + } + + private void validEntityBeforeSave(MesExCoil entity){ + } + + @Override + public Boolean deleteWithValidByIds(Collection ids, Boolean isValid) { + if(isValid){ + } + return baseMapper.deleteBatchIds(ids) > 0; + } + + @Override + public Map syncExCoilData() { + Date maxInsdate = baseMapper.getMaxInsdate(); + boolean isFullSync = (maxInsdate == null); + + List> apiRows; + if (isFullSync) { + apiRows = fetchAllFromApi(); + } else { + apiRows = fetchIncrementalFromApi(maxInsdate); + } + + int insertCount = 0; + int updateCount = 0; + Date now = new Date(); + + for (Map row : apiRows) { + MesExCoil entity = mapRowToEntity(row); + entity.setSyncTime(now); + + LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); + wrapper.eq(MesExCoil::getExcoilid, entity.getExcoilid()); + MesExCoil existing = baseMapper.selectOne(wrapper); + + if (existing != null) { + entity.setExId(existing.getExId()); + baseMapper.updateById(entity); + updateCount++; + } else { + baseMapper.insert(entity); + insertCount++; + } + } + + Map result = new HashMap<>(); + result.put("totalFetched", apiRows.size()); + result.put("insertCount", insertCount); + result.put("updateCount", updateCount); + result.put("fullSync", isFullSync); + return result; + } + + @SuppressWarnings("unchecked") + private List> fetchAllFromApi() { + List> all = new ArrayList<>(); + int page = 1; + int pageSize = 500; + String baseUrl = getInternalApiBaseUrl(); + + while (true) { + String url = baseUrl + "/sql-server-api/excoil?page=" + page + "&pageSize=" + pageSize; + Map response = getRestTemplate().getForObject(url, Map.class); + if (response == null) break; + + Map data = (Map) response.get("data"); + if (data == null) break; + + List> rows = (List>) data.get("rows"); + if (rows == null || rows.isEmpty()) break; + + all.addAll(rows); + if (rows.size() < pageSize) break; + page++; + } + return all; + } + + @SuppressWarnings("unchecked") + private List> fetchIncrementalFromApi(Date since) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String startTime = sdf.format(since); + String baseUrl = getInternalApiBaseUrl(); + String url = baseUrl + "/sql-server-api/excoil/by-insdate?startTime=" + startTime; + + Map response = getRestTemplate().getForObject(url, Map.class); + if (response != null && response.get("data") instanceof List) { + return (List>) response.get("data"); + } + return new ArrayList<>(); + } + + private MesExCoil mapRowToEntity(Map row) { + MesExCoil entity = new MesExCoil(); + entity.setExcoilid(str(row.get("excoilid"))); + entity.setEncoilid(str(row.get("encoilid"))); + entity.setHotCoilid(str(row.get("hot_coilid"))); + entity.setGrade(str(row.get("grade"))); + entity.setOrderQuality(str(row.get("order_quality"))); + entity.setProductType(str(row.get("product_type"))); + entity.setStatus(str(row.get("status"))); + entity.setShift(str(row.get("shift"))); + entity.setParkType(str(row.get("park_type"))); + entity.setSideTrim(str(row.get("side_trim"))); + entity.setEntryThick(bd(row.get("entry_thick"))); + entity.setEntryWidth(bd(row.get("entry_width"))); + entity.setEntryWeight(bd(row.get("entry_weight"))); + entity.setUsedEntryWeight(bd(row.get("used_entry_weight"))); + entity.setExitThick(bd(row.get("exit_thick"))); + entity.setExitWidth(bd(row.get("exit_width"))); + entity.setExitLength(bd(row.get("exit_length"))); + entity.setExitWeight(bd(row.get("exit_weight"))); + entity.setCalcExitWeight(bd(row.get("calc_exit_weight"))); + entity.setMeasExitWeight(bd(row.get("meas_exit_weight"))); + entity.setExitPosDev(bd(row.get("exit_pos_dev"))); + entity.setExitNegDev(bd(row.get("exit_neg_dev"))); + entity.setInnerDiameter(bd(row.get("inner_diameter"))); + entity.setOuterDiameter(bd(row.get("outer_diameter"))); + entity.setHeadpos(bd(row.get("headpos"))); + entity.setTailpos(bd(row.get("tailpos"))); + entity.setQuality(bd(row.get("quality"))); + entity.setShapeQuality(bd(row.get("shape_quality"))); + entity.setCrew(str(row.get("crew"))); + entity.setReportFlag(longVal(row.get("report_flag"))); + entity.setSubid(longVal(row.get("subid"))); + entity.setRn(longVal(row.get("rn"))); + entity.setOnlineDate(parseDate(row.get("online_date"))); + entity.setStartDate(parseDate(row.get("start_date"))); + entity.setEndDate(parseDate(row.get("end_date"))); + entity.setWeldedDate(parseDate(row.get("welded_date"))); + entity.setInsdate(parseDate(row.get("insdate"))); + entity.setComments(str(row.get("comments"))); + entity.setRemark(str(row.get("remark"))); + return entity; + } + + private String str(Object value) { + return value == null ? null : String.valueOf(value); + } + + private BigDecimal bd(Object value) { + if (value == null) return null; + if (value instanceof BigDecimal) return (BigDecimal) value; + if (value instanceof Number) return BigDecimal.valueOf(((Number) value).doubleValue()); + try { return new BigDecimal(String.valueOf(value)); } catch (Exception e) { return null; } + } + + private Long longVal(Object value) { + if (value == null) return null; + if (value instanceof Number) return ((Number) value).longValue(); + try { return Long.parseLong(String.valueOf(value)); } catch (Exception e) { return null; } + } + + private Date parseDate(Object value) { + if (value == null) return null; + if (value instanceof Date) return (Date) value; + String s = String.valueOf(value); + if (s.isEmpty() || "null".equals(s)) return null; + try { + if (s.contains("T")) { + s = s.replace("T", " "); + if (s.length() > 19) s = s.substring(0, 19); + } + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s); + } catch (Exception e) { + return null; + } + } +} diff --git a/klp-mes/src/main/resources/mapper/excoil/MesExCoilMapper.xml b/klp-mes/src/main/resources/mapper/excoil/MesExCoilMapper.xml new file mode 100644 index 00000000..d7abc93e --- /dev/null +++ b/klp-mes/src/main/resources/mapper/excoil/MesExCoilMapper.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +