ignore文件
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
package com.ktg.system.strategy;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import com.ktg.common.annotation.Log;
|
||||
import com.ktg.common.core.domain.entity.SysAutoCodePart;
|
||||
import com.ktg.common.core.domain.entity.SysAutoCodeResult;
|
||||
import com.ktg.common.core.domain.entity.SysAutoCodeRule;
|
||||
import com.ktg.common.enums.BusinessType;
|
||||
import com.ktg.common.enums.PartTypeEnum;
|
||||
import com.ktg.common.utils.StringUtils;
|
||||
import com.ktg.system.service.IAutoCodePartService;
|
||||
import com.ktg.system.service.IAutoCodeResultService;
|
||||
import com.ktg.system.service.IAutoCodeRuleService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class AutoCodeUtil {
|
||||
public static ThreadLocal<Boolean> threadLocal = new ThreadLocal<>();
|
||||
|
||||
@Autowired
|
||||
private IAutoCodeRuleService iAutoCodeRuleService;
|
||||
|
||||
@Autowired
|
||||
private IAutoCodePartService iAutoCodePartService;
|
||||
|
||||
@Autowired
|
||||
private IAutoCodeResultService iAutoCodeResultService;
|
||||
|
||||
@Autowired
|
||||
private PartTypeHandler partTypeHandler;
|
||||
|
||||
private String lastSerialNo;
|
||||
|
||||
@Log(title = "生成业务编号",businessType = BusinessType.INSERT)
|
||||
synchronized
|
||||
public String genSerialCode(String ruleCode,String inputCharacter){
|
||||
|
||||
//查找编码规则
|
||||
SysAutoCodeRule rule = iAutoCodeRuleService.getOne(ruleCode);
|
||||
Assert.notNull(rule,"未获取到指定类型:[{}]的业务编码生成规则",ruleCode);
|
||||
|
||||
//查找规则组成
|
||||
SysAutoCodePart partParam = new SysAutoCodePart();
|
||||
partParam.setRuleId(rule.getRuleId());
|
||||
List<SysAutoCodePart> parts = iAutoCodePartService.listPart(partParam);
|
||||
List<SysAutoCodePart> collect = parts.stream().filter(part->PartTypeEnum.PART_TYPE_SERIALNO.getCode().equals(part.getPartType())).collect(Collectors.toList());
|
||||
|
||||
Assert.isTrue(collect.size()<2,"编码规则[{}]流水号方式的组成只能存在一个",ruleCode);
|
||||
|
||||
StringBuilder buff = new StringBuilder();
|
||||
parts.forEach(codePart ->{
|
||||
codePart.setInputCharacter(inputCharacter);
|
||||
//根据当前组成部分,获取当前组成部分的结果
|
||||
String partStr = partTypeHandler.choiceExecute(codePart);
|
||||
|
||||
//如果是流水号部分,则进行记录
|
||||
if(StringUtils.equals(codePart.getPartType(),PartTypeEnum.PART_TYPE_SERIALNO.getCode())){
|
||||
lastSerialNo = partStr;
|
||||
}
|
||||
//将获取到的部分组装进整体编码中
|
||||
buff.append(partStr);
|
||||
});
|
||||
|
||||
Assert.notBlank(buff.toString(),"规则:[{}]生成的编码为空!",ruleCode);
|
||||
|
||||
String autoCode = paddingStr(rule,buff);
|
||||
|
||||
//将生成结果保存到数据库
|
||||
saveAutoCodeResult(rule,autoCode,inputCharacter);
|
||||
return autoCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据编码规则的配置进行补齐操作
|
||||
* @param rule
|
||||
* @param sb
|
||||
* @return
|
||||
*/
|
||||
private String paddingStr(SysAutoCodeRule rule,StringBuilder sb){
|
||||
String isPadding = rule.getIsPadded();
|
||||
if("Y".equals(isPadding)){
|
||||
int maxLength = rule.getMaxLength();
|
||||
String paddingChar = rule.getPaddedChar();
|
||||
StringBuilder resultStr = new StringBuilder();
|
||||
long length = maxLength - sb.length();
|
||||
Assert.isTrue(maxLength>sb.length(),"生成的编码[{}]已经超出规则中配置的最大长度:[{}]",sb.toString(),maxLength);
|
||||
|
||||
if("L".equals(rule.getPaddedMethod())){
|
||||
//左补齐
|
||||
//使用指定字符补齐左侧后,再将生成的编码添加到右侧
|
||||
for(;length>0;length --){
|
||||
resultStr.append(paddingChar);
|
||||
}
|
||||
resultStr.append(sb);
|
||||
}else{
|
||||
//右补齐
|
||||
//将生成的编码添加到左侧后,再使用指定字符补齐右侧
|
||||
resultStr.append(sb);
|
||||
for(;length>0;length --){
|
||||
resultStr.append(paddingChar);
|
||||
}
|
||||
}
|
||||
return resultStr.toString();
|
||||
}
|
||||
return sb.toString(); //如果不需要补齐,则直接返回
|
||||
}
|
||||
|
||||
private void saveAutoCodeResult(SysAutoCodeRule rule,String autoCode,String inputChar){
|
||||
Boolean flag = threadLocal.get(); //针对当前线程的判断 flag = true则数据库中没有当前规则的生成记录
|
||||
if(flag !=null && flag){
|
||||
SysAutoCodeResult rs = new SysAutoCodeResult();
|
||||
rs.setRuleId(rule.getRuleId());
|
||||
rs.setGenDate(DateUtil.format(LocalDateTime.now(),"yyyyMMddHHmmss"));
|
||||
rs.setLastResult(autoCode);
|
||||
rs.setGenIndex(1);
|
||||
rs.setLastSerialNo(Integer.parseInt(lastSerialNo));
|
||||
rs.setLastInputChar(inputChar);
|
||||
iAutoCodeResultService.saveAutoCodeResult(rs);
|
||||
}else{
|
||||
//直接更新对应的记录(我们默认一个RULE_CODE只有一种方式)
|
||||
SysAutoCodeResult bo = new SysAutoCodeResult();
|
||||
bo.setRuleId(rule.getRuleId());
|
||||
List<SysAutoCodeResult> results = iAutoCodeResultService.list(bo);
|
||||
Assert.notEmpty(results,"未查询到规则{[]}对应的结果记录",rule.getRuleCode());
|
||||
SysAutoCodeResult rs = results.get(0);
|
||||
rs.setLastResult(autoCode);
|
||||
rs.setGenDate(DateUtil.format(LocalDateTime.now(),"yyyyMMddHHmmss"));
|
||||
rs.setGenIndex(rs.getGenIndex()+1);
|
||||
rs.setLastSerialNo(Integer.parseInt(lastSerialNo));
|
||||
rs.setLastInputChar(inputChar);
|
||||
iAutoCodeResultService.updateAutoCodeResult(rs);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.ktg.system.strategy;
|
||||
|
||||
import com.ktg.common.core.domain.entity.SysAutoCodePart;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Order(2)
|
||||
public class PartTypeFixCharHandler implements PartTypeTemplate {
|
||||
@Override
|
||||
public String partHandle(SysAutoCodePart sysAutoCodePart) {
|
||||
return sysAutoCodePart.getFixCharacter();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.ktg.system.strategy;
|
||||
|
||||
import com.ktg.common.core.domain.entity.SysAutoCodePart;
|
||||
import com.ktg.common.enums.PartTypeEnum;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class PartTypeHandler {
|
||||
|
||||
@Autowired
|
||||
List<PartTypeTemplate> partTypeTemplates;
|
||||
|
||||
public String choiceExecute(SysAutoCodePart sysAutoCodePart){
|
||||
String partType = sysAutoCodePart.getPartType();
|
||||
return partTypeTemplates.get(PartTypeEnum.getByCode(partType).getBeanIndex()).partHandle(sysAutoCodePart);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.ktg.system.strategy;
|
||||
|
||||
import com.ktg.common.core.domain.entity.SysAutoCodePart;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@Component
|
||||
@Order(0)
|
||||
public class PartTypeInputCharHandler implements PartTypeTemplate {
|
||||
|
||||
|
||||
@Override
|
||||
public String partHandle(SysAutoCodePart sysAutoCodePart) {
|
||||
String inputCharacter = sysAutoCodePart.getInputCharacter();
|
||||
Assert.notNull(inputCharacter,"编码规则传入字符不能为空!");
|
||||
Assert.isTrue(inputCharacter.length() == sysAutoCodePart.getPartLength(),"传入字符的长度错误!");
|
||||
return inputCharacter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.ktg.system.strategy;
|
||||
|
||||
import com.ktg.common.core.domain.entity.SysAutoCodePart;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@Component
|
||||
@Order(1)
|
||||
public class PartTypeNowDateHandler implements PartTypeTemplate {
|
||||
|
||||
@Override
|
||||
public String partHandle(SysAutoCodePart sysAutoCodePart) {
|
||||
String formartDate = sysAutoCodePart.getDateFormart();
|
||||
return DateTimeFormatter.ofPattern(formartDate).format(LocalDateTime.now());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.ktg.system.strategy;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.ktg.common.core.domain.entity.SysAutoCodePart;
|
||||
import com.ktg.common.core.domain.entity.SysAutoCodeResult;
|
||||
import com.ktg.common.enums.CycleMethodMnum;
|
||||
import com.ktg.system.service.IAutoCodeResultService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Order(3)
|
||||
public class PartTypeSerialNoHandler implements PartTypeTemplate {
|
||||
|
||||
@Autowired
|
||||
private IAutoCodeResultService iAutoCodeResultService;
|
||||
|
||||
@Override
|
||||
public String partHandle(SysAutoCodePart sysAutoCodePart) {
|
||||
|
||||
if("Y".equals(sysAutoCodePart.getCycleFlag())){
|
||||
return cycleY(sysAutoCodePart);
|
||||
}else{
|
||||
return cycleN(sysAutoCodePart);
|
||||
}
|
||||
}
|
||||
|
||||
//循环方式(要传入当前的循环规则参数)
|
||||
private String cycleY(SysAutoCodePart sysAutoCodePart){
|
||||
String method = sysAutoCodePart.getCycleMethod();
|
||||
String param ="";
|
||||
if(CycleMethodMnum.CYCLE_METHOD_OTHER.getCode().equals(method)){
|
||||
param = sysAutoCodePart.getInputCharacter();
|
||||
}else{
|
||||
switch (CycleMethodMnum.getByCode(method)){
|
||||
case CYCLE_METHOD_YEAR:
|
||||
param = cn.hutool.core.date.DateUtil.format(LocalDateTime.now(),"yyyy");
|
||||
break;
|
||||
case CYCLE_METHOD_MONTH:
|
||||
param = cn.hutool.core.date.DateUtil.format(LocalDateTime.now(),"yyyyMM");
|
||||
break;
|
||||
case CYCLE_METHOD_DAY:
|
||||
param = cn.hutool.core.date.DateUtil.format(LocalDateTime.now(),"yyyyMMdd");
|
||||
break;
|
||||
case CYCLE_METHOD_HOUR:
|
||||
param = cn.hutool.core.date.DateUtil.format(LocalDateTime.now(),"yyyyMMddHH");
|
||||
break;
|
||||
case CYCLE_METHOD_MINUTE:
|
||||
param = cn.hutool.core.date.DateUtil.format(LocalDateTime.now(),"yyyyMMddHHmm");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
List<SysAutoCodeResult> rs = getAutoCodeResult(sysAutoCodePart.getRuleId(),param,method);
|
||||
if(CollectionUtil.isNotEmpty(rs)){
|
||||
//如果在编码记录表中有记录,则在最后一个流水号上加上步长,返回新的流水号
|
||||
AutoCodeUtil.threadLocal.set(false);
|
||||
Integer lastSerialNo = rs.get(0).getLastSerialNo();
|
||||
return String.format("%0"+sysAutoCodePart.getPartLength()+"d",lastSerialNo+sysAutoCodePart.getSeriaStep());
|
||||
}else {
|
||||
//如果在编码记录表中不存在,则直接返回起始流水号
|
||||
AutoCodeUtil.threadLocal.set(true);
|
||||
return String.format("%0"+sysAutoCodePart.getPartLength()+"d",sysAutoCodePart.getSeriaStartNo());
|
||||
}
|
||||
}
|
||||
|
||||
//不循环方式
|
||||
private String cycleN(SysAutoCodePart sysAutoCodePart){
|
||||
List<SysAutoCodeResult> rs = getAutoCodeResult(sysAutoCodePart.getRuleId(),"","");
|
||||
if(CollectionUtil.isNotEmpty(rs)){
|
||||
//存在记录则在当前记录加上步长
|
||||
Integer lastSerialNo = rs.get(0).getLastSerialNo();
|
||||
return String.format("%0"+sysAutoCodePart.getPartLength()+"d",lastSerialNo+sysAutoCodePart.getSeriaStep());
|
||||
}else{
|
||||
//不存在记录则从头开始
|
||||
AutoCodeUtil.threadLocal.set(true);
|
||||
return String.format("%0"+sysAutoCodePart.getPartLength()+"d",sysAutoCodePart.getSeriaStartNo());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//从编码结果记录表中查找当前指定循环规则的流水号记录
|
||||
private List<SysAutoCodeResult> getAutoCodeResult(Long ruleId,String param,String cycleMethod){
|
||||
SysAutoCodeResult queryParam = new SysAutoCodeResult();
|
||||
queryParam.setRuleId(ruleId);//ruleId要一致
|
||||
|
||||
if(CycleMethodMnum.CYCLE_METHOD_OTHER.getCode().equals(cycleMethod)){
|
||||
//如果循环方式是手工输入指定的字符
|
||||
queryParam.setLastInputChar(param);
|
||||
}else{
|
||||
//如果循环方式是按格式化的日期
|
||||
queryParam.setGenDate(param);//这里的param将按照 gen_date like #{param}+'%' 的方式进行模糊查询,数据库中记录的永远都是yyyMMddHHmmss格式的
|
||||
}
|
||||
return iAutoCodeResultService.list(queryParam);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.ktg.system.strategy;
|
||||
|
||||
import com.ktg.common.core.domain.entity.SysAutoCodePart;
|
||||
|
||||
public interface PartTypeTemplate {
|
||||
/**
|
||||
* 分段的处理规则
|
||||
* @param sysAutoCodePart
|
||||
* @return
|
||||
*/
|
||||
String partHandle(SysAutoCodePart sysAutoCodePart);
|
||||
}
|
||||
Reference in New Issue
Block a user