CREATE TABLE ts_consultant_kpi(
    `serial_no` INT NOT NULL AUTO_INCREMENT  COMMENT '序号' ,
    `statis_date` VARCHAR(32)    COMMENT '统计日期 格式YYYYMMDD' ,
    `org_id` INT    COMMENT '公司id' ,
    `building_id` VARCHAR(64)    COMMENT '楼盘id' ,
    `building_name` VARCHAR(1024)    COMMENT '楼盘名称' ,
    `user_id` INT    COMMENT '置业顾问ID' ,
    `user_name` VARCHAR(128)    COMMENT '置业顾问名称' ,
	`phone` VARCHAR(32)    COMMENT '置业顾问电话' ,
    `new_persons` INT    COMMENT '新增客户' ,
    `share_num` INT    COMMENT '分享次数' ,
    `visit_persons` INT    COMMENT '分享访问人数' ,
    `visit_num` INT    COMMENT '分享访问次数' ,
    `share_persons` INT    COMMENT '分享拓客' ,
    `home_page_persons` INT    COMMENT '主页访问人数' ,
    `home_page_nums` INT    COMMENT '主页访问次数' ,
    `chat_persons` INT    COMMENT '咨询数' ,
    `favor_num` INT    COMMENT '点赞数' ,
    PRIMARY KEY (serial_no)
) COMMENT = '置业顾问KPI ';;

ALTER TABLE ts_consultant_kpi ADD INDEX idx_sts_consultant_date(statis_date);;
ALTER TABLE ts_consultant_kpi ADD INDEX idx_sts_consultant_org(org_id,building_id,statis_date);;

CREATE TABLE ts_activity_daily(
    `serial_no` INT NOT NULL AUTO_INCREMENT  COMMENT '序号' ,
    `statis_date` VARCHAR(32)    COMMENT '统计时间' ,
    `org_id` INT    COMMENT '公司id' ,
    `building_id` VARCHAR(64)    COMMENT '楼盘id' ,
    `building_name` VARCHAR(1024)    COMMENT '楼盘名称' ,
    `target_type` VARCHAR(32)    COMMENT '活动类型' ,
    `target_id` VARCHAR(64)    COMMENT '活动ID' ,
    `target_name` VARCHAR(1024)    COMMENT '活动名称' ,
    `share_num` INT    COMMENT '分享次数' ,
    `share_persons` INT    COMMENT '分享人数' ,
    `visit_num` INT    COMMENT '访问次数' ,
    `visit_persons` INT    COMMENT '访问人数' ,
    `new_persons` INT    COMMENT '新增用户' ,
    `new_customers` INT    COMMENT '新增客户' ,
    PRIMARY KEY (serial_no)
) COMMENT = '活动统计 ';;

ALTER TABLE ts_activity_daily ADD INDEX sts_activity_org(org_id);;


CREATE TABLE ta_customer_from(
    `serial_no` INT NOT NULL AUTO_INCREMENT  COMMENT '序号' ,
    `person_id` VARCHAR(32)    COMMENT '用户ID' ,
    `customer_id` VARCHAR(32)    COMMENT '客户ID' ,
    `person_name` VARCHAR(128)    COMMENT '用户名' ,
    `target_type` VARCHAR(32)    COMMENT '目标类型' ,
    `target_id` VARCHAR(64)    COMMENT '目标ID' ,
    `target_name` VARCHAR(128)    COMMENT '目标名称' ,
    `org_id` INT    COMMENT '公司id' ,
    `building_id` VARCHAR(64)    COMMENT '楼盘id' ,
    `building_name` VARCHAR(128)    COMMENT '楼盘名称' ,
    `share_person_id` VARCHAR(64)    COMMENT '分享人ID' ,
    `share_person_name` VARCHAR(128)    COMMENT '分享人名称' ,
    `scene_id` VARCHAR(32)    COMMENT '场景ID' ,
    `is_org_first` TINYINT(1)    COMMENT '首次进入小程序' ,
    `is_project_first` TINYINT(1)    COMMENT '首次进入项目' ,
    `create_date` DATETIME    COMMENT '创建时间' ,
    `remark` VARCHAR(512)    COMMENT '备注' ,
    PRIMARY KEY (serial_no)
) COMMENT = '客户来源 ';;

ALTER TABLE ta_customer_from ADD INDEX idx_customer_from_org(org_id);;
ALTER TABLE ta_customer_from ADD INDEX idx_cusromer_from_project(org_id,building_id);;


ALTER TABLE `ta_person_building` 
ADD INDEX `idx_building_user`(`building_id`, `user_id`),
ADD INDEX `idx_building_only_user`(`user_id`);

ALTER TABLE `ta_share_count` 
ADD INDEX `idx_share_cout_person`(`person_id`);

ALTER TABLE `ta_favor` 
ADD INDEX `idx_favor_target`(`be_favor`, `tagert_type`);

ALTER TABLE `ta_chat` 
ADD INDEX `idx_chat_send`(`send_person`),
ADD INDEX `idx_char_receive`(`receive_person`);

ALTER TABLE `ta_person_visit_record` 
ADD INDEX `idx_event_target`(`org_id`, `event_type`, `target_id`, `visit_time`);

ALTER TABLE `ta_recommend_customer` 
ADD INDEX `idx_consultant_sts`(`org_id`, `realty_consultant`, `create_date`);

ALTER TABLE `ta_share_person_from` 
ADD COLUMN `target_name` varchar(1024) NULL COMMENT '目标名称' AFTER `is_first_time`;

ALTER TABLE `ta_share_count` 
ADD COLUMN `target_name` varchar(1024) NULL COMMENT '对象名称' AFTER `create_date`;

ALTER TABLE `ta_share_count` 
ADD COLUMN `org_id` int NULL COMMENT '组织ID' AFTER `target_name`;
ALTER TABLE `ta_share_count` 
ADD INDEX `idx_share_cout_org`(`org_id`);

--
INSERT INTO `td_biz_event_type`(`type_id`, `type_name`, `status`, `create_date`, `parent_type_id`) VALUES ('other', '其他', 1, '2020-04-27 15:57:10', NULL);


-- 菜单数据
update sys_menu t
set t.`status` = -1
where t.`menu_id` = 85;

delete from ta_role_menu where menu_id = 85;

INSERT INTO `sys_menu`(`menu_id`, `code`, `name`, `parent_code`, `icon`, `app_type`, `status`, `menu_root`, `sort_num`)
VALUES (110, '/statistical', '数据统计', '-1', NULL, 'estateagent-admin', 1, 110, 2);
INSERT INTO `sys_menu`(`menu_id`, `code`, `name`, `parent_code`, `icon`, `app_type`, `status`, `menu_root`, `sort_num`)
VALUES (111, '/statistical/monitor', '数据报表', '110', NULL, 'estateagent-admin', 1, 110, 1);
INSERT INTO `sys_menu`(`menu_id`, `code`, `name`, `parent_code`, `icon`, `app_type`, `status`, `menu_root`, `sort_num`)
VALUES (112, '/statistical/activity', '活动统计', '110', NULL, 'estateagent-admin', 1, 110, 2);
INSERT INTO `sys_menu`(`menu_id`, `code`, `name`, `parent_code`, `icon`, `app_type`, `status`, `menu_root`, `sort_num`)
VALUES (113, '/statistical/consultant', '置业顾问KPI', '110', NULL, 'estateagent-admin', 1, 110, 4);
INSERT INTO `sys_menu`(`menu_id`, `code`, `name`, `parent_code`, `icon`, `app_type`, `status`, `menu_root`, `sort_num`)
VALUES (114, '/statistical/building', '项目统计', '110', NULL, 'estateagent-admin', 1, 110, 3);

INSERT ta_role_menu ( role_id, menu_id )
SELECT s.role_id, 110 
FROM 	ta_user t
	LEFT JOIN ta_user_role s ON t.user_id = s.user_id 
WHERE
	t.is_admin = 1 
	AND s.role_id IS NOT NULL 
GROUP BY
	s.role_id;

INSERT ta_role_menu ( role_id, menu_id )
SELECT s.role_id, 111
FROM 	ta_user t
	LEFT JOIN ta_user_role s ON t.user_id = s.user_id 
WHERE
	t.is_admin = 1 
	AND s.role_id IS NOT NULL 
GROUP BY
	s.role_id;

INSERT ta_role_menu ( role_id, menu_id )
SELECT s.role_id, 112
FROM 	ta_user t
	LEFT JOIN ta_user_role s ON t.user_id = s.user_id 
WHERE
	t.is_admin = 1 
	AND s.role_id IS NOT NULL 
GROUP BY
	s.role_id;

INSERT ta_role_menu ( role_id, menu_id )
SELECT s.role_id, 113
FROM 	ta_user t
	LEFT JOIN ta_user_role s ON t.user_id = s.user_id 
WHERE
	t.is_admin = 1 
	AND s.role_id IS NOT NULL 
GROUP BY
	s.role_id;
		
INSERT ta_role_menu ( role_id, menu_id )
SELECT s.role_id, 114
FROM 	ta_user t
	LEFT JOIN ta_user_role s ON t.user_id = s.user_id 
WHERE
	t.is_admin = 1 
	AND s.role_id IS NOT NULL 
GROUP BY
	s.role_id;


-- 菜单按钮
INSERT INTO `sys_button_in_menu`(`btn_id`, `menu_id`, `name`, `status`, `code`, `remark`)
VALUES (212, 114, '导出', 1, 'admin.statistical.building.export', NULL);
INSERT INTO `sys_button_in_menu`(`btn_id`, `menu_id`, `name`, `status`, `code`, `remark`)
VALUES (213, 112, '导出', 1, 'admin.statistical.activity.export', NULL);
INSERT INTO `sys_button_in_menu`(`btn_id`, `menu_id`, `name`, `status`, `code`, `remark`)
VALUES (214, 113, '导出', 1, 'admin.statistical.consultant.export', NULL);


INSERT ta_role_button ( role_id, btn_id )
SELECT s.role_id, 212
FROM 	ta_user t
	LEFT JOIN ta_user_role s ON t.user_id = s.user_id 
WHERE
	t.is_admin = 1 
	AND s.role_id IS NOT NULL 
GROUP BY
	s.role_id;

INSERT ta_role_button ( role_id, btn_id )
SELECT s.role_id, 213
FROM 	ta_user t
	LEFT JOIN ta_user_role s ON t.user_id = s.user_id 
WHERE
	t.is_admin = 1 
	AND s.role_id IS NOT NULL 
GROUP BY
	s.role_id;

INSERT ta_role_button ( role_id, btn_id )
SELECT s.role_id, 214
FROM 	ta_user t
	LEFT JOIN ta_user_role s ON t.user_id = s.user_id 
WHERE
	t.is_admin = 1 
	AND s.role_id IS NOT NULL 
GROUP BY
	s.role_id;

INSERT ta_role_button ( role_id, btn_id )
SELECT s.role_id, 215
FROM 	ta_user t
	LEFT JOIN ta_user_role s ON t.user_id = s.user_id 
WHERE
	t.is_admin = 1 
	AND s.role_id IS NOT NULL 
GROUP BY
	s.role_id;

INSERT ta_role_button ( role_id, btn_id )
SELECT s.role_id, 216
FROM 	ta_user t
	LEFT JOIN ta_user_role s ON t.user_id = s.user_id 
WHERE
	t.is_admin = 1 
	AND s.role_id IS NOT NULL 
GROUP BY
	s.role_id;

INSERT ta_role_button ( role_id, btn_id )
SELECT s.role_id, 217
FROM 	ta_user t
	LEFT JOIN ta_user_role s ON t.user_id = s.user_id 
WHERE
	t.is_admin = 1 
	AND s.role_id IS NOT NULL 
GROUP BY
	s.role_id;


-- 置业KPI日统计
delimiter $$
CREATE PROCEDURE `pro_consultant_kpi`(IN `st_date` varchar(32))
BEGIN

DECLARE day_start datetime;
DECLARE day_end datetime;

SET day_start = STR_TO_DATE(st_date, '%Y%m%d');
SET day_end = STR_TO_DATE(concat(st_date, '235959'), '%Y%m%d%H%i%s');


-- 先删除当天的
delete from ts_consultant_kpi where statis_date = st_date;

-- 重新插入
insert into ts_consultant_kpi (
	statis_date,
	org_id,
	building_id,
	building_name,
	user_id,
	user_name,
	phone,
	new_persons,
	share_num,
	visit_persons,
	visit_num,
	share_persons,
	home_page_persons,
	home_page_nums,
	chat_persons,
	favor_num	
	)
select
	st_date as statis_date,
	t.org_id,
	s.building_id,
	n.building_name,
	t.user_id,
	t.user_name,
	t.phone,
	
	-- 新增客户的时间字段不正确
	-- 可能错误: create_date 是之前的时间, 但是最近才分配置业的情况统计不到
	(select count(*) from ta_recommend_customer a 
		where a.org_id = t.org_id
			and (a.realty_consultant = concat(t.user_id, '') or a.realty_consultant = m.person_id)
			and a.create_date BETWEEN day_start and day_end
			and a.building_id = s.building_id 
			and a.status > 0
			) as new_persons,
	
	-- 分享次数
	-- 不区分项目
	(select count(*) as share_num from ta_share_count b
		where b.person_id = m.person_id
		and b.create_date BETWEEN day_start and day_end
		and (
			b.tagert_type IN ( 'consultant', 'project', 'activity', 'group', 'h5', 'help', 'news', 'main' )
				OR b.tagert_type like 'house%'
				OR b.tagert_type like 'live%'
			)
		) as share_num,
	
	-- 分享访问人数
	(select count(DISTINCT c.person_id) from ta_share_person_from c
		where c.org_id = t.org_id
		and c.share_person = concat(t.user_id, '')
		and (
			c.target_type IN ( 'card_share', 'building_share', 'dynamic_share', 'group_share', 'h5_share', 'help_share', 'house_share', 'live_share', 'news_share', 'poster' )
				or ( c.target_type = 'share' and c.target_id = 'index' )
				)
		and (c.building_id is null or c.building_id = s.building_id)
		and c.create_date BETWEEN day_start and day_end
		) as visit_persons,
		
	-- 分享访问次数
	(select count(*) from ta_share_person_from d
		where d.org_id = t.org_id
		and d.share_person = concat(t.user_id, '')
		and (
			d.target_type IN ( 'card_share', 'building_share', 'dynamic_share', 'group_share', 'h5_share', 'help_share', 'house_share', 'live_share', 'news_share', 'poster' )
				or ( d.target_type = 'share' and d.target_id = 'index' )
				)
		and (d.building_id is null or d.building_id = s.building_id)
		and d.create_date BETWEEN day_start and day_end
		) as visit_num,
		
	-- 分享拓客
	(select count(DISTINCT e.person_id) from ta_customer_from e
		where e.org_id = t.org_id
		and e.building_id = s.building_id
		and e.share_person_id = m.person_id
		and e.target_type is not null
		and e.is_project_first = 1
		) as share_persons,
		
	-- 主页访问人数
	(select count(DISTINCT f.person_id) from ta_person_visit_record f
		where f.org_id = t.org_id
		and f.building_id = s.building_id
		and f.event_type = 'card'
		and f.target_id = m.person_id
		and f.visit_time BETWEEN day_start and day_end
	) as home_page_persons,
	
	-- 主页访问次数
	(select count(*) from ta_person_visit_record g
		where g.org_id = t.org_id
		and g.building_id = s.building_id
		and g.event_type = 'card'
		and g.target_id = m.person_id
		and g.visit_time BETWEEN day_start and day_end
	) as home_page_nums,
	
	-- 咨询数
	-- 不区分项目
	(select count(DISTINCT h.send_person) from ta_chat h
		where h.receive_person = m.person_id
		and h.create_date BETWEEN day_start and day_end
	) as chat_persons,
	
	-- 点赞数
	-- 不区分项目
	(select count(*) from ta_favor i
		where i.be_favor = m.person_id
		and i.tagert_type = 'consultant'
		and i.create_date BETWEEN day_start and day_end
	) as favor_num
	
from ta_user t
left join ta_person_building s on t.user_id = s.user_id
left join ta_person m on t.org_id = m.org_id and t.user_id = m.user_id and m.status = 1
left join ta_building n on n.building_id = s.building_id
where t.is_consultant = 1;

END$$


delimiter $$
CREATE PROCEDURE `pro_update_share_target_name`()
BEGIN
	
	-- 分享项目
	UPDATE ta_share_person_from t
	INNER JOIN ta_building s ON s.building_id = t.target_id
	SET t.target_name = s.building_name
	WHERE t.target_type = 'building_share'
	AND t.target_name is null;
	
	UPDATE ta_share_count t
	INNER JOIN ta_building s ON s.building_id = t.be_share
	SET t.target_name = s.building_name,
	  t.org_id = s.org_id
	WHERE t.tagert_type = 'project'
	AND t.target_name is null;
	
	-- 普通活动
	UPDATE ta_share_person_from t
	INNER JOIN ta_building_dynamic s ON s.dynamic_id = t.target_id
	SET t.target_name = s.title
	WHERE t.target_type = 'dynamic_share'
	AND t.target_name is null;
	
	UPDATE ta_share_count t
	INNER JOIN ta_building_dynamic s ON s.dynamic_id = t.be_share
	SET t.target_name = s.title,
	  t.org_id = s.org_id
	WHERE t.tagert_type = 'activity'
	AND t.target_name is null;
	
	-- 助力活动
	UPDATE ta_share_person_from t
	INNER JOIN ta_help_activity s ON s.help_activity_id = t.target_id
	SET t.target_name = s.title,
	  t.org_id = s.org_id
	WHERE t.target_type = 'help_share'
	AND t.target_name is null;
	
	UPDATE ta_share_count t
	INNER JOIN ta_help_activity s ON s.help_activity_id = t.be_share
	SET t.target_name = s.title
	WHERE t.tagert_type = 'help'
	AND t.target_name is null;
		
	-- 拼团活动
	UPDATE ta_share_person_from t
	INNER JOIN ta_share_activity s ON s.group_activity_id = t.target_id
	SET t.target_name = s.activity_name
	WHERE t.target_type = 'group_share'
	AND t.target_name is null;
	
	UPDATE ta_share_count t
	INNER JOIN ta_share_activity s ON s.group_activity_id = t.be_share
	SET t.target_name = s.activity_name,
	  t.org_id = s.org_id
	WHERE t.tagert_type = 'group'
	AND t.target_name is null;
	
	-- H5 活动
	UPDATE ta_share_person_from t
	INNER JOIN ta_drainage s ON s.drainage_id = t.target_id
	SET t.target_name = s.`name`
	WHERE t.target_type = 'h5_share'
	AND t.target_name is null;
	
	UPDATE ta_share_count t
	INNER JOIN ta_drainage s ON s.drainage_id = t.be_share
	SET t.target_name = s.`name`,
	  t.org_id = s.org_id
	WHERE t.tagert_type = 'h5'
	AND t.target_name is null;
	
	-- 资讯
	UPDATE ta_share_person_from t
	INNER JOIN ta_news s ON s.news_id = t.target_id
	SET t.target_name = s.news_name
	WHERE t.target_type = 'news_share'
	AND t.target_name is null;
	
	UPDATE ta_share_count t
	INNER JOIN ta_news s ON s.news_id = t.be_share
	SET t.target_name = s.news_name,
	  t.org_id = s.org_id
	WHERE t.tagert_type = 'news'
	AND t.target_name is null;
	
	-- 直播活动
	UPDATE ta_share_person_from t
	INNER JOIN ta_live_activity s ON s.live_activity_id = t.target_id
	SET t.target_name = s.live_activity_title
	WHERE t.target_type = 'live_share'
	AND t.target_name is null;
	
	UPDATE ta_share_count t
	INNER JOIN ta_live_activity s ON s.live_activity_id = t.be_share
	SET t.target_name = s.live_activity_title,
	  t.org_id = s.org_id
	WHERE t.tagert_type like 'live%'
	AND t.target_name is null;
	
	-- 房源列表
	UPDATE ta_share_person_from t
	INNER JOIN ta_sales_batch s ON s.sales_batch_id = t.target_id
	INNER JOIN ta_building m on m.building_id = s.building_id
	SET t.target_name = m.building_name
	WHERE t.target_type = 'house_share'
	AND t.target_name is null;
	
	UPDATE ta_share_count t
	INNER JOIN ta_sales_batch s ON s.sales_batch_id = t.be_share
	INNER JOIN ta_building m on m.building_id = s.building_id
	SET t.target_name = m.building_name,
	  t.org_id = s.org_id
	WHERE t.tagert_type like 'house%'
	AND t.target_name is null;
	
	
	-- 更新 org
	UPDATE ta_share_count t
	INNER JOIN ta_person s ON s.person_id = t.be_share
	SET t.org_id = s.org_id
	WHERE t.tagert_type = 'consultant'
	AND t.org_id is null;
	
END$$

delimiter $$
CREATE PROCEDURE `pro_activity_daily`(IN `st_date` varchar(32))
BEGIN
DECLARE day_start datetime;
DECLARE day_end datetime;

SET day_start = STR_TO_DATE(st_date, '%Y-%m-%d');
SET day_end = STR_TO_DATE(concat(st_date, ' 235959'), '%Y-%m-%d %H%i%s');


delete from ts_activity_daily where statis_date = st_date;

insert into ts_activity_daily (
	`statis_date`,
	`org_id`,
	`building_id`,
	`building_name`,
	`target_type`,
	`target_id`,
	`target_name`,
	`share_persons`,
	`share_num`,
	`visit_persons`,
	`visit_num`,
	`new_persons`,
	`new_customers`
)

select 
	st_date as statis_date,
	a.org_id,
	a.building_id,
	a.building_name,
	a.target_type,
	a.target_id,
	a.target_name,

	-- 活动分享人数
	(SELECT count(DISTINCT b.person_id) FROM ta_share_count b 
	WHERE b.org_id = a.org_id
		AND b.tagert_type like concat(a.target_type,'%')
		AND b.be_share = a.target_id
		AND b.create_date BETWEEN day_start AND day_end
	) as share_persons,

	-- 活动分享次数
	(SELECT count(*) FROM ta_share_count c 
	WHERE c.org_id = a.org_id
		AND c.tagert_type like concat(a.target_type,'%')
		AND c.be_share = a.target_id
		AND c.create_date BETWEEN day_start AND day_end
	) as share_num,

	-- 活动访问人数
	(select count( DISTINCT d.person_id ) from ta_person_visit_record d
	WHERE d.org_id = a.org_id
		AND if(d.target_type is null ,d.event_type = a.target_type, d.target_type = REPLACE(a.target_type, 'activity', 'dynamic'))
		AND d.target_id = a.target_id
		AND d.`event` = 'detail'
		AND d.visit_time BETWEEN day_start AND day_end
	) as visit_persons,

	-- 活动访问次数
	(select count(*) from ta_person_visit_record e
	WHERE e.org_id = a.org_id
		AND if(e.target_type is null ,e.event_type = a.target_type, e.target_type = REPLACE(a.target_type, 'activity', 'dynamic'))
		AND e.target_id = a.target_id
		AND e.`event` = 'detail'
		AND e.visit_time BETWEEN day_start AND day_end
	) as visit_num,

	-- 分享新增用户
	(select count(*) from ta_share_person_from f
	where	f.org_id = a.org_id
	AND f.target_type = concat(REPLACE(a.target_type, 'activity', 'dynamic'), '_share')
	AND f.target_id = a.target_id
	AND f.`status` > 0
	AND f.create_date BETWEEN day_start AND day_end
	AND f.is_first_time = 1
	) as new_persons,

	-- 分享新增客户
	(select count(*) from ta_customer_from g
	inner join ta_person h on h.person_id = g.person_id
	where	g.org_id = a.org_id
	AND g.target_type = a.target_type
	AND g.target_id = a.target_id
	AND g.create_date BETWEEN day_start AND day_end
	AND g.is_org_first = 1
	) as new_customers

from (
	-- 增加时间的过滤, 是为了减少统计的数据量
	-- 如果统计的时候, 活动还没有创建, 那么活动不创建
	-- 主要是针对初始化数据
	
	-- 报名活动
	select t.org_id, t.building_id, s.building_name, t.dynamic_id as target_id, t.title as target_name, 'activity' as target_type, t.create_date from ta_building_dynamic t
	left join ta_building s on s.building_id = t.building_id
	where t.`status` > -1
	union all
	
	-- h5
	select t.org_id, t.building_id, s.building_name, t.drainage_id as target_id, t.`name` as target_name, 'h5' as target_type, t.create_date from ta_drainage t
	left join ta_building s on s.building_id = t.building_id
	where t.`status` > -1
	union all
	
	-- 助力
	select t.org_id, t.building_id, s.building_name, t.help_activity_id as target_id, t.title as target_name, 'help' as target_type, t.create_date from ta_help_activity t
	left join ta_building s on s.building_id = t.building_id
	where t.`status` > -1
	union all
	
	-- 拼团
	select t.org_id, t.building_id, s.building_name, t.group_activity_id as target_id, t.activity_name as target_name, 'group' as target_type, t.start_time as create_date from ta_share_activity t
	left join ta_building s on s.building_id = t.building_id
	where t.`status` > -1
	union all
		
	-- 直播
	select t.org_id, t.building_id, s.building_name, t.live_activity_id as target_id, t.live_activity_title as target_name, 'live' as target_type, t.create_date from ta_live_activity t 
	left join ta_building s on s.building_id = t.building_id
	where t.`status` > -1
	) a;

END$$

delimiter $$
CREATE PROCEDURE `pro_init_activity_daily`(IN `days` int)
BEGIN
	DECLARE inx int DEFAULT 1;
	DECLARE dt VARCHAR(10);
	DECLARE nw datetime DEFAULT now();
	
	while inx <= days do
		set dt = DATE_FORMAT(DATE_SUB(nw, INTERVAL inx DAY),"%Y-%m-%d");

		call pro_activity_daily(dt);

		set inx = inx + 1;		
	end while;
END$$


CREATE EVENT `evt_update_share_target_name_2h`
ON SCHEDULE
EVERY 2 HOUR STARTS CURRENT_TIMESTAMP
DO BEGIN
	CALL pro_update_share_target_name();
END;



-- 数据初始化脚本
delimiter $$
CREATE PROCEDURE `pro_init_consultant_kpi`(IN days int)
BEGIN
	DECLARE inx int DEFAULT 1;
	DECLARE dt VARCHAR(10);
	DECLARE nw datetime DEFAULT now();
	
	while inx <= days do
		set dt = DATE_FORMAT(DATE_SUB(nw, INTERVAL inx DAY),"%Y%m%d");

		call pro_consultant_kpi(dt);

		set inx = inx + 1;		
	end while;
END$$

-- 置业KPI统计每 4 小时执行一次
CREATE EVENT `evt_consultant_kpi_daily`
ON SCHEDULE
EVERY 4 HOUR STARTS CURRENT_TIMESTAMP
DO BEGIN

call pro_consultant_kpi(DATE_FORMAT(now(),"%Y%m%d"));

END;

-- 活动统计每 4 小时执行一次
CREATE EVENT `evt_activity_daily`
ON SCHEDULE
EVERY 4 HOUR STARTS CURRENT_TIMESTAMP
DO BEGIN

call pro_activity_daily(DATE_FORMAT(now(),"%Y-%m-%d"));

END;

ALTER TABLE td_live_dict ADD sort INT ( 11 );

UPDATE td_live_dict 
SET sort = 6 
WHERE
	id = 1;
UPDATE td_live_dict 
SET sort = 5 
WHERE
	id = 2;
UPDATE td_live_dict 
SET sort = 4 
WHERE
	id = 3;
UPDATE td_live_dict 
SET sort = 3 
WHERE
	id = 4;
UPDATE td_live_dict 
SET sort = 2 
WHERE
	id = 5;
UPDATE td_live_dict 
SET sort = 1 
WHERE
	id = 6;
	
ALTER TABLE ta_share_person_from ADD INDEX idx_share_person_personId ( person_id );
ALTER TABLE ta_person_from_record ADD INDEX idx_from_record_personId ( share_person_id );
ALTER TABLE ta_extend_content ADD create_user INT ( 11 );
ALTER TABLE ta_drainage ADD create_user INT ( 11 );

INSERT INTO `td_report` ( `report_code`, `report_name`, `is_project`, `remark`, `status`, `create_date` )
VALUES
	( 'total_number_of_visit_users', '今日访问次数', NULL, NULL, 1, '2020-04-23 16:07:18' );
INSERT INTO ta_building_report ( `report_code`, `create_date`, `org_id` ) SELECT
'total_number_of_visit_users',
NOW( ),
org_id 
FROM
	ta_miniapp;
	
UPDATE ta_recommend_customer t
LEFT JOIN ta_person p ON t.person_id = p.person_id 
SET t.sex = p.gender 
WHERE
	t.sex IS NULL;





