订单号生成记录表:
drop table if exists other_order_record_sequence;
create table other_order_record_sequence(
id bigint(20) primary key auto_increment,
order_record_sequence_type varchar(5) not null comment '订单类型',
seq_date date not null comment '日期',
sequence_count int(11) not null comment '订单数',
belonged_company bigint(20) not null comment '公司id'
)engine = innodb default charset = utf8mb4 comment '单号记录表';
订单生成存储过程:
drop procedure if exists get_order_number;
DELIMITER ;;
-- order_type_ordinal订单类型标注,order_type_code订单类型在单号中的标识,company_id公司id
create procedure get_order_number(IN order_type_ordinal varchar(5),IN order_type_code varchar(5),IN company_id bigint(20));
begin
DECLARE exist_sequence int default 1;
DECLARE sequence_number int(11);
DECLARE t_error INTEGER DEFAULT 0;
DECLARE cur_sequence CURSOR FOR select sequence_count + 1 from other_order_record_sequence where belonged_company = company_id and seq_date = curdate() and order_record_sequence_type = order_type_ordinal limit 1 for update;
DECLARE continue HANDLER FOR NOT FOUND SET exist_sequence = 0;
open cur_sequence;
fetch cur_sequence into sequence_number;
close cur_sequence;
if exist_sequence = 0 then
set sequence_number = 1;
insert into other_order_record_sequence(order_record_sequence_type,sequence_count,belonged_company,seq_date) values (order_type_ordinal,sequence_number,company_id,curdate());
else
update other_order_record_sequence set sequence_count = sequence_number where belonged_company = company_id and seq_date = curdate() and order_record_sequence_type = order_type_ordinal;
end if;
select CONCAT(replace(curdate(),'-',''),order_type_code,LPAD(company_id,5,0),LPAD(sequence_number,5,0));
end ;;
DELIMITER ;;