添加生成唯一单据编号功能

This commit is contained in:
cjl
2019-02-01 15:40:44 +08:00
parent a95389ecbf
commit 66baa3dfa0
7 changed files with 92 additions and 2 deletions

View File

@@ -5240,4 +5240,54 @@ alter table jsh_serial_number change delete_Flag delete_Flag varchar(1) DEFAULT
-- ----------------------------
-- 删除单据子表单据主表id字段对应外键约束
-- ----------------------------
ALTER TABLE jsh_depotitem DROP FOREIGN KEY jsh_depotitem_ibfk_1;
ALTER TABLE jsh_depotitem DROP FOREIGN KEY jsh_depotitem_ibfk_1;
-- ----------------------------
-- 时间2019年2月1日
-- version1.0.2
-- 此次更新添加sequence表用于获取一个唯一的数值
-- 特别提醒之后的sql都是在之前基础上迭代可以对已存在的系统进行数据保留更新
-- ----------------------------
-- ----------------------------
-- 添加表tbl_sequence
-- ----------------------------
DROP TABLE IF EXISTS `tbl_sequence`;
CREATE TABLE tbl_sequence (
seq_name VARCHAR(50) NOT NULL COMMENT '序列名称',
minvalue bigint(20) NOT NULL COMMENT '最小值',
maxvalue bigint(20) NOT NULL COMMENT '最大值',
current_val bigint(20) NOT NULL COMMENT '当前值',
increment_val INT DEFAULT '1' NOT NULL COMMENT '增长步数',
remark VARCHAR(500) DEFAULT null COMMENT '备注',
PRIMARY KEY (seq_name)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='sequence表';
-- ----------------------------
-- 添加表单据编号sequence
-- ----------------------------
insert into tbl_sequence (seq_name, minvalue, maxvalue, current_val, increment_val,remark) values ('depot_number_seq', 1, 999999999999999999, 1, 1,'单据编号sequence');
-- ----------------------------
-- 创建function _nextval() 用于获取当前序列号
-- ----------------------------
DELIMITER //
create function _nextval(name varchar(50))
returns long
begin
declare _cur bigint;
declare _maxvalue bigint; -- 接收最大值
declare _increment int; -- 接收增长步数
set _increment = (select increment_val from tbl_sequence where seq_name = name);
set _maxvalue = (select maxvalue from tbl_sequence where seq_name = name);
set _cur = (select current_val from tbl_sequence where seq_name = name);
update tbl_sequence -- 更新当前值
set current_val = _cur + increment_val
where seq_name = name ;
if(_cur + _increment >= _maxvalue) then -- 判断是都达到最大值
update tbl_sequence
set current_val = minvalue
where seq_name = name ;
end if;
return _cur;
end;
//
DELIMITER ;

View File

@@ -61,6 +61,16 @@ public class BusinessConstants {
* 批量插入sql时最大的数据条数
* */
public static final int BATCH_INSERT_MAX_NUMBER = 500;
/**
* sequence名称
* */
//sequence返回字符串的最小长度
public static final Long SEQ_TO_STRING_MIN_LENGTH = 1000000L;
//sequence长度小于基准长度时前追加基础值
public static final String SEQ_TO_STRING_LESS_INSERT = "0";
//单据编号
public static final String DEPOT_NUMBER_SEQ = "depot_number_seq";

View File

@@ -77,7 +77,8 @@ public class DepotHeadController {
BaseResponseInfo res = new BaseResponseInfo();
Map<String, Object> map = new HashMap<String, Object>();
try {
String number = depotHeadService.buildNumber(type, subType, beginTime, endTime);
// String number = depotHeadService.buildNumber(type, subType, beginTime, endTime);
String number = depotHeadService.buildOnlyNumber();
map.put("DefaultNumber", number);
res.code = 200;
res.data = map;

View File

@@ -107,4 +107,9 @@ public interface DepotHeadMapperEx {
* 更新单据主表信息
* */
void updatedepotHead(DepotHead depotHead);
/**
* 获得一个全局唯一的数作为订单号的追加
* */
Long getBuildOnlyNumber(@Param("seq_name") String seq_name);
}

View File

@@ -20,6 +20,7 @@ import com.jsh.erp.service.supplier.SupplierService;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.utils.StringUtil;
import com.jsh.erp.utils.Tools;
import lombok.Synchronized;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
@@ -153,6 +154,26 @@ public class DepotHeadService {
example.createCriteria().andIdIn(ids);
return depotHeadMapper.updateByExampleSelective(depotHead, example);
}
/**
* 创建一个唯一的序列号
* */
public String buildOnlyNumber(){
Long buildOnlyNumber=null;
synchronized (this){
buildOnlyNumber= depotHeadMapperEx.getBuildOnlyNumber(BusinessConstants.DEPOT_NUMBER_SEQ);
}
if(buildOnlyNumber<BusinessConstants.SEQ_TO_STRING_MIN_LENGTH){
StringBuffer sb=new StringBuffer(buildOnlyNumber.toString());
int len=BusinessConstants.SEQ_TO_STRING_MIN_LENGTH.toString().length()-sb.length();
for(int i=0;i<len;i++){
sb.insert(0,BusinessConstants.SEQ_TO_STRING_LESS_INSERT);
}
return sb.toString();
}else{
return buildOnlyNumber.toString();
}
}
public String buildNumber(String type, String subType, String beginTime, String endTime) {
String newNumber = "0001"; //新编号

View File

@@ -389,5 +389,8 @@
</set>
where Id = #{id,jdbcType=BIGINT}
</update>
<select id="getBuildOnlyNumber" resultType="java.lang.Long">
select _nextval(#{seq_name}) from dual;
</select>
</mapper>