alter table ta_miniapp add fdd_server_host varchar(255);
alter table ta_miniapp add fdd_download_host varchar(255);
alter table ta_raise add invite tinyint(1);


INSERT INTO `sys_menu`(`menu_id`, `code`, `name`, `parent_code`, `icon`, `app_type`, `status`, `menu_root`, `sort_num`) VALUES (109, '/eContract/manage/list', '合同管理', '106', NULL, 'estateagent-admin', 1, 106, 3);
INSERT INTO `sys_menu`(`menu_id`, `code`, `name`, `parent_code`, `icon`, `app_type`, `status`, `menu_root`, `sort_num`) VALUES (108, '/eContract/businessConfig/list', '业务配置', '106', NULL, 'estateagent-admin', 1, 106, 2);
INSERT INTO `sys_menu`(`menu_id`, `code`, `name`, `parent_code`, `icon`, `app_type`, `status`, `menu_root`, `sort_num`) VALUES (107, '/eContract/template/list', '合同模板', '106', NULL, 'estateagent-admin', 1, 106, 1);
INSERT INTO `sys_menu`(`menu_id`, `code`, `name`, `parent_code`, `icon`, `app_type`, `status`, `menu_root`, `sort_num`) VALUES (106, '/eContract', '电子合同', '-1', NULL, 'estateagent-admin', 1, 106, 1);


insert INTO ta_role_menu (role_id,menu_id) SELECT role_id,106 from ta_role WHERE is_admin = 1;
insert INTO ta_role_menu (role_id,menu_id) SELECT role_id,107 from ta_role WHERE is_admin = 1;
insert INTO ta_role_menu (role_id,menu_id) SELECT role_id,108 from ta_role WHERE is_admin = 1;
insert INTO ta_role_menu (role_id,menu_id) SELECT role_id,109 from ta_role WHERE is_admin = 1;


INSERT INTO `sys_button_in_menu`(`btn_id`, `menu_id`, `name`, `status`, `code`, `remark`) VALUES (205, 107, '新增', 1, 'admin.eContractManager.add.post', NULL);
INSERT INTO `sys_button_in_menu`(`btn_id`, `menu_id`, `name`, `status`, `code`, `remark`) VALUES (206, 107, '删除', 1, 'admin.eContractManager.del', NULL);
INSERT INTO `sys_button_in_menu`(`btn_id`, `menu_id`, `name`, `status`, `code`, `remark`) VALUES (207, 107, '查看详情', 1, 'admin.eContractManager.detail.get', NULL);
INSERT INTO `sys_button_in_menu`(`btn_id`, `menu_id`, `name`, `status`, `code`, `remark`) VALUES (208, 108, '新增', 1, 'admin.eContractBusiness.add.post', NULL);
INSERT INTO `sys_button_in_menu`(`btn_id`, `menu_id`, `name`, `status`, `code`, `remark`) VALUES (209, 108, '删除', 1, 'admin.eContractBusiness.del', NULL);
INSERT INTO `sys_button_in_menu`(`btn_id`, `menu_id`, `name`, `status`, `code`, `remark`) VALUES (210, 108, '查看详情', 1, 'admin.eContractBusiness.detail.get', NULL);
INSERT INTO `sys_button_in_menu`(`btn_id`, `menu_id`, `name`, `status`, `code`, `remark`) VALUES (211, 109, '查看详情', 1, 'admin.eContractTemplate.detail.get', NULL);


insert INTO ta_role_button (role_id,btn_id) SELECT role_id,205 from ta_role WHERE is_admin = 1;
insert INTO ta_role_button (role_id,btn_id) SELECT role_id,206 from ta_role WHERE is_admin = 1;
insert INTO ta_role_button (role_id,btn_id) SELECT role_id,207 from ta_role WHERE is_admin = 1;
insert INTO ta_role_button (role_id,btn_id) SELECT role_id,208 from ta_role WHERE is_admin = 1;
insert INTO ta_role_button (role_id,btn_id) SELECT role_id,209 from ta_role WHERE is_admin = 1;
insert INTO ta_role_button (role_id,btn_id) SELECT role_id,210 from ta_role WHERE is_admin = 1;
insert INTO ta_role_button (role_id,btn_id) SELECT role_id,211 from ta_role WHERE is_admin = 1;


DROP TABLE IF EXISTS `ta_raise_invite`;
CREATE TABLE `ta_raise_invite`  (
  `raise_invite_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '认筹被邀请id',
  `create_date` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `org_id` int(11) NULL DEFAULT NULL COMMENT '公司id',
  `raise_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '认筹id',
  `sales_batch_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '销售批次id',
  `building_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '项目id',
  `invite_tel` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '被邀请电话号码'
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '认筹被邀请清单 ' ROW_FORMAT = Dynamic;


DROP TABLE IF EXISTS `ta_company_seal`;
CREATE TABLE `ta_company_seal`  (
  `seal_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '印章id',
  `create_date` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `company_id` int(11) NULL DEFAULT NULL COMMENT '企业id',
  `seal_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '印章名称',
  `seal_img` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '印章图',
  `default_seal` int(11) NULL DEFAULT NULL COMMENT '默认章',
  `signature_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '签章id',
  `status` int(11) NULL DEFAULT NULL COMMENT '状态 1是有效0是无效-1是删除',
  PRIMARY KEY (`seal_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 52 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '公司印章 ' ROW_FORMAT = Dynamic;


DROP TABLE IF EXISTS `ta_company_org`;
CREATE TABLE `ta_company_org`  (
  `company_org_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '企业和组织关系id',
  `create_date` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `org_id` int(11) NULL DEFAULT NULL COMMENT '公司id',
  `company_id` int(11) NULL DEFAULT NULL COMMENT '企业id',
  PRIMARY KEY (`company_org_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 30 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '企业和组织关系表 ' ROW_FORMAT = Dynamic;


DROP TABLE IF EXISTS `ta_contract`;
CREATE TABLE `ta_contract`  (
  `contract_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '合同id',
  `company_id` int(11) NULL DEFAULT NULL COMMENT '企业id',
  `org_id` int(11) NULL DEFAULT NULL COMMENT '公司id',
  `building_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '楼盘id',
  `contract_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '合同标题',
  `target_type` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '关联业务类型',
  `target_id` int(11) NULL DEFAULT NULL COMMENT '关联业务',
  `invoice_target_type` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '关联单据类型',
  `invoice_target_id` int(11) NULL DEFAULT NULL COMMENT '关联单据id',
  `signatory_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '甲方名称',
  `signatory_time` datetime(0) NULL DEFAULT NULL COMMENT '甲方签署时间',
  `customer_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '乙方名称',
  `customer_tel` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '乙方手机号',
  `customer_idcard` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '乙方身份证号',
  `customer_time` datetime(0) NULL DEFAULT NULL COMMENT '乙方签署时间',
  `contract_download_url` varchar(3096) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `contract_view_url` varchar(3096) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `archives` tinyint(1) NULL DEFAULT NULL COMMENT '是否归档',
  `archives_time` datetime(0) NULL DEFAULT NULL COMMENT '归档时间',
  `create_date` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `settlement` tinyint(1) NULL DEFAULT NULL COMMENT '是否结算',
  `settlement_time` datetime(0) NULL DEFAULT NULL COMMENT '结算时间',
  `settlement_price` int(11) NULL DEFAULT NULL COMMENT '结算单价 分请做好转换',
  `person_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '小程序用户id',
  `status` int(2) NULL DEFAULT NULL COMMENT '0是合同未上传1是企业自动签署合同2是客户签署合同',
  `reason` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`contract_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 216 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '合同列表 ' ROW_FORMAT = Dynamic;


DROP TABLE IF EXISTS `ta_contract_business`;
CREATE TABLE `ta_contract_business`  (
  `contract_business_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '合同与业务关系表',
  `contract_template_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '合同模板id',
  `contract_template_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '合同标题',
  `target_type` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '业务类型 raise房源认筹',
  `target_id` int(11) NULL DEFAULT NULL COMMENT '业务id raise对应销售批次id',
  `target_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '业务标题',
  `company_id` int(11) NULL DEFAULT NULL COMMENT '企业id',
  `company_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '企业标题',
  `update_date` datetime(0) NULL DEFAULT NULL,
  `create_date` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `org_id` int(11) NULL DEFAULT NULL COMMENT '公司id',
  `seal_id` int(11) NULL DEFAULT NULL COMMENT '印章id',
  `signatory_keywords` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '甲方签章定位关键字',
  `signatory_strategy` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '甲方签章策略 与法大大一致就行',
  `customer_keywords` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '乙方签章定位关键字',
  `customer_strategy` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '乙方签章策略',
  `status` int(11) NULL DEFAULT NULL COMMENT '状态 1是有效0是无效-1是删除',
  PRIMARY KEY (`contract_business_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 39 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '合同与业务关系表 ' ROW_FORMAT = Dynamic;


DROP TABLE IF EXISTS `ta_contract_user`;
CREATE TABLE `ta_contract_user`  (
  `contract_user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '电子合同用户id',
  `mini_openid` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '小程序端的openid',
  `auth_status` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '认证状态 registered已注册unregistered未注册verify审核中legalize已认证unlegalize未认证',
  `reason` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '认证失败原因',
  `create_date` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `org_id` int(11) NULL DEFAULT NULL COMMENT '公司id',
  `id_card` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '身份证号',
  `tel` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '手机号',
  `person_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '小程序用户id',
  `fadada_code` varchar(127) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '法大大的客户编号',
  `certified_address` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '个人认证地址',
  `certified_serial_number` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '认证序列号',
  `status` int(2) NULL DEFAULT NULL,
  PRIMARY KEY (`contract_user_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 154 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '电子合同注册用户表 ' ROW_FORMAT = Dynamic;


DROP TABLE IF EXISTS `ta_company`;
CREATE TABLE `ta_company`  (
  `company_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '企业id',
  `company_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '企业名称',
  `company_code` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '企业编号',
  `fadada_code` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '法大大客户编号',
  `company_manager_type` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '认证企业人员的身份 legal法人agent代理人',
  `certified_serial_number` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '认证序列号 认证的序列号',
  `certified_address` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '实名认证地址 实名认证地址',
  `certified_status` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '实名认证状态 实名认证状态',
  `reason` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '不通过原因',
  `verify_time` datetime(0) NULL DEFAULT NULL COMMENT '审核时间',
  `certified_type` int(2) NULL DEFAULT NULL COMMENT '认证方式 认证方式',
  `create_date` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `status` int(11) NULL DEFAULT NULL COMMENT '状态 1有效0无效-1删除',
  `credit_code` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '统一社会信用代码',
  `bank_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '银行名称',
  `bank_account` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '银行帐号',
  `subbranch_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '开户支行名称',
  `user_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '姓名',
  `tel` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '手机号',
  `id_card` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '身份证号码',
  `authorize_status` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '自动签章授权状态',
  `authorize_type` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '授权类型',
  `authorize_transaction_id` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '授权流水号',
  `authorize_address` varchar(3072) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '授权签署地址',
  `authorize_contract_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '授权合同编号',
  `authorize_description` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '自动签章授权结果描述',
  `authorize_download_url` varchar(3072) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '授权签署下载地址',
  `authorize_view_url` varchar(3072) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '授权签署查看地址',
  PRIMARY KEY (`company_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 93 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '企业认证表 ' ROW_FORMAT = Dynamic;


DROP TABLE IF EXISTS `ta_contract_template`;
CREATE TABLE `ta_contract_template`  (
  `contract_template_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '合同模板id',
  `create_date` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `org_id` int(11) NULL DEFAULT NULL COMMENT '公司id',
  `contract_template_name` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '合同模板标题',
  `contract_template_address` varchar(3072) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '合同模板地址',
  `status` int(11) NULL DEFAULT NULL COMMENT '状态 1有效0无效-1删除',
  PRIMARY KEY (`contract_template_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 41 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '合同模板 ' ROW_FORMAT = Dynamic;



ALTER  TABLE  `ta_raise_invite`  ADD  INDEX idx_orgId (  `org_id`  );
ALTER  TABLE  `ta_contract_user`  ADD  INDEX idx_orgId (  `org_id`  );
ALTER  TABLE  `ta_company_org`  ADD  INDEX idx_orgId (  `org_id`  );
ALTER  TABLE  `ta_contract`  ADD  INDEX idx_orgId (  `org_id`  );
ALTER  TABLE  `ta_contract_business`  ADD  INDEX idx_orgId (  `org_id`  );
ALTER  TABLE  `ta_contract_user`  ADD  INDEX idx_orgId (  `org_id`  );


INSERT INTO `sys_sms_setting`(`serial_no`, `sms_name`, `sms_type`, `tpl_code`, `tpl_sign`, `status`) VALUES (6, '电子合同', 'contract', '0804', '橙蕉', 1);


UPDATE ta_person t
INNER JOIN ( SELECT a.person_id, sum( a.points_amount ) AS points FROM ta_points_records a GROUP BY a.person_id ) s ON t.person_id = s.person_id 
SET t.remark = t.points,
t.points = s.points 
WHERE
  t.points > s.points 
  OR t.points < s.points;