ALTER TABLE cai_recharge_order
    ADD COLUMN `fast_pay` tinyint NOT NULL DEFAULT 0 COMMENT '是否为首充';

ALTER TABLE cai_user_info
    ADD COLUMN `fast_pay_time` datetime COMMENT '首充时间' after fast_pay;

CREATE TABLE `cai_fast_pay_total`
(
    `id`           bigint unsigned NOT NULL AUTO_INCREMENT,
    `count_date`   date            NOT NULL COMMENT '时间',
    `count`        bigint(20)      NOT NULL DEFAULT 0 COMMENT '人数',
    `refresh_time` datetime        not null default now() comment '刷新时间',
    `create_time`  datetime        NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    PRIMARY KEY (`id`) USING BTREE,
    INDEX `idx_date` (`count_date`) USING BTREE
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
  ROW_FORMAT = DYNAMIC COMMENT ='每日首充统计';


CREATE TABLE `cai_union_total`
(
    `id`                    bigint unsigned NOT NULL AUTO_INCREMENT,
    `union_id`              bigint(20)      not null comment '工会ID',
    `union_name`            varchar(255)    not null comment '工会名称',
    `union_earnings_total`  bigint(20)      not null default 0 COMMENT '工会收益',
    `anchor_earnings_total` bigint(20)      not null default 0 COMMENT '工会主播收入',
    `count_date`            date            NOT NULL COMMENT '时间',
    `refresh_time`          datetime        not null default now() comment '刷新时间',
    `data_type`             tinyint         not null COMMENT '日期类型',
    `create_time`           datetime        NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    PRIMARY KEY (`id`) USING BTREE,
    INDEX `idx_date` (`count_date`) USING BTREE
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
  ROW_FORMAT = DYNAMIC COMMENT ='工会每日/每周收益统计';

-- 菜单 SQL
insert into sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
values(1966159974722318338, '工会每日收益', '1737000285728587777', '1', 'unionTotal', 'cai/unionTotal/index', 1, 0, 'C', '0', '0', 'cai:unionTotal:list', '#', 'admin', sysdate(), '', null, '工会每日收益菜单');

-- 按钮 SQL
insert into sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
values(1966159974722318339, '工会每日收益查询', 1966159974722318338, '1',  '#', '', 1, 0, 'F', '0', '0', 'cai:unionTotal:query',        '#', 'admin', sysdate(), '', null, '');

ALTER TABLE cai_union
    ADD COLUMN `union_earnings_total`  bigint(20) not null default 0 COMMENT '工会收益',
    ADD COLUMN `anchor_earnings_total` bigint(20) not null default 0 COMMENT '工会主播收入';


UPDATE cai_recharge_order t1
    JOIN (
        -- 子查询：找到每个user_id对应的最小id（最早记录）
        SELECT user_id, MIN(id) AS first_id
        FROM cai_recharge_order
        where pay_status = 1 and pay_time is not null and admin = 0
        GROUP BY user_id
    ) t2 ON t1.id = t2.first_id
SET t1.fast_pay = 1
