-- =====================================================
-- 优惠券模块 — 发放 / 领取 / 核销 模拟数据（可重复执行）
-- 模块：business-marketing / business-coupon
-- 版本：1.0.0
-- 创建时间：2026-07-06
-- 依赖：
--   1. sys_tn_tenant_info 存在 tenant_id='1'、tenant_code='1' 的店铺租户
--   2. marketing.sql、marketing_coupon_scene_seed.sql 已执行
--   3. app_coupon_template / app_member_coupon / app_member_coupon_verify_log 表已建
--   4. app_member 表已存在演示会员（marketing_mock_data.sql 已插入）
-- 执行顺序：marketing_mock_data.sql → marketing_coupon_mock_data.sql
-- 号段：1926061330000000201～1926061330000000233（本脚本专用）
-- 清理：按固定 id 删除；remarks='marketing_coupon_mock_data'
-- 业务链：
--   发放(app_coupon_template 多类型：满减/折扣/无门槛/兑换)
--     → 领取(app_member_coupon 多状态：未使用/已使用/过期/冻结/作废)
--     → 核销(app_member_coupon_verify_log 线下扫码流水)
-- 依赖会员（来自 marketing_mock_data.sql）：
--   顾客 李杏花 1926061330000000120 / 13900001001
--   顾客 赵来客 1926061330000000123 / 13900001004
--   店员 王小二 1926061330000000121 / 13900001002
-- =====================================================

-- ---- 清理（子表 → 主表） ----
DELETE FROM `app_member_coupon_verify_log` WHERE `id` IN (
  '1926061330000000230', '1926061330000000231', '1926061330000000232',
  '1926061330000000233'
);
DELETE FROM `app_member_coupon` WHERE `id` IN (
  '1926061330000000210', '1926061330000000211', '1926061330000000212',
  '1926061330000000213', '1926061330000000214', '1926061330000000215',
  '1926061330000000216', '1926061330000000217', '1926061330000000218',
  '1926061330000000219', '1926061330000000220'
);
DELETE FROM `app_coupon_template` WHERE `id` IN (
  '1926061330000000201', '1926061330000000202', '1926061330000000203', '1926061330000000204'
);

-- =====================================================
-- 一、发放：优惠券模板（4 种类型）
-- =====================================================

-- 1. 满减券 — 满100减20（线下到店）
INSERT INTO `app_coupon_template` (
  `id`, `coupon_code`, `coupon_name`, `coupon_type`, `coupon_image`,
  `discount_type`, `discount_value`, `min_consume_amount`, `max_discount_amount`,
  `use_scope`, `scope_ids`, `exclude_scope_ids`,
  `total_count`, `issued_count`, `used_count`, `per_member_limit`, `get_type`, `exchange_points`,
  `validity_type`, `valid_start_date`, `valid_end_date`, `valid_days`,
  `get_start_date`, `get_end_date`,
  `use_platforms`, `member_levels`, `can_overlay`, `status`, `description`, `scene_code`,
  `create_by`, `create_date`, `update_by`, `update_date`, `del_flag`, `remarks`, `tenant_code`, `tenant_id`
) VALUES (
  '1926061330000000201',
  'CPN_FULL_100_20',
  '满100减20元券',
  1,
  'https://picsum.photos/seed/coupon-full100-20/400/200',
  1, 20.00, 100.00, NULL,
  1, NULL, NULL,
  1000, 5, 3, 3, 1, NULL,
  1, DATE_SUB(CURDATE(), INTERVAL 30 DAY), DATE_ADD(CURDATE(), INTERVAL 180 DAY), NULL,
  DATE_SUB(CURDATE(), INTERVAL 30 DAY), DATE_ADD(CURDATE(), INTERVAL 180 DAY),
  '["MINI"]', NULL, 0, 1,
  '单笔消费满100元可减20元，仅限线下到店核销，不可与其他优惠叠加。',
  'STORE_OFFLINE',
  'mock', DATE_SUB(NOW(), INTERVAL 30 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
);

-- 2. 折扣券 — 满80打8折（商城线上，最大优惠50）
INSERT INTO `app_coupon_template` (
  `id`, `coupon_code`, `coupon_name`, `coupon_type`, `coupon_image`,
  `discount_type`, `discount_value`, `min_consume_amount`, `max_discount_amount`,
  `use_scope`, `scope_ids`, `exclude_scope_ids`,
  `total_count`, `issued_count`, `used_count`, `per_member_limit`, `get_type`, `exchange_points`,
  `validity_type`, `valid_start_date`, `valid_end_date`, `valid_days`,
  `get_start_date`, `get_end_date`,
  `use_platforms`, `member_levels`, `can_overlay`, `status`, `description`, `scene_code`,
  `create_by`, `create_date`, `update_by`, `update_date`, `del_flag`, `remarks`, `tenant_code`, `tenant_id`
) VALUES (
  '1926061330000000202',
  'CPN_DISCOUNT_80_8',
  '满80享8折券',
  2,
  'https://picsum.photos/seed/coupon-discount80/400/200',
  2, 8.0, 80.00, 50.00,
  1, NULL, NULL,
  800, 3, 1, 2, 1, NULL,
  1, DATE_SUB(CURDATE(), INTERVAL 20 DAY), DATE_ADD(CURDATE(), INTERVAL 160 DAY), NULL,
  DATE_SUB(CURDATE(), INTERVAL 20 DAY), DATE_ADD(CURDATE(), INTERVAL 160 DAY),
  '["MINI","APP"]', NULL, 0, 1,
  '单笔消费满80元享8折优惠，最多优惠50元，可用于线上商城下单。',
  'MALL',
  'mock', DATE_SUB(NOW(), INTERVAL 20 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
);

-- 3. 无门槛券 — 5元无门槛（线下到店）
INSERT INTO `app_coupon_template` (
  `id`, `coupon_code`, `coupon_name`, `coupon_type`, `coupon_image`,
  `discount_type`, `discount_value`, `min_consume_amount`, `max_discount_amount`,
  `use_scope`, `scope_ids`, `exclude_scope_ids`,
  `total_count`, `issued_count`, `used_count`, `per_member_limit`, `get_type`, `exchange_points`,
  `validity_type`, `valid_start_date`, `valid_end_date`, `valid_days`,
  `get_start_date`, `get_end_date`,
  `use_platforms`, `member_levels`, `can_overlay`, `status`, `description`, `scene_code`,
  `create_by`, `create_date`, `update_by`, `update_date`, `del_flag`, `remarks`, `tenant_code`, `tenant_id`
) VALUES (
  '1926061330000000203',
  'CPN_FREE_5',
  '5元无门槛券',
  3,
  'https://picsum.photos/seed/coupon-free5/400/200',
  1, 5.00, 0.00, NULL,
  1, NULL, NULL,
  2000, 4, 2, 5, 1, NULL,
  2, NULL, NULL, 30,
  DATE_SUB(CURDATE(), INTERVAL 15 DAY), DATE_ADD(CURDATE(), INTERVAL 150 DAY),
  '["MINI"]', NULL, 0, 1,
  '无门槛5元优惠券，领取后30天内有效，仅限线下到店核销。',
  'STORE_OFFLINE',
  'mock', DATE_SUB(NOW(), INTERVAL 15 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
);

-- 4. 兑换券 — 100积分兑换招牌菜（线下到店，积分兑换）
INSERT INTO `app_coupon_template` (
  `id`, `coupon_code`, `coupon_name`, `coupon_type`, `coupon_image`,
  `discount_type`, `discount_value`, `min_consume_amount`, `max_discount_amount`,
  `use_scope`, `scope_ids`, `exclude_scope_ids`,
  `total_count`, `issued_count`, `used_count`, `per_member_limit`, `get_type`, `exchange_points`,
  `validity_type`, `valid_start_date`, `valid_end_date`, `valid_days`,
  `get_start_date`, `get_end_date`,
  `use_platforms`, `member_levels`, `can_overlay`, `status`, `description`, `scene_code`,
  `create_by`, `create_date`, `update_by`, `update_date`, `del_flag`, `remarks`, `tenant_code`, `tenant_id`
) VALUES (
  '1926061330000000204',
  'CPN_EXCHANGE_DISH',
  '招牌菜兑换券',
  4,
  'https://picsum.photos/seed/coupon-exchange-dish/400/200',
  1, 0.00, 0.00, NULL,
  1, NULL, NULL,
  500, 1, 1, 1, 3, 100,
  2, NULL, NULL, 15,
  DATE_SUB(CURDATE(), INTERVAL 15 DAY), DATE_ADD(CURDATE(), INTERVAL 150 DAY),
  '["MINI"]', NULL, 0, 1,
  '使用100积分兑换招牌菜一份，领取后15天内有效，仅限线下到店核销。',
  'STORE_OFFLINE',
  'mock', DATE_SUB(NOW(), INTERVAL 15 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
);

-- =====================================================
-- 二、领取：会员优惠券实例（覆盖 5 种状态）
-- 状态：0-未使用 1-已使用 2-过期 3-冻结 4-作废
-- 会员：
--   李杏花 1926061330000000120 / MKT202606250001 / 13900001001
--   赵来客 1926061330000000123 / MKT202606250004 / 13900001004
-- =====================================================

INSERT INTO `app_member_coupon` (
  `id`, `coupon_sn`, `member_id`, `member_no`, `member_mobile`,
  `template_id`, `coupon_code`, `coupon_name`, `coupon_type`,
  `discount_type`, `discount_value`, `min_consume_amount`, `max_discount_amount`,
  `scene_code`, `use_scope`, `scope_ids`, `exclude_scope_ids`,
  `get_type`, `get_date`, `get_source`, `activity_id`, `exchange_points`,
  `valid_start_date`, `valid_end_date`,
  `status`, `use_date`, `use_order_id`, `use_order_no`, `use_amount`, `order_amount`,
  `return_date`, `return_reason`, `is_returned`,
  `freeze_date`, `freeze_reason`, `freeze_order_id`,
  `create_by`, `create_date`, `update_by`, `update_date`, `del_flag`, `remarks`, `tenant_code`, `tenant_id`
) VALUES
-- 1) 李杏花 · 满100减20 · 未使用
(
  '1926061330000000210',
  'CPNMKT2026070600001',
  '1926061330000000120', 'MKT202606250001', '13900001001',
  '1926061330000000201', 'CPN_FULL_100_20', '满100减20元券', 1,
  1, 20.00, 100.00, NULL,
  'STORE_OFFLINE', 1, NULL, NULL,
  1, DATE_SUB(NOW(), INTERVAL 6 DAY), 'MINI', NULL, NULL,
  DATE_SUB(CURDATE(), INTERVAL 30 DAY), DATE_ADD(CURDATE(), INTERVAL 180 DAY),
  0, NULL, NULL, NULL, NULL, NULL,
  NULL, NULL, 0,
  NULL, NULL, NULL,
  'mock', DATE_SUB(NOW(), INTERVAL 6 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
),
-- 2) 李杏花 · 满100减20 · 已使用（线下核销，订单128元抵20）
(
  '1926061330000000211',
  'CPNMKT2026070600002',
  '1926061330000000120', 'MKT202606250001', '13900001001',
  '1926061330000000201', 'CPN_FULL_100_20', '满100减20元券', 1,
  1, 20.00, 100.00, NULL,
  'STORE_OFFLINE', 1, NULL, NULL,
  1, DATE_SUB(NOW(), INTERVAL 8 DAY), 'MINI', NULL, NULL,
  DATE_SUB(CURDATE(), INTERVAL 30 DAY), DATE_ADD(CURDATE(), INTERVAL 180 DAY),
  1, DATE_SUB(NOW(), INTERVAL 4 DAY), NULL, 'CPNVFY20260706001', 20.00, 128.00,
  NULL, NULL, 0,
  NULL, NULL, NULL,
  'mock', DATE_SUB(NOW(), INTERVAL 8 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
),
-- 3) 李杏花 · 5元无门槛 · 未使用
(
  '1926061330000000212',
  'CPNMKT2026070600003',
  '1926061330000000120', 'MKT202606250001', '13900001001',
  '1926061330000000203', 'CPN_FREE_5', '5元无门槛券', 3,
  1, 5.00, 0.00, NULL,
  'STORE_OFFLINE', 1, NULL, NULL,
  1, DATE_SUB(NOW(), INTERVAL 3 DAY), 'MINI', NULL, NULL,
  DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_ADD(CURDATE(), INTERVAL 27 DAY),
  0, NULL, NULL, NULL, NULL, NULL,
  NULL, NULL, 0,
  NULL, NULL, NULL,
  'mock', DATE_SUB(NOW(), INTERVAL 3 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
),
-- 4) 李杏花 · 满80享8折 · 已使用（商城线上核销，订单96元抵19.2）
(
  '1926061330000000213',
  'CPNMKT2026070600004',
  '1926061330000000120', 'MKT202606250001', '13900001001',
  '1926061330000000202', 'CPN_DISCOUNT_80_8', '满80享8折券', 2,
  2, 8.0, 80.00, 50.00,
  'MALL', 1, NULL, NULL,
  1, DATE_SUB(NOW(), INTERVAL 12 DAY), 'MINI', NULL, NULL,
  DATE_SUB(CURDATE(), INTERVAL 20 DAY), DATE_ADD(CURDATE(), INTERVAL 160 DAY),
  1, DATE_SUB(NOW(), INTERVAL 5 DAY), 'ORD20260701001', 'ORD20260701001', 19.20, 96.00,
  NULL, NULL, 0,
  NULL, NULL, NULL,
  'mock', DATE_SUB(NOW(), INTERVAL 12 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
),
-- 5) 赵来客 · 满100减20 · 已使用（线下核销，订单156元抵20）
(
  '1926061330000000214',
  'CPNMKT2026070600005',
  '1926061330000000123', 'MKT202606250004', '13900001004',
  '1926061330000000201', 'CPN_FULL_100_20', '满100减20元券', 1,
  1, 20.00, 100.00, NULL,
  'STORE_OFFLINE', 1, NULL, NULL,
  1, DATE_SUB(NOW(), INTERVAL 7 DAY), 'MINI', NULL, NULL,
  DATE_SUB(CURDATE(), INTERVAL 30 DAY), DATE_ADD(CURDATE(), INTERVAL 180 DAY),
  1, DATE_SUB(NOW(), INTERVAL 2 DAY), NULL, 'CPNVFY20260706002', 20.00, 156.00,
  NULL, NULL, 0,
  NULL, NULL, NULL,
  'mock', DATE_SUB(NOW(), INTERVAL 7 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
),
-- 6) 赵来客 · 5元无门槛 · 过期（领取后30天已过）
(
  '1926061330000000215',
  'CPNMKT2026070600006',
  '1926061330000000123', 'MKT202606250004', '13900001004',
  '1926061330000000203', 'CPN_FREE_5', '5元无门槛券', 3,
  1, 5.00, 0.00, NULL,
  'STORE_OFFLINE', 1, NULL, NULL,
  1, DATE_SUB(NOW(), INTERVAL 40 DAY), 'MINI', NULL, NULL,
  DATE_SUB(NOW(), INTERVAL 40 DAY), DATE_SUB(NOW(), INTERVAL 10 DAY),
  2, NULL, NULL, NULL, NULL, NULL,
  NULL, NULL, 0,
  NULL, NULL, NULL,
  'mock', DATE_SUB(NOW(), INTERVAL 40 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
),
-- 7) 赵来客 · 满80享8折 · 冻结（商城下单冻结中）
(
  '1926061330000000216',
  'CPNMKT2026070600007',
  '1926061330000000123', 'MKT202606250004', '13900001004',
  '1926061330000000202', 'CPN_DISCOUNT_80_8', '满80享8折券', 2,
  2, 8.0, 80.00, 50.00,
  'MALL', 1, NULL, NULL,
  1, DATE_SUB(NOW(), INTERVAL 2 DAY), 'MINI', NULL, NULL,
  DATE_SUB(CURDATE(), INTERVAL 20 DAY), DATE_ADD(CURDATE(), INTERVAL 160 DAY),
  3, NULL, NULL, NULL, NULL, NULL,
  NULL, NULL, 0,
  DATE_SUB(NOW(), INTERVAL 1 DAY), '订单预占冻结', 'ORD20260705008',
  'mock', DATE_SUB(NOW(), INTERVAL 2 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
),
-- 8) 李杏花 · 招牌菜兑换券 · 已使用（积分兑换后线下核销）
(
  '1926061330000000217',
  'CPNMKT2026070600008',
  '1926061330000000120', 'MKT202606250001', '13900001001',
  '1926061330000000204', 'CPN_EXCHANGE_DISH', '招牌菜兑换券', 4,
  1, 0.00, 0.00, NULL,
  'STORE_OFFLINE', 1, NULL, NULL,
  3, DATE_SUB(NOW(), INTERVAL 10 DAY), 'MINI', NULL, 100,
  DATE_SUB(NOW(), INTERVAL 10 DAY), DATE_ADD(CURDATE(), INTERVAL 5 DAY),
  1, DATE_SUB(NOW(), INTERVAL 1 DAY), NULL, 'CPNVFY20260706003', 0.00, 0.00,
  NULL, NULL, 0,
  NULL, NULL, NULL,
  'mock', DATE_SUB(NOW(), INTERVAL 10 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
),
-- 9) 赵来客 · 满100减20 · 作废（后台手动作废）
(
  '1926061330000000218',
  'CPNMKT2026070600009',
  '1926061330000000123', 'MKT202606250004', '13900001004',
  '1926061330000000201', 'CPN_FULL_100_20', '满100减20元券', 1,
  1, 20.00, 100.00, NULL,
  'STORE_OFFLINE', 1, NULL, NULL,
  2, DATE_SUB(NOW(), INTERVAL 9 DAY), 'ADMIN', NULL, NULL,
  DATE_SUB(CURDATE(), INTERVAL 30 DAY), DATE_ADD(CURDATE(), INTERVAL 180 DAY),
  4, NULL, NULL, NULL, NULL, NULL,
  NULL, NULL, 0,
  NULL, NULL, NULL,
  'mock', DATE_SUB(NOW(), INTERVAL 9 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
),
-- 10) 李杏花 · 5元无门槛 · 已使用（线下核销，订单38元抵5）
(
  '1926061330000000219',
  'CPNMKT2026070600010',
  '1926061330000000120', 'MKT202606250001', '13900001001',
  '1926061330000000203', 'CPN_FREE_5', '5元无门槛券', 3,
  1, 5.00, 0.00, NULL,
  'STORE_OFFLINE', 1, NULL, NULL,
  1, DATE_SUB(NOW(), INTERVAL 5 DAY), 'MINI', NULL, NULL,
  DATE_SUB(NOW(), INTERVAL 5 DAY), DATE_ADD(CURDATE(), INTERVAL 25 DAY),
  1, DATE_SUB(NOW(), INTERVAL 1 DAY), NULL, 'CPNVFY20260706004', 5.00, 38.00,
  NULL, NULL, 0,
  NULL, NULL, NULL,
  'mock', DATE_SUB(NOW(), INTERVAL 5 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
),
-- 11) 赵来客 · 5元无门槛 · 未使用
(
  '1926061330000000220',
  'CPNMKT2026070600011',
  '1926061330000000123', 'MKT202606250004', '13900001004',
  '1926061330000000203', 'CPN_FREE_5', '5元无门槛券', 3,
  1, 5.00, 0.00, NULL,
  'STORE_OFFLINE', 1, NULL, NULL,
  1, DATE_SUB(NOW(), INTERVAL 1 DAY), 'MINI', NULL, NULL,
  DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_ADD(CURDATE(), INTERVAL 29 DAY),
  0, NULL, NULL, NULL, NULL, NULL,
  NULL, NULL, 0,
  NULL, NULL, NULL,
  'mock', DATE_SUB(NOW(), INTERVAL 1 DAY), 'mock', NOW(), '0', 'marketing_coupon_mock_data', '1', '1'
);

-- =====================================================
-- 三、核销：线下扫码核销流水（对应已使用券 211/214/217/219）
-- 店员 王小二 1926061330000000121
-- =====================================================

INSERT INTO `app_member_coupon_verify_log` (
  `id`, `member_coupon_id`, `coupon_template_id`, `coupon_name`, `member_id`,
  `verify_type`, `verify_time`, `operator_id`, `operator_name`, `verify_token`,
  `verify_tenant_id`, `verify_tenant_code`,
  `create_by`, `create_date`, `update_by`, `update_date`, `del_flag`, `remarks`, `tenant_code`, `tenant_id`
) VALUES
-- 核销 券211：李杏花 满100减20
(
  '1926061330000000230',
  '1926061330000000211',
  '1926061330000000201',
  '满100减20元券',
  '1926061330000000120',
  'OFFLINE_SCAN',
  DATE_SUB(NOW(), INTERVAL 4 DAY),
  '1926061330000000121',
  '王小二',
  'CPNVFY20260706001',
  '1',
  '1',
  'mock',
  DATE_SUB(NOW(), INTERVAL 4 DAY),
  'mock',
  NOW(),
  '0',
  'marketing_coupon_mock_data',
  '1',
  '1'
),
-- 核销 券214：赵来客 满100减20
(
  '1926061330000000231',
  '1926061330000000214',
  '1926061330000000201',
  '满100减20元券',
  '1926061330000000123',
  'OFFLINE_SCAN',
  DATE_SUB(NOW(), INTERVAL 2 DAY),
  '1926061330000000121',
  '王小二',
  'CPNVFY20260706002',
  '1',
  '1',
  'mock',
  DATE_SUB(NOW(), INTERVAL 2 DAY),
  'mock',
  NOW(),
  '0',
  'marketing_coupon_mock_data',
  '1',
  '1'
),
-- 核销 券217：李杏花 招牌菜兑换券
(
  '1926061330000000232',
  '1926061330000000217',
  '1926061330000000204',
  '招牌菜兑换券',
  '1926061330000000120',
  'OFFLINE_SCAN',
  DATE_SUB(NOW(), INTERVAL 1 DAY),
  '1926061330000000121',
  '王小二',
  'CPNVFY20260706003',
  '1',
  '1',
  'mock',
  DATE_SUB(NOW(), INTERVAL 1 DAY),
  'mock',
  NOW(),
  '0',
  'marketing_coupon_mock_data',
  '1',
  '1'
),
-- 核销 券219：李杏花 5元无门槛
(
  '1926061330000000233',
  '1926061330000000219',
  '1926061330000000203',
  '5元无门槛券',
  '1926061330000000120',
  'OFFLINE_SCAN',
  DATE_SUB(NOW(), INTERVAL 1 DAY),
  '1926061330000000121',
  '王小二',
  'CPNVFY20260706004',
  '1',
  '1',
  'mock',
  DATE_SUB(NOW(), INTERVAL 1 DAY),
  'mock',
  NOW(),
  '0',
  'marketing_coupon_mock_data',
  '1',
  '1'
);
