克来机电 - 流程编号生成规则自定义

main
ic_excellent 2023-02-13 12:11:16 +08:00
parent 8a8dada8f4
commit e6a67339a9
12 changed files with 634 additions and 0 deletions

View File

@ -0,0 +1,41 @@
package weaver.bokang.xiao.common.mapper;
import aiyh.utils.annotation.recordset.ParamMapper;
import aiyh.utils.annotation.recordset.Select;
import aiyh.utils.annotation.recordset.SqlMapper;
import aiyh.utils.annotation.recordset.Update;
import java.util.Map;
/**
* @ClassName WorkflowMapper
* @Author
* @Date 2023/2/3 9:57
* @Description <h1></h1>
**/
@SqlMapper
public interface WorkflowMapper {
/**
* <h2>requestId</h2>
* @param requestId requestId
* @param tableName
* @return
*/
@Select("select * from $t{tableName} where requestid = #{requestId}")
Map<String,Object> queryWorkflowByRequestId(@ParamMapper("requestId") String requestId,@ParamMapper("tableName") String tableName);
/**
* <h2>requestId</h2>
* @param tableName
* @param fieldName
* @param fieldValue
* @param requestId requestId
* @return
*/
@Update("update $t{tableName} set $t{fieldName} = #{fieldValue} where requestid = #{requestId}")
boolean updateWorkflowMsgByRequestId(@ParamMapper("tableName") String tableName,
@ParamMapper("fieldName") String fieldName,
@ParamMapper("fieldValue") String fieldValue,
@ParamMapper("requestId") String requestId);
}

View File

@ -0,0 +1,52 @@
package weaver.bokang.xiao.kljd_code_generate.action;
import aiyh.utils.Util;
import aiyh.utils.action.SafeCusBaseAction;
import aiyh.utils.annotation.ActionDefaultTestValue;
import aiyh.utils.annotation.ActionDesc;
import aiyh.utils.annotation.PrintParamMark;
import aiyh.utils.annotation.RequiredMark;
import lombok.Setter;
import weaver.bokang.xiao.common.mapper.WorkflowMapper;
import weaver.bokang.xiao.kljd_code_generate.util.CodeGenerateUtil;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;
import java.util.Map;
/**
* @ClassName CodeGenerateAction
* @Author
* @Date 2023/2/6 13:18
* @Description <h1></h1>
**/
@ActionDesc(value = "流程编号生成",author = "bokang.xiao")
@Setter
public class CodeGenerateAction extends SafeCusBaseAction {
private final WorkflowMapper workflowMapper = Util.getMapper(WorkflowMapper.class);
private final CodeGenerateUtil codeGenerateUtil = new CodeGenerateUtil();
@RequiredMark("流程编码字段名")
@PrintParamMark
@ActionDefaultTestValue("cs3")
private String codeField;
@Override
public void doSubmit(String requestId, String billTable, int workflowId, User user, RequestInfo requestInfo) {
log.info(String.format("=================== {%s} begin ======================", this.getClass().getName()));
log.info(String.format("params:[requestId ==>%s],[codeField ==>%s]", requestId,codeField));
Map<String, Object> workflowMessage = workflowMapper.queryWorkflowByRequestId(requestId, billTable);
String sourceCode = Util.null2String(workflowMessage.get(codeField));
if(!"".equals(sourceCode)) {
log.error("已经编码过了,无需编码");
return;
}
String workflowCode = codeGenerateUtil.codeBuildByWorkflow(String.valueOf(workflowId), requestId, workflowMessage);
workflowMapper.updateWorkflowMsgByRequestId(billTable,codeField,workflowCode,requestId);
}
}

View File

@ -0,0 +1,20 @@
package weaver.bokang.xiao.kljd_code_generate.entity;
import lombok.Data;
/**
* @ClassName CodeBuildEntity
* @Author
* @Date 2023/2/6 13:24
* @Description <h1></h1>
**/
@Data
public class CodeBuildEntity {
private Integer fieldType;
private String workflowField;
private String workflowFieldName;
private Integer changeRule;
private String cusText;
private Integer isAlone;
}

View File

@ -0,0 +1,17 @@
package weaver.bokang.xiao.kljd_code_generate.entity;
import lombok.Data;
/**
* @ClassName CodeConditionEntity
* @Author
* @Date 2023/2/6 13:22
* @Description <h1></h1>
**/
@Data
public class CodeConditionEntity {
private String fieldId;
private Integer compare;
private String value;
private String fieldName;
}

View File

@ -0,0 +1,27 @@
package weaver.bokang.xiao.kljd_code_generate.entity;
import lombok.Data;
import java.util.List;
/**
* @ClassName CodeRuleConfig
* @Author
* @Date 2023/2/6 13:20
* @Description <h1></h1>
**/
@Data
public class CodeRuleConfig {
private Integer id;
private String workflowType;
private String description;
private String uniqueCode;
private Integer flowType;
private Integer flowNumber;
private Integer startNumber;
private List<CodeBuildEntity> codeBuildEntityList;
private List<CodeConditionEntity> codeConditionEntityList;
private List<String> reserveNumberList;
}

View File

@ -0,0 +1,19 @@
package weaver.bokang.xiao.kljd_code_generate.entity;
import lombok.Data;
/**
* @ClassName FlowEntity
* @Author
* @Date 2023/2/7 10:17
* @Description <h1></h1>
**/
@Data
public class FlowEntity {
private Integer id;
private Integer flowNumber;
private String workflowType;
private Integer codeRule;
private String flowKey;
}

View File

@ -0,0 +1,42 @@
package weaver.bokang.xiao.kljd_code_generate.enum_type;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName ChangeRuleEnum
* @Author
* @Date 2023/2/6 15:28
* @Description <h1></h1>
**/
public enum ChangeRuleEnum {
/** 字段值 */
FIELD_VALUE(0),
/** 固定值 */
DEFAULT_VALUE(1),
/** sql转换 */
CUS_SQL(2),
/** 浏览按钮显示值 */
BROWSE_VALUE(3),
/** 下拉框显示值 */
SELECT_VALUE(4);
private static final Map<Integer, ChangeRuleEnum> LOOK_UP = new HashMap<>(8);
static {
for (ChangeRuleEnum changeRuleEnum : EnumSet.allOf(ChangeRuleEnum.class)){
LOOK_UP.put(changeRuleEnum.value,changeRuleEnum);
}
}
public final Integer value;
ChangeRuleEnum(int value){
this.value = value;
}
public static ChangeRuleEnum getEnum(int value){
return LOOK_UP.get(value);
}
}

View File

@ -0,0 +1,41 @@
package weaver.bokang.xiao.kljd_code_generate.enum_type;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName CompareEnum
* @Author
* @Date 2023/2/6 15:05
* @Description <h1></h1>
**/
public enum CompareEnum {
/** 大于 */
MORE_THAN(0),
/** 小于 */
LESS_THAN(1),
/** 等于 */
EQUAL(2),
/** 不等于 */
NO_EQUAL(3);
private static final Map<Integer, CompareEnum> LOOK_UP = new HashMap<>(8);
static {
for (CompareEnum compareEnum : EnumSet.allOf(CompareEnum.class)){
LOOK_UP.put(compareEnum.value,compareEnum);
}
}
public final Integer value;
CompareEnum(int value){
this.value = value;
}
public static CompareEnum getEnum(int value){
return LOOK_UP.get(value);
}
}

View File

@ -0,0 +1,42 @@
package weaver.bokang.xiao.kljd_code_generate.enum_type;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName FieldTypeEnum
* @Author
* @Date 2023/2/6 16:03
* @Description <h1></h1>
**/
public enum FieldTypeEnum {
/** 流程字段 */
WORKFLOW_FIELD(0),
/** 字符串 */
STRING(1),
/** 浏览按钮 */
BROWSE(2),
/** 下拉框 */
SELECT(3),
/** 流水号 */
FLOW_NUMBER(4);
private static final Map<Integer, FieldTypeEnum> LOOK_UP = new HashMap<>(8);
static {
for (FieldTypeEnum fieldTypeEnum : EnumSet.allOf(FieldTypeEnum.class)){
LOOK_UP.put(fieldTypeEnum.value,fieldTypeEnum);
}
}
public final Integer value;
FieldTypeEnum(int value){
this.value = value;
}
public static FieldTypeEnum getEnum(int value){
return LOOK_UP.get(value);
}
}

View File

@ -0,0 +1,54 @@
package weaver.bokang.xiao.kljd_code_generate.mapper;
import aiyh.utils.annotation.recordset.ParamMapper;
import aiyh.utils.annotation.recordset.Select;
import aiyh.utils.annotation.recordset.SqlMapper;
import weaver.bokang.xiao.kljd_code_generate.entity.CodeBuildEntity;
import weaver.bokang.xiao.kljd_code_generate.entity.CodeConditionEntity;
import weaver.bokang.xiao.kljd_code_generate.entity.CodeRuleConfig;
import java.util.List;
/**
* @ClassName CodeRuleMapper
* @Author
* @Date 2023/2/6 13:19
* @Description <h1></h1>
**/
@SqlMapper
public interface CodeRuleMapper {
/**
* <h2>id</h2>
* @param workflowId id
* @return
*/
@Select("select * from uf_code_build_rule where workflow_type = #{workflowId}")
List<CodeRuleConfig> queryCodeRuleConfig(@ParamMapper("workflowId") String workflowId);
/**
* <h2>mainId</h2>
* @param mainId id
* @return
*/
@Select("select * from uf_code_build_rule_dt1 where mainid = #{mainId}")
List<CodeConditionEntity> queryConditionByMainId(@ParamMapper("mainId") int mainId);
/**
* <h2>mainId</h2>
* @param mainId id
* @return
*/
@Select("select * from uf_code_build_rule_dt2 where mainid = #{mainId}")
List<CodeBuildEntity> queryBuildByMainId(@ParamMapper("mainId") int mainId);
/**
* <h2>mainId</h2>
* @param mainId id
* @return
*/
@Select("select * from uf_code_build_rule_dt3 where mainid = #{mainId}")
List<String> queryReserveNumberByMainId(@ParamMapper("mainId") int mainId);
}

View File

@ -0,0 +1,41 @@
package weaver.bokang.xiao.kljd_code_generate.mapper;
import aiyh.utils.annotation.recordset.*;
import weaver.bokang.xiao.kljd_code_generate.entity.FlowEntity;
/**
* @ClassName FlowMapper
* @Author
* @Date 2023/2/7 11:07
* @Description <h1></h1>
**/
@SqlMapper
public interface FlowMapper {
/**
* <h2></h2>
* @param codeRule
* @param flowKey
* @return
*/
@Select("select max(flow_number) from uf_flow_info where code_rule = #{codeRule} and flow_key = #{flowKey}")
int getFlowByParam(@ParamMapper("codeRule") int codeRule,@ParamMapper("flowKey") String flowKey);
/**
* <h2></h2>
* @param codeRule
* @return
*/
@Select("select max(flow_number) from uf_flow_info where code_rule = #{codeRule}")
int getFlowByParam(@ParamMapper("codeRule") int codeRule);
/**
* <h2></h2>
* @param flowEntity
* @return
*/
@Update("update uf_flow_info set flow_number = #{flowNumber},workflow_type = #{workflowType},code_rule = #{codeRule},flow_key = #{flowKey} where id = #{id}")
boolean updateFlow(FlowEntity flowEntity);
}

View File

@ -0,0 +1,238 @@
package weaver.bokang.xiao.kljd_code_generate.util;
import aiyh.utils.Util;
import aiyh.utils.excention.CustomerException;
import org.apache.log4j.Logger;
import weaver.bokang.xiao.kljd_code_generate.entity.CodeBuildEntity;
import weaver.bokang.xiao.kljd_code_generate.entity.CodeConditionEntity;
import weaver.bokang.xiao.kljd_code_generate.entity.CodeRuleConfig;
import weaver.bokang.xiao.kljd_code_generate.entity.FlowEntity;
import weaver.bokang.xiao.kljd_code_generate.enum_type.ChangeRuleEnum;
import weaver.bokang.xiao.kljd_code_generate.enum_type.CompareEnum;
import weaver.bokang.xiao.kljd_code_generate.enum_type.FieldTypeEnum;
import weaver.bokang.xiao.kljd_code_generate.mapper.CodeRuleMapper;
import weaver.bokang.xiao.kljd_code_generate.mapper.FlowMapper;
import weaver.conn.RecordSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @ClassName CodeGenerate
* @Author
* @Date 2023/2/6 14:07
* @Description <h1></h1>
**/
public class CodeGenerateUtil {
private final Logger logger = Util.getLogger();
private final CodeRuleMapper codeRuleMapper = Util.getMapper(CodeRuleMapper.class);
private final FlowMapper flowMapper = Util.getMapper(FlowMapper.class);
/**
* <h2>id</h2>
* @param workflowId id
* @return
*/
public List<CodeRuleConfig> getRuleConfig(String workflowId){
List<CodeRuleConfig> codeRuleConfigList = codeRuleMapper.queryCodeRuleConfig(workflowId);
codeRuleConfigList.forEach(codeRule ->{
int mainId = codeRule.getId();
List<CodeBuildEntity> codeBuildEntities = codeRuleMapper.queryBuildByMainId(mainId);
List<CodeConditionEntity> codeConditionEntities = codeRuleMapper.queryConditionByMainId(mainId);
List<String> reserveNumberList = codeRuleMapper.queryReserveNumberByMainId(mainId);
codeRule.setCodeBuildEntityList(codeBuildEntities);
codeRule.setCodeConditionEntityList(codeConditionEntities);
codeRule.setReserveNumberList(reserveNumberList);
});
return codeRuleConfigList;
}
/**
* <h2></h2>
* @param workflowId id
* @param mainMap
* @return
*/
public String codeBuildByWorkflow(String workflowId,String requestId, Map<String,Object> mainMap){
String codeNumber = "";
List<CodeRuleConfig> ruleConfig = getRuleConfig(workflowId);
logger.info("编码生成配置信息 ==>"+ruleConfig);
for (CodeRuleConfig codeRuleConfig : ruleConfig) {
List<CodeConditionEntity> codeConditionEntityList = codeRuleConfig.getCodeConditionEntityList();
boolean isBeginBuild = true;
for (CodeConditionEntity codeConditionEntity : codeConditionEntityList) {
int compare = codeConditionEntity.getCompare();
String value = codeConditionEntity.getValue();
String fieldName = codeConditionEntity.getFieldName();
CompareEnum compareEnum = CompareEnum.getEnum(compare);
String fieldValue = Util.null2String(mainMap.get(fieldName));
switch (compareEnum){
case EQUAL:{
isBeginBuild = fieldValue.equals(value);
}break;
case NO_EQUAL:{
isBeginBuild = !fieldValue.equals(value);
}break;
case MORE_THAN:{
isBeginBuild = Util.getIntValue(value) > Util.getIntValue(fieldValue);
}break;
case LESS_THAN:{
isBeginBuild = Util.getIntValue(value) < Util.getIntValue(fieldValue);
}break;
default:break;
}
if(!isBeginBuild) {
break;
}
}
if(isBeginBuild){
codeNumber = codeBuild(codeRuleConfig, mainMap);
break;
}
}
return codeNumber;
}
/**
* <h2></h2>
* @param codeRuleConfig
* @param mainMap
* @return
*/
public String codeBuild(CodeRuleConfig codeRuleConfig, Map<String,Object> mainMap){
StringBuilder stringBuilder = new StringBuilder();
StringBuilder flowKeyBuilder = new StringBuilder();
List<CodeBuildEntity> codeBuildEntityList = codeRuleConfig.getCodeBuildEntityList();
for (CodeBuildEntity codeBuildEntity : codeBuildEntityList) {
int changeRule = codeBuildEntity.getChangeRule();
int fieldType = codeBuildEntity.getFieldType();
String workflowField = codeBuildEntity.getWorkflowField();
String workflowFieldName = codeBuildEntity.getWorkflowFieldName();
String cusText = codeBuildEntity.getCusText();
int isAlone = codeBuildEntity.getIsAlone();
ChangeRuleEnum changeRuleEnum = ChangeRuleEnum.getEnum(changeRule);
FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getEnum(fieldType);
String currentStr = "";
switch (fieldTypeEnum){
case WORKFLOW_FIELD:{
currentStr = Util.null2String(mainMap.get(workflowFieldName));
}break;
case STRING:{
currentStr = cusText;
}break;
case FLOW_NUMBER:{
currentStr = "{flowNumber}";
}break;
default:break;
}
switch (changeRuleEnum){
case CUS_SQL:{
currentStr = executeCusSql(cusText, mainMap);
}break;
case DEFAULT_VALUE:{
currentStr = currentStr;
}
default:break;
}
if(isAlone == 0){
flowKeyBuilder.append(currentStr).append("-");
}
stringBuilder.append(currentStr);
}
String workflowCode = stringBuilder.toString();
int maxFlow = 0;
String flowAloneKey = flowKeyBuilder.toString();
if(flowKeyBuilder.length() > 0){
flowAloneKey = flowAloneKey.substring(0,flowAloneKey.length() - 1);
if(!"".equals(flowAloneKey)){
maxFlow = flowMapper.getFlowByParam(codeRuleConfig.getId(), flowAloneKey);
}else {
maxFlow = flowMapper.getFlowByParam(codeRuleConfig.getId());
}
}else {
maxFlow = flowMapper.getFlowByParam(codeRuleConfig.getId());
}
maxFlow = Math.max(maxFlow, 0);
maxFlow = Math.max(maxFlow,codeRuleConfig.getStartNumber());
logger.info("流水号 ==>"+maxFlow);
if(codeRuleConfig.getFlowType() != 0 && workflowCode.contains("{flowNumber}")){
List<String> reserveNumberList = codeRuleConfig.getReserveNumberList();
//跳过预留编号
while (true){
maxFlow ++;
String maxFlowStr = maxFlow + "";
//若流水方式为流水位数控制则添加字符0
if(codeRuleConfig.getFlowType() == 1){
int leftZero = codeRuleConfig.getFlowNumber() - maxFlowStr.length();
StringBuilder leftZeroBuilder = new StringBuilder(maxFlowStr);
for (int i = 0; i < leftZero; i++) {
leftZeroBuilder.insert(0,"0");
}
if(leftZero < 0){
throw new CustomerException("流水位数超出限制,流水号 ==>"+workflowCode.replace("{flowNumber}",maxFlowStr));
}
maxFlowStr = leftZeroBuilder.toString();
}
String tempCode = workflowCode.replace("{flowNumber}",maxFlowStr);
if(!reserveNumberList.contains(tempCode)){
workflowCode = tempCode;
FlowEntity flowEntity = new FlowEntity();
flowEntity.setFlowNumber(maxFlow);
flowEntity.setCodeRule(codeRuleConfig.getId());
flowEntity.setFlowKey(flowAloneKey);
int modeId = Util.getIntValue(Util.getModeIdByTableName("uf_flow_info"));
int dataId = Util.getModeDataId("uf_flow_info", modeId, 1);
flowEntity.setId(dataId);
flowMapper.updateFlow(flowEntity);
Util.rebuildModeDataShareByAsync(1,modeId,dataId);
break;
}
}
}
logger.info("流程编号信息 ==>"+workflowCode);
return workflowCode;
}
/**
* <h2>sql</h2>
* @param sourceStr sql
* @param mainMap
* @return
*/
public String executeCusSql(String sourceStr,Map<String,Object> mainMap){
String result = "";
List<String> paramList = new ArrayList<>();
String queryString = getQueryString(sourceStr, mainMap,paramList);
RecordSet recordSet = new RecordSet();
recordSet.executeQuery(queryString,paramList);
if(recordSet.next()){
result = Util.null2String(recordSet.getString(1));
}
return result;
}
/**
* <h2></h2>
* @param sourceStr
* @param mainMap
* @param paramList
* @return
*/
public String getQueryString(String sourceStr,Map<String,Object> mainMap,List<String> paramList){
String regx = "#\\{(?<fieldName>\\S+)}";
Pattern compile = Pattern.compile(regx);
Matcher matcher = compile.matcher(sourceStr);
while (matcher.find()){
String fieldName = matcher.group("fieldName");
paramList.add(fieldName);
sourceStr = sourceStr.replace("#{" + fieldName + "}", "?");
paramList.add(Util.null2String(mainMap.get(fieldName)));
}
return sourceStr;
}
}