63 lines
2.7 KiB
PL/PgSQL

-- Standardize Points Ratio Migration Script
-- Purpose: Convert point values from "1 Yuan = 100 Points" (Cents) to "1 Yuan = 1 Point" (Integer).
-- Target Tables: user_points, user_points_ledger, orders.
BEGIN;
-- 1. Update User Points Current Balance
-- Description: Reduce all current point balances by factor of 100.
UPDATE user_points
SET points = FLOOR(points / 100);
-- 2. Update User Points Ledger (History)
-- Description: Adjust historical records to reflect the new unit.
UPDATE user_points_ledger
SET points = FLOOR(points / 100);
-- 3. Update Orders Points Usage
-- Description: Adjust recorded points usage in orders.
UPDATE orders
SET points_amount = FLOOR(points_amount / 100)
WHERE points_amount > 0;
-- 4. Update Task Center Rewards (Points)
-- Description: Adjust configured point rewards in Task Center.
-- Note: Logic prioritizes 'points' in JSON payload, then 'quantity'.
-- Updating 'quantity' is safe. Updating JSON is complex in standard SQL without knowing exact structure/version.
-- Assuming simple structure {"points": 100} or similar.
-- 4a. Update Quantity for type 'points'
UPDATE task_center_task_rewards
SET quantity = FLOOR(quantity / 100)
WHERE reward_type = 'points' AND quantity >= 100;
-- 4b. Update RewardPayload? (Optional/Manual)
-- Warning: Modifying JSON string requires robust parsing.
-- Providing a best-effort text replacement for simple cases {"points": 100} -> {"points": 1}
-- This relies on the pattern '"points": ' followed by numbers.
-- Recommendation: Manually verify Task Center configurations in Admin Panel after migration.
-- 5. System Config Update (Values)
-- Description: Convert known config values if they represent points.
-- Example: 'register_points', 'daily_sign_in_points' (if they exist).
-- This attempts to update common point-related configs.
UPDATE system_configs
SET config_value = FLOOR(config_value / 100)
WHERE config_key IN ('register_reward_points', 'daily_sign_in_reward_points') AND config_value REGEXP '^[0-9]+$';
-- 6. System Config Update (Exchange Rate)
-- Description: Ensure the new config key is set.
INSERT INTO system_configs (config_key, config_value, remark, created_at, updated_at)
VALUES ('points_exchange_rate', '1', '1元对应多少积分', NOW(), NOW())
ON DUPLICATE KEY UPDATE config_value = '1';
COMMIT;
-- NOTE on 'user_inventory':
-- Some points data might be embedded in 'remark' field (e.g. '|redeemed_points=100').
-- Updating these embedded strings via SQL is complex and risky.
-- Refunds for OLD items (redeemed before migration) might fail or deduct excessive points
-- if this field is not updated.
-- Recommendation: Handle 'refund' logic gracefully for legacy items in code if possible,
-- or accept that old items cannot be refunded automatically.