From 68f56278a5f74337118233f915aa42dcb2118687 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Tue, 17 May 2022 11:03:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E6=A3=80=E9=AA=8C?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=88=9B=E5=BB=BA=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/xkrs/dao/QcSourceDao.java | 6 +++ .../service/impl/QcSourceServiceImpl.java | 51 ++++++++++++------- .../java/com/xkrs/util/LocalDateUtils.java | 3 +- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/xkrs/dao/QcSourceDao.java b/src/main/java/com/xkrs/dao/QcSourceDao.java index 5b6f60b..8cbe307 100644 --- a/src/main/java/com/xkrs/dao/QcSourceDao.java +++ b/src/main/java/com/xkrs/dao/QcSourceDao.java @@ -3,9 +3,11 @@ package com.xkrs.dao; import com.xkrs.model.entity.QcSourceEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Component; +import javax.transaction.Transactional; import java.util.List; /** @@ -17,4 +19,8 @@ public interface QcSourceDao extends JpaRepository, Jpa @Query(value = "SELECT * FROM qc_source WHERE batch_no LIKE %?1% AND machine_no LIKE %?2% AND material_no LIKE %?3% AND mould_no LIKE %?4% AND variety_no LIKE %?5% AND craft_item_no LIKE %?6% ORDER BY create_time DESC", nativeQuery = true) List queryQcSource(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo); + @Transactional(rollbackOn = Exception.class) + @Modifying(clearAutomatically = true) + @Query(value = "UPDATE qc_source SET create_time_format = ?2 WHERE id = ?1", nativeQuery = true) + void formatCreateTime(Integer id, String formatCreateTime); } \ No newline at end of file diff --git a/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java b/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java index 9316720..31f9896 100644 --- a/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java @@ -179,23 +179,6 @@ public class QcSourceServiceImpl implements QcSourceService { return outputEncapsulationObject(PromptMessageEnum.SUCCESS, resultList, locale); } - public static String formatDuring(long mss) { - long days = mss / (1000 * 60 * 60 * 24); - long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); - long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60); - long seconds = (mss % (1000 * 60)) / 1000; - if (days <= 0 && hours <= 0 && minutes <= 0) { - return seconds + " 秒 "; - } - if (days <= 0 && hours <= 0) { - return minutes + " 分钟 " + seconds + " 秒 "; - } - if (days <= 0) { - return hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 "; - } - return days + " 天 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 "; - } - /** * 生成返回结果 * @@ -548,9 +531,39 @@ public class QcSourceServiceImpl implements QcSourceService { */ @Override public String formatCreateTime() { + System.out.println("开始格式化创建时间历史数据"); + List sourceEntityList = qcSourceDao.findAll(); + long startTimeMillis = System.currentTimeMillis(); + int listSize = sourceEntityList.size(); + for (int i = 0; i < listSize; i++) { + QcSourceEntity sourceEntity = sourceEntityList.get(i); + String createTime = sourceEntity.getCreateTime(); + String formatCurrentSecond = LocalDateUtils.formatCurrentSecond(createTime); + qcSourceDao.formatCreateTime(sourceEntity.getId(), formatCurrentSecond); + if (i % 10 == 0) { + long spendTimeMillis = System.currentTimeMillis() - startTimeMillis; + double remainTimeMillis = (listSize - i) * spendTimeMillis * 1D / i; + System.out.println("已完成(" + i + "/" + listSize + "),用时" + formatDuring(spendTimeMillis) + ",剩余" + formatDuring((long) remainTimeMillis)); + } + } + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "格式化创建时间历史数据成功", locale); + } - - return null; + public static String formatDuring(long mss) { + long days = mss / (1000 * 60 * 60 * 24); + long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); + long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60); + long seconds = (mss % (1000 * 60)) / 1000; + if (days <= 0 && hours <= 0 && minutes <= 0) { + return seconds + " 秒 "; + } + if (days <= 0 && hours <= 0) { + return minutes + " 分钟 " + seconds + " 秒 "; + } + if (days <= 0) { + return hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 "; + } + return days + " 天 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 "; } } diff --git a/src/main/java/com/xkrs/util/LocalDateUtils.java b/src/main/java/com/xkrs/util/LocalDateUtils.java index a9b0275..c77d008 100644 --- a/src/main/java/com/xkrs/util/LocalDateUtils.java +++ b/src/main/java/com/xkrs/util/LocalDateUtils.java @@ -14,6 +14,7 @@ public class LocalDateUtils { } public static String formatCurrentSecond(String second) { - return DateUtil.format(new Date(second + "000"), "yyyy-MM-dd HH:mm:ss"); + long timeMillis = Long.parseLong(second + "000"); + return DateUtil.format(new Date(timeMillis), "yyyy-MM-dd HH:mm:ss"); } }