|
|
@@ -1,5 +1,6 @@
|
|
|
package org.dromara.risk.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
@@ -17,6 +18,8 @@ import org.dromara.risk.mapper.RiskDangerSourceMapper;
|
|
|
import org.dromara.risk.service.IRiskDangerSourceService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@@ -90,12 +93,42 @@ public class RiskDangerSourceServiceImpl implements IRiskDangerSourceService {
|
|
|
*/
|
|
|
@Override
|
|
|
public int insertByBo(RiskDangerSourceBo bo) {
|
|
|
+ // 上报时间前端不传时取当前时间
|
|
|
+ if (bo.getReportTime() == null) {
|
|
|
+ bo.setReportTime(new Date());
|
|
|
+ }
|
|
|
+ // 危险源编号:前端未传时系统自动生成(避免 UNIQUE KEY uk_source_code 冲突)
|
|
|
+ if (StringUtils.isBlank(bo.getSourceCode())) {
|
|
|
+ bo.setSourceCode(generateSourceCode());
|
|
|
+ }
|
|
|
RiskDangerSource add = MapstructUtils.convert(bo, RiskDangerSource.class);
|
|
|
int row = baseMapper.insert(add);
|
|
|
bo.setId(add.getId());
|
|
|
return row;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 生成危险源编号:DAS + yyyyMMddHHmmss + 3位随机数
|
|
|
+ * 冲突自动重试 5 次,极端情况用 UUID 兜底,保证 UNIQUE KEY uk_source_code 不冲突
|
|
|
+ */
|
|
|
+ private String generateSourceCode() {
|
|
|
+ String prefix = "DAS";
|
|
|
+ String ts = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
|
|
|
+ java.util.concurrent.ThreadLocalRandom random = java.util.concurrent.ThreadLocalRandom.current();
|
|
|
+ for (int i = 0; i < 5; i++) {
|
|
|
+ String suffix = StringUtils.leftPad(
|
|
|
+ Integer.toString(random.nextInt(1000)), 3, '0');
|
|
|
+ String code = prefix + ts + suffix;
|
|
|
+ Long exists = baseMapper.selectCount(Wrappers.<RiskDangerSource>lambdaQuery()
|
|
|
+ .eq(RiskDangerSource::getSourceCode, code));
|
|
|
+ if (exists == null || exists == 0) {
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 极端情况:同秒生成的 5 个都撞了,加 UUID 后缀兜底
|
|
|
+ return prefix + ts + IdUtil.fastSimpleUUID().substring(0, 4).toUpperCase();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 修改危险源
|
|
|
*/
|