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

create table `ac_company` (
  id int unsigned primary key not null auto_increment,
  name varchar(500) not null comment '名称',
  business_code varchar(100) comment '商业登记证号',
  address varchar(500) comment '注册地址',
  logo_url varchar(500) comment 'Logo',
  creator_id int unsigned,
  create_time datetime,
  updater_id int unsigned,
  update_time datetime
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='公司';

create table `ac_company_app` (
  company_id int unsigned not null,
  app_id varchar(100) not null,
  primary key(company_id, app_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='公司绑定应用';

create table `ac_company_qywx` (
  company_id int unsigned,
  corp_id varchar(100) not null,
  corp_full_name varchar(255),
  corp_industry varchar(50),
  corp_name varchar(255),
  corp_scale varchar(50),
  corp_square_logo_url varchar(255),
  corp_sub_industry varchar(255),
  location varchar(255),
  corp_type varchar(30),
  permanent_code varchar(255) comment '永久授权码'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='公司绑定企业微信';

create table `ac_company_qywx_agent` (
  company_id int unsigned,
  corp_id varchar(100) not null,
  suite_code varchar(30),
  agent_id int comment '应用在授权的企业里面的agentId',
  permanent_code varchar(255) comment '永久授权码'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='公司绑定企业微信应用信息';

create table `ac_account` (
  id int unsigned primary key not null auto_increment,
  username varchar(100) not null comment '账号',
  password varchar(100) not null comment '加密密码',
  salt varchar(100) not null comment '加密盐值',
  realname varchar(100) comment '真实姓名',
  email varchar(100) comment '邮箱',
  mobile varchar(100) not null comment '手机号',
  type int comment '账户类型 0 - 管理账户, 1 - 普通账户',
  enabled bool comment '是否启用',
  creator_id int unsigned,
  create_time datetime,
  updater_id int unsigned,
  update_time datetime
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='个人账户';

create table `ac_account_company` (
  company_id int unsigned,
  account_id int unsigned,
  primary key(company_id, account_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='账户绑定企业';

create table `ac_account_qywx` (
  company_id int unsigned,
  corp_id varchar(100) not null,
  account_id int unsigned,
  user_id varchar(100) not null,
  avatar varchar(255),
  name varchar(50),
  admin boolean default 0,
  primary key(corp_id, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='账户绑定企业微信';

create table `ac_role` (
  id int unsigned primary key not null auto_increment,
  code varchar(100) not null comment '编号',
  name varchar(300) not null comment '名称',
  description varchar(1000) comment '描述',
  type int comment '角色类型 0 - 管理角色, 1 - 普通角色',
  company_id int unsigned,
  creator_id int unsigned,
  create_time datetime,
  updater_id int unsigned,
  update_time datetime
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='角色';

create table `ac_account_role` (
  company_id int unsigned,
  account_id int unsigned,
  role_id int unsigned,
  primary key(account_id, role_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='账户绑定角色';

create table `ac_resource_module` (
  id int unsigned primary key not null auto_increment,
  app_id varchar(100) comment '应用',
  name varchar(100) comment '名称'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='资源模块';

create table `ac_resource_group` (
  id int unsigned primary key not null auto_increment,
  app_id varchar(100) comment '应用',
  module_id int unsigned not null,
  name varchar(100) comment '名称'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='资源分组';

create table `ac_resource` (
  id int unsigned primary key not null auto_increment,
  app_id varchar(100) comment '应用',
  group_id int unsigned not null,
  name varchar(1000) comment '名称',
  type varchar(100) comment '类型 MENU,BUTTON,API',
  url varchar(300) comment 'URL',
  method varchar(10) comment 'Http Method: POST/GET/PUT/DELETE',
  classify varchar(30) comment '分组'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='资源';

create table `ac_role_resource` (
  app_id varchar(100) comment '应用',
  company_id int unsigned,
  role_id int unsigned,
  resource_id int unsigned,
  primary key(role_id, resource_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='角色绑定资源';

# 账号
insert into ac_account(id,username,password,salt,realname,email,mobile,type,enabled,
                       creator_id,create_time,updater_id,update_time)
values (1,'18888888888','3e8451e274a8ee847872194e584a4145','18888888888','Administrator',
          'admin@usoftchina.com', '18888888888', 0, 1, 1, now(), 1, now());

# 公司
insert into ac_company(id, name, business_code, address, logo_url, creator_id, create_time, updater_id, update_time)
values (1, '测试账套', '000000000000000000','深圳市南山区粤海街道高新技术产业园科技南五路英唐大厦六楼',
        'https://co-image.qichacha.com/CompanyImage/104eb3c232bbac93393a5e204d6a47d1.jpg?x-oss-process=style/qcc_cmp',
        1, now(), 1, now());

insert into ac_company_app(company_id, app_id) values (1, 'trade-app');
insert into ac_account_company(account_id, company_id) values (1, 1);

# 角色
insert into ac_role(id,code,name,description,type,company_id,creator_id,create_time,updater_id,update_time)
    values (1, 'ROLE_ADMIN', '管理员', '公司管理人员，拥有所有权限', 0, 1, 1, now(), 1, now());
insert into ac_role(id,code,name,description,type,company_id,creator_id,create_time,updater_id,update_time)
values (2, 'ROLE_NORMAL', '普通用户', '公司普通用户', 0, 1, 1, now(), 1, now());
insert into ac_account_role(company_id, account_id, role_id) values (1, 1, 1);

# 资源模块
insert into ac_resource_module(id, app_id, name) values (1, 'trade-app', '采购');
insert into ac_resource_module(id, app_id, name) values (2, 'trade-app', '销售');
insert into ac_resource_module(id, app_id, name) values (3, 'trade-app', '库存');
insert into ac_resource_module(id, app_id, name) values (4, 'trade-app', '资金');
insert into ac_resource_module(id, app_id, name) values (5, 'trade-app', '资料');
# 资源组
insert into ac_resource_group(id, app_id, module_id, name) values (1, 'trade-app', 1, '采购单');
insert into ac_resource_group(id, app_id, module_id, name) values (2, 'trade-app', 1, '采购验收单');
insert into ac_resource_group(id, app_id, module_id, name) values (3, 'trade-app', 1, '采购验退单');
insert into ac_resource_group(id, app_id, module_id, name) values (4, 'trade-app', 1, '采购询价单');
insert into ac_resource_group(id, app_id, module_id, name) values (5, 'trade-app', 1, '采购明细表');
insert into ac_resource_group(id, app_id, module_id, name) values (6, 'trade-app', 1, '采购付款一览表');
insert into ac_resource_group(id, app_id, module_id, name) values (7, 'trade-app', 2, '销售订单');
insert into ac_resource_group(id, app_id, module_id, name) values (8, 'trade-app', 2, '销售出货单');
insert into ac_resource_group(id, app_id, module_id, name) values (9, 'trade-app', 2, '销售退货单');
insert into ac_resource_group(id, app_id, module_id, name) values (10, 'trade-app', 2, '以销定购');
insert into ac_resource_group(id, app_id, module_id, name) values (11, 'trade-app', 2, '销售明细表');
insert into ac_resource_group(id, app_id, module_id, name) values (12, 'trade-app', 2, '销售收款一览表');
insert into ac_resource_group(id, app_id, module_id, name) values (13, 'trade-app', 2, '销售利润表');
insert into ac_resource_group(id, app_id, module_id, name) values (14, 'trade-app', 3, '调拨单');
insert into ac_resource_group(id, app_id, module_id, name) values (15, 'trade-app', 3, '制造单');
insert into ac_resource_group(id, app_id, module_id, name) values (16, 'trade-app', 3, '其它入库单');
insert into ac_resource_group(id, app_id, module_id, name) values (17, 'trade-app', 3, '其它出库单');
insert into ac_resource_group(id, app_id, module_id, name) values (18, 'trade-app', 3, '盘点单');
insert into ac_resource_group(id, app_id, module_id, name) values (19, 'trade-app', 3, '物料出入库明细表');
insert into ac_resource_group(id, app_id, module_id, name) values (20, 'trade-app', 3, '物料收发汇总表');
insert into ac_resource_group(id, app_id, module_id, name) values (21, 'trade-app', 3, '物料库存数量金额表');
insert into ac_resource_group(id, app_id, module_id, name) values (22, 'trade-app', 4, '付款单');
insert into ac_resource_group(id, app_id, module_id, name) values (23, 'trade-app', 4, '收款单');
insert into ac_resource_group(id, app_id, module_id, name) values (24, 'trade-app', 4, '核销单');
insert into ac_resource_group(id, app_id, module_id, name) values (25, 'trade-app', 4, '其它收支单');
insert into ac_resource_group(id, app_id, module_id, name) values (26, 'trade-app', 4, '资金转存');
insert into ac_resource_group(id, app_id, module_id, name) values (27, 'trade-app', 4, '供应商对账单');
insert into ac_resource_group(id, app_id, module_id, name) values (28, 'trade-app', 4, '应付账款明细表');
insert into ac_resource_group(id, app_id, module_id, name) values (29, 'trade-app', 4, '客户对账单');
insert into ac_resource_group(id, app_id, module_id, name) values (30, 'trade-app', 4, '应收款明细表');
insert into ac_resource_group(id, app_id, module_id, name) values (31, 'trade-app', 4, '资金账户余额表');
insert into ac_resource_group(id, app_id, module_id, name) values (32, 'trade-app', 5, '客户资料');
insert into ac_resource_group(id, app_id, module_id, name) values (33, 'trade-app', 5, '供应商管理');
insert into ac_resource_group(id, app_id, module_id, name) values (34, 'trade-app', 5, '商品管理');
insert into ac_resource_group(id, app_id, module_id, name) values (35, 'trade-app', 5, '仓库管理');
insert into ac_resource_group(id, app_id, module_id, name) values (36, 'trade-app', 5, '职员管理');
insert into ac_resource_group(id, app_id, module_id, name) values (37, 'trade-app', 5, '账户管理');
insert into ac_resource_group(id, app_id, module_id, name) values (38, 'trade-app', 5, '发货地址管理');
insert into ac_resource_group(id, app_id, module_id, name) values (39, 'trade-app', 5, '客户类别');
insert into ac_resource_group(id, app_id, module_id, name) values (40, 'trade-app', 5, '供应商类别');
insert into ac_resource_group(id, app_id, module_id, name) values (41, 'trade-app', 5, '商品类别');
insert into ac_resource_group(id, app_id, module_id, name) values (42, 'trade-app', 5, '支出类别');
insert into ac_resource_group(id, app_id, module_id, name) values (43, 'trade-app', 5, '收入类别');
insert into ac_resource_group(id, app_id, module_id, name) values (44, 'trade-app', 5, '物料品牌');
insert into ac_resource_group(id, app_id, module_id, name) values (45, 'trade-app', 5, '计量单位');
insert into ac_resource_group(id, app_id, module_id, name) values (46, 'trade-app', 5, '结算方式');
insert into ac_resource_group(id, app_id, module_id, name) values (47, 'trade-app', 5, '辅助属性');
insert into ac_resource_group(id, app_id, module_id, name) values (48, 'trade-app', 5, '客户物料编码');
insert into ac_resource_group(id, app_id, module_id, name) values (49, 'trade-app', 5, '单据编码规则');
# 资源
insert into ac_resource(id, app_id, name, group_id, type, url, method, classify) values
  (1,'trade-app', '查询',1,'MENU','/api/purchase/purchase/list','GET','QUERY');
insert into ac_resource(id, app_id, name, group_id, type, url, method, classify) values
  (2,'trade-app', '新增',1,'MENU','/api/purchase/purchase/save','POST','ADD');
insert into ac_resource(id, app_id, name, group_id, type, url, method, classify) values
  (3,'trade-app', '修改',1,'BUTTON','/api/purchase/purchase/save','POST','UPDATE');
insert into ac_resource(id, app_id, name, group_id, type, url, method, classify) values
  (4,'trade-app', '审核',1,'BUTTON','/api/purchase/purchase/audit','POST','AUDIT');
insert into ac_resource(id, app_id, name, group_id, type, url, method, classify) values
  (5,'trade-app', '审核',1,'BUTTON','/api/purchase/purchase/batchAudit','POST','AUDIT');
insert into ac_resource(id, app_id, name, group_id, type, url, method, classify) values
  (6,'trade-app', '反审核',1,'BUTTON','/api/purchase/purchase/unAudit','POST','UNAUDIT');
insert into ac_resource(id, app_id, name, group_id, type, url, method, classify) values
  (7,'trade-app', '反审核',1,'BUTTON','/api/purchase/purchase/batchUnAudit','POST','UNAUDIT');
insert into ac_resource(id, app_id, name, group_id, type, url, method, classify) values
  (8,'trade-app', '删除',1,'BUTTON','/api/purchase/purchase/delete','GET','DELETE');
insert into ac_resource(id, app_id, name, group_id, type, url, method, classify) values
  (9,'trade-app', '打印',1,'BUTTON','/api/purchase/purchase/print','GET','PRINT');

