"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SunflowQuery = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const eaas_inverter_model_1 = require("../eaas-inverter/eaas-inverter-model");
const power_usage_1 = require("@esome-dev/sunflow-entity/dist-server/service/power-usage/power-usage");
const power_usage_model_1 = require("@esome-dev/sunflow-entity/dist-server/service/power-usage/power-usage-model");
const sunflow_entity_1 = require("@esome-dev/sunflow-entity");
const moment_1 = tslib_1.__importDefault(require("moment"));
const sunflow_type_1 = require("./sunflow-type");
let SunflowQuery = class SunflowQuery {
    async expectedSolarSavingFeeByDate(powerPlantId, startDate, endDate, groupBy, context) {
        const { domain } = context.state;
        if (sunflow_entity_1.IS_DEV) {
            startDate = (0, moment_1.default)(sunflow_entity_1.DEV_TODAY).startOf('day').format('YYYY-MM-DD');
            endDate = (0, moment_1.default)(sunflow_entity_1.DEV_TODAY).endOf('day').format('YYYY-MM-DD');
        }
        const powerUsageByDate = await (0, power_usage_model_1.getPowerUsageByDate)(powerPlantId, startDate, endDate);
        const thisMonthInverterData = await (0, eaas_inverter_model_1.getInverterSummaryDailyByPowerPlantId)(powerPlantId, startDate, endDate);
        // 시간대별 적용 금액 맵 만들기
        const pricingMap = {};
        for (const item of powerUsageByDate) {
            const date = (0, moment_1.default)(item.date).format('YYYY-MM-DD');
            const hour = item.hour.toString().padStart(2, '0');
            const dateKey = `${date}-${hour}`;
            pricingMap[dateKey] = item.hourlyApplyPricing;
        }
        // 그룹핑 합산
        const grouped = {};
        for (const inverter of thisMonthInverterData) {
            const date = (0, moment_1.default)(inverter.date).format('YYYY-MM-DD');
            const hour = inverter.period.toString().padStart(2, '0');
            const dateKey = `${date}-${hour}`;
            const pricing = pricingMap[dateKey] || 0;
            const saving = pricing * inverter.value || 0;
            const groupKey = (0, moment_1.default)(inverter.date).format(groupBy);
            grouped[groupKey] = (grouped[groupKey] || 0) + saving;
        }
        return Object.keys(grouped)
            .sort()
            .map(key => ({ date: key, value: grouped[key] }));
    }
};
exports.SunflowQuery = SunflowQuery;
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => [sunflow_type_1.DateValuePair], {
        nullable: true,
        description: '기간 내 태양광 절감 효과를 groupBy 단위로 합산하여 반환'
    }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('powerPlantId')),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('startDate')),
    tslib_1.__param(2, (0, type_graphql_1.Arg)('endDate')),
    tslib_1.__param(3, (0, type_graphql_1.Arg)('groupBy', { defaultValue: 'YYYY-MM-DD' })),
    tslib_1.__param(4, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, String, String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], SunflowQuery.prototype, "expectedSolarSavingFeeByDate", null);
exports.SunflowQuery = SunflowQuery = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(power_usage_1.PowerUsage)
], SunflowQuery);
//# sourceMappingURL=sunflow-query.js.map