-- REV004 account adjust dictionaries
-- Target DB: sw_system (PostgreSQL)
-- Purpose:
--   1) Reuse existing legacy reason/type/proc/business dictionaries
--   2) Add the minimal new dictionaries needed for REV004 object/status semantics
-- Note:
--   This script is idempotent by (type) and (dict_type, value) checks.

BEGIN;

-- ============================================================
-- 1. Dict types
-- ============================================================
INSERT INTO system_dict_type (name, type, status, remark, creator, updater, category)
SELECT v.name, v.type, 0, v.remark, 'omx', 'omx', 'REV004'
FROM (VALUES
    ('红冲原因', 'redink_reason', 'REV004 红冲/冲正原因'),
    ('账务调整对象类型', 'account_adjust_object_type', 'REV004 objectType 字典'),
    ('账务调整结果状态', 'account_adjust_result_status', 'REV004 resultStatus 字典'),
    ('账务调整审批状态', 'account_adjust_approval_status', 'REV004 approvalStatus 字典'),
    ('账务调整回写状态', 'account_adjust_writeback_status', 'REV004 writeBackStatus 字典')
) AS v(name, type, remark)
WHERE NOT EXISTS (
    SELECT 1 FROM system_dict_type t WHERE t.type = v.type AND t.deleted = 0
);

-- ============================================================
-- 2. Dict data
-- ============================================================
INSERT INTO system_dict_data (sort, label, value, dict_type, status, color_type, css_class, remark, creator, updater)
SELECT v.sort, v.label, v.value, v.dict_type, 0, v.color_type, v.css_class, v.remark, 'omx', 'omx'
FROM (VALUES
    -- redink_reason
    (10, '收费错误', '1', 'redink_reason', 'danger', '', 'REV004 红冲原因'),
    (20, '用户拒缴', '2', 'redink_reason', 'warning', '', 'REV004 红冲原因'),
    (30, '银行撤销', '3', 'redink_reason', 'warning', '', 'REV004 红冲原因'),
    (40, '网络异常', '4', 'redink_reason', 'info', '', 'REV004 红冲原因'),
    (90, '其它', '9', 'redink_reason', 'default', '', 'REV004 红冲原因'),

    -- account_adjust_object_type
    (10, '预付费退款', 'PREPAID_REFUND', 'account_adjust_object_type', 'success', '', 'REV004 objectType'),
    (20, '红冲记录', 'REDINK_RECORD', 'account_adjust_object_type', 'danger', '', 'REV004 objectType'),
    (30, '坏账申请', 'BAD_DEBT_RECORD', 'account_adjust_object_type', 'warning', '', 'REV004 objectType'),
    (40, '核销调整', 'WRITTENOFF_ADJUST', 'account_adjust_object_type', 'warning', '', 'REV004 objectType'),
    (50, '价差调整', 'PRICE_DIFF_ADJUST', 'account_adjust_object_type', 'info', '', 'REV004 objectType'),
    (60, '违约金减免', 'LATE_FEE_REDUCE', 'account_adjust_object_type', 'info', '', 'REV004 objectType'),
    (70, '账单拆分', 'SPLIT_ADJUST', 'account_adjust_object_type', 'primary', '', 'REV004 objectType'),

    -- account_adjust_result_status
    (10, '成功', 'SUCCESS', 'account_adjust_result_status', 'success', '', 'REV004 resultStatus'),
    (20, '待审批', 'PENDING_APPROVAL', 'account_adjust_result_status', 'warning', '', 'REV004 resultStatus'),
    (30, '失败', 'FAIL', 'account_adjust_result_status', 'danger', '', 'REV004 resultStatus'),

    -- account_adjust_approval_status
    (10, '无需审批', 'NOT_REQUIRED', 'account_adjust_approval_status', 'default', '', 'REV004 approvalStatus'),
    (20, '待审批', 'PENDING_APPROVAL', 'account_adjust_approval_status', 'warning', '', 'REV004 approvalStatus'),
    (30, '审批通过', 'APPROVED', 'account_adjust_approval_status', 'success', '', 'REV004 approvalStatus'),
    (40, '审批驳回', 'REJECTED', 'account_adjust_approval_status', 'danger', '', 'REV004 approvalStatus'),

    -- account_adjust_writeback_status
    (10, '已回写', 'UPDATED', 'account_adjust_writeback_status', 'success', '', 'REV004 writeBackStatus'),
    (20, '待回写', 'PENDING', 'account_adjust_writeback_status', 'warning', '', 'REV004 writeBackStatus'),
    (30, '已跳过', 'SKIPPED', 'account_adjust_writeback_status', 'default', '', 'REV004 writeBackStatus')
) AS v(sort, label, value, dict_type, color_type, css_class, remark)
WHERE NOT EXISTS (
    SELECT 1
    FROM system_dict_data d
    WHERE d.dict_type = v.dict_type
      AND d.value = v.value
      AND d.deleted = 0
);

COMMIT;
