"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getThisMonthSmpAndRec = exports.getSummaryByTypeAndMonth = void 0;
const shell_1 = require("@things-factory/shell");
const dataset_1 = require("@things-factory/dataset");
const shell_2 = require("@things-factory/shell");
const getSummaryByTypeAndMonth = async (type, fromDate, toDate, page, limit) => {
    const domain = await (0, shell_1.getRepository)(shell_2.Domain).findOneBy({ name: 'SYSTEM' });
    const dataSet = await (0, shell_1.getRepository)(dataset_1.DataSet).findOneBy({ name: type, domain: { id: domain.id } });
    const queryBuilder = await (0, shell_1.getRepository)(dataset_1.DataSummary)
        .createQueryBuilder('d_summary')
        .select('d_summary.key_01', 'date')
        .addSelect('d_summary.data_01', 'HIGH_PRC')
        .addSelect('d_summary.data_02', 'LOW_PRC')
        .addSelect('d_summary.data_03', 'AVG_PRC')
        .where('d_summary.domain_id = :domainId', { domainId: domain.id })
        .andWhere('d_summary.data_set_id = :dataSetId', { dataSetId: dataSet.id })
        .andWhere('d_summary.date BETWEEN :fromDate AND :toDate', { fromDate, toDate })
        .orderBy('d_summary.date', 'DESC');
    if (limit > 0) {
        queryBuilder.limit(limit).offset((page - 1) * limit);
    }
    const items = await queryBuilder.getRawMany();
    const total = await queryBuilder.getCount();
    return { items, total };
};
exports.getSummaryByTypeAndMonth = getSummaryByTypeAndMonth;
// 최근 30일의 평균 데이터
const getThisMonthSmpAndRec = async (type) => {
    const domain = await (0, shell_1.getRepository)(shell_2.Domain).findOneBy({ name: 'SYSTEM' });
    const dataSet = await (0, shell_1.getRepository)(dataset_1.DataSet).findOneBy({ name: type, domain: { id: domain.id } });
    const queryBuilder = await (0, shell_1.getRepository)(dataset_1.DataSummary)
        .createQueryBuilder('d_summary')
        .select('AVG(d_summary.data_03)', 'AVG_PRC')
        .where('d_summary.domain_id = :domainId', { domainId: domain.id })
        .andWhere('d_summary.data_set_id = :dataSetId', { dataSetId: dataSet.id })
        .andWhere("d_summary.date::timestamp >= NOW() - INTERVAL '30 day'");
    return await queryBuilder.getRawOne();
};
exports.getThisMonthSmpAndRec = getThisMonthSmpAndRec;
//# sourceMappingURL=market-model.js.map