DROP TABLE IF EXISTS `extension_center_statistic`;

/*==============================================================*/
/* Table: extension_center_statistic                                     */
/*==============================================================*/
CREATE TABLE `extension_center_statistic` (
	`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
	`salesman_unique_identity` VARCHAR(32) NOT NULL COMMENT '销售人员唯一标识',
	`salesman_id` VARCHAR(32) NOT NULL COMMENT '销售人员id',
	`time` DATE NOT NULL COMMENT '时间',
	`today_click_num` BIGINT(20) NULL DEFAULT '0' COMMENT '今日点击次数',
	`today_deal_num` INT(11) NULL DEFAULT '0' COMMENT '今日成交单数',
	`today_deal_amount` BIGINT(20) NULL DEFAULT '0' COMMENT '今日成交金额',
	`today_register_num` INT(11) NULL DEFAULT '0' COMMENT '今日注册用户数',
	`merchant_no` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '商家编号',
	`create_at` BIGINT(20) NULL DEFAULT '0' COMMENT '创建时间',
	`create_by` VARCHAR(32) NULL DEFAULT '' COMMENT '创建人id',
	`update_at` BIGINT(20) NULL DEFAULT '0' COMMENT '更新时间',
	`update_by` VARCHAR(32) NULL DEFAULT '' COMMENT '更新人id',
	PRIMARY KEY (`id`),
	UNIQUE INDEX `salesman_id_time_index` (`salesman_id`, `time`),
	INDEX `salesman_unique_identity` (`salesman_unique_identity`),
	INDEX `time` (`time`),
	INDEX `salesman_id` (`salesman_id`)
)
COMMENT='推广中心数据统计表'
ENGINE=INNODB
;


DROP TABLE IF EXISTS `book_serve_statement`;
CREATE TABLE `book_serve_statement` (
	`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
	`serve_no` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '服务单号',
	`unit_price` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '单次服务价格',
	`service_staff_level` TINYINT(4) NOT NULL DEFAULT '0' COMMENT '服务人员等级',
	`service_staff_id` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '服务人员id',
	`service_cost` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '服务成本',
	`scheduling_cost` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '调度费',
	`scheduling_cost_pec` INT(11) NOT NULL DEFAULT '0' COMMENT '调度费比例',
	`material_cost` BIGINT(20) NULL DEFAULT '0' COMMENT '单次物料成本',
	`platform_division_proportion` INT(11) NULL DEFAULT '0' COMMENT '平台抽成比例',
	`plalform_division` BIGINT(20) NULL DEFAULT '0' COMMENT '平台抽成金额',
	`merchant_division_proportion` INT(11) NULL DEFAULT '0' COMMENT '商户抽成比例',
	`merchant_division` BIGINT(20) NULL DEFAULT '0' COMMENT '商户抽成金额',
	`merchant_id` BIGINT(20) NULL DEFAULT '0' COMMENT '商户账号id',
	`district_division_proportion` INT(11) NULL DEFAULT '0' COMMENT '区总抽成比例',
	`district_division` BIGINT(20) NULL DEFAULT '0' COMMENT '区总抽成金额',
	`district_id` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '区总员id',
	`promoter_division_proportion` INT(11) NULL DEFAULT '0' COMMENT '推广员抽成比例',
	`promoter_division` BIGINT(20) NULL DEFAULT '0' COMMENT '推广员抽成金额',
	`promoter_id` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '推广人员id',
	`partner_earnings` BIGINT(20) NULL DEFAULT '0' COMMENT '合伙人收益',
	`partner_id` VARCHAR(32) NULL DEFAULT '' COMMENT '合伙人id',
	`create_time` BIGINT(20) NULL DEFAULT '0',
	PRIMARY KEY (`id`)
)
COMMENT='服务完成后对应的结算单'
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;


DROP TABLE IF EXISTS `order_anticipate_division`;
CREATE TABLE `order_anticipate_division` (
	`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
	`order_no` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '订单编号',
	`parent_order_no` VARCHAR(32) NULL DEFAULT '' COMMENT '主订单编号',
	`pay_money` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '订单价格',
	`service_cost` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '预期服务成本',
	`scheduling_cost` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '预期调度成本',
	`material_cost` BIGINT(20) NULL DEFAULT '0' COMMENT '预期物料成本',
	`platform_division_proportion` INT(11) NULL DEFAULT '0' COMMENT '平台抽成比例',
	`platform_division` BIGINT(20) NULL DEFAULT '0' COMMENT '平台抽成金额',
	`platform_division_one` BIGINT(20) NULL DEFAULT '0' COMMENT '平台抽成金额-单次',
	`platform_division_settled` BIGINT(20) NULL DEFAULT '0' COMMENT '已结算平台抽成金额',
	`merchant_no` VARCHAR(32) NULL DEFAULT '' COMMENT '商户账号id',
	`merchant_division` BIGINT(20) NULL DEFAULT '0' COMMENT '商户抽成金额',
	`merchant_division_one` BIGINT(20) NULL DEFAULT '0' COMMENT '商户抽成金额-单次',
	`merchant_division_proportion` INT(11) NULL DEFAULT '0' COMMENT '商户抽成比例',
	`merchant_division_settled` BIGINT(20) NULL DEFAULT '0' COMMENT '已结算商户抽成金额',
	`district_id` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '区总员id',
	`district_division` BIGINT(20) NULL DEFAULT '0' COMMENT '区总抽成金额',
	`district_division_one` BIGINT(20) NULL DEFAULT '0' COMMENT '区总抽成金额-单次',
	`district_division_proportion` INT(11) NULL DEFAULT '0' COMMENT '区总抽成比例',
	`material_cost_settled` BIGINT(20) NULL DEFAULT '0' COMMENT '已结算物料成本',
	`scheduling_cost_settled` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '已结算调度成本',
	`district_division_settled` BIGINT(20) NULL DEFAULT '0' COMMENT '已结算区总抽成金额',
	`promoter_id` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '推广人员id',
	`promoter_division` BIGINT(20) NULL DEFAULT '0' COMMENT '推广员抽成金额',
	`promoter_division_one` BIGINT(20) NULL DEFAULT '0' COMMENT '推广员抽成金额-单次',
	`promoter_division_proportion` INT(11) NULL DEFAULT '0' COMMENT '推广员抽成比例',
	`promoter_division_settled` BIGINT(20) NULL DEFAULT '0' COMMENT '已结算推广员抽成金额',
	`partner_id` VARCHAR(32) NULL DEFAULT '' COMMENT '合伙人id',
	`partner_earnings` BIGINT(20) NULL DEFAULT '0' COMMENT '合伙人收益',
	`money_after_deduct` BIGINT(20) NULL DEFAULT '0' COMMENT '扣除固定支出后剩余金额',
	`money_after_deduct_one` BIGINT(20) NULL DEFAULT '0' COMMENT '扣除固定支出后剩余金额-单次',
	`partner_earnings_settled` BIGINT(20) NULL DEFAULT '0' COMMENT '已结算合伙人收益',
	`service_cost_settled` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '已结算服务成本',
	`create_time` BIGINT(20) NULL DEFAULT '0' COMMENT '创建时间',
	`update_time` BIGINT(20) NULL DEFAULT '0' COMMENT '更新时间',
	`settle_status` TINYINT(4) NULL DEFAULT '0' COMMENT '订单结算状态 0 未结算完 1已完成结算',
	`refund_status` TINYINT(4) NULL DEFAULT '0' COMMENT '订单退款状态 0 未退款 1已退款',
	PRIMARY KEY (`id`)
)
COMMENT='订单预期分成情况'
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;

DROP TABLE IF EXISTS `promoter`;
CREATE TABLE `promoter` (
	`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
	`user_id` VARCHAR(32) NULL DEFAULT '' COMMENT '用户id',
	`parent_id` VARCHAR(32) NULL DEFAULT '' COMMENT '上级id',
	`merchant_no` VARCHAR(32) NULL DEFAULT '' COMMENT '商户编号',
	`unique_id` VARCHAR(32) NULL DEFAULT '' COMMENT 'md5用户id生成的8位唯一字符串',
	`division_proportion` INT(11) NULL DEFAULT '0' COMMENT '通用分成比例 万分比',
	`accumulative_total` BIGINT(20) NULL DEFAULT '0' COMMENT '累计收益',
	`type` TINYINT(4) NULL DEFAULT '0' COMMENT '类型 2员工 1客户 0渠道',
	`status` TINYINT(4) NULL DEFAULT '0' COMMENT '状态',
	`nick_name` VARCHAR(255) NULL DEFAULT '' COMMENT '昵称',
	`age` INT(5) NULL DEFAULT '0' COMMENT '年龄',
	`mobile` VARCHAR(16) NULL DEFAULT '' COMMENT '手机号',
	`district_id` INT(10) NULL DEFAULT '0' COMMENT '区id',
	`detail_address` VARCHAR(128) NULL DEFAULT '' COMMENT '详细地址',
	`full_address` VARCHAR(128) NULL DEFAULT '' COMMENT '完整住址',
	`lat` DECIMAL(10,6) NULL DEFAULT '0.000000' COMMENT '纬度',
	`lng` DECIMAL(10,6) NULL DEFAULT '0.000000' COMMENT '经度',
	`head_url` VARCHAR(50) NULL DEFAULT '' COMMENT '头像',
	`entry_date` DATE NULL DEFAULT NULL COMMENT '入职时间',
	`create_at` BIGINT(20) NULL DEFAULT '0' COMMENT '创建时间',
	`create_by` VARCHAR(20) NULL DEFAULT '' COMMENT '创建者',
	`update_at` BIGINT(20) NULL DEFAULT '0' COMMENT '更新时间',
	`update_by` VARCHAR(20) NULL DEFAULT '' COMMENT '更新者',
	`del_flag` TINYINT(4) NULL DEFAULT '0' COMMENT '删除标志',
	`channel_id` BIGINT(20) NULL DEFAULT '0' COMMENT '渠道id',
	PRIMARY KEY (`id`),
	UNIQUE INDEX `user_id` (`user_id`),
	UNIQUE INDEX `unique_id` (`unique_id`),
	INDEX `merchant_no` (`merchant_no`)
)
COMMENT='推广员资料表'
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=133
;


/*==============================================================*/
/* Table: vorder                                       						*/
/*==============================================================*/


ALTER TABLE `vorder`
add column salesman_id	VARCHAR(32) NULL DEFAULT '' COMMENT '销售人员id',
	ADD COLUMN `village_id` VARCHAR(32) NULL DEFAULT '' COMMENT '小区id' AFTER `user_id`;
/*==============================================================*/
/* Table: consumer_info                                       						*/
/*==============================================================*/
alter table consumer_info
add column salesman_id	VARCHAR(32) NULL DEFAULT '' COMMENT '销售人员id';

 -- 新增接口权限
INSERT INTO `function_permission` (`id`, `name`, `url`, `method`, `create_time`, `del_flag`) VALUES (238, '推广员列表', '/admin/web/promoters', 'GET', 1, 0);
INSERT INTO `function_permission` (`id`, `name`, `url`, `method`, `create_time`, `del_flag`) VALUES (239, '推广员明细', '(/admin/web/promoters/).*', 'GET', 1, 0);
INSERT INTO `function_permission` (`id`, `name`, `url`, `method`, `create_time`, `del_flag`) VALUES (240, '更新推广员', '(/admin/web/promoters/).*', 'PUT', 1, 0);
INSERT INTO `function_permission` (`id`, `name`, `url`, `method`, `create_time`, `del_flag`) VALUES (241, '更新推广员状态', '(/admin/web/promoters/).*', 'PATCH', 1, 0);
INSERT INTO `function_permission` (`id`, `name`, `url`, `method`, `create_time`, `del_flag`) VALUES (242, '更新品类报价', '(/admin/web/categories/).*(/priced)', 'POST', 1, 0);
INSERT INTO `function_permission` (`id`, `name`, `url`, `method`, `create_time`, `del_flag`) VALUES (243, '获取品类报价', '(/admin/web/categories/).*(/priced)', 'GET', 1, 0);
INSERT INTO `function_permission` (`id`, `name`, `url`, `method`, `create_time`, `del_flag`) VALUES (244, '推广员订单明细', '(/admin/web/promoters/).*(/order)', 'GET', 1, 0);
INSERT INTO `function_permission` (`id`, `name`, `url`, `method`, `create_time`, `del_flag`) VALUES (245, '导出推广员', '/admin/web/promoters/excel', 'GET', 1, 0);
INSERT INTO `function_permission` (`id`, `name`, `url`, `method`, `create_time`, `del_flag`) VALUES (246, '导入推广员', '/admin/web/promoters/excel', 'POST', 1, 0);
INSERT INTO `function_permission` (`id`, `name`, `url`, `method`, `create_time`, `del_flag`) VALUES (247, '初始化推广员', '/admin/updTableData/initPromoter', 'GET', 1, 0);

 -- 新增菜单并绑定接口权限
INSERT INTO `permission_info` (`id`, `permission_name`, `create_time`, `remark`, `del_flag`, `permission_url`, `parent_id`, `level`, `icon`, `sort`, `type`, `create_by`, `update_time`, `update_by`, `fids`)
VALUES
	(86, '推广中心', 1563380306474, '', 0, '', 0, 1, 'icon-list', 90000, 1, '1705', 1563383390142, '1705', ''),
	(87, '推广员', 1563380434697, '', 0, '/admin/web/salesMan', 86, 2, '', 90100, 1, '1705', 1563380434697, '1705', '237,238,239,240,241');
 -- 保洁表增加等级 默认为高级 1
ALTER TABLE `cleaner_info`
	ADD COLUMN `level` TINYINT(4) NULL DEFAULT '1' COMMENT '1-高级 2-资深  3金牌' AFTER `del_flag`;
 -- 商品更新默认分销为1，按比例算
ALTER TABLE `vgoods`
	CHANGE COLUMN `sale_type` `sale_type` TINYINT(4) NULL DEFAULT '1' COMMENT '分销类型:1-实付比例，2-固定金额' AFTER `coupon_batch_id`;
	
 -- 品类表增加服务成本、物料成本、单价
ALTER TABLE `goods_category`
	ADD COLUMN `unit_price` BIGINT NULL DEFAULT '0' COMMENT '单价' AFTER `del_flag`,
	ADD COLUMN `base_service_cost` BIGINT NULL DEFAULT '0' COMMENT '单次服务成本-初级' AFTER `unit_price`,
	ADD COLUMN `middle_service_cost` BIGINT NULL DEFAULT '0' COMMENT '单次服务成本-中级' AFTER `base_service_cost`,
	ADD COLUMN `high_service_cost` BIGINT NULL DEFAULT '0' COMMENT '单次服务成本-高级' AFTER `middle_service_cost`,
	ADD COLUMN `material_cost` BIGINT NULL DEFAULT '0' COMMENT '单次物料成本' AFTER `high_service_cost`;
		
