# 团队余额相关需求
# 部门表增加balance、max_user_count
ALTER TABLE `sys_dept`
    ADD COLUMN `balance` DECIMAL(14,2) NULL DEFAULT '0.00' COMMENT '团队余额' AFTER `model_parm`,
	ADD COLUMN `max_user_count` INT NULL DEFAULT '0' COMMENT '团队账号限制' AFTER `balance`,
	ADD INDEX `parent_id` (`parent_id`);

# 订单表增加dept_id
ALTER TABLE `ai_order`
    ADD COLUMN `dept_id` BIGINT NULL DEFAULT NULL COMMENT '部门ID' AFTER `third_party_order_num`,
	ADD INDEX `dept_id` (`dept_id`);
ALTER TABLE `ai_order`
    ADD INDEX `create_time` (`create_time`);

# AI视频生成统计数据表 - ai_video_report_data
CREATE TABLE `ai_video_report_data` (
	`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
	`date_key` VARCHAR(12) NOT NULL COMMENT '统计时间，到小时(\'%Y-%m-%d %H\')' COLLATE 'utf8mb4_unicode_ci',
	`dept_id` BIGINT NOT NULL COMMENT '部门ID',
	`user_id` BIGINT NOT NULL COMMENT '用户ID，用户表的ID，延用其他表设计 ',
	`score` DECIMAL(14,2) NOT NULL DEFAULT '0.00' COMMENT '消耗积分，按任务创建时间统计',
	`order_count` BIGINT NOT NULL DEFAULT '0' COMMENT '实际订单数，只统计已生成成功的任务',
	`use_tokens` BIGINT NOT NULL DEFAULT '0' COMMENT '三方消耗tokens数量，按任务创建时间统计',
	`create_time` DATETIME NULL DEFAULT (now()) COMMENT '创建时间',
	`update_time` DATETIME NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
	PRIMARY KEY (`id`) USING BTREE,
	UNIQUE INDEX `u_date_dept_user` (`dept_id`, `date_key`, `user_id`) USING BTREE
)
COMMENT='AI视频生成统计数据表，作为其他统计报表的数据源'
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB;

# 团队（部门）余额变动表 - ai_group_balance_change_record
CREATE TABLE `ai_group_balance_change_record` (
                                                  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
                                                  `relation_order_no` VARCHAR(64) NULL DEFAULT NULL COMMENT '关联(充值/退款)订单号' COLLATE 'utf8mb4_0900_ai_ci',
                                                  `dept_id` BIGINT NOT NULL COMMENT '部门ID',
                                                  `type` TINYINT NOT NULL COMMENT '操作类型（0-充值、1-退款、2-下发、3-回收、4-手动修改）',
                                                  `change_amount` DECIMAL(12,2) NOT NULL COMMENT '变更金额',
                                                  `result_amount` DECIMAL(14,2) NOT NULL COMMENT '变更后金额',
                                                  `remark` VARCHAR(50) NULL DEFAULT NULL COMMENT '备注（目前只作为手工修改时的备注）' COLLATE 'utf8mb4_unicode_ci',
                                                  `create_by` BIGINT NULL DEFAULT NULL COMMENT '创建者',
                                                  `create_time` DATETIME NOT NULL DEFAULT (now()) COMMENT '创建时间',
                                                  `update_time` DATETIME NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                                  PRIMARY KEY (`id`) USING BTREE,
                                                  INDEX `relation_order_no` (`relation_order_no`) USING BTREE,
                                                  INDEX `dept_id` (`dept_id`) USING BTREE
)
    COMMENT='团队（部门）余额变动表'
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB;

# 团队（部门）对应火山引擎配置表 - ai_dept_ark_config
CREATE TABLE `ai_dept_ark_config` (
                                      `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
                                      `dept_id` BIGINT NOT NULL COMMENT '部门ID',
                                      `model_parm` TEXT NULL DEFAULT NULL COMMENT '视频模型列表JSON(label+value)' COLLATE 'utf8mb4_0900_ai_ci',
                                      `project` VARCHAR(255) NULL DEFAULT NULL COMMENT 'Byte project，加密' COLLATE 'utf8mb4_0900_ai_ci',
                                      `byte_api_key` VARCHAR(255) NULL DEFAULT NULL COMMENT 'Byte API Key，加密' COLLATE 'utf8mb4_0900_ai_ci',
                                      `create_by` BIGINT NULL DEFAULT NULL COMMENT '创建者',
                                      `create_time` DATETIME NULL DEFAULT (now()) COMMENT '创建时间',
                                      `update_by` BIGINT NULL DEFAULT NULL COMMENT '更新者',
                                      `update_time` DATETIME NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                      PRIMARY KEY (`id`) USING BTREE,
                                      INDEX `dept_id` (`dept_id`) USING BTREE
)
    COMMENT='团队（部门）对应火山引擎配置表'
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB;

# 团队（部门）充值退款订单表 - ai_charge_refund_order
CREATE TABLE `ai_charge_refund_order` (
                                          `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                          `del_flag` CHAR(1) NOT NULL DEFAULT '0' COMMENT '删除标志（0代表存在 2代表删除）' COLLATE 'utf8mb4_0900_ai_ci',
                                          `create_by` BIGINT NULL DEFAULT NULL COMMENT '创建者',
                                          `create_time` DATETIME NOT NULL DEFAULT (now()) COMMENT '创建时间',
                                          `update_time` DATETIME NOT NULL DEFAULT (now()) COMMENT '更新时间',
                                          `order_num` VARCHAR(50) NOT NULL COMMENT '订单编号' COLLATE 'utf8mb4_0900_ai_ci',
                                          `third_party_order_num` VARCHAR(50) NULL DEFAULT NULL COMMENT '第三方单号(预留)' COLLATE 'utf8mb4_0900_ai_ci',
                                          `dept_id` BIGINT NOT NULL COMMENT '部门ID',
                                          `order_type` TINYINT NOT NULL COMMENT '订单类型(0-充值;1-退款)',
                                          `money` DECIMAL(12,2) NULL DEFAULT NULL COMMENT '充值金额',
                                          `amount` DECIMAL(12,2) NOT NULL COMMENT '积分，均为正数，通过订单类型判断是加还是减',
                                          `remark` VARCHAR(50) NULL DEFAULT NULL COMMENT '备注' COLLATE 'utf8mb4_0900_ai_ci',
                                          `status` TINYINT(1) NOT NULL COMMENT '状态：0-进行中 1-已完成 2-失败',
                                          PRIMARY KEY (`id`) USING BTREE,
                                          INDEX `create_by` (`create_by`) USING BTREE,
                                          INDEX `third_party_order_num` (`third_party_order_num`) USING BTREE,
                                          INDEX `dept_id` (`dept_id`) USING BTREE,
                                          INDEX `create_time` (`create_time`) USING BTREE,
                                          INDEX `order_num` (`order_num`) USING BTREE
)
    COMMENT='团队（部门）充值退款订单表'
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB;

ALTER TABLE `ai_user`
    ADD INDEX `dept_id` (`dept_id`);
