|
|
|
@ -39,6 +39,8 @@ import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import java.time.LocalDate; |
|
|
|
import java.time.ZoneId; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Date; |
|
|
|
@ -488,6 +490,60 @@ public class RohsServiceImpl extends ServiceImpl<RohsMapper, RohsEntity> impleme |
|
|
|
rohsMaterialService.remove(removeWrapper); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional |
|
|
|
public RohsEntity updateCompletedExpiryInfo(RohsEntity rohs, String userName) { |
|
|
|
if (rohs == null || StringUtils.isBlank(rohs.getSite()) || StringUtils.isBlank(rohs.getReferenceNo())) { |
|
|
|
throw new RuntimeException("工厂(site)和序列号(referenceNo)不能为空"); |
|
|
|
} |
|
|
|
RohsEntity exists = this.getDetail(rohs.getSite(), rohs.getReferenceNo()); |
|
|
|
if (exists == null) { |
|
|
|
throw new RuntimeException("未找到对应RoHs单据"); |
|
|
|
} |
|
|
|
if (!"已完成".equals(exists.getStatus())) { |
|
|
|
throw new RuntimeException("仅已完成状态的单据允许更新失效日期"); |
|
|
|
} |
|
|
|
if (!"Y".equalsIgnoreCase(StringUtils.trimToEmpty(exists.getIsExpiring()))) { |
|
|
|
throw new RuntimeException("当前单据非即将失效状态,不允许更新失效日期"); |
|
|
|
} |
|
|
|
if (rohs.getExpiredDate() == null) { |
|
|
|
throw new RuntimeException("报告日期不能为空"); |
|
|
|
} |
|
|
|
Integer validUntilValue = rohs.getValidUntilValue(); |
|
|
|
if (validUntilValue == null || validUntilValue <= 0) { |
|
|
|
throw new RuntimeException("有效期数值必须大于0"); |
|
|
|
} |
|
|
|
String validUntil = StringUtils.trimToEmpty(rohs.getValidUntil()); |
|
|
|
if (StringUtils.isBlank(validUntil)) { |
|
|
|
throw new RuntimeException("有效期单位不能为空"); |
|
|
|
} |
|
|
|
|
|
|
|
Date expiryDate = calculateExpiryDateFromRule(rohs.getExpiredDate(), validUntilValue, validUntil); |
|
|
|
if (expiryDate == null) { |
|
|
|
throw new RuntimeException("有效期单位不合法,无法计算失效日期"); |
|
|
|
} |
|
|
|
|
|
|
|
RohsEntity updateEntity = new RohsEntity(); |
|
|
|
updateEntity.setExpiredDate(rohs.getExpiredDate()); |
|
|
|
updateEntity.setValidUntilValue(validUntilValue); |
|
|
|
updateEntity.setValidUntil(validUntil); |
|
|
|
updateEntity.setExpiryDate(expiryDate); |
|
|
|
updateEntity.setIsExpiring(isAboutToExpire(expiryDate) ? "Y" : "N"); |
|
|
|
updateEntity.setUpdateDate(new Date()); |
|
|
|
updateEntity.setUpdateBy(StringUtils.isNotBlank(userName) ? userName : exists.getUpdateBy()); |
|
|
|
|
|
|
|
QueryWrapper<RohsEntity> updateWrapper = new QueryWrapper<>(); |
|
|
|
updateWrapper.eq("site", rohs.getSite()).eq("reference_no", rohs.getReferenceNo()); |
|
|
|
this.update(updateEntity, updateWrapper); |
|
|
|
return this.getDetail(rohs.getSite(), rohs.getReferenceNo()); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional |
|
|
|
public int refreshAboutToExpireFlag() { |
|
|
|
return this.baseMapper.refreshAboutToExpireFlag(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 流程干预提交(节点更新/流程干预) |
|
|
|
*/ |
|
|
|
@ -746,4 +802,79 @@ public class RohsServiceImpl extends ServiceImpl<RohsMapper, RohsEntity> impleme |
|
|
|
rohs.setHsfStandard(""); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private Date calculateExpiryDateFromRule(Date expiredDate, Integer validUntilValue, String validUntilUnit) { |
|
|
|
if (expiredDate == null || validUntilValue == null || validUntilValue <= 0) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
String unitType = normalizeValidUntilUnitType(validUntilUnit); |
|
|
|
if (StringUtils.isBlank(unitType)) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
LocalDate baseDate = toLocalDate(expiredDate); |
|
|
|
if (baseDate == null) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
LocalDate resultDate; |
|
|
|
switch (unitType) { |
|
|
|
case "day": |
|
|
|
resultDate = baseDate.plusDays(validUntilValue.longValue()); |
|
|
|
break; |
|
|
|
case "week": |
|
|
|
resultDate = baseDate.plusWeeks(validUntilValue.longValue()); |
|
|
|
break; |
|
|
|
case "month": |
|
|
|
resultDate = baseDate.plusMonths(validUntilValue.longValue()); |
|
|
|
break; |
|
|
|
case "year": |
|
|
|
resultDate = baseDate.plusYears(validUntilValue.longValue()); |
|
|
|
break; |
|
|
|
default: |
|
|
|
return null; |
|
|
|
} |
|
|
|
return java.sql.Date.valueOf(resultDate); |
|
|
|
} |
|
|
|
|
|
|
|
private String normalizeValidUntilUnitType(String unitValue) { |
|
|
|
String text = StringUtils.trimToEmpty(unitValue).toLowerCase(); |
|
|
|
if (StringUtils.isBlank(text)) { |
|
|
|
return ""; |
|
|
|
} |
|
|
|
if ("d".equals(text) || "day".equals(text) || "days".equals(text) |
|
|
|
|| text.contains("日") || text.contains("天")) { |
|
|
|
return "day"; |
|
|
|
} |
|
|
|
if ("w".equals(text) || "week".equals(text) || "weeks".equals(text) || text.contains("周")) { |
|
|
|
return "week"; |
|
|
|
} |
|
|
|
if ("m".equals(text) || "month".equals(text) || "months".equals(text) || text.contains("月")) { |
|
|
|
return "month"; |
|
|
|
} |
|
|
|
if ("y".equals(text) || "year".equals(text) || "years".equals(text) || text.contains("年")) { |
|
|
|
return "year"; |
|
|
|
} |
|
|
|
return ""; |
|
|
|
} |
|
|
|
|
|
|
|
private boolean isAboutToExpire(Date expiryDate) { |
|
|
|
LocalDate expiryLocalDate = toLocalDate(expiryDate); |
|
|
|
if (expiryLocalDate == null) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
LocalDate limitDate = LocalDate.now().plusMonths(1); |
|
|
|
return !expiryLocalDate.isAfter(limitDate); |
|
|
|
} |
|
|
|
|
|
|
|
private LocalDate toLocalDate(Date date) { |
|
|
|
if (date == null) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
// java.sql.Date may throw UnsupportedOperationException on toInstant() |
|
|
|
if (date instanceof java.sql.Date) { |
|
|
|
return ((java.sql.Date) date).toLocalDate(); |
|
|
|
} |
|
|
|
return java.time.Instant.ofEpochMilli(date.getTime()) |
|
|
|
.atZone(ZoneId.systemDefault()) |
|
|
|
.toLocalDate(); |
|
|
|
} |
|
|
|
} |