-- 芍药居推四免一任务迁移
-- 执行前请确认当前库前缀为 eb_，并已备份生产数据。

CREATE TABLE IF NOT EXISTS `eb_syj_promote_user_amount` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `uid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户ID',
  `pending_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '当前待推广金额',
  `add_time` int(10) unsigned NOT NULL DEFAULT '0',
  `update_time` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='芍药居用户待推广金额汇总';

CREATE TABLE IF NOT EXISTS `eb_syj_promote_task` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `uid` int(10) unsigned NOT NULL DEFAULT '0',
  `task_no` varchar(64) NOT NULL DEFAULT '',
  `source_order_id` int(10) unsigned NOT NULL DEFAULT '0',
  `source_order_no` varchar(64) NOT NULL DEFAULT '',
  `source_split_index` int(10) unsigned NOT NULL DEFAULT '1',
  `base_amount` decimal(10,2) NOT NULL DEFAULT '4333.00',
  `reward_rates` varchar(255) NOT NULL DEFAULT '',
  `fee_rate` decimal(5,2) NOT NULL DEFAULT '7.00',
  `status` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '0进行中 1已完成 2提前兑现 3已关闭 4异常 5审核中',
  `progress_count` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `target_count` tinyint(3) unsigned NOT NULL DEFAULT '4',
  `reward_trigger_status` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '0未触发 1成功 2失败 3处理中',
  `reward_trigger_no` varchar(64) NOT NULL DEFAULT '',
  `exception_reason` varchar(500) NOT NULL DEFAULT '',
  `finish_time` int(10) unsigned NOT NULL DEFAULT '0',
  `cashout_time` int(10) unsigned NOT NULL DEFAULT '0',
  `add_time` int(10) unsigned NOT NULL DEFAULT '0',
  `update_time` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_task_no` (`task_no`),
  UNIQUE KEY `uniq_source_split` (`source_order_id`,`source_split_index`),
  UNIQUE KEY `uniq_reward_trigger_no` (`reward_trigger_no`),
  KEY `idx_uid_status` (`uid`,`status`),
  KEY `idx_source_order` (`source_order_id`),
  KEY `idx_add_time` (`add_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='芍药居推四免一推广任务';

CREATE TABLE IF NOT EXISTS `eb_syj_promote_amount_log` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `uid` int(10) unsigned NOT NULL DEFAULT '0',
  `order_id` int(10) unsigned NOT NULL DEFAULT '0',
  `order_no` varchar(64) NOT NULL DEFAULT '',
  `amount` decimal(10,2) NOT NULL DEFAULT '0.00',
  `before_amount` decimal(10,2) NOT NULL DEFAULT '0.00',
  `after_amount` decimal(10,2) NOT NULL DEFAULT '0.00',
  `type` varchar(32) NOT NULL DEFAULT '',
  `link_id` int(10) unsigned NOT NULL DEFAULT '0',
  `mark` varchar(255) NOT NULL DEFAULT '',
  `add_time` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_order_type` (`order_id`,`type`),
  KEY `idx_uid_type` (`uid`,`type`),
  KEY `idx_add_time` (`add_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='芍药居待推广金额流水';

CREATE TABLE IF NOT EXISTS `eb_syj_promote_task_record` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `task_id` int(10) unsigned NOT NULL DEFAULT '0',
  `uid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '推荐人',
  `order_uid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '下单用户',
  `order_id` int(10) unsigned NOT NULL DEFAULT '0',
  `order_no` varchar(64) NOT NULL DEFAULT '',
  `source_type` varchar(32) NOT NULL DEFAULT '',
  `trigger_timing` varchar(32) NOT NULL DEFAULT '',
  `step_no` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `reward_rate` decimal(5,2) NOT NULL DEFAULT '0.00',
  `status` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '1有效 2退款失效',
  `add_time` int(10) unsigned NOT NULL DEFAULT '0',
  `update_time` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_order` (`order_id`),
  KEY `idx_task_status` (`task_id`,`status`),
  KEY `idx_uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='芍药居推广任务推荐订单进度';

CREATE TABLE IF NOT EXISTS `eb_syj_promote_settlement` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `task_id` int(10) unsigned NOT NULL DEFAULT '0',
  `uid` int(10) unsigned NOT NULL DEFAULT '0',
  `settle_type` varchar(32) NOT NULL DEFAULT '',
  `gross_amount` decimal(10,2) NOT NULL DEFAULT '0.00',
  `fee_rate` decimal(5,2) NOT NULL DEFAULT '0.00',
  `fee_amount` decimal(10,2) NOT NULL DEFAULT '0.00',
  `net_amount` decimal(10,2) NOT NULL DEFAULT '0.00',
  `bill_id` int(10) unsigned NOT NULL DEFAULT '0',
  `brokerage_id` int(10) unsigned NOT NULL DEFAULT '0',
  `audit_status` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '0待审核 1通过 2拒绝',
  `audit_uid` int(10) unsigned NOT NULL DEFAULT '0',
  `audit_remark` varchar(255) NOT NULL DEFAULT '',
  `audit_time` int(10) unsigned NOT NULL DEFAULT '0',
  `add_time` int(10) unsigned NOT NULL DEFAULT '0',
  `update_time` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_task_type` (`task_id`,`settle_type`),
  KEY `idx_uid` (`uid`),
  KEY `idx_audit` (`audit_status`,`add_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='芍药居推广任务结算';

CREATE TABLE IF NOT EXISTS `eb_syj_promote_reward_trigger` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `task_id` int(10) unsigned NOT NULL DEFAULT '0',
  `uid` int(10) unsigned NOT NULL DEFAULT '0',
  `trigger_no` varchar(64) NOT NULL DEFAULT '',
  `trigger_amount` decimal(10,2) NOT NULL DEFAULT '4333.00',
  `brokerage_status` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `points_status` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `level_task_status` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `brokerage_link_id` varchar(255) NOT NULL DEFAULT '',
  `points_link_id` varchar(255) NOT NULL DEFAULT '',
  `level_task_link_id` varchar(255) NOT NULL DEFAULT '',
  `retry_count` int(10) unsigned NOT NULL DEFAULT '0',
  `error_msg` varchar(500) NOT NULL DEFAULT '',
  `add_time` int(10) unsigned NOT NULL DEFAULT '0',
  `update_time` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_task` (`task_id`),
  UNIQUE KEY `uniq_trigger_no` (`trigger_no`),
  KEY `idx_status` (`points_status`,`level_task_status`,`brokerage_status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='芍药居推广任务等价奖励触发';

DELETE FROM `eb_system_config` WHERE `menu_name` IN (
  'syj_task_base_amount',
  'syj_task_target_count',
  'syj_task_reward_rates',
  'syj_early_cashout_fee_rate',
  'syj_task_generate_timing',
  'syj_task_order_dedupe',
  'syj_reward_trigger_enable'
);

INSERT INTO `eb_system_config` (`menu_name`, `type`, `input_type`, `config_tab_id`, `parameter`, `upload_type`, `required`, `width`, `high`, `value`, `info`, `desc`, `sort`, `status`) VALUES
('syj_task_base_amount', 'text', 'input', 0, '', 0, '', 0, 0, '4333', '芍药居任务基准金额', '每满多少有效消费生成1个推广任务', 0, 1),
('syj_task_target_count', 'text', 'input', 0, '', 0, '', 0, 0, '4', '芍药居任务目标单数', '推四免一目标推荐单数', 0, 1),
('syj_task_reward_rates', 'text', 'input', 0, '', 0, '', 0, 0, '[10,20,30,40]', '芍药居任务奖励比例', 'JSON数组，按进度档位配置', 0, 1),
('syj_early_cashout_fee_rate', 'text', 'input', 0, '', 0, '', 0, 0, '7', '芍药居提前兑现扣费比例', '1-3单提前兑现扣费百分比', 0, 1),
('syj_task_generate_timing', 'text', 'input', 0, '', 0, '', 0, 0, 'on_confirm', '芍药居任务生成节点', '首版默认确认收货', 0, 1),
('syj_task_order_dedupe', 'text', 'input', 0, '', 0, '', 0, 0, 'order', '芍药居推荐订单去重方式', '首版按订单去重', 0, 1),
('syj_reward_trigger_enable', 'text', 'input', 0, '', 0, '', 0, 0, '1', '芍药居奖励触发开关', '是否启用等价奖励触发', 0, 1);

UPDATE `eb_system_config` SET `value` = '0' WHERE `menu_name` = 'hjf_queue_pool_enable';
