格式化检验数据创建时间

This commit is contained in:
liuchengqian 2022-05-17 11:03:34 +08:00
parent 9e2a28914e
commit 68f56278a5
3 changed files with 40 additions and 20 deletions

View File

@ -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<QcSourceEntity, Integer>, 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<QcSourceEntity> 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);
}

View File

@ -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<QcSourceEntity> 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 + "";
}
}

View File

@ -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");
}
}