const { Sequelize, DataTypes } = require('sequelize');

export const TablesConfig = [
    // 设备数据记录表（存储推送的实时数据）
    {
        tableNameCn: '设备数据表',
        tableName: 'device_data',
        schema: {
            id: {
                type: DataTypes.BIGINT,
                allowNull: false,
                primaryKey: true,
                autoIncrement: true,
                comment: '自增主键'
            },
            device_id: {
                type: DataTypes.STRING(50),
                allowNull: false,
                comment: '设备ID，关联device.device_id'
            },
            data: {
                type: DataTypes.JSON,
                allowNull: false,
                comment: '推送的数据，如{power:on,temperature:24,mode:cool,fan_speed:medium,auto_control:on}'
            },
            received_time: {
                type: DataTypes.DATE,
                allowNull: false,
                comment: '数据接收时间（服务器时间）'
            },
            device_time: {
                type: DataTypes.DATE,
                allowNull: true,
                comment: '设备上报的时间戳（若有）'
            },
            created_at: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW,
                comment: '记录创建时间'
            }
        },
        association: [
        ]
    },
    // 设备故障表
    {
        tableNameCn: '设备故障表',
        tableName: 'device_fault',
        schema: {
            id: {
                type: DataTypes.BIGINT,
                allowNull: false,
                primaryKey: true,
                autoIncrement: true,
                comment: '自增主键'
            },
            device_id: {
                type: DataTypes.STRING(50),
                allowNull: false,
                comment: '设备ID，关联device.device_id'
            },
            fault_type: {
                type: DataTypes.STRING(50),
                allowNull: false,
                comment: '故障类型，如“离线”“数据异常”“硬件故障”'
            },
            fault_code: {
                type: DataTypes.STRING(50),
                allowNull: true,
                comment: '故障代码，如 LOST、E1 等'
            },
            fault_description: {
                type: DataTypes.TEXT,
                allowNull: true,
                comment: '故障详细描述'
            },
            fault_origin_id: {
                type: DataTypes.STRING(64),
                allowNull: true,
                unique: true,
                comment: '第三方故障记录ID，用于去重'
            },
            occurred_time: {
                type: DataTypes.DATE,
                allowNull: false,
                comment: '故障发生时间'
            },
            level: {
                type: DataTypes.TINYINT,
                allowNull: false,
                defaultValue: 0,
                comment: '故障等级：1=一级，2=二级，3=三级'
            },
            status: {
                type: DataTypes.TINYINT,
                allowNull: false,
                defaultValue: 0,
                comment: '处理状态：0=未处理，1=处理中，2=已解决'
            },
            resolved_time: {
                type: DataTypes.DATE,
                allowNull: true,
                comment: '故障解决时间'
            },
            created_at: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW,
                comment: '记录创建时间'
            },
            updated_at: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW,
                comment: '记录更新时间'
            }
        },
        association: [
        ]
    },
    // 设备主表（关联区域表）
    {
        tableNameCn: '设备表',
        tableName: 'device',
        schema: {
            id: {
                type: DataTypes.BIGINT,
                allowNull: false,
                primaryKey: true,
                autoIncrement: true,
                comment: '自增主键'
            },
            device_id: {
                type: DataTypes.STRING(50),
                allowNull: false,
                unique: true,
                comment: '设备自定义标识'
            },
            device_ad: {
                type: DataTypes.STRING(50),
                allowNull: false,
                unique: true,
                comment: '设备唯一标识（由设备商提供）'
            },
            region_key: {
                type: DataTypes.BIGINT,
                allowNull: false,
                comment: '区域编号，关联region.id'
            },
            device_type: {
                type: DataTypes.STRING(50),
                allowNull: false,
                comment: '设备类型，如“空调”“出风口”'
            },
            device_name: {
                type: DataTypes.STRING(100),
                allowNull: true,
                comment: '设备名称（可选）'
            },
            control_params: {
                type: DataTypes.JSON,
                allowNull: true,
                comment: '该设备支持的参数定义，JSON格式{"power":["on","off"]}'
            },
            device_state: {
                type: DataTypes.TINYINT,
                allowNull: false,
                defaultValue: 0,
                comment: '设备状态：0=正常 1=硬件故障 2=通信故障 3=离线'
            },
            created_at: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW,
                comment: '创建时间'
            },
            updated_at: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW,
                comment: '更新时间'
            }
        },
        association: [
            { type: "hasMany", target: "device_data", foreignKey: "device_id" },
            { type: "hasMany", target: "device_fault", foreignKey: "device_id" }
        ]
    },
    // 人员信息表
    {
        tableNameCn: '用户信息表',
        tableName: 'user_info',
        schema: {
            id: {
                type: DataTypes.BIGINT,
                allowNull: false,
                primaryKey: true,
                autoIncrement: true,
                comment: '自增主键'
            },
            loginId: { 
                type: Sequelize.STRING(255), 
                allowNull: false,
                Comment: '账户，唯一'
            },
            pwd: { 
                type: Sequelize.STRING(255),
                Comment: '密码' 
            },
            token: { 
                type: Sequelize.STRING(255),
                Comment: 'token' 
            },
            tokenMs: { 
                type: Sequelize.DATE,
                Comment: 'token有效期' 
            },
            name: { 
                type: Sequelize.STRING(255),
                Comment: '用户名称' 
            }
        },
        association: [
        ]
    },
    // 区域配置表
    {
        tableNameCn: '区域配置表',
        tableName: 'region',
        schema: {
            id: {
                type: DataTypes.BIGINT,
                allowNull: false,
                primaryKey: true,
                autoIncrement: true,
                comment: '区域编号（regionKey）'
            },
            room_id: {
                type: DataTypes.STRING(100),
                allowNull: false,
                comment: '房间ID，关联设备商提供的房间ID'
            },
            name: {
                type: DataTypes.STRING(100),
                allowNull: false,
                comment: '区域名称'
            },
            floor_id: {
                type: DataTypes.STRING(100),
                comment: '楼层ID，关联设备商提供的楼层ID'
            },
            type: {
                type: DataTypes.STRING(100),
                allowNull: false,
                comment: '所属楼层'
            },
            building_id: {
                type: DataTypes.STRING(100),
                comment: '楼栋ID，关联设备商提供的楼栋ID'
            },
            groups: {
                type: DataTypes.STRING(100),
                allowNull: false,
                comment: '所属楼栋'
            },
            max_capacity: {
                type: DataTypes.INTEGER,
                comment: '最大承载量'
            },
            sort_order: {
                type: DataTypes.INTEGER,
                allowNull: false,
                defaultValue: 0,
                comment: '排序序号'
            },
            address: {
                type: DataTypes.STRING(100),
                comment: '外机地址'
            },
            created_at: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW,
                comment: '创建时间'
            },
            updated_at: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW,
                comment: '更新时间'
            }
        },
        association: [
            { type: "hasMany", target: "device", foreignKey: "region_key" }
        ]
    },
    // 设备日志表
    {
        tableNameCn: '设备日志表',
        tableName: 'device_logs',
        schema: {
            id: {
                type: DataTypes.BIGINT,
                allowNull: false,
                primaryKey: true,
                autoIncrement: true,
                comment: '自增主键'
            },
            device_id: {
                type: DataTypes.STRING(50),
                allowNull: false,
                comment: '设备标识'
            },
            device_type: {
                type: DataTypes.STRING(50),
                allowNull: false,
                comment: '设备类型，如“空调”“出风口”'
            },
            operation_content: {
                type: DataTypes.JSON,
                allowNull: true,
                comment: '操作内容{"option":"on","time":"2026-06-26 12:00:00"}'
            },
            status: {
                type: DataTypes.INTEGER,
                allowNull: false,
                comment: '操作状态，0否1是'
            },
            created_at: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW,
                comment: '创建时间'
            },
            updated_at: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW,
                comment: '更新时间'
            }
        },
        association: [
        ]
    },
    // 日程表
    {
        tableNameCn: '日程表',
        tableName: 'schedule',
        schema: {
            id: {
                type: DataTypes.BIGINT,
                allowNull: false,
                primaryKey: true,
                autoIncrement: true,
                comment: '自增主键'
            },
            title: {
                type: Sequelize.STRING(255),
                allowNull: false,
                comment: '日程标题'
            },
            schedule_date: {
                type: DataTypes.DATE,
                allowNull: true,
                comment: '日期'
            },
            ac_control: {
                type: Sequelize.STRING(50),
                allowNull: false,
                comment: '操作状态:off关on开'
            },
            todo_item: {
                type: DataTypes.JSON,
                allowNull: true,
                comment: '待办事项，除了开关以外可以设定模式、温度等。例：{"温度":"24.5","模式":"制冷","风速":"低风"}'
            },
            schedule_time: {
                type: Sequelize.STRING(50),
                allowNull: true,
                comment: '定时时间，如08:30:00'
            },
            set_conditions: {
                type: DataTypes.JSON,
                allowNull: true,
                comment: '设定条件，除了定时以外可以通过条件开启或关闭设备。例：{"温度":"≤26&≥22"}'
            },
            begin_validity: {
                type: DataTypes.DATE,
                allowNull: true,
                comment: '有效时间起，不设置则为空'
            },
            end_validity: {
                type: DataTypes.DATE,
                allowNull: true,
                comment: '有效时间止，不设置则为空'
            },
            outward_time: {
                type: Sequelize.STRING(200),
                allowNull: true,
                comment: '有效除外时间，多个通过逗号分隔'
            },
            created_at: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: Sequelize.NOW,
                comment: '创建时间'
            },
            updated_at: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: Sequelize.NOW,
                comment: '修改时间'
            }
        },
        association: [
            { type: "hasMany", target: "schedule_device", foreignKey: "schedule_id" }
        ]
    },
    // 日程设备关联表
    {
        tableNameCn: '日程设备关联表',
        tableName: 'schedule_device',
        schema: {
            id: {
                type: DataTypes.BIGINT,
                allowNull: false,
                primaryKey: true,
                autoIncrement: true,
                comment: '自增主键'
            },
            schedule_id: {
                type: DataTypes.BIGINT,
                allowNull: false,
                comment: '日程ID，关联schedule.id'
            },
            device_id: {
                type: DataTypes.STRING(50),
                allowNull: false,
                comment: '设备ID，关联device.device_id'
            },
            region_key: {
                type: DataTypes.BIGINT,
                allowNull: false,
                comment: '区域key，关联region.id，表示设备所属区域'
            },
            created_at: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW,
                comment: '创建时间'
            }
        },
        association: [
            { type: "hasMany", target: "device", foreignKey: "device_id" },
            { type: "hasMany", target: "region", foreignKey: "region_key" }
        ]
    },
];

