"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const UserOnlineTime_1 = __importDefault(require("../../models/UserOnlineTime"));
const sequelize_1 = require("sequelize");
const ListUserOnlineOffline = ({ date, userId, status }) => __awaiter(void 0, void 0, void 0, function* () {
    let where_clause = {};
    if (userId && status) {
        where_clause = {
            [sequelize_1.Op.and]: [
                { userId: userId },
                { status: status },
                {
                    "$createdAt$": sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("date", sequelize_1.Sequelize.col("createdAt")), `${date}`)
                },
            ]
        };
    }
    else if (userId) {
        where_clause = {
            [sequelize_1.Op.and]: [
                { userId: userId },
                {
                    "$createdAt$": sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("date", sequelize_1.Sequelize.col("createdAt")), `${date}`)
                },
            ]
        };
    }
    else if (status) {
        where_clause = {
            [sequelize_1.Op.and]: [
                { status: status },
                {
                    "$createdAt$": sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("date", sequelize_1.Sequelize.col("createdAt")), `${date}`)
                },
            ]
        };
    }
    else {
        where_clause = {
            [sequelize_1.Op.and]: [
                {
                    "$createdAt$": sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("date", sequelize_1.Sequelize.col("createdAt")), `${date}`)
                },
            ]
        };
    }
    const users = yield UserOnlineTime_1.default.findAll({
        where: where_clause,
        raw: true,
        attributes: ['id', 'userId', 'status',
            [sequelize_1.Sequelize.fn("DATE_FORMAT", sequelize_1.Sequelize.col("UserOnlineTime.onlineTime"), "%Y-%m-%d %H:%i:%s"), "onlineTime"],
            [sequelize_1.Sequelize.fn("DATE_FORMAT", sequelize_1.Sequelize.col("UserOnlineTime.createdAt"), "%Y-%m-%d %H:%i:%s"), "createdAt"],
            [sequelize_1.Sequelize.fn("DATE_FORMAT", sequelize_1.Sequelize.col("UserOnlineTime.updatedAt"), "%Y-%m-%d %H:%i:%s"), "updatedAt"]],
        order: [["id", "ASC"]],
    });
    return users;
});
exports.default = ListUserOnlineOffline;
