CREATE DATABASE `saas_process` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
use `saas_process`;

create table `process_rule`
(
id int unsigned primary key not null auto_increment,
company_id int unsigned not null,
biz_module varchar(100) comment '业务模块',
biz_type varchar(30) not null comment '业务类型',
name varchar(100) not null comment '流程名称',
enabled boolean comment '是否启用',
creator_id int unsigned,
creator_name varchar(100),
create_time datetime,
updater_id int unsigned,
updater_name varchar(100),
update_time datetime
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='流程规则';

create table `process_rule_node`
(
id int unsigned primary key not null auto_increment,
company_id int unsigned not null,
rule_id int unsigned not null,
order_num int comment '顺序',
name varchar(100) not null comment '节点名称',
type smallint default 1 comment '节点类型 1-审批 2-知会',
handler_type smallint default 1 comment '操作人员类型 1-发起人自选 2-人员 3-角色 4-部门领导',
handler text comment '操作人员',
sign_type smallint default 1 comment '1-会签 2-或签',
creator_id int unsigned,
creator_name varchar(100),
create_time datetime,
updater_id int unsigned,
updater_name varchar(100),
update_time datetime
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='流程规则节点';

create table `process_instance`
(
id int unsigned primary key not null auto_increment,
company_id int unsigned not null,
rule_id int unsigned not null,
biz_id int unsigned not null comment '业务单据ID',
biz_code varchar(100) comment '业务单据编号',
view_type varchar(100) comment '视图类型',
status smallint default 1 comment '1-已发起 2-已结束',
operation smallint comment '结束操作 1-同意 2-不同意 3-强制同意 4-强制不同意',
creator_id int unsigned,
creator_name varchar(100),
create_time datetime,
updater_id int unsigned,
updater_name varchar(100),
update_time datetime
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='流程实例';

create table `process_instance_item`
(
id int unsigned primary key not null auto_increment,
company_id int unsigned not null,
instance_id int unsigned not null,
rule_node_id int unsigned not null,
order_num int comment '顺序',
handler_id int unsigned not null comment '执行人',
handler_name varchar(100) comment '执行人名',
status smallint default 1 comment '执行状态 1-未开始 2-已开始 3-已结束',
operation smallint default 1 comment '操作 1-同意 2-驳回 3-变更 4-阅读 5-自动结束 6-强制结束',
comment text comment '审批意见',
remark text comment '备注',
starter_id int unsigned,
starter_name varchar(100),
start_time datetime comment '开始时间',
creator_id int unsigned,
creator_name varchar(100),
create_time datetime,
updater_id int unsigned,
updater_name varchar(100),
update_time datetime
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='流程实例任务';