编码配置

master
lzq 2025-12-20 17:22:16 +08:00
parent c6385f4c06
commit 3f13341a3b
11 changed files with 95 additions and 131 deletions

View File

@ -31,12 +31,18 @@ public class SnConfigController {
return R.success();
}
/**
*
*/
@GetMapping("/next")
public R<?> next(@RequestParam(required = false, defaultValue = "Default") String sncode,
@RequestParam(required = false) String pici) {
public R<?> next(@RequestParam(value = "sncode", required = false, defaultValue = "Default") String sncode,
@RequestParam(value = "pici", required = false) String pici) {
return R.success(SnUtil.next(sncode, pici));
}
/**
*
*/
@GetMapping("/reset")
public R<?> reset(@RequestParam("sncode") String sncode) {
snConfigService.reset(sncode);

View File

@ -32,7 +32,7 @@ import java.util.stream.Collectors;
@Slf4j
@RequiredArgsConstructor
public class SnConfigService extends ServiceImpl<SnConfigMapper, SnConfigEntity> implements IService<SnConfigEntity> {
private final IncSnService incSnService;
private final SnIncService snIncService;
@Transactional(rollbackFor = Exception.class)
public Sn getSn(String sncode) {
@ -51,7 +51,7 @@ public class SnConfigService extends ServiceImpl<SnConfigMapper, SnConfigEntity>
boolean exists = this.exists(Wrappers.<SnConfigEntity>lambdaQuery().eq(SnConfigEntity::getSncode, sncode));
Assert.isFalse(exists, () -> Exceptions.exception("编码配置已存在:{}", sncode));
List<HashMap<String, Object>> configList = addSnConfigParam.getConfig();
Tuple2<List<SectionConfig>, List<IncSnEntity>> tuple2 = resolveConfig(configList);
Tuple2<List<SectionConfig>, List<SnIncEntity>> tuple2 = resolveConfig(configList);
List<SectionConfig> configs = tuple2.get_0();
String example = configs.stream().map(SectionConfig::genExample).collect(Collectors.joining());
@ -63,22 +63,22 @@ public class SnConfigService extends ServiceImpl<SnConfigMapper, SnConfigEntity>
.setMemo(addSnConfigParam.getMemo())
);
saveIncSnEntityList(tuple2.get_1());
saveSnIncEntityList(tuple2.get_1());
}
private Tuple2<List<SectionConfig>, List<IncSnEntity>> resolveConfig(List<HashMap<String, Object>> configList) {
private Tuple2<List<SectionConfig>, List<SnIncEntity>> resolveConfig(List<HashMap<String, Object>> configList) {
Assert.notEmpty(configList, () -> Exceptions.exception("编码配置不能为空"));
List<SectionConfig> configs = new ArrayList<>(configList.size());
List<IncSnEntity> incSnEntityList = new LinkedList<>();
Set<String> incSnEntityCodeList = new HashSet<>();
List<SnIncEntity> snIncEntityList = new LinkedList<>();
Set<String> snIncEntityCodeList = new HashSet<>();
for (HashMap<String, Object> config : configList) {
SectionConfig sectionConfig = SnUtil.resolve(config);
if (sectionConfig instanceof IncSectionConfig incSectionConfig) {
PadMode padMode = incSectionConfig.getPadMode();
String code = incSectionConfig.getCode();
Assert.isFalse(incSnEntityCodeList.contains(code), () -> Exceptions.exception("编码配置已存在:{}", code));
incSnEntityCodeList.add(code);
incSnEntityList.add(new IncSnEntity()
Assert.isFalse(snIncEntityCodeList.contains(code), () -> Exceptions.exception("编码配置已存在:{}", code));
snIncEntityCodeList.add(code);
snIncEntityList.add(new SnIncEntity()
.setCode(code)
.setVal((long) incSectionConfig.getInitialVal())
.setStep(incSectionConfig.getStep())
@ -87,21 +87,21 @@ public class SnConfigService extends ServiceImpl<SnConfigMapper, SnConfigEntity>
.setPadMode(padMode)
.setPadVal(incSectionConfig.getPadVal())
.setPadLen(incSectionConfig.getPadLen())
.setRollback(incSectionConfig.getRollback())
.setRollbackMode(incSectionConfig.getRollbackMode())
.setAllowOverflow(incSectionConfig.getAllowOverflow()));
}
configs.add(sectionConfig);
}
return Tuple2.of(configs, incSnEntityList);
return Tuple2.of(configs, snIncEntityList);
}
private void saveIncSnEntityList(List<IncSnEntity> incSnEntityList) {
if (incSnEntityList.isEmpty()) return;
List<String> incSnCodes = incSnEntityList.stream().map(IncSnEntity::getCode).collect(Collectors.toList());
boolean exists = incSnService.exists(Wrappers.<IncSnEntity>lambdaQuery().in(IncSnEntity::getCode, incSnCodes));
Assert.isFalse(exists, () -> Exceptions.exception("编码配置已存在:{}", incSnCodes));
incSnService.saveBatch(incSnEntityList);
private void saveSnIncEntityList(List<SnIncEntity> snIncEntityList) {
if (snIncEntityList.isEmpty()) return;
List<String> snIncCodes = snIncEntityList.stream().map(SnIncEntity::getCode).collect(Collectors.toList());
boolean exists = snIncService.exists(Wrappers.<SnIncEntity>lambdaQuery().in(SnIncEntity::getCode, snIncCodes));
Assert.isFalse(exists, () -> Exceptions.exception("编码配置已存在:{}", snIncCodes));
snIncService.saveBatch(snIncEntityList);
}
/**
@ -114,18 +114,18 @@ public class SnConfigService extends ServiceImpl<SnConfigMapper, SnConfigEntity>
Assert.notNull(oldData, () -> Exceptions.exception("编码配置不存在"));
List<SectionConfig> oldConfig = oldData.getConfig();
List<HashMap<String, Object>> newConfig = modifySnConfigParam.getConfig();
Tuple2<List<SectionConfig>, List<IncSnEntity>> tuple2 = resolveConfig(newConfig);
Tuple2<List<SectionConfig>, List<SnIncEntity>> tuple2 = resolveConfig(newConfig);
List<String> oldIncSectionConfigCodeList = oldConfig.stream()
.filter(it -> it instanceof IncSectionConfig)
.map(it -> ((IncSectionConfig) it).getCode())
.collect(Collectors.toList());
List<IncSnEntity> oldIncSnEntityList;
List<SnIncEntity> oldSnIncEntityList;
if (CollUtil.isEmpty(oldIncSectionConfigCodeList)) {
oldIncSnEntityList = Collections.emptyList();
oldSnIncEntityList = Collections.emptyList();
} else {
oldIncSnEntityList = incSnService.list(Wrappers.lambdaQuery(IncSnEntity.class).in(IncSnEntity::getCode, oldIncSectionConfigCodeList));
oldSnIncEntityList = snIncService.list(Wrappers.lambdaQuery(SnIncEntity.class).in(SnIncEntity::getCode, oldIncSectionConfigCodeList));
}
modifyIncSnSectionConfig(oldIncSnEntityList, tuple2.get_1());
modifySnIncSectionConfig(oldSnIncEntityList, tuple2.get_1());
List<SectionConfig> configs = tuple2.get_0();
String example = configs.stream().map(SectionConfig::genExample).collect(Collectors.joining());
this.updateById(new SnConfigEntity()
@ -138,35 +138,35 @@ public class SnConfigService extends ServiceImpl<SnConfigMapper, SnConfigEntity>
);
}
private void modifyIncSnSectionConfig(List<IncSnEntity> oldIncSnEntityList, List<IncSnEntity> newIncSnEntityList) {
if (oldIncSnEntityList.isEmpty() && newIncSnEntityList.isEmpty()) return;
private void modifySnIncSectionConfig(List<SnIncEntity> oldSnIncEntityList, List<SnIncEntity> newSnIncEntityList) {
if (oldSnIncEntityList.isEmpty() && newSnIncEntityList.isEmpty()) return;
// 旧无 新有 --> 新增
List<IncSnEntity> addList = difference(newIncSnEntityList, oldIncSnEntityList);
if (CollUtil.isNotEmpty(addList)) saveIncSnEntityList(addList);
List<SnIncEntity> addList = difference(newSnIncEntityList, oldSnIncEntityList);
if (CollUtil.isNotEmpty(addList)) saveSnIncEntityList(addList);
// 旧有 新有 --> 修改
List<IncSnEntity> modList = intersection(oldIncSnEntityList, newIncSnEntityList);
if (CollUtil.isNotEmpty(modList)) incSnService.updateBatchById(modList);
List<SnIncEntity> modList = intersection(oldSnIncEntityList, newSnIncEntityList);
if (CollUtil.isNotEmpty(modList)) snIncService.updateBatchById(modList);
// 旧有 新无 --> 删除
List<IncSnEntity> delList = difference(oldIncSnEntityList, newIncSnEntityList);
if (CollUtil.isNotEmpty(delList)) incSnService.removeBatchByIds(delList);
List<SnIncEntity> delList = difference(oldSnIncEntityList, newSnIncEntityList);
if (CollUtil.isNotEmpty(delList)) snIncService.removeBatchByIds(delList);
}
private List<IncSnEntity> difference(List<IncSnEntity> list1, List<IncSnEntity> list2) {
List<String> codes = list2.stream().map(IncSnEntity::getCode).collect(Collectors.toList());
private List<SnIncEntity> difference(List<SnIncEntity> list1, List<SnIncEntity> list2) {
List<String> codes = list2.stream().map(SnIncEntity::getCode).collect(Collectors.toList());
return list1.stream()
.filter(it -> !codes.contains(it.getCode()))
.collect(Collectors.toList());
}
private List<IncSnEntity> intersection(List<IncSnEntity> list1, List<IncSnEntity> list2) {
Map<String, IncSnEntity> map = GroupUtil.k_o(list2, IncSnEntity::getCode);
private List<SnIncEntity> intersection(List<SnIncEntity> list1, List<SnIncEntity> list2) {
Map<String, SnIncEntity> map = GroupUtil.k_o(list2, SnIncEntity::getCode);
return list1.stream()
.filter(it -> map.containsKey(it.getCode()))
.map(it -> {
IncSnEntity newData = map.get(it.getCode());
SnIncEntity newData = map.get(it.getCode());
PadMode padMode = newData.getPadMode();
Integer padLen = newData.getPadLen();
return new IncSnEntity()
return new SnIncEntity()
.setId(it.getId())
.setStep(newData.getStep())
.setInitialVal(newData.getInitialVal())
@ -174,7 +174,7 @@ public class SnConfigService extends ServiceImpl<SnConfigMapper, SnConfigEntity>
.setPadVal(newData.getPadVal())
.setPadLen(padLen)
.setMaxVal(padMode == PadMode.Wu ? Long.MAX_VALUE : (long) (Math.pow(10, padLen) - 1))
.setRollback(newData.getRollback())
.setRollbackMode(newData.getRollbackMode())
.setAllowOverflow(newData.getAllowOverflow());
})
.collect(Collectors.toList());
@ -218,17 +218,17 @@ public class SnConfigService extends ServiceImpl<SnConfigMapper, SnConfigEntity>
.collect(Collectors.toList());
if (CollUtil.isEmpty(codes)) return;
List<IncSnEntity> incSnEntityList = incSnService
.list(Wrappers.lambdaQuery(IncSnEntity.class).in(IncSnEntity::getCode, codes))
.stream().map(it -> BeanUtil.copyProperties(it, IncSnEntity.class)
List<SnIncEntity> snIncEntityList = snIncService
.list(Wrappers.lambdaQuery(SnIncEntity.class).in(SnIncEntity::getCode, codes))
.stream().map(it -> BeanUtil.copyProperties(it, SnIncEntity.class)
.setVal(it.getInitialVal())
.setLastTime(null)
.setLastPici(null)
)
.collect(Collectors.toList());
incSnService.remove(Wrappers.lambdaQuery(IncSnEntity.class).in(IncSnEntity::getCode, codes));
snIncService.remove(Wrappers.lambdaQuery(SnIncEntity.class).in(SnIncEntity::getCode, codes));
incSnService.saveBatch(incSnEntityList);
snIncService.saveBatch(snIncEntityList);
}
}

View File

@ -20,8 +20,8 @@ import java.util.Objects;
@Setter
@ToString
@Accessors(chain = true)
@TableName("sys_inc_sn")
public class IncSnEntity {
@TableName("sys_sn_inc")
public class SnIncEntity {
/**
* Id
@ -70,7 +70,7 @@ public class IncSnEntity {
/**
*
*/
private RollbackMode rollback;
private RollbackMode rollbackMode;
/**
*
*/
@ -87,7 +87,7 @@ public class IncSnEntity {
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
IncSnEntity that = (IncSnEntity) o;
SnIncEntity that = (SnIncEntity) o;
return Objects.equals(code, that.code);
}

View File

@ -7,6 +7,6 @@ import org.apache.ibatis.annotations.Mapper;
*
*/
@Mapper
public interface IncSnMapper extends BaseMapper<IncSnEntity> {
public interface SnIncMapper extends BaseMapper<SnIncEntity> {
}

View File

@ -21,24 +21,24 @@ import java.util.List;
*
*/
@Slf4j
public class IncSnService extends ServiceImpl<IncSnMapper, IncSnEntity> implements IService<IncSnEntity> {
public class SnIncService extends ServiceImpl<SnIncMapper, SnIncEntity> implements IService<SnIncEntity> {
/**
*
*
* @param incSnEntity
* @param snIncEntity
*/
public void add(IncSnEntity incSnEntity) {
this.save(incSnEntity);
public void add(SnIncEntity snIncEntity) {
this.save(snIncEntity);
}
/**
*
*
* @param incSnEntity
* @param snIncEntity
*/
public void modify(IncSnEntity incSnEntity) {
this.updateById(incSnEntity);
public void modify(SnIncEntity snIncEntity) {
this.updateById(snIncEntity);
}
/**
@ -57,25 +57,25 @@ public class IncSnService extends ServiceImpl<IncSnMapper, IncSnEntity> implemen
* @param id Id
* @return SysIncCodeEntity
*/
public IncSnEntity detail(Long id) {
public SnIncEntity detail(Long id) {
return this.getById(id);
}
/**
*
*
* @param incSnEntity
* @param snIncEntity
* @param pageParam
* @return PageResult&lt;SysIncCodeEntity&gt;
*/
public PageResult<IncSnEntity> paging(PageParam pageParam, IncSnEntity incSnEntity) {
return PageResult.of(this.page(pageParam.toPage(), Wrappers.query(incSnEntity)));
public PageResult<SnIncEntity> paging(PageParam pageParam, SnIncEntity snIncEntity) {
return PageResult.of(this.page(pageParam.toPage(), Wrappers.query(snIncEntity)));
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public Long inc(String code, String pici) {
IncSnEntity incCodeEntity = this.getOne(Wrappers.<IncSnEntity>lambdaQuery()
.eq(IncSnEntity::getCode, code));
SnIncEntity incCodeEntity = this.getOne(Wrappers.<SnIncEntity>lambdaQuery()
.eq(SnIncEntity::getCode, code));
Assert.notNull(incCodeEntity, () -> Exceptions.exception("编码规则不存在"));
long newVal = 0;
@ -92,8 +92,8 @@ public class IncSnService extends ServiceImpl<IncSnMapper, IncSnEntity> implemen
}
}
RollbackMode rollback = incCodeEntity.getRollback();
if (rollback == null) rollback = RollbackMode.Wu;
RollbackMode rollbackMode = incCodeEntity.getRollbackMode();
if (rollbackMode == null) rollbackMode = RollbackMode.Wu;
LocalDateTime now = LocalDateTime.now();
LocalDateTime lastTime = incCodeEntity.getLastTime();
@ -101,7 +101,7 @@ public class IncSnService extends ServiceImpl<IncSnMapper, IncSnEntity> implemen
lastTime = now;
}
switch (rollback) {
switch (rollbackMode) {
case Wu: {
newVal = val + incCodeEntity.getStep();
}
@ -193,10 +193,10 @@ public class IncSnService extends ServiceImpl<IncSnMapper, IncSnEntity> implemen
}
break;
default:
throw Exceptions.exception("未知的回卷模式:{}", rollback);
throw Exceptions.exception("未知的回卷模式:{}", rollbackMode);
}
this.updateById(new IncSnEntity().setId(incCodeEntity.getId())
this.updateById(new SnIncEntity().setId(incCodeEntity.getId())
.setLastTime(now)
.setLastPici(pici)
.setVal(newVal));

View File

@ -1,8 +1,8 @@
package com.njzscloud.common.sn.config;
import com.njzscloud.common.sn.IncSnService;
import com.njzscloud.common.sn.SnConfigController;
import com.njzscloud.common.sn.SnConfigService;
import com.njzscloud.common.sn.SnIncService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@ -14,13 +14,13 @@ import org.springframework.context.annotation.Configuration;
public class SnAutoConfiguration {
@Bean
public IncSnService sysIncSnService() {
return new IncSnService();
public SnIncService snIncService() {
return new SnIncService();
}
@Bean
public SnConfigService sysSnConfigService(IncSnService incSnService) {
return new SnConfigService(incSnService);
public SnConfigService sysSnConfigService(SnIncService snIncService) {
return new SnConfigService(snIncService);
}
@Bean

View File

@ -2,7 +2,7 @@ package com.njzscloud.common.sn.support;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.njzscloud.common.sn.IncSnService;
import com.njzscloud.common.sn.SnIncService;
import com.njzscloud.common.sn.contant.PadMode;
import com.njzscloud.common.sn.contant.RollbackMode;
import lombok.RequiredArgsConstructor;
@ -35,11 +35,11 @@ public class IncSection implements ISnSection {
*
*/
private final Integer padLen;
private final IncSnService incSnService = SpringUtil.getBean(IncSnService.class);
private final SnIncService snIncService = SpringUtil.getBean(SnIncService.class);
/**
*
*/
private RollbackMode rollback;
private RollbackMode rollbackMode;
/**
*
*/
@ -47,7 +47,7 @@ public class IncSection implements ISnSection {
@Override
public String next(String pici) {
Long val = incSnService.inc(code, pici);
Long val = snIncService.inc(code, pici);
return padMode == PadMode.Zuo ? StrUtil.padPre(val.toString(), padLen, padVal) :
padMode == PadMode.You ? StrUtil.padAfter(val.toString(), padLen, padVal) :
val.toString();

View File

@ -46,7 +46,7 @@ public class IncSectionConfig implements SectionConfig {
/**
*
*/
private RollbackMode rollback;
private RollbackMode rollbackMode;
/**
*
*/
@ -69,8 +69,8 @@ public class IncSectionConfig implements SectionConfig {
code = MapUtil.getStr(config, "code");
step = MapUtil.getInt(config, "step");
initialVal = MapUtil.getInt(config, "initialVal");
rollback = Dict.parse(MapUtil.getStr(config, "rollback"), RollbackMode.values());
if (rollback == null) rollback = RollbackMode.Wu;
rollbackMode = Dict.parse(MapUtil.getStr(config, "rollback"), RollbackMode.values());
if (rollbackMode == null) rollbackMode = RollbackMode.Wu;
allowOverflow = MapUtil.getBool(config, "allowOverflow", Boolean.TRUE);
padMode = Dict.parse(MapUtil.getStr(config, "padMode"), PadMode.values());
if (padMode == null) padMode = PadMode.Wu;

View File

@ -6,6 +6,7 @@ spring:
security:
auth-allows:
- /oss/**
- /user/register
- /endpoint/reload
- /permission/refresh_cache
- /static/**

View File

@ -3,6 +3,9 @@ server:
tomcat:
max-http-form-post-size: 20MB
spring:
threads:
virtual:
enabled: true
servlet:
multipart:
max-file-size: 20MB

View File

@ -12388,7 +12388,7 @@
"primaryKey": null,
"notNull": 1,
"autoIncrement": null,
"defaultValue": "",
"defaultValue": "''",
"stndDictId": null,
"stndDictKey": null,
"stndFieldId": null,
@ -26456,9 +26456,9 @@
"defKey": "assignment_trans_time",
"defName": "指派清运公司时间",
"intro": "",
"baseDataType": "DECIMAL",
"baseDataType": "DATETIME",
"bizDomainType": "",
"dbDataType": "DECIMAL",
"dbDataType": "DATETIME",
"dataLen": "",
"numScale": "",
"primaryKey": 0,
@ -27290,52 +27290,6 @@
"attr20": "",
"origin": "UI"
},
{
"id": "47B8FDF5-9C6E-4B59-A7AD-4470275E3BCF",
"defKey": "station_id",
"defName": "站点 Id",
"intro": null,
"baseDataType": "BIGINT",
"bizDomainType": "",
"dbDataType": "BIGINT",
"dataLen": null,
"numScale": null,
"primaryKey": 0,
"notNull": 1,
"autoIncrement": 0,
"defaultValue": "",
"stndDictId": null,
"stndFieldId": null,
"stndDictKey": null,
"stndFieldKey": null,
"stndComplianceLevel": null,
"stndComplianceType": null,
"dictFrom": "",
"dictItems": null,
"fieldTier": "",
"mark": null,
"attr1": null,
"attr2": null,
"attr3": null,
"attr4": null,
"attr5": null,
"attr6": null,
"attr7": null,
"attr8": null,
"attr9": null,
"attr10": null,
"attr11": null,
"attr12": null,
"attr13": null,
"attr14": null,
"attr15": null,
"attr16": null,
"attr17": null,
"attr18": null,
"attr19": null,
"attr20": null,
"origin": "PASTE"
},
{
"id": "A74661B9-E5C6-4535-97F7-E66404357A23",
"defKey": "trans_status",
@ -30463,7 +30417,7 @@
"primaryKey": 0,
"notNull": 1,
"autoIncrement": 0,
"defaultValue": "",
"defaultValue": "0",
"stndDictId": "",
"stndFieldId": "",
"stndDictKey": "",
@ -43262,7 +43216,7 @@
"readonly": false,
"allowWs": false
},
"updateTime": 1766138044775,
"signature": "85e73fbec29cde9576a187a502816a5b",
"updateTime": 1766222361035,
"signature": "16d16948c102a427a463bd77aa80e03e",
"branchId": "1111"
}