﻿-- 预约池增加保洁类型字段
ALTER TABLE `book`
	ADD COLUMN `cleaner_type` TINYINT NULL DEFAULT '0' COMMENT '保洁类型 0保洁师 1保养师' AFTER `del_flag`;
ALTER TABLE `book`
	DROP INDEX `merchant_no`,
	ADD UNIQUE INDEX `merchant_no` (`merchant_no`, `book_time`, `time_type`, `district_id`, `cleaner_type`);

	
-- 跟新保养师的类型
UPDATE cleaner_info c SET type = 1 WHERE NOT EXISTS(SELECT 1 FROM rel_cleaner_skill r WHERE r.user_id = c.user_id AND r.skill_no = 'GCN1809300239114');


ALTER TABLE consumer_detail_info ADD COLUMN contact_phone VARCHAR(12) NULL DEFAULT NULL COMMENT '联系电话';

ALTER TABLE consumer_detail_info ADD COLUMN cleaner_name VARCHAR(50) NULL DEFAULT NULL COMMENT '服务保洁师';

ALTER TABLE consumer_detail_info ADD COLUMN address VARCHAR(255) NULL DEFAULT NULL COMMENT '地址';

ALTER TABLE consumer_detail_info ADD COLUMN buy_date BIGINT(20) NULL DEFAULT NULL COMMENT '购买时间';

ALTER TABLE consumer_detail_info ADD COLUMN server_date BIGINT(20) NULL DEFAULT NULL COMMENT '服务时间';

ALTER TABLE consumer_detail_info ADD COLUMN sum_balance_cnt INT(11) NULL DEFAULT NULL COMMENT '剩余服务次数';

ALTER TABLE consumer_detail_info ADD COLUMN remark VARCHAR(255) NULL DEFAULT NULL COMMENT '客户备注信息';

ALTER TABLE consumer_detail_info ADD COLUMN 
merchant_name VARCHAR(50) NULL DEFAULT '' COMMENT '商户名称';

ALTER TABLE consumer_detail_info ADD COLUMN 
not_call_num INT(11) NULL DEFAULT NULL COMMENT '未电话天数';

ALTER TABLE consumer_detail_info ADD COLUMN 
total_buy_num INT(11) NULL DEFAULT '0' COMMENT '累计购买次数';

ALTER TABLE consumer_detail_info ADD COLUMN 
max_card_type_name VARCHAR(50) NULL DEFAULT '' COMMENT '拥有最高卡类名称';

ALTER TABLE consumer_detail_info ADD COLUMN 
buy_date_str VARCHAR(50) NULL DEFAULT '' COMMENT '购买时间字符串';

ALTER TABLE consumer_detail_info ADD COLUMN 
server_date_str VARCHAR(50) NULL DEFAULT '' COMMENT '服务时间字符串';

ALTER TABLE `consumer_detail_info`
	CHANGE COLUMN `nick_name` `nick_name` VARCHAR(50) NULL DEFAULT '' COMMENT '用户昵称' AFTER `user_id`,
	CHANGE COLUMN `register_at` `register_at` BIGINT(20) NULL DEFAULT '0' COMMENT '注册时间' AFTER `nick_name`,
	CHANGE COLUMN `register_mobile` `register_mobile` VARCHAR(12) NULL DEFAULT '' COMMENT '注册手机号' AFTER `register_at`,
	CHANGE COLUMN `level` `level` VARCHAR(10) NULL DEFAULT '' COMMENT '意愿等级' AFTER `register_mobile`,
	CHANGE COLUMN `channel_no` `channel_no` VARCHAR(32) NULL DEFAULT '' COMMENT '渠道编号' AFTER `level`,
	CHANGE COLUMN `channel_name` `channel_name` VARCHAR(255) NULL DEFAULT '' COMMENT '渠道名称' AFTER `channel_no`,
	CHANGE COLUMN `card_detail` `card_detail` VARCHAR(255) NULL DEFAULT '' COMMENT '服务卡明细' AFTER `channel_name`,
	CHANGE COLUMN `max_card_type` `max_card_type` TINYINT(4) NULL DEFAULT '0' COMMENT '最高等级服务卡类型' AFTER `card_detail`,
	CHANGE COLUMN `sum_total_service_cnt` `sum_total_service_cnt` INT(11) NULL DEFAULT '0' COMMENT '服务卡总次数' AFTER `max_card_type`,
	CHANGE COLUMN `sum_serviced_cnt` `sum_serviced_cnt` INT(11) NULL DEFAULT '0' COMMENT '已服务总次数' AFTER `sum_total_service_cnt`,
	CHANGE COLUMN `sum_booked_cnt` `sum_booked_cnt` INT(11) NULL DEFAULT '0' COMMENT '已预约总次数' AFTER `sum_serviced_cnt`,
	CHANGE COLUMN `follow_no` `follow_no` VARCHAR(32) NULL DEFAULT '' COMMENT '跟进客户编号' AFTER `last_feedback_evaluation_status`,
	CHANGE COLUMN `last_follow_at` `last_follow_at` BIGINT(20) NULL DEFAULT '0' COMMENT '最新跟进时间' AFTER `follow_num`,
	CHANGE COLUMN `admin_id` `admin_id` VARCHAR(32) NULL DEFAULT '' COMMENT '跟进人id' AFTER `last_follow_at`,
	CHANGE COLUMN `follow_name` `follow_name` VARCHAR(50) NULL DEFAULT '' COMMENT '跟进人名称' AFTER `admin_id`,
	CHANGE COLUMN `contact_phone` `contact_phone` VARCHAR(12) NULL DEFAULT '' COMMENT '联系电话' AFTER `follow_name`,
	CHANGE COLUMN `cleaner_name` `cleaner_name` VARCHAR(50) NULL DEFAULT '' COMMENT '服务保洁师' AFTER `contact_phone`,
	CHANGE COLUMN `address` `address` VARCHAR(255) NULL DEFAULT '' COMMENT '地址' AFTER `cleaner_name` ,
	CHANGE COLUMN `buy_date` `buy_date` BIGINT(20) NULL DEFAULT '0' COMMENT '购买时间' AFTER `address` ,
	CHANGE COLUMN `server_date` `server_date` BIGINT(20) NULL DEFAULT '0' COMMENT '服务时间' AFTER `buy_date` ,
	CHANGE COLUMN `sum_balance_cnt` `sum_balance_cnt` INT(11) NULL DEFAULT '0' COMMENT '剩余服务次数' AFTER `server_date` ,
	CHANGE COLUMN `remark` `remark` VARCHAR(255) NULL DEFAULT '0' COMMENT '客户备注信息' AFTER `sum_balance_cnt` ;

ALTER TABLE `rel_consumer_merchant`
	CHANGE COLUMN `merchant_no` `merchant_no` VARCHAR(100) NULL DEFAULT '' AFTER `user_id`;

ALTER ALGORITHM = UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `rel_consumer_merchant_view` AS SELECT `c`.`user_id` AS `user_id`, IF((IFNULL(`tmp1`.`merchant_no`,'') = IFNULL(`tmp2`.`coupon_merchant_no`,'')), IFNULL(`tmp1`.`merchant_no`,''), GROUP_CONCAT(IFNULL(`tmp1`.`merchant_no`,''),',', IFNULL(`tmp2`.`coupon_merchant_no`,'') SEPARATOR ',')) AS `merchant_no`
FROM ((`1hjz`.`consumer_info` `c`
LEFT JOIN (
SELECT `t1`.`user_id` AS `user_id`, IF((`t1`.`order_merchant` = `t2`.`card_merchant_no`),`t1`.`order_merchant`, GROUP_CONCAT(`t1`.`order_merchant`,',',`t2`.`card_merchant_no` SEPARATOR ',')) AS `merchant_no`
FROM (((
SELECT `t`.`user_id` AS `user_id`, GROUP_CONCAT(`t`.`merchant_no`
ORDER BY `t`.`merchant_no` ASC SEPARATOR ',') AS `order_merchant`
FROM (
SELECT `c`.`user_id` AS `user_id`,`v`.`merchant_no` AS `merchant_no`
FROM (`1hjz`.`consumer_info` `c`
JOIN `1hjz`.`vorder` `v`)
WHERE (`c`.`user_id` = `v`.`user_id`)
GROUP BY `c`.`user_id`,`v`.`merchant_no`) `t`
GROUP BY `t`.`user_id`)) `t1`
JOIN (
SELECT `t`.`user_id` AS `user_id`, GROUP_CONCAT(`t`.`merchant_no`
ORDER BY `t`.`merchant_no` ASC SEPARATOR ',') AS `card_merchant_no`
FROM (
SELECT `c`.`user_id` AS `user_id`,`s`.`merchant_no` AS `merchant_no`
FROM (`1hjz`.`consumer_info` `c`
JOIN `1hjz`.`service_card` `s`)
WHERE (`c`.`user_id` = `s`.`user_id`)
GROUP BY `c`.`user_id`,`s`.`merchant_no`) `t`
GROUP BY `t`.`user_id`) `t2`)
WHERE (`t1`.`user_id` = `t2`.`user_id`)
GROUP BY `t1`.`user_id`) `tmp1` ON((`c`.`user_id` = `tmp1`.`user_id`)))
LEFT JOIN (
SELECT `t`.`user_id` AS `user_id`, GROUP_CONCAT(`t`.`merchant_no`
ORDER BY `t`.`merchant_no` ASC SEPARATOR ',') AS `coupon_merchant_no`
FROM (
SELECT `c`.`user_id` AS `user_id`,`cp`.`merchant_no` AS `merchant_no`
FROM (`1hjz`.`consumer_info` `c`
JOIN `1hjz`.`coupon` `cp`)
WHERE (`c`.`user_id` = `cp`.`user_id`)
GROUP BY `c`.`user_id`,`cp`.`merchant_no`) `t`
GROUP BY `t`.`user_id`) `tmp2` ON((`c`.`user_id` = `tmp2`.`user_id`)))
GROUP BY `c`.`user_id`,`tmp1`.`merchant_no`,`tmp2`.`coupon_merchant_no`
