Commit 1690938a authored by 耿迪迪's avatar 耿迪迪

并发生成序列号

parent 3ae1c138
...@@ -9,7 +9,7 @@ import com.zehong.common.core.domain.BaseEntity; ...@@ -9,7 +9,7 @@ import com.zehong.common.core.domain.BaseEntity;
* 序列号对象 t_serial_number_info * 序列号对象 t_serial_number_info
* *
* @author zehong * @author zehong
* @date 2024-06-07 * @date 2024-06-13
*/ */
public class TSerialNumberInfo extends BaseEntity public class TSerialNumberInfo extends BaseEntity
{ {
...@@ -20,23 +20,23 @@ public class TSerialNumberInfo extends BaseEntity ...@@ -20,23 +20,23 @@ public class TSerialNumberInfo extends BaseEntity
/** 表类型 */ /** 表类型 */
@Excel(name = "表类型") @Excel(name = "表类型")
private Long tableType; private String tableType;
/** 列名 */ /** 列名 */
@Excel(name = "列名") @Excel(name = "列名")
private Long columnName; private String columnName;
/** 最大值 */ /** 最大值 */
@Excel(name = "最大值") @Excel(name = "最大值")
private String maxNum; private Long maxNum;
/** 序号长度 */ /** 序号长度 */
@Excel(name = "序号长度") @Excel(name = "序号长度")
private String numLength; private Long numLength;
/** 版本号 */ /** 版本号 */
@Excel(name = "版本号") @Excel(name = "版本号")
private String version; private Long version;
/** 删除状态:0.否 1.是 */ /** 删除状态:0.否 1.是 */
@Excel(name = "删除状态:0.否 1.是") @Excel(name = "删除状态:0.否 1.是")
...@@ -51,48 +51,48 @@ public class TSerialNumberInfo extends BaseEntity ...@@ -51,48 +51,48 @@ public class TSerialNumberInfo extends BaseEntity
{ {
return numberId; return numberId;
} }
public void setTableType(Long tableType) public void setTableType(String tableType)
{ {
this.tableType = tableType; this.tableType = tableType;
} }
public Long getTableType() public String getTableType()
{ {
return tableType; return tableType;
} }
public void setColumnName(Long columnName) public void setColumnName(String columnName)
{ {
this.columnName = columnName; this.columnName = columnName;
} }
public Long getColumnName() public String getColumnName()
{ {
return columnName; return columnName;
} }
public void setMaxNum(String maxNum) public void setMaxNum(Long maxNum)
{ {
this.maxNum = maxNum; this.maxNum = maxNum;
} }
public String getMaxNum() public Long getMaxNum()
{ {
return maxNum; return maxNum;
} }
public void setNumLength(String numLength) public void setNumLength(Long numLength)
{ {
this.numLength = numLength; this.numLength = numLength;
} }
public String getNumLength() public Long getNumLength()
{ {
return numLength; return numLength;
} }
public void setVersion(String version) public void setVersion(Long version)
{ {
this.version = version; this.version = version;
} }
public String getVersion() public Long getVersion()
{ {
return version; return version;
} }
...@@ -114,9 +114,9 @@ public class TSerialNumberInfo extends BaseEntity ...@@ -114,9 +114,9 @@ public class TSerialNumberInfo extends BaseEntity
.append("columnName", getColumnName()) .append("columnName", getColumnName())
.append("maxNum", getMaxNum()) .append("maxNum", getMaxNum())
.append("numLength", getNumLength()) .append("numLength", getNumLength())
.append("version", getVersion())
.append("updateTime", getUpdateTime()) .append("updateTime", getUpdateTime())
.append("updateBy", getUpdateBy()) .append("updateBy", getUpdateBy())
.append("version", getVersion())
.append("isDel", getIsDel()) .append("isDel", getIsDel())
.append("remark", getRemark()) .append("remark", getRemark())
.toString(); .toString();
......
package com.zehong.system.service; package com.zehong.system.service;
import java.util.List; import java.util.List;
import java.util.Map;
import com.zehong.system.domain.TSerialNumberInfo; import com.zehong.system.domain.TSerialNumberInfo;
/** /**
...@@ -58,4 +60,11 @@ public interface ITSerialNumberInfoService ...@@ -58,4 +60,11 @@ public interface ITSerialNumberInfoService
* @return 结果 * @return 结果
*/ */
public int deleteTSerialNumberInfoById(Long numberId); public int deleteTSerialNumberInfoById(Long numberId);
/**
* 根据表类型获取序列号
* @param tableType 表表类型
* @return
*/
Map<String,String> getSerialNumberByTableType(String tableType);
} }
package com.zehong.system.service.impl; package com.zehong.system.service.impl;
import java.util.List; import com.zehong.common.exception.CustomException;
import com.zehong.common.utils.DateUtils; import com.zehong.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zehong.system.mapper.TSerialNumberInfoMapper;
import com.zehong.system.domain.TSerialNumberInfo; import com.zehong.system.domain.TSerialNumberInfo;
import com.zehong.system.mapper.TSerialNumberInfoMapper;
import com.zehong.system.service.ITSerialNumberInfoService; import com.zehong.system.service.ITSerialNumberInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* 序列号Service业务层处理 * 序列号Service业务层处理
...@@ -92,4 +96,38 @@ public class TSerialNumberInfoServiceImpl implements ITSerialNumberInfoService ...@@ -92,4 +96,38 @@ public class TSerialNumberInfoServiceImpl implements ITSerialNumberInfoService
{ {
return tSerialNumberInfoMapper.deleteTSerialNumberInfoById(numberId); return tSerialNumberInfoMapper.deleteTSerialNumberInfoById(numberId);
} }
/**
* 根据表类型获取序列号
* @param tableType 表表类型
* @return
*/
@Override
public synchronized Map<String,String> getSerialNumberByTableType(String tableType){
//获取最新编号
TSerialNumberInfo tSerialNumberInfo = new TSerialNumberInfo();
tSerialNumberInfo.setTableType(tableType);
List<TSerialNumberInfo> serialNumberInfoList = tSerialNumberInfoMapper.selectTSerialNumberInfoList(tSerialNumberInfo);
Map<String,String> numberMap = new HashMap<>();
//更新编号
serialNumberInfoList.stream().forEach(item ->{
//自旋更新
int result;
int lookNum = 0;
do{
lookNum ++;
Long maxNum = item.getMaxNum();
maxNum++;
maxNum = maxNum.toString().length() > item.getNumLength() ? 0 : maxNum;
String number = String.format("%0"+ item.getNumLength() +"d",maxNum);
numberMap.put(item.getColumnName(),number);
item.setMaxNum(maxNum);
result = tSerialNumberInfoMapper.updateTSerialNumberInfo(item);
if(result == 0) item = tSerialNumberInfoMapper.selectTSerialNumberInfoById(item.getNumberId());
}while (result == 0 && lookNum < 5);
if(lookNum > 4) throw new CustomException("序列号生成失败");
});
return numberMap;
}
} }
...@@ -18,14 +18,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ...@@ -18,14 +18,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</resultMap> </resultMap>
<sql id="selectTSerialNumberInfoVo"> <sql id="selectTSerialNumberInfoVo">
select number_id, table_type, column_name, max_num, num_length, update_time, update_by, version, is_del, remark from t_serial_number_info select number_id, table_type, `column_name`, max_num, num_length, update_time, update_by, version, is_del, remark from t_serial_number_info
</sql> </sql>
<select id="selectTSerialNumberInfoList" parameterType="TSerialNumberInfo" resultMap="TSerialNumberInfoResult"> <select id="selectTSerialNumberInfoList" parameterType="TSerialNumberInfo" resultMap="TSerialNumberInfoResult">
<include refid="selectTSerialNumberInfoVo"/> <include refid="selectTSerialNumberInfoVo"/>
<where> <where>
<if test="tableType != null "> and table_type = #{tableType}</if> <if test="tableType != null "> and table_type = #{tableType}</if>
<if test="columnName != null "> and column_name like concat('%', #{columnName}, '%')</if> <if test="columnName != null "> and `column_name` like concat('%', #{columnName}, '%')</if>
<if test="maxNum != null and maxNum != ''"> and max_num = #{maxNum}</if> <if test="maxNum != null and maxNum != ''"> and max_num = #{maxNum}</if>
<if test="numLength != null and numLength != ''"> and num_length = #{numLength}</if> <if test="numLength != null and numLength != ''"> and num_length = #{numLength}</if>
<if test="version != null and version != ''"> and version = #{version}</if> <if test="version != null and version != ''"> and version = #{version}</if>
...@@ -42,7 +42,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ...@@ -42,7 +42,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
insert into t_serial_number_info insert into t_serial_number_info
<trim prefix="(" suffix=")" suffixOverrides=","> <trim prefix="(" suffix=")" suffixOverrides=",">
<if test="tableType != null">table_type,</if> <if test="tableType != null">table_type,</if>
<if test="columnName != null">column_name,</if> <if test="columnName != null">`column_name`,</if>
<if test="maxNum != null">max_num,</if> <if test="maxNum != null">max_num,</if>
<if test="numLength != null">num_length,</if> <if test="numLength != null">num_length,</if>
<if test="updateTime != null">update_time,</if> <if test="updateTime != null">update_time,</if>
...@@ -68,16 +68,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ...@@ -68,16 +68,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
update t_serial_number_info update t_serial_number_info
<trim prefix="SET" suffixOverrides=","> <trim prefix="SET" suffixOverrides=",">
<if test="tableType != null">table_type = #{tableType},</if> <if test="tableType != null">table_type = #{tableType},</if>
<if test="columnName != null">column_name = #{columnName},</if> <if test="columnName != null">`column_name` = #{columnName},</if>
<if test="maxNum != null">max_num = #{maxNum},</if> <if test="maxNum != null">max_num = #{maxNum},</if>
<if test="numLength != null">num_length = #{numLength},</if> <if test="numLength != null">num_length = #{numLength},</if>
<if test="updateTime != null">update_time = #{updateTime},</if> <if test="updateTime != null">update_time = #{updateTime},</if>
<if test="updateBy != null">update_by = #{updateBy},</if> <if test="updateBy != null">update_by = #{updateBy},</if>
<if test="version != null">version = #{version},</if> <if test="version != null">version = #{version} + 1,</if>
<if test="isDel != null">is_del = #{isDel},</if> <if test="isDel != null">is_del = #{isDel},</if>
<if test="remark != null">remark = #{remark},</if> <if test="remark != null">remark = #{remark},</if>
</trim> </trim>
where number_id = #{numberId} where number_id = #{numberId} and version = #{version}
</update> </update>
<delete id="deleteTSerialNumberInfoById" parameterType="Long"> <delete id="deleteTSerialNumberInfoById" parameterType="Long">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment