部门编码新规则自定义生成
parent
46f1c9fde1
commit
ef675bef1f
@ -0,0 +1,314 @@
|
||||
package com.engine.hrm.util;
|
||||
|
||||
import com.engine.hrm.entity.RuleCodeType;
|
||||
import com.engine.xmgsecond.util.DepartmentCodeUtil;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import weaver.conn.RecordSet;
|
||||
import weaver.general.BaseBean;
|
||||
import weaver.general.Util;
|
||||
import weaver.hrm.company.DepartmentComInfo;
|
||||
import weaver.hrm.company.SubCompanyComInfo;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class CodeRuleManager extends BaseBean {
|
||||
|
||||
private static CodeRuleManager codeRuleManager = new CodeRuleManager();
|
||||
|
||||
private SubCompanyComInfo subCompanyComInfo = new SubCompanyComInfo();
|
||||
private DepartmentComInfo departmentComInfo = new DepartmentComInfo();
|
||||
|
||||
public static String NULL = "NULL";
|
||||
public static String SEPARATOR = "-";
|
||||
|
||||
public static CodeRuleManager getCodeRuleManager() {
|
||||
return codeRuleManager;
|
||||
}
|
||||
|
||||
private CodeRuleManager() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param serialtype SUBCOMPANY("SUBCOMPANY"), DEPARTMENT("DEPARTMENT"), JOBTITLES("JOBTITLES"), USER("USER")
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public String generateRuleCode(RuleCodeType serialtype, String inputCode) throws Exception {
|
||||
return this.generateRuleCode(serialtype, null, null, null, inputCode);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param serialtype SUBCOMPANY("SUBCOMPANY"), DEPARTMENT("DEPARTMENT"), JOBTITLES("JOBTITLES"), USER("USER")
|
||||
* @param subcompanyCode 分部编号
|
||||
* @return 部门
|
||||
* @throws Exception
|
||||
*/
|
||||
public String generateRuleCode(RuleCodeType serialtype, String subcompanyid, String inputCode,String supDeptid) throws Exception {
|
||||
|
||||
if (StringUtils.isNotEmpty(inputCode)) {
|
||||
checkReservedIfDel(serialtype.getValue(), inputCode);
|
||||
return inputCode;
|
||||
}
|
||||
RecordSet recordSet = new RecordSet();
|
||||
// 查询启用的编码规则
|
||||
String sql = "select * from hrm_coderule where serialtype = ? and serialenable = 1";
|
||||
recordSet.executeQuery(sql, serialtype.getValue());
|
||||
if (recordSet.getCounts() <=0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
//QC3600738 新美光HR项目 部门自定义编码 新建(卡片 导入等)
|
||||
if("DEPARTMENT".equals(serialtype.getValue())) {
|
||||
DepartmentCodeUtil departmentCodeUtil = new DepartmentCodeUtil();
|
||||
return departmentCodeUtil.generateRuleCode(subcompanyid,"",supDeptid);
|
||||
}
|
||||
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param serialtype SUBCOMPANY("SUBCOMPANY"), DEPARTMENT("DEPARTMENT"), JOBTITLES("JOBTITLES"), USER("USER")
|
||||
* @param subcompanyid 分部id
|
||||
* @param deptid 部门id
|
||||
* @return 返回编码规则生成的编码
|
||||
* @throws Exception
|
||||
*/
|
||||
public String generateRuleCode(RuleCodeType serialtype, String subcompanyid, String deptid, String jobtitlesid, String inputCode) throws Exception {
|
||||
if (StringUtils.isNotEmpty(inputCode)) {
|
||||
checkReservedIfDel(serialtype.getValue(), inputCode);
|
||||
return inputCode;
|
||||
}
|
||||
RecordSet recordSet = new RecordSet();
|
||||
// 查询启用的编码规则
|
||||
String sql = "select * from hrm_coderule where serialtype = ? and serialenable = 1";
|
||||
recordSet.executeQuery(sql, serialtype.getValue());
|
||||
if (recordSet.getCounts() <=0) {
|
||||
return "";
|
||||
}
|
||||
recordSet.next();
|
||||
|
||||
//QC3600738 新美光HR项目 部门自定义编码 重新生成编号
|
||||
if("DEPARTMENT".equals(serialtype.getValue())) {
|
||||
DepartmentCodeUtil departmentCodeUtil = new DepartmentCodeUtil();
|
||||
return departmentCodeUtil.generateRuleCode(subcompanyid,deptid,"");
|
||||
}
|
||||
|
||||
// 先匹配到规则的唯一编码
|
||||
String oneselftype = recordSet.getString("oneselftype");
|
||||
String id = recordSet.getString("id");
|
||||
String[] oneselftypeList = StringUtils.isEmpty(oneselftype) ? new String[]{} : oneselftype.split(",");
|
||||
Map<String, String> map = new HashMap<>();
|
||||
LocalDate now = LocalDate.now();
|
||||
for (String type : oneselftypeList) {
|
||||
switch (RuleCodeType.getByValue(type)) {
|
||||
case DAY:
|
||||
map.put(RuleCodeType.DAY.getValue(), String.format("%02d", now.getDayOfMonth()));
|
||||
case MONTH:
|
||||
map.put(RuleCodeType.MONTH.getValue(), String.format("%02d", now.getMonthValue()));
|
||||
case YEAR:
|
||||
map.put(RuleCodeType.YEAR.getValue(), String.valueOf(now.getYear()));
|
||||
break;
|
||||
case SUBCOMPANY:
|
||||
map.put("subcompanyid", subcompanyid);
|
||||
break;
|
||||
case DEPARTMENT:
|
||||
map.put("deptid", deptid);
|
||||
break;
|
||||
case JOBTITLES:
|
||||
map.put("jobtitlesid", jobtitlesid);
|
||||
break;
|
||||
}
|
||||
}
|
||||
String uniqueconstitute = map.isEmpty() ? CodeRuleManager.buildUniqueconstituteNULL() : CodeRuleManager.buildUniqueconstitute(map.get(RuleCodeType.YEAR.getValue()), map.get(RuleCodeType.MONTH.getValue()),
|
||||
map.get(RuleCodeType.DAY.getValue()), map.get("subcompanyid"), map.get("deptid"), map.get("jobtitlesid"));
|
||||
// int index = 0;
|
||||
while (true) {
|
||||
// index ++;
|
||||
String code = this.buildCode(id, subcompanyid, deptid, jobtitlesid, uniqueconstitute);
|
||||
sql = "select * from hrm_coderulereserved where reservedcode = ? and coderuleid = ?";
|
||||
recordSet.executeQuery(sql, code, id);
|
||||
if (recordSet.getCounts() <= 0 && !checkIsExist(code, serialtype.getValue())) {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
// throw new Exception("自动创建编码失败!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 检验预留编号是否存在 存在就删除
|
||||
* @param serialtype
|
||||
* @param inputCode
|
||||
*/
|
||||
public void checkReservedIfDel(String serialtype, String inputCode) {
|
||||
try {
|
||||
RecordSet rs = new RecordSet();
|
||||
String sql = "select hcr.id from hrm_coderulereserved hcr left join hrm_coderule hc ON hcr.coderuleid = hc.id where hc.serialtype = ? and reservedcode = ?";
|
||||
rs.executeQuery(sql, serialtype, inputCode);
|
||||
List<String> ids = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
ids.add(rs.getString("id"));
|
||||
}
|
||||
if (CollectionUtils.isNotEmpty(ids)) {
|
||||
sql = "delete from hrm_coderulereserved where id in("+StringUtils.strip(ids.toString(),"[]")+")";
|
||||
rs.executeUpdate(sql);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
writeLog(e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkIsExist(String code, String serialtype) {
|
||||
RecordSet rs = new RecordSet();
|
||||
String sql = "";
|
||||
// 判断该字段是否已经被试用
|
||||
switch (RuleCodeType.getByValue(serialtype)) {
|
||||
case SUBCOMPANY:
|
||||
sql = "select id from HrmSubCompany where subcompanycode = ?";
|
||||
break;
|
||||
case DEPARTMENT:
|
||||
sql = "select id from HrmDepartment where departmentcode = ?";
|
||||
break;
|
||||
case JOBTITLES:
|
||||
sql = "select id from HrmJobTitles where jobtitlecode = ?";
|
||||
break;
|
||||
case USER:
|
||||
sql = "select id from hrmresource where workcode = ?";
|
||||
break;
|
||||
}
|
||||
rs.executeQuery(sql, code);
|
||||
if (rs.getCounts() > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String buildCode(String id, String subcompanyid, String deptid,String jobtitlesid, String uniqueconstitute) throws Exception {
|
||||
LocalDate localDate = LocalDate.now();
|
||||
// 生成编号
|
||||
RecordSet recordSet = new RecordSet();
|
||||
String sql = "select * from hrm_coderuledetail where coderuleid = ? ORDER BY showorder";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
recordSet.executeQuery(sql, id);
|
||||
while (recordSet.next()) {
|
||||
String ruletype = recordSet.getString("ruletype");
|
||||
String rulevalue = Util.null2String(recordSet.getString("rulevalue")).trim();
|
||||
switch (RuleCodeType.getByValue(ruletype)) {
|
||||
case STRING:
|
||||
sb.append(rulevalue);
|
||||
break;
|
||||
case YEAR:
|
||||
sb.append(localDate.getYear());
|
||||
break;
|
||||
case MONTH:
|
||||
sb.append(String.format("%02d", localDate.getMonthValue()));
|
||||
break;
|
||||
case DAY:
|
||||
sb.append(String.format("%02d", localDate.getDayOfMonth()));
|
||||
break;
|
||||
case SUBCOMPANY:
|
||||
sb.append(subCompanyComInfo.getSubCompanyCode(subcompanyid));
|
||||
break;
|
||||
case DEPARTMENT:
|
||||
sb.append(departmentComInfo.getDepartmentCode(deptid));
|
||||
break;
|
||||
case JOBTITLES:
|
||||
sb.append(getJobtitlesCode(jobtitlesid));
|
||||
break;
|
||||
case NUMBER:
|
||||
int number = this.getNumber(id, 0, uniqueconstitute);
|
||||
// if ((number+ "").length() > Integer.parseInt(rulevalue) - 1) {
|
||||
// throw new Exception("流水号超出定义位数!");
|
||||
// }
|
||||
sb.append(String.format("%0"+rulevalue+"d", number));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private String getJobtitlesCode(String jobtitlesid) {
|
||||
RecordSet rs = new RecordSet();
|
||||
String sql = "select jobtitlecode from hrmjobtitles where id = ?";
|
||||
rs.executeQuery(sql, jobtitlesid);
|
||||
while (rs.next()) {
|
||||
return rs.getString("jobtitlecode");
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取流水号
|
||||
* @param id
|
||||
*/
|
||||
private int getNumber(String id, int index, String uniqueconstitute) throws Exception {
|
||||
if (index == 5) {
|
||||
throw new Exception("编号重复!");
|
||||
}
|
||||
RecordSet rs = new RecordSet();
|
||||
String sql = "select * from hrm_coderulerecord where coderuleid = ? and uniqueconstitute = ?";
|
||||
rs.executeQuery(sql, id, uniqueconstitute);
|
||||
int currentnumber = -1;
|
||||
int version = -1;
|
||||
if (rs.next()) {
|
||||
currentnumber = Integer.parseInt(rs.getString("currentnumber"));
|
||||
version = Integer.parseInt(rs.getString("version"));
|
||||
} else {
|
||||
// 不存在 插入默认起始编号
|
||||
sql = "insert into hrm_coderulerecord (coderuleid, uniqueconstitute, startnumber, currentnumber, version) values (? ,? ,? ,?, ?)";
|
||||
if (rs.executeUpdate(sql, id, uniqueconstitute, 1, 1, 1)) {
|
||||
currentnumber = 1;
|
||||
version = 1;
|
||||
}
|
||||
}
|
||||
|
||||
sql = "update hrm_coderulerecord set version = ?, currentnumber = ? where coderuleid = ? and version = ? and uniqueconstitute = ?";
|
||||
// 通过乐观锁的实现方式处理集群下的并发可能
|
||||
rs.executeUpdate(sql, version + 1, currentnumber + 1, id, version, uniqueconstitute);
|
||||
if (rs.next() && rs.getInt(0) <= 0) {
|
||||
// 说明存在并发 未执行成功 重新获取
|
||||
index ++;
|
||||
return getNumber(id, index, uniqueconstitute);
|
||||
}
|
||||
return currentnumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置编码规则中的流水号
|
||||
* @param id
|
||||
* @param startnumber
|
||||
*/
|
||||
private void resetRuleCode(String id, String startnumber) {
|
||||
RecordSet rs = new RecordSet();
|
||||
String sql = "update hrm_coderule set resetdate = ?, currentnumber = ? where id = ?";
|
||||
rs.executeUpdate(sql, LocalDate.now().toString(), startnumber, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 组成起始编号表唯一性的编号
|
||||
* @return
|
||||
*/
|
||||
public static String buildUniqueconstitute(String year, String month, String day, String subcompanyid, String deptid, String jobtitlesid) {
|
||||
return Util.null2String(year, NULL) + SEPARATOR + Util.null2String(month, NULL) + SEPARATOR
|
||||
+ Util.null2String(day, NULL) + SEPARATOR + Util.null2String(subcompanyid, NULL)
|
||||
+ SEPARATOR + Util.null2String(deptid, NULL) + SEPARATOR + Util.null2String(jobtitlesid, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 组成起始编号表唯一性的编号
|
||||
* @return
|
||||
*/
|
||||
public static String buildUniqueconstituteNULL() {
|
||||
return NULL + SEPARATOR + NULL + SEPARATOR
|
||||
+ NULL + SEPARATOR + NULL
|
||||
+ SEPARATOR + NULL + SEPARATOR + NULL;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.engine.xmgsecond.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
/**
|
||||
* @Author liang.cheng
|
||||
* @Date 2025/3/25 14:50
|
||||
* @Description:
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class DeptCodeRule {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private Integer subcompany;
|
||||
|
||||
private String code;
|
||||
|
||||
private String firstNum;
|
||||
|
||||
private String secondNum;
|
||||
|
||||
private String threeNum;
|
||||
|
||||
private String remark;
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue