-- czc231 integral supplement for task two
-- 目标库：czc231 环境的 yangtangyoupin
-- 执行方式：mysql -h 8.136.120.231 -u yangtangyoupin -p yangtangyoupin < docs/company-czc231-integral-imgration-supplement.sql
-- 数据来源：docs/dump-xsj33_2026-04-10_10-15-02_mysql_data.sql
--
-- 口径：
-- 1) 目标个人奖金记录：
--    327003,326997,326991,326978,326932,326931,326929,326922,326883,326882,326875,326869
-- 2) 判重字段：eb_user_integral_record.wa_selfbonus_logid
-- 3) 积分换算：integral_value = FLOOR_TO_3(wa_selfbonus_log.money * 0.5)
--    对应 Java 逻辑：bonusAmount.multiply(0.5).setScale(3, RoundingMode.DOWN)
-- 4) 静态对账结果（基于 dump 快照）：
--    候选记录数 = 12
--    理论补齐积分总和 = 1481.299
-- 5) 候选记录涉及用户：
--    92996,93169,93209,92457,93056,92760,92974,92896,93098,92582
-- 6) 候选明细（wa_selfbonus_log.id -> uid -> money -> theoretical integral）：
--    326869 -> 92996 -> 238.625 -> 119.312
--    326875 -> 93169 -> 205.642 -> 102.821
--    326882 -> 93209 -> 238.625 -> 119.312
--    326883 -> 92457 -> 406.402 -> 203.201
--    326922 -> 93056 -> 216.146 -> 108.073
--    326929 -> 92760 -> 245.784 -> 122.892
--    326931 -> 92974 -> 215.233 -> 107.616
--    326932 -> 92896 -> 246.997 -> 123.498
--    326978 -> 93169 -> 209.297 -> 104.648
--    326991 -> 93098 -> 246.181 -> 123.090
--    326997 -> 92896 -> 240.516 -> 120.258
--    327003 -> 92582 -> 253.157 -> 126.578
-- 7) 按用户汇总：
--    92457 -> 1 条 -> 203.201
--    92582 -> 1 条 -> 126.578
--    92760 -> 1 条 -> 122.892
--    92896 -> 2 条 -> 243.756
--    92974 -> 1 条 -> 107.616
--    92996 -> 1 条 -> 119.312
--    93056 -> 1 条 -> 108.073
--    93098 -> 1 条 -> 123.090
--    93169 -> 2 条 -> 207.469
--    93209 -> 1 条 -> 119.312
-- 8) 实际数据库执行结果（2026-04-19）：
--    existing_candidate_count = 12
--    task2_missing_count = 0
--    inserted_count = 0
--    inserted_integral_sum = 0.000
--    说明：当前数据库中这 12 个 wa_selfbonus_logid 已全部存在于 eb_user_integral_record，
--    且当前库中的这 12 条记录均归属 uid=92034，因此本次执行为 no-op。

SET NAMES utf8mb4;
START TRANSACTION;

-- 执行前核查
SELECT uid, integral AS integral_before
FROM eb_user
WHERE uid IN (92457,92582,92760,92896,92974,92996,93056,93098,93169,93209)
ORDER BY uid;

SELECT COUNT(1) AS existing_candidate_count
FROM eb_user_integral_record
WHERE wa_selfbonus_logid IN (327003,326997,326991,326978,326932,326931,326929,326922,326883,326882,326875,326869);

-- 任务二：按指定 wa_selfbonus_log.id 补齐缺失积分记录
DROP TEMPORARY TABLE IF EXISTS tmp_czc231_task2_missing;
CREATE TEMPORARY TABLE tmp_czc231_task2_missing AS
SELECT
  w.user_id AS uid,
  w.id AS wa_selfbonus_logid,
  CAST(w.id AS CHAR) AS link_id,
  TRUNCATE(w.money * 0.5, 3) AS integral_value,
  w.money AS bonus_money,
  w.created_at AS created_at,
  w.updated_at AS updated_at
FROM wa_selfbonus_log w
LEFT JOIN eb_user_integral_record r
  ON r.uid = w.user_id
 AND r.wa_selfbonus_logid = w.id
WHERE w.id IN (327003,326997,326991,326978,326932,326931,326929,326922,326883,326882,326875,326869)
  AND w.type = 1
  AND w.money > 0
  AND r.id IS NULL;

SELECT COUNT(1) AS task2_missing_count
FROM tmp_czc231_task2_missing;

DROP TEMPORARY TABLE IF EXISTS tmp_czc231_task2_to_insert;
CREATE TEMPORARY TABLE tmp_czc231_task2_to_insert AS
SELECT
  seq.uid,
  seq.link_id,
  'selfbonus' AS link_type,
  1 AS type,
  '个人奖金奖励' AS title,
  seq.integral_value AS integral,
  CAST(seq.balance_calc AS SIGNED) AS balance,
  CONCAT('个人奖金变动奖励，奖金金额：', seq.bonus_money, '，积分：', CAST(seq.integral_value AS SIGNED)) AS mark,
  3 AS status,
  0 AS frozen_time,
  0 AS thaw_time,
  seq.created_at AS create_time,
  seq.updated_at AS update_time,
  seq.wa_selfbonus_logid
FROM (
  SELECT
    ordered.uid,
    ordered.link_id,
    ordered.integral_value,
    ordered.bonus_money,
    ordered.created_at,
    ordered.updated_at,
    ordered.wa_selfbonus_logid,
    @running_balance := IF(
      @current_uid = ordered.uid,
      @running_balance + ordered.integral_value,
      ordered.base_integral + ordered.integral_value
    ) AS balance_calc,
    @current_uid := ordered.uid AS current_uid_marker
  FROM (
    SELECT
      m.uid,
      m.link_id,
      m.integral_value,
      m.bonus_money,
      m.created_at,
      m.updated_at,
      m.wa_selfbonus_logid,
      u.integral AS base_integral
    FROM tmp_czc231_task2_missing m
    INNER JOIN eb_user u ON u.uid = m.uid
    ORDER BY m.uid, m.created_at, m.wa_selfbonus_logid
  ) ordered
  JOIN (SELECT @current_uid := NULL, @running_balance := 0) vars
) seq;

INSERT INTO eb_user_integral_record
(
  uid, link_id, link_type, type, title, integral, balance, mark, status,
  frozen_time, thaw_time, create_time, update_time, wa_selfbonus_logid
)
SELECT
  uid, link_id, link_type, type, title, integral, balance, mark, status,
  frozen_time, thaw_time, create_time, update_time, wa_selfbonus_logid
FROM tmp_czc231_task2_to_insert
ORDER BY uid, create_time, wa_selfbonus_logid;

-- 同步增加 eb_user.integral（仅加本次新增积分）
UPDATE eb_user e
INNER JOIN (
  SELECT uid, IFNULL(SUM(integral), 0) AS add_integral
  FROM tmp_czc231_task2_to_insert
  GROUP BY uid
) t ON t.uid = e.uid
SET e.integral = e.integral + t.add_integral;

-- 执行后核查
SELECT COUNT(1) AS inserted_count
FROM tmp_czc231_task2_to_insert;

SELECT IFNULL(SUM(integral_value), 0) AS inserted_integral_sum
FROM tmp_czc231_task2_missing;

SELECT uid, COUNT(1) AS inserted_count_per_user, IFNULL(SUM(integral), 0) AS inserted_integral_per_user
FROM tmp_czc231_task2_to_insert
GROUP BY uid
ORDER BY uid;

SELECT COUNT(1) AS candidate_count_after
FROM eb_user_integral_record
WHERE wa_selfbonus_logid IN (327003,326997,326991,326978,326932,326931,326929,326922,326883,326882,326875,326869);

SELECT uid, integral AS integral_after
FROM eb_user
WHERE uid IN (92457,92582,92760,92896,92974,92996,93056,93098,93169,93209)
ORDER BY uid;

COMMIT;
