/*==============================================================*/
/* DBMS name:      MySQL 5.0                                    */
/* Created on:     2018/8/24 18:10:25                           */
/*==============================================================*/
drop table if exists consumer_follow_overview;

drop table if exists feedback_order;

drop table if exists follow_detail;

drop table if exists rel_admin_follow_consumer;

drop table if exists service_finish_push_log;

drop table if exists consumer_detail_info;

drop table if exists rel_consumer_merchant;

/*==============================================================*/
/* Table: consumer_follow_overview                              */
/*==============================================================*/
create table consumer_follow_overview
(
   id                   int not null auto_increment,
	 follow_no          	varchar(32) comment '跟进客户编号',
   user_id              bigint not null comment '客户id',
   feedback_num         int default 0 comment '回访次数',
   follow_num           int default 0 comment '跟进次数',
   last_follow_at       bigint default 0 comment '最近跟进时间',
   status               tinyint default 1 comment '状态：0-待跟进，1-跟进中',
   create_source        tinyint comment '来源：1-回访工单，2-用户跟进',
   create_at            bigint comment '创建时间',
   create_by            varchar(32) comment '创建人',
   update_at            bigint comment '更新时间',
   update_by            varchar(32) comment '更新人',
   primary key (id)
);

alter table consumer_follow_overview comment '客户跟进状况';

/*==============================================================*/
/* Table: feedback_order                                        */
/*==============================================================*/
create table feedback_order
(
   id                   int not null auto_increment,
   book_detail_id       bigint not null,
   feedback_no          varchar(32) comment '回访编号',
   book_no              varchar(32) comment '预约编号',
   user_id              varchar(32) comment '客户id',
   merchant_no          national varchar(32) not null comment '商家编号',
   card_type            tinyint default 0 comment '服务卡类型',
   book_time            date comment '预约日期',
   book_week            tinyint default 0 comment '预约星期',
   time_type            tinyint default 0 comment '时间类型',
   address_telephone    national varchar(16) comment '联系人号码',
   linkman              national varchar(16) comment '联系人姓名',
   cleaner_name         national varchar(32) comment '保洁师名称',
   address_id           bigint unsigned not null comment '地址id',
   full_address         national varchar(128) comment '服务地址',
   channel_id           bigint comment '渠道id',
	 channel_name         varchar(50) comment '渠道名称',
   serviced_cnt       	int comment '已服务次数',
   total_service_cnt    int comment '服务卡总次数',
   created_at           bigint unsigned default 0 comment '创建时间',
   created_by           national varchar(32) comment '创建者',
   updated_at           bigint unsigned default 0 comment '更新时间',
   updated_by           national varchar(32) comment '更新者',
   attitude_score       int comment '服务态度评分',
   quality_score        int comment '服务质量评分',
   efficiency_score     int comment '服务速度评分',
   level          			varchar(10) comment '意愿等级',
   answers_status       tinyint default 0 comment '应答状态：0-待处理，1-未接听，2-正常',
   feedback_status      tinyint default 0 comment '回访状态：0-未回访，1-已回访',
   feedback_content     varchar(255) comment '回访内容',
   feedback_by          varchar(32) comment '回访人',
   feedback_at          bigint comment '回访时间',
   primary key (id)
);

alter table feedback_order comment '回访工单';

/*==============================================================*/
/* Table: follow_detail                                         */
/*==============================================================*/
create table follow_detail
(
   id                   int not null auto_increment,
   follow_overview_id   int not null,
   follow_no  					varchar(32) comment '跟进客户编号',
   level                varchar(10) comment '意愿等级',
   follow_context       varchar(255) comment '跟进内容',
   create_at            bigint comment '创建时间',
   create_by            varchar(32) comment '创建人',
   primary key (id)
);

alter table follow_detail comment '跟进明细';

/*==============================================================*/
/* Table: rel_admin_follow_consumer                             */
/*==============================================================*/
create table rel_admin_follow_consumer
(
	 id                   int not null auto_increment,
   follow_overview_id   int not null comment '跟进客户id',
	 follow_no  					varchar(32) comment '跟进客户编号',
   admin_id             bigint not null comment '跟进人员id',
   status               tinyint default 0 comment '跟进状态：1-跟进中，2-已移除',
   feedback_num         int default 0 comment '回访次数',
   follow_num           int default 0 comment '跟进次数',
   create_source        tinyint comment '来源：1-回访工单，2-用户跟进',
   create_at            bigint comment '创建时间',
   create_by            varchar(32) comment '创建人',
	 update_at            bigint comment '更新时间',   
	 update_by            varchar(32) comment '更新人', 
   primary key (id)
);

alter table rel_admin_follow_consumer comment '员工与客户跟进情况';


/*==============================================================*/
/* Table: service_finish_push_log                               */
/*==============================================================*/
create table service_finish_push_log
(
   id                   int not null auto_increment,
   book_detail_id       bigint not null comment '预约id',
   book_no              varchar(32) comment '预约编号',
   feedback_msg         varchar(255) comment '回访工单创建信息',
   sms_msg              varchar(255) comment '短信推送信息',
   tmpl_msg             varchar(255) comment '消息模版推送信息',
   create_dt            datetime comment '创建时间',
   code                 tinyint comment '状态码',
   primary key (id)
);

alter table service_finish_push_log comment '服务完成推送日志';


/*==============================================================*/
/* Table: consumer_detail_info                                  */
/*==============================================================*/
create table consumer_detail_info
(
   user_id              varchar(32) not null comment '用户id',
   nick_name            varchar(50) comment '用户昵称',
   register_at          bigint comment '注册时间',
   register_mobile      varchar(12) comment '注册手机号',
	 level       					varchar(10) comment '意愿等级',
   channel_no           varchar(32) comment '渠道编号',
   channel_name         varchar(255) comment '渠道名称',
   card_detail          varchar(255) comment '服务卡明细',
   max_card_type        tinyint comment '最高等级服务卡类型',
   sum_total_service_cnt int comment '服务卡总次数',
   sum_serviced_cnt     int comment '已服务总次数',
   sum_booked_cnt       int comment '已预约总次数',
   last_evaluation_status    tinyint default 0 comment '最新用户评价状态',
   last_feedback_evaluation_status tinyint default 0 comment '最新回访状态',
   follow_no            varchar(32) comment '跟进客户编号',
   feedback_num         int default 0 comment '已回访总次数',
   follow_num           int default 0 comment '跟进总次数',
   last_follow_at       bigint comment '最新跟进时间',
   admin_id             varchar(32) comment '跟进人id',
   follow_name          varchar(50) comment '跟进人名称',
   primary key (user_id)
);

alter table consumer_detail_info comment '客户详细信息';


/*==============================================================*/
/* Table: rel_consumer_merchant                                 */
/*==============================================================*/
create table rel_consumer_merchant
(
   user_id              varchar(32) not null,
   merchant_no          varchar(100),
   primary key (user_id)
);

alter table rel_consumer_merchant comment '客户商户关系';

-- 更新用户表level字段类型
alter table consumer_info modify column level varchar(10) comment '意愿等级'