表结构
-- auto-generated definition
create table t_emc_service_order
(
f_id int auto_increment comment '主键id'
primary key,
f_order_no varchar(32) not null comment '服务单号',
f_user_id varchar(45) not null comment '用户id',
f_user_tel varchar(11) not null comment '用户手机号',
f_service_id int not null comment '服务id',
f_order_status tinyint not null comment '服务单状态(1-初始化,2-未使用,3-使用中,4-已过期,5-退款中,6-已退款,9-作废)',
f_mall_order_no varchar(32) null comment '商城主订单号',
f_mall_skucode varchar(32) null comment '商城sku码',
f_service_rank varchar(16) default '' not null comment '会员服务类型(general-大众,silver-白银,gold-黄金,platinum-铂金,diamond-钻石)',
f_purchase_amount decimal(9, 2) null comment '服务购买金额',
f_accruing_amount decimal(9, 2) null comment '累计已省金额',
f_service_start_time datetime null comment '服务开始时间',
f_service_end_time datetime null comment '服务结束时间',
f_created_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
f_modified_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '修改时间'
)
comment '用户购买服务单表';
-- auto-generated definition
create table t_emc_privilege_record
(
f_id int auto_increment comment '主键id'
primary key,
f_user_id varchar(45) not null comment '用户id',
f_order_no varchar(32) not null comment '服务单号',
f_sub_service_id int not null comment '子服务id',
f_item_id int not null comment '服务明细单id',
f_privilege_id int not null comment '权益id',
f_coupon_rule_id varchar(32) not null comment '券规则id',
f_received_status tinyint not null comment '权益状态(1-待下发,2-已下发,3-已领取,4-已使用,5-已失效)',
f_coupon_id varchar(32) null comment '用户领取的券id',
f_acquire_time datetime null comment '权益领取时间',
f_privilege_start_time datetime null comment '权益生效时间',
f_privilege_end_time datetime null comment '权益到期时间',
f_created_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
f_modified_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '修改时间'
)
comment '用户权益兑换记录表';
-- auto-generated definition
create table t_emc_user_info
(
f_id int auto_increment comment '主键id'
primary key,
f_user_id varchar(45) not null comment '用户id',
f_user_name varchar(32) default '' not null comment '用户姓名',
f_is_member tinyint(1) not null comment '是否会员(0-否,1-是)',
f_user_rank varchar(16) default '' not null comment '会员身份(super-超级会员,general-大众会员,silver-白银会员,gold-黄金会员,platinum-铂金会员,diamond-钻石会员)',
f_member_start_time datetime not null comment '会员开始时间',
f_member_end_time datetime not null comment '会员结束时间',
f_created_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
f_modified_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '修改时间',
constraint idx_userinfo_userid
unique (f_user_id)
)
comment '用户信息表';
用户信息表通过userId和订单表关联
订单表通过订单号orderNo和权益记录表关联
存储过程
delimiter $
create function generateTestJobData(number int) #--创建一个函数名为bigInsto 入参为(参数名,数据类型)
returns varchar(200)#--返回数据,数据类型
begin
#--函数体开始
declare rand_str varchar(150) default 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789'; #--定义一个rand_str的变量 数据类型,默认值
declare rand_orderStatus varchar(100) default '23456';
declare coupon_ids varchar(100) default '594741,594746,594749,594750,594751,594753,594754,594757,594760,594774,594782';
declare countNumber int default 1;
declare perNumber int default 1;
declare orderNo varchar(150) default NULL;
declare userId varchar(150) default NULL;
declare serviceId int(4) default NULL;
declare statusId int(4) default NULL;
declare preStatus int default 5;
declare coupon_id varchar(32) default null;
declare memberMark tinyint(1) default 0;
WHILE countNumber <= number
DO
SET userId = REPLACE(UUID(), '-', '');
SET orderNo = CONCAT('EMC-JOB', SUBSTRING(UPPER(REPLACE(UUID(), '-', '')), 1, 15));
SET serviceId = CEILING(RAND() * 500000 + 500000);
SET statusId = SUBSTRING(rand_orderStatus, ceil(rand() * 4), 1);
IF statusId = 2 THEN
SET memberMark = 0;
ELSEIF statusId = 3 THEN
SET memberMark = 1;
ELSEIF statusId = 5 THEN
SET memberMark = 1;
ELSEIF statusId = 4 THEN
SET memberMark = 0;
ELSEIF statusId = 6 THEN
SET memberMark = 0;
END IF;
insert into t_emc_user_info(f_user_id, f_user_name, f_is_member, f_user_rank, f_member_start_time,
f_member_end_time)
VALUES (userId, 'JOB', memberMark, 'super', now(), now());
#---insert 语句
insert into t_emc_service_order(f_order_no, f_user_id, f_user_tel, f_service_id, f_order_status,
f_mall_order_no, f_mall_skucode, f_service_rank, f_purchase_amount,
f_accruing_amount, f_service_start_time, f_service_end_time, f_created_time,
f_modified_time)
VALUES (orderNo, userId,
concat('1', substring(cast(3 + (rand() * 10) % 7 AS char(50)), 1, 1),
right(left(trim(cast(rand() AS char(50))), 11), 9)),
serviceId, statusId, orderNo, concat('sku-', SUBSTRING(rand_str, ceil(rand() * 6), 6)), 'super',
null,
null,
now(), now(), now(), now());
## 金融劵
insert into t_emc_privilege_record(f_user_id, f_order_no, f_sub_service_id, f_item_id, f_privilege_id,
f_coupon_rule_id, f_received_status, f_coupon_id, f_acquire_time,
f_privilege_start_time, f_privilege_end_time, f_created_time,
f_modified_time)
VALUES (userId, orderNo, serviceId, countNumber, countNumber, '1156', preStatus, '594568', now(), now(),
now(), now(), now());
# 生活劵
loop_name:
loop
IF perNumber = 7 THEN
SET perNumber = 1;
leave loop_name; -- 判断条件成立则结束循环 好比java中的 boeak
ELSEIF perNumber != 7 THEN
SET perNumber = perNumber + 1;
END IF;
IF statusId = 2 THEN
SET preStatus = 1;
ELSEIF statusId = 3 THEN
SET preStatus = floor(2 + rand() * (5 - 2));
ELSEIF statusId = 5 THEN
SET preStatus = floor(2 + rand() * (5 - 2));
ELSEIF statusId = 4 THEN
SET preStatus = 5;
ELSEIF statusId = 6 THEN
SET preStatus = 5;
END IF;
IF preStatus = 4 THEN
SELECT id
INTO coupon_id
FROM (
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(coupon_ids, ',', help_topic_id + 1), ',',
-1) AS id
FROM mysql.help_topic
WHERE help_topic_id < LENGTH(coupon_ids) - LENGTH(REPLACE(coupon_ids, ',', '')) + 1
) AS str
order by RAND()
LIMIT 1;
END IF;
insert into t_emc_privilege_record(f_user_id, f_order_no, f_sub_service_id, f_item_id,
f_privilege_id, f_coupon_rule_id, f_received_status, f_coupon_id,
f_acquire_time, f_privilege_start_time, f_privilege_end_time,
f_created_time, f_modified_time)
VALUES (userId, orderNo, serviceId, countNumber, countNumber, CEILING(RAND() * 500000 + 500000),
preStatus, coupon_id, now(), now(), now(), now(), now());
end loop; -- 循环结束
SET countNumber = countNumber + 1;
END WHILE;
return countNumber; #--返回值
end;
#--函数体结束
#查看log_bin_trust_function_creators是否开启:默认关闭 批量插入数据必须为开启状态
show variables like 'log_bin_trust_function_creators';
# 开启
set global log_bin_trust_function_creators = 1;
# 查当前创建的所有自定义函数
show function status;
# 删除自定义的函数
drop function generateTestJobData;
# 生成数据
select generateTestJobData(20000);
# truncate table t_emc_service_order;