"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getIssueHistorySummaryByPowerPlantId = exports.getIssueHistoriesByPowerPlantId = void 0;
const shell_1 = require("@things-factory/shell");
const dataset_1 = require("@things-factory/dataset");
const getIssueHistoriesByPowerPlantId = async (powerPlantId, fromDate, toDate, pagination) => {
    const { page, limit } = pagination;
    const issueHistories = (0, shell_1.getRepository)(dataset_1.DataOoc)
        .createQueryBuilder('do')
        .select('id', 'id')
        .addSelect('key_02', 'inverter_no')
        .addSelect('collected_at', 'collected_at')
        .addSelect("(data::json ->> 'COM')", 'com')
        .addSelect("(data::json ->> 'RUN')", 'run')
        .addSelect("(data::json ->> 'FAULT')", 'fault')
        .where('domain_id = :powerPlantId', { powerPlantId })
        .andWhere('name = :name', { name: '인버터' })
        .andWhere('work_date >= :fromDate', { fromDate: fromDate })
        .andWhere('work_date <= :toDate', { toDate: toDate })
        .orderBy('collected_at', 'DESC')
        .limit(limit)
        .offset((page - 1) * limit);
    return await issueHistories.getRawMany();
};
exports.getIssueHistoriesByPowerPlantId = getIssueHistoriesByPowerPlantId;
const getIssueHistorySummaryByPowerPlantId = async (powerPlantId, fromDate, toDate) => {
    const issueHistories = (0, shell_1.getRepository)(dataset_1.DataOoc)
        .createQueryBuilder('do')
        .select('COUNT(domain_id)', 'count')
        .addSelect("COUNT(CASE WHEN COALESCE(NULLIF((data::json ->> 'COM'), '')::NUMERIC, 0) = 0 THEN NULL ELSE 1 END)", 'com')
        .addSelect("COUNT(CASE WHEN COALESCE(NULLIF((data::json ->> 'RUN'), '')::NUMERIC, 0) = 0 THEN NULL ELSE 1 END)", 'run')
        .where('domain_id = :powerPlantId', { powerPlantId })
        .andWhere('name = :name', { name: '인버터' })
        .andWhere('work_date >= :fromDate', { fromDate: fromDate })
        .andWhere('work_date <= :toDate', { toDate: toDate })
        .groupBy('domain_id');
    return await issueHistories.getRawOne();
};
exports.getIssueHistorySummaryByPowerPlantId = getIssueHistorySummaryByPowerPlantId;
//# sourceMappingURL=issue-history-model.js.map