-- ============================================================
-- 黄精粉健康商城 HJF 数据库迁移脚本
-- 版本：Phase 3（改造复用版）
-- 日期：2026-03-21
-- 执行说明：
--   1. 兼容 MySQL 5.7+，数据库前缀为 eb_
--   2. 按顺序执行 P3-01 ~ P3-07
--   3. 所有操作均做幂等处理，可重复执行
--   4. 遵循 PRD 改造复用原则：会员等级复用 eb_agent_level 体系，
--      使用 eb_user.agent_level (FK) 代替独立的 member_level 字段
-- ============================================================

-- ============================================================
-- P3-01: 公排池表
-- ============================================================

CREATE TABLE IF NOT EXISTS `eb_queue_pool` (
  `id`            int(11)        NOT NULL AUTO_INCREMENT              COMMENT '自增主键',
  `uid`           int(11)        NOT NULL DEFAULT 0                   COMMENT '用户 ID',
  `order_id`      varchar(50)    NOT NULL DEFAULT ''                  COMMENT '来源订单号（eb_store_order.order_id）',
  `amount`        decimal(10,2)  NOT NULL DEFAULT 3600.00             COMMENT '报单金额（元）',
  `queue_no`      int(11)        NOT NULL DEFAULT 0                   COMMENT '全局排队序号（自增，唯一）',
  `status`        tinyint(1)     NOT NULL DEFAULT 0                   COMMENT '状态：0=排队中 1=已退款',
  `refund_time`   int(11)        NOT NULL DEFAULT 0                   COMMENT '退款时间（Unix 时间戳）',
  `trigger_batch` int(11)        NOT NULL DEFAULT 0                   COMMENT '触发退款的批次号',
  `add_time`      int(11)        NOT NULL DEFAULT 0                   COMMENT '入队时间（Unix 时间戳）',
  PRIMARY KEY (`id`),
  UNIQUE  KEY `uniq_queue_no`         (`queue_no`),
  INDEX         `idx_uid`             (`uid`),
  INDEX         `idx_status_add_time` (`status`, `add_time`),
  INDEX         `idx_trigger_batch`   (`trigger_batch`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='公排池';


-- ============================================================
-- P3-02: 积分释放日志表
-- ============================================================

CREATE TABLE IF NOT EXISTS `eb_points_release_log` (
  `id`           int(11)      NOT NULL AUTO_INCREMENT             COMMENT '自增主键',
  `uid`          int(11)      NOT NULL DEFAULT 0                  COMMENT '用户 ID',
  `points`       int(11)      NOT NULL DEFAULT 0                  COMMENT '积分数量（绝对值）',
  `pm`           tinyint(1)   NOT NULL DEFAULT 1                  COMMENT '收支方向：1=收入 0=支出',
  `type`         varchar(50)  NOT NULL DEFAULT ''                 COMMENT '类型：reward_direct/reward_umbrella/release/consume',
  `title`        varchar(255) NOT NULL DEFAULT ''                 COMMENT '标题',
  `mark`         varchar(500) NOT NULL DEFAULT ''                 COMMENT '备注',
  `status`       varchar(30)  NOT NULL DEFAULT 'frozen'           COMMENT '状态：frozen=冻结 released=已释放 consumed=已消费',
  `order_id`     varchar(50)  NOT NULL DEFAULT ''                 COMMENT '关联订单号（奖励来源），释放记录为空',
  `release_date` date                  DEFAULT NULL               COMMENT '释放日期（每日释放时填写）',
  `add_time`     int(11)      NOT NULL DEFAULT 0                  COMMENT '记录时间（Unix 时间戳）',
  PRIMARY KEY (`id`),
  INDEX `idx_uid_type`     (`uid`, `type`),
  INDEX `idx_uid_add_time` (`uid`, `add_time`),
  INDEX `idx_release_date` (`release_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='积分释放明细日志';


-- ============================================================
-- P3-03: eb_agent_level 扩展字段（改造复用：增加积分奖励字段）
-- ============================================================

DROP PROCEDURE IF EXISTS `hjf_migrate_agent_level`;

DELIMITER $$
CREATE PROCEDURE `hjf_migrate_agent_level`()
BEGIN

  IF NOT EXISTS (
    SELECT 1 FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'eb_agent_level' AND COLUMN_NAME = 'direct_reward_points'
  ) THEN
    ALTER TABLE `eb_agent_level`
      ADD COLUMN `direct_reward_points` int(11) NOT NULL DEFAULT 0 COMMENT '直推奖励积分（每单）';
  END IF;

  IF NOT EXISTS (
    SELECT 1 FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'eb_agent_level' AND COLUMN_NAME = 'umbrella_reward_points'
  ) THEN
    ALTER TABLE `eb_agent_level`
      ADD COLUMN `umbrella_reward_points` int(11) NOT NULL DEFAULT 0 COMMENT '伞下奖励积分（每单，级差基数）';
  END IF;

END$$
DELIMITER ;

CALL `hjf_migrate_agent_level`();
DROP PROCEDURE IF EXISTS `hjf_migrate_agent_level`;


-- ============================================================
-- P3-04: eb_user / eb_store_product / eb_store_order 扩展字段
--
-- 注意：不再新增 member_level 字段，复用已有的 agent_level (FK→eb_agent_level.id)
-- ============================================================

DROP PROCEDURE IF EXISTS `hjf_migrate_columns`;

DELIMITER $$
CREATE PROCEDURE `hjf_migrate_columns`()
BEGIN

  -- ---- eb_user 字段（不含 member_level，复用 agent_level）----
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'eb_user' AND COLUMN_NAME = 'no_assess'
  ) THEN
    ALTER TABLE `eb_user`
      ADD COLUMN `no_assess` tinyint(1) NOT NULL DEFAULT 0 COMMENT '不计入伞下业绩：1=不计入';
  END IF;

  IF NOT EXISTS (
    SELECT 1 FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'eb_user' AND COLUMN_NAME = 'frozen_points'
  ) THEN
    ALTER TABLE `eb_user`
      ADD COLUMN `frozen_points` int(11) NOT NULL DEFAULT 0 COMMENT '待释放（冻结）积分';
  END IF;

  IF NOT EXISTS (
    SELECT 1 FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'eb_user' AND COLUMN_NAME = 'available_points'
  ) THEN
    ALTER TABLE `eb_user`
      ADD COLUMN `available_points` int(11) NOT NULL DEFAULT 0 COMMENT '可用积分';
  END IF;

  -- ---- eb_store_product 字段 ----
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'eb_store_product' AND COLUMN_NAME = 'is_queue_goods'
  ) THEN
    ALTER TABLE `eb_store_product`
      ADD COLUMN `is_queue_goods` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否报单商品：1=是';
  END IF;

  IF NOT EXISTS (
    SELECT 1 FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'eb_store_product' AND COLUMN_NAME = 'allow_pay_types'
  ) THEN
    ALTER TABLE `eb_store_product`
      ADD COLUMN `allow_pay_types` varchar(255) NOT NULL DEFAULT '' COMMENT '允许积分支付类型（JSON数组）';
  END IF;

  IF NOT EXISTS (
    SELECT 1 FROM information_schema.STATISTICS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'eb_store_product' AND INDEX_NAME = 'idx_is_queue_goods'
  ) THEN
    ALTER TABLE `eb_store_product` ADD INDEX `idx_is_queue_goods` (`is_queue_goods`);
  END IF;

  -- ---- eb_store_order 字段 ----
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'eb_store_order' AND COLUMN_NAME = 'is_queue_goods'
  ) THEN
    ALTER TABLE `eb_store_order`
      ADD COLUMN `is_queue_goods` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否报单商品订单：1=是';
  END IF;

  IF NOT EXISTS (
    SELECT 1 FROM information_schema.STATISTICS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'eb_store_order' AND INDEX_NAME = 'idx_is_queue_goods'
  ) THEN
    ALTER TABLE `eb_store_order` ADD INDEX `idx_is_queue_goods` (`is_queue_goods`);
  END IF;

END$$
DELIMITER ;

CALL `hjf_migrate_columns`();
DROP PROCEDURE IF EXISTS `hjf_migrate_columns`;


-- ============================================================
-- P3-05: 初始化会员等级数据到 eb_agent_level（改造复用）
--
-- 将原分销员等级改为五级会员等级体系：
--   grade=1 → 创客 (direct=500, umbrella=0)
--   grade=2 → 云店 (direct=800, umbrella=300)
--   grade=3 → 服务商 (direct=1000, umbrella=200)
--   grade=4 → 分公司 (direct=1300, umbrella=300)
--
-- 注意：普通会员 = agent_level=0（无记录），不需要插入
--
-- 先将 CRMEB 原有 demo 等级软删除，然后插入 HJF 会员等级
-- ============================================================

UPDATE `eb_agent_level`
  SET `is_del` = 1
  WHERE `name` NOT IN ('创客', '云店', '服务商', '分公司')
    AND `is_del` = 0;

INSERT INTO `eb_agent_level`
  (`name`, `grade`, `image`, `color`, `one_brokerage`, `two_brokerage`,
   `direct_reward_points`, `umbrella_reward_points`, `status`, `is_del`, `add_time`)
SELECT '创客', 1, '', '#FF9800', 0, 0, 500, 0, 1, 0, UNIX_TIMESTAMP()
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `eb_agent_level` WHERE `name` = '创客' AND `is_del` = 0
);

INSERT INTO `eb_agent_level`
  (`name`, `grade`, `image`, `color`, `one_brokerage`, `two_brokerage`,
   `direct_reward_points`, `umbrella_reward_points`, `status`, `is_del`, `add_time`)
SELECT '云店', 2, '', '#2196F3', 0, 0, 800, 300, 1, 0, UNIX_TIMESTAMP()
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `eb_agent_level` WHERE `name` = '云店' AND `is_del` = 0
);

INSERT INTO `eb_agent_level`
  (`name`, `grade`, `image`, `color`, `one_brokerage`, `two_brokerage`,
   `direct_reward_points`, `umbrella_reward_points`, `status`, `is_del`, `add_time`)
SELECT '服务商', 3, '', '#9C27B0', 0, 0, 1000, 200, 1, 0, UNIX_TIMESTAMP()
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `eb_agent_level` WHERE `name` = '服务商' AND `is_del` = 0
);

INSERT INTO `eb_agent_level`
  (`name`, `grade`, `image`, `color`, `one_brokerage`, `two_brokerage`,
   `direct_reward_points`, `umbrella_reward_points`, `status`, `is_del`, `add_time`)
SELECT '分公司', 4, '', '#F44336', 0, 0, 1300, 300, 1, 0, UNIX_TIMESTAMP()
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `eb_agent_level` WHERE `name` = '分公司' AND `is_del` = 0
);


-- ============================================================
-- P3-06: 初始化等级升级任务到 eb_agent_level_task（改造复用）
--
-- 新增任务类型：
--   type=6 → 直推报单单数
--   type=7 → 伞下报单业绩（含业绩分离）
--   type=8 → 最低直推人数
--
-- 各等级任务配置：
--   创客(grade=1): type=6, number=3 (直推3单)
--   云店(grade=2): type=7, number=30 (伞下30单) + type=8, number=3 (至少3直推)
--   服务商(grade=3): type=7, number=100 + type=8, number=3
--   分公司(grade=4): type=7, number=1000 + type=8, number=3
-- ============================================================

DROP PROCEDURE IF EXISTS `hjf_init_agent_tasks`;

DELIMITER $$
CREATE PROCEDURE `hjf_init_agent_tasks`()
BEGIN
  DECLARE v_level_id_1 INT DEFAULT 0;
  DECLARE v_level_id_2 INT DEFAULT 0;
  DECLARE v_level_id_3 INT DEFAULT 0;
  DECLARE v_level_id_4 INT DEFAULT 0;

  SELECT id INTO v_level_id_1 FROM eb_agent_level WHERE grade = 1 AND is_del = 0 LIMIT 1;
  SELECT id INTO v_level_id_2 FROM eb_agent_level WHERE grade = 2 AND is_del = 0 LIMIT 1;
  SELECT id INTO v_level_id_3 FROM eb_agent_level WHERE grade = 3 AND is_del = 0 LIMIT 1;
  SELECT id INTO v_level_id_4 FROM eb_agent_level WHERE grade = 4 AND is_del = 0 LIMIT 1;

  -- 创客：直推报单3单
  IF v_level_id_1 > 0 AND NOT EXISTS (
    SELECT 1 FROM eb_agent_level_task WHERE level_id = v_level_id_1 AND type = 6 AND is_del = 0
  ) THEN
    INSERT INTO eb_agent_level_task (level_id, `name`, type, number, `desc`, sort, status, is_del, add_time)
    VALUES (v_level_id_1, '直推报单满3单', 6, 3, '直推下级购买报单商品满3单升级为创客', 1, 1, 0, UNIX_TIMESTAMP());
  END IF;

  -- 云店：伞下报单30单 + 至少3个直推
  IF v_level_id_2 > 0 THEN
    IF NOT EXISTS (
      SELECT 1 FROM eb_agent_level_task WHERE level_id = v_level_id_2 AND type = 7 AND is_del = 0
    ) THEN
      INSERT INTO eb_agent_level_task (level_id, `name`, type, number, `desc`, sort, status, is_del, add_time)
      VALUES (v_level_id_2, '伞下报单满30单', 7, 30, '伞下业绩（含分离）达到30单升级为云店', 1, 1, 0, UNIX_TIMESTAMP());
    END IF;
    IF NOT EXISTS (
      SELECT 1 FROM eb_agent_level_task WHERE level_id = v_level_id_2 AND type = 8 AND is_del = 0
    ) THEN
      INSERT INTO eb_agent_level_task (level_id, `name`, type, number, `desc`, sort, status, is_del, add_time)
      VALUES (v_level_id_2, '至少3个直推', 8, 3, '需至少3个直推下级才可升级为云店', 2, 1, 0, UNIX_TIMESTAMP());
    END IF;
  END IF;

  -- 服务商：伞下报单100单 + 至少3个直推
  IF v_level_id_3 > 0 THEN
    IF NOT EXISTS (
      SELECT 1 FROM eb_agent_level_task WHERE level_id = v_level_id_3 AND type = 7 AND is_del = 0
    ) THEN
      INSERT INTO eb_agent_level_task (level_id, `name`, type, number, `desc`, sort, status, is_del, add_time)
      VALUES (v_level_id_3, '伞下报单满100单', 7, 100, '伞下业绩（含分离）达到100单升级为服务商', 1, 1, 0, UNIX_TIMESTAMP());
    END IF;
    IF NOT EXISTS (
      SELECT 1 FROM eb_agent_level_task WHERE level_id = v_level_id_3 AND type = 8 AND is_del = 0
    ) THEN
      INSERT INTO eb_agent_level_task (level_id, `name`, type, number, `desc`, sort, status, is_del, add_time)
      VALUES (v_level_id_3, '至少3个直推', 8, 3, '需至少3个直推下级才可升级为服务商', 2, 1, 0, UNIX_TIMESTAMP());
    END IF;
  END IF;

  -- 分公司：伞下报单1000单 + 至少3个直推
  IF v_level_id_4 > 0 THEN
    IF NOT EXISTS (
      SELECT 1 FROM eb_agent_level_task WHERE level_id = v_level_id_4 AND type = 7 AND is_del = 0
    ) THEN
      INSERT INTO eb_agent_level_task (level_id, `name`, type, number, `desc`, sort, status, is_del, add_time)
      VALUES (v_level_id_4, '伞下报单满1000单', 7, 1000, '伞下业绩（含分离）达到1000单升级为分公司', 1, 1, 0, UNIX_TIMESTAMP());
    END IF;
    IF NOT EXISTS (
      SELECT 1 FROM eb_agent_level_task WHERE level_id = v_level_id_4 AND type = 8 AND is_del = 0
    ) THEN
      INSERT INTO eb_agent_level_task (level_id, `name`, type, number, `desc`, sort, status, is_del, add_time)
      VALUES (v_level_id_4, '至少3个直推', 8, 3, '需至少3个直推下级才可升级为分公司', 2, 1, 0, UNIX_TIMESTAMP());
    END IF;
  END IF;

END$$
DELIMITER ;

CALL `hjf_init_agent_tasks`();
DROP PROCEDURE IF EXISTS `hjf_init_agent_tasks`;


-- ============================================================
-- P3-07: eb_system_config 初始化配置项
-- ============================================================

INSERT IGNORE INTO `eb_system_config`
  (`is_store`, `menu_name`, `type`, `input_type`, `config_tab_id`,
   `parameter`, `upload_type`, `required`, `width`, `high`,
   `value`, `info`, `desc`, `sort`, `status`)
VALUES

(0, 'hjf_trigger_multiple', 'text', 'input', 0,
 '', 0, '', 100, 0,
 '4', '公排触发倍数', '每进入N单公排触发退款第1单，默认4', 10, 1),

(0, 'hjf_release_rate', 'text', 'input', 0,
 '', 0, '', 100, 0,
 '4', '积分每日释放比例(‰)', '每日释放：frozen_points × N / 1000，默认4（即4‰）', 20, 1),

(0, 'hjf_fee_rate', 'text', 'input', 0,
 '', 0, '', 100, 0,
 '7', '提现手续费率(%)', '申请提现时收取的手续费比例，默认7%', 30, 1);


-- ============================================================
-- P3-08: 如果已有旧的 member_level 字段，将数据迁移到 agent_level
-- ============================================================

DROP PROCEDURE IF EXISTS `hjf_migrate_member_to_agent_level`;

DELIMITER $$
CREATE PROCEDURE `hjf_migrate_member_to_agent_level`()
BEGIN

  IF EXISTS (
    SELECT 1 FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'eb_user' AND COLUMN_NAME = 'member_level'
  ) THEN
    -- 将 member_level 数值映射到 agent_level (FK → eb_agent_level.id)
    UPDATE eb_user u
      INNER JOIN eb_agent_level al ON al.grade = u.member_level AND al.is_del = 0
    SET u.agent_level = al.id
    WHERE u.member_level > 0 AND (u.agent_level = 0 OR u.agent_level IS NULL);
  END IF;

END$$
DELIMITER ;

CALL `hjf_migrate_member_to_agent_level`();
DROP PROCEDURE IF EXISTS `hjf_migrate_member_to_agent_level`;


-- ============================================================
-- 迁移完成校验（可手动执行检查）
-- ============================================================

-- SELECT id, name, grade, direct_reward_points, umbrella_reward_points
--   FROM eb_agent_level WHERE is_del = 0 ORDER BY grade;

-- SELECT alt.id, al.name AS level_name, alt.type, alt.number, alt.name AS task_name
--   FROM eb_agent_level_task alt
--   JOIN eb_agent_level al ON al.id = alt.level_id
--   WHERE alt.is_del = 0 AND al.is_del = 0
--   ORDER BY al.grade, alt.type;

-- SELECT COLUMN_NAME FROM information_schema.COLUMNS
--   WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'eb_user'
--   AND COLUMN_NAME IN ('no_assess','frozen_points','available_points');

-- SELECT menu_name, value FROM eb_system_config
--   WHERE menu_name LIKE 'hjf_%' ORDER BY sort;
