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

并发生成序列号

parent 3ae1c138
......@@ -7,9 +7,9 @@ import com.zehong.common.core.domain.BaseEntity;
/**
* 序列号对象 t_serial_number_info
*
*
* @author zehong
* @date 2024-06-07
* @date 2024-06-13
*/
public class TSerialNumberInfo extends BaseEntity
{
......@@ -20,88 +20,88 @@ public class TSerialNumberInfo extends BaseEntity
/** 表类型 */
@Excel(name = "表类型")
private Long tableType;
private String tableType;
/** 列名 */
@Excel(name = "列名")
private Long columnName;
private String columnName;
/** 最大值 */
@Excel(name = "最大值")
private String maxNum;
private Long maxNum;
/** 序号长度 */
@Excel(name = "序号长度")
private String numLength;
private Long numLength;
/** 版本号 */
@Excel(name = "版本号")
private String version;
private Long version;
/** 删除状态:0.否 1.是 */
@Excel(name = "删除状态:0.否 1.是")
private String isDel;
public void setNumberId(Long numberId)
public void setNumberId(Long numberId)
{
this.numberId = numberId;
}
public Long getNumberId()
public Long getNumberId()
{
return numberId;
}
public void setTableType(Long tableType)
public void setTableType(String tableType)
{
this.tableType = tableType;
}
public Long getTableType()
public String getTableType()
{
return tableType;
}
public void setColumnName(Long columnName)
public void setColumnName(String columnName)
{
this.columnName = columnName;
}
public Long getColumnName()
public String getColumnName()
{
return columnName;
}
public void setMaxNum(String maxNum)
public void setMaxNum(Long maxNum)
{
this.maxNum = maxNum;
}
public String getMaxNum()
public Long getMaxNum()
{
return maxNum;
}
public void setNumLength(String numLength)
public void setNumLength(Long numLength)
{
this.numLength = numLength;
}
public String getNumLength()
public Long getNumLength()
{
return numLength;
}
public void setVersion(String version)
public void setVersion(Long version)
{
this.version = version;
}
public String getVersion()
public Long getVersion()
{
return version;
}
public void setIsDel(String isDel)
public void setIsDel(String isDel)
{
this.isDel = isDel;
}
public String getIsDel()
public String getIsDel()
{
return isDel;
}
......@@ -109,16 +109,16 @@ public class TSerialNumberInfo extends BaseEntity
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("numberId", getNumberId())
.append("tableType", getTableType())
.append("columnName", getColumnName())
.append("maxNum", getMaxNum())
.append("numLength", getNumLength())
.append("updateTime", getUpdateTime())
.append("updateBy", getUpdateBy())
.append("version", getVersion())
.append("isDel", getIsDel())
.append("remark", getRemark())
.toString();
.append("numberId", getNumberId())
.append("tableType", getTableType())
.append("columnName", getColumnName())
.append("maxNum", getMaxNum())
.append("numLength", getNumLength())
.append("version", getVersion())
.append("updateTime", getUpdateTime())
.append("updateBy", getUpdateBy())
.append("isDel", getIsDel())
.append("remark", getRemark())
.toString();
}
}
}
\ No newline at end of file
package com.zehong.system.service;
import java.util.List;
import java.util.Map;
import com.zehong.system.domain.TSerialNumberInfo;
/**
......@@ -58,4 +60,11 @@ public interface ITSerialNumberInfoService
* @return 结果
*/
public int deleteTSerialNumberInfoById(Long numberId);
/**
* 根据表类型获取序列号
* @param tableType 表表类型
* @return
*/
Map<String,String> getSerialNumberByTableType(String tableType);
}
package com.zehong.system.service.impl;
import java.util.List;
import com.zehong.common.exception.CustomException;
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.mapper.TSerialNumberInfoMapper;
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业务层处理
......@@ -92,4 +96,38 @@ public class TSerialNumberInfoServiceImpl implements ITSerialNumberInfoService
{
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"
</resultMap>
<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>
<select id="selectTSerialNumberInfoList" parameterType="TSerialNumberInfo" resultMap="TSerialNumberInfoResult">
<include refid="selectTSerialNumberInfoVo"/>
<where>
<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="numLength != null and numLength != ''"> and num_length = #{numLength}</if>
<if test="version != null and version != ''"> and version = #{version}</if>
......@@ -42,7 +42,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
insert into t_serial_number_info
<trim prefix="(" suffix=")" suffixOverrides=",">
<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="numLength != null">num_length,</if>
<if test="updateTime != null">update_time,</if>
......@@ -68,16 +68,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
update t_serial_number_info
<trim prefix="SET" suffixOverrides=",">
<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="numLength != null">num_length = #{numLength},</if>
<if test="updateTime != null">update_time = #{updateTime},</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="remark != null">remark = #{remark},</if>
</trim>
where number_id = #{numberId}
where number_id = #{numberId} and version = #{version}
</update>
<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