-- set innodb lock wait timeout
SET SESSION innodb_lock_wait_timeout = 7200;

CREATE TABLE clue_pool
(
    `id`              VARCHAR(32)  NOT NULL COMMENT 'id',
    `name`            VARCHAR(255) NOT NULL COMMENT '线索池名称',
    `scope_id`        TEXT         NOT NULL COMMENT '成员id',
    `organization_id` VARCHAR(32)  NOT NULL COMMENT '组织架构id',
    `owner_id`        TEXT         NOT NULL COMMENT '管理员id',
    `enable`          BIT(1)       NOT NULL DEFAULT 1 COMMENT '启用/禁用',
    `auto`            BIT(1)       NOT NULL DEFAULT 0 COMMENT '自动回收',
    `create_time`     BIGINT       NOT NULL COMMENT '创建时间',
    `update_time`     BIGINT       NOT NULL COMMENT '更新时间',
    `create_user`     VARCHAR(32)  NOT NULL COMMENT '创建人',
    `update_user`     VARCHAR(32)  NOT NULL COMMENT '更新人',
    PRIMARY KEY (id)
) COMMENT = '线索池'
    ENGINE = InnoDB
    DEFAULT CHARSET = utf8mb4
    COLLATE = utf8mb4_general_ci;

CREATE INDEX idx_organization_id ON clue_pool (organization_id ASC);

CREATE TABLE clue_pool_pick_rule
(
    `id`                 VARCHAR(32) NOT NULL COMMENT 'ID',
    `pool_id`            VARCHAR(32) NOT NULL COMMENT '线索池ID',
    `limit_on_number`    BIT(1)      NOT NULL DEFAULT 1 COMMENT '是否限制领取数量',
    `pick_number`        INT COMMENT '领取数量',
    `limit_pre_owner`    BIT(1)      NOT NULL DEFAULT 1 COMMENT '是否限制前归属人领取',
    `pick_interval_days` INT COMMENT '领取间隔天数',
    `create_user`        VARCHAR(32) NOT NULL COMMENT '创建人',
    `create_time`        BIGINT      NOT NULL COMMENT '创建时间',
    `update_user`        VARCHAR(32) NOT NULL COMMENT '更新人',
    `update_time`        BIGINT      NOT NULL COMMENT '更新时间',
    PRIMARY KEY (id)
) COMMENT = '线索池领取规则'
    ENGINE = InnoDB
    DEFAULT CHARSET = utf8mb4
    COLLATE = utf8mb4_general_ci;

CREATE INDEX idx_pool_id ON clue_pool_pick_rule (pool_id ASC);

CREATE TABLE clue_pool_recycle_rule
(
    `id`            VARCHAR(32) NOT NULL COMMENT 'ID',
    `pool_id`       VARCHAR(32) NOT NULL COMMENT '线索池ID',
    `operator`      VARCHAR(10) COMMENT '操作符',
    `condition`     TEXT COMMENT '回收条件',
    `create_time`   BIGINT      NOT NULL COMMENT '创建时间',
    `update_time`   BIGINT      NOT NULL COMMENT '更新时间',
    `create_user`   VARCHAR(32) NOT NULL COMMENT '创建人',
    `update_user`   VARCHAR(32) NOT NULL COMMENT '更新人',
    PRIMARY KEY (id)
) COMMENT = '线索池回收规则'
    ENGINE = InnoDB
    DEFAULT CHARSET = utf8mb4
    COLLATE = utf8mb4_general_ci;

CREATE INDEX idx_pool_id ON clue_pool_recycle_rule (pool_id ASC);

CREATE TABLE clue_capacity
(
    `id`              VARCHAR(32) NOT NULL COMMENT 'id',
    `organization_id` VARCHAR(32) NOT NULL COMMENT '组织架构ID',
    `scope_id`        TEXT        NOT NULL COMMENT '范围ID',
    `capacity`        INT         COMMENT '库容;NULL:不限制',
    `create_time`     BIGINT      NOT NULL COMMENT '创建时间',
    `update_time`     BIGINT      NOT NULL COMMENT '更新时间',
    `create_user`     VARCHAR(32) NOT NULL COMMENT '创建人',
    `update_user`     VARCHAR(32) NOT NULL COMMENT '更新人',
    PRIMARY KEY (id)
) COMMENT = '线索池库容设置'
    ENGINE = InnoDB
    DEFAULT CHARSET = utf8mb4
    COLLATE = utf8mb4_general_ci;

CREATE INDEX idx_organization_id ON clue_capacity (organization_id ASC);

CREATE TABLE clue(
     `id` VARCHAR(32) NOT NULL   COMMENT 'id' ,
     `name` VARCHAR(255) NOT NULL   COMMENT '客户名称' ,
     `owner` VARCHAR(32)    COMMENT '负责人' ,
     `last_stage` VARCHAR(30)    COMMENT '上次修改前的线索状态' ,
     `stage` VARCHAR(30) NOT NULL   COMMENT '线索状态' ,
     `collection_time` BIGINT    COMMENT '领取时间' ,
     `contact` VARCHAR(255)    COMMENT '联系人名称' ,
     `phone` VARCHAR(30)    COMMENT '联系人电话' ,
     `products`        VARCHAR(1000)   NOT NULL COMMENT '意向产品',
     `organization_id` VARCHAR(32) NOT NULL   COMMENT '组织id' ,
     `create_time` BIGINT NOT NULL   COMMENT '创建时间' ,
     `update_time` BIGINT NOT NULL   COMMENT '更新时间' ,
     `create_user` VARCHAR(32) NOT NULL   COMMENT '创建人' ,
     `update_user` VARCHAR(32) NOT NULL   COMMENT '更新人' ,
     `transition_type` VARCHAR(30)   DEFAULT 'NONE' COMMENT '转移成客户或者商机' ,
     `transition_id` VARCHAR(32)    COMMENT '客户id或者商机id' ,
     `in_shared_pool` BIT(1) NOT NULL  DEFAULT 0 COMMENT '是否在线索池' ,
     `follower` VARCHAR(32)    COMMENT '最新跟进人' ,
     `follow_time` BIGINT(255)    COMMENT '最新跟进时间' ,
     `pool_id` VARCHAR(32)    COMMENT '线索池ID' ,
     PRIMARY KEY (id)
)  COMMENT = '线索'
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_general_ci;

CREATE INDEX idx_organization_id ON `clue` (organization_id ASC);
CREATE INDEX idx_pool_id ON `clue` (pool_id ASC);
CREATE INDEX idx_follower ON clue (follower ASC);
CREATE INDEX idx_follow_time ON clue (follow_time ASC);

CREATE TABLE clue_field
(
    `id`          VARCHAR(32)  NOT NULL COMMENT 'id',
    `resource_id` VARCHAR(32)  NOT NULL COMMENT '线索id',
    `field_id`    VARCHAR(32)  NOT NULL COMMENT '自定义属性id',
    `field_value` VARCHAR(255) NOT NULL COMMENT '自定义属性值',
    PRIMARY KEY (id)
) COMMENT = '线索自定义属性'
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_general_ci;

CREATE INDEX idx_resource_id_field_id_field_value ON clue_field (resource_id, field_id, field_value);


CREATE TABLE clue_field_blob
(
    `id`          VARCHAR(32) NOT NULL COMMENT 'id',
    `resource_id` VARCHAR(32) NOT NULL COMMENT '客户联系人id',
    `field_id`    VARCHAR(32) NOT NULL COMMENT '自定义属性id',
    `field_value` TEXT        NOT NULL COMMENT '自定义属性值',
    PRIMARY KEY (id)
) COMMENT = '线索自定义属性大文本'
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_general_ci;

CREATE INDEX idx_resource_id ON clue_field_blob (resource_id ASC);


CREATE TABLE clue_owner
(
    `id`              VARCHAR(32) NOT NULL COMMENT 'id',
    `clue_id`         VARCHAR(32) NOT NULL COMMENT '线索id',
    `owner`           VARCHAR(32) NOT NULL COMMENT '责任人',
    `collection_time` BIGINT      NOT NULL COMMENT '领取时间',
    `end_time`        BIGINT      NOT NULL COMMENT '结束时间',
    `operator`        VARCHAR(32) NOT NULL COMMENT '操作人',
    PRIMARY KEY (id)
) COMMENT = '线索历史责任人'
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_general_ci;

CREATE INDEX idx_clue_id ON clue_owner (clue_id ASC);


-- set innodb lock wait timeout to default
SET SESSION innodb_lock_wait_timeout = DEFAULT;
