"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EaasInverterQuery = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const shell_1 = require("@things-factory/shell");
const eaas_inverter_type_1 = require("./eaas-inverter-type");
const eaas_inverter_model_1 = require("./eaas-inverter-model");
const inverter_1 = require("@esome-dev/inverter");
const moment_timezone_1 = tslib_1.__importDefault(require("moment-timezone"));
const dataset_1 = require("@things-factory/dataset");
const sunflow_entity_1 = require("@esome-dev/sunflow-entity");
let EaasInverterQuery = class EaasInverterQuery {
    async inverterStatusListByPowerPlantId(powerPlantId, context) {
        const { user, domain } = context.state;
        const inverterStatuses = await (0, eaas_inverter_model_1.getInverterStatusByPowerPlantId)(powerPlantId);
        return inverterStatuses;
    }
    async inverterRtuBindingListByPowerPlantId(powerPlantId, context) {
        const { user, domain } = context.state;
        const inverterRtuBindingList = await (0, eaas_inverter_model_1.getInverterRtuBindingListByPowerPlantId)(powerPlantId);
        return inverterRtuBindingList;
    }
    async inverterSummaryByPowerPlantId(powerPlantId, context) {
        const { user, domain } = context.state;
        const inverterSummary = await (0, eaas_inverter_model_1.getInverterSummaryByPowerPlantId)(powerPlantId);
        return inverterSummary;
    }
    async inverterDetailByPowerPlantId(powerPlantId, context) {
        const { user, domain } = context.state;
        let result = [];
        const iss = await (0, eaas_inverter_model_1.getInverterStatusByPowerPlantId)(powerPlantId);
        for (const is of iss) {
            const inverterDetail = await (0, eaas_inverter_model_1.getInverterDetailByPowerPlantIdAndInverterId)(powerPlantId, is.ivt_id, is.collected_at);
            const data = JSON.parse(inverterDetail?.data) || {};
            result.push({
                no: inverterDetail.no,
                com: is.com,
                run: is.run,
                ac: is.ac,
                dc: is.dc,
                DC_V: data?.DC_V || 0, // DC 전압
                DC_I: data?.DC_I || 0, // DC 전류
                DC_P: data?.DC_P || 0, // DC 전력
                IV_R: data?.IV_R || 0, // AC 전압 R-S
                IV_S: data?.IV_S || 0, // AC 전압 S-T
                IV_T: data?.IV_T || 0, // AC 전압 T-R
                IC_R: data?.IC_R || 0, // AC 전류 R
                IC_S: data?.IC_S || 0, // AC 전류 S
                IC_T: data?.IC_T || 0, // AC 전류 T
                AC_P: data?.AC_P || 0, // AC 전력
                collectedAt: is.collected_at, // 수신시간
                isNoSignal: is.isNoSignal // 통신 장애 여부
            });
        }
        result = await this.bindingInverterData(result, powerPlantId);
        return result;
    }
    async inverterSeperateDailyByPowerPlantId(powerPlantId, date, context) {
        const { user, domain } = context.state;
        let inverterDailyData = await (0, eaas_inverter_model_1.getInverterSeperateyDailyByPowerPlantId)(powerPlantId, date);
        return inverterDailyData;
    }
    async inverterSeperateMonthlyByPowerPlantId(powerPlantId, date, context) {
        const { user, domain } = context.state;
        let inverterMonthlyData = await (0, eaas_inverter_model_1.getInverterSeperateMonthlyByPowerPlantId)(powerPlantId, date);
        return inverterMonthlyData;
    }
    async inverterSeperateYearlyByPowerPlantId(powerPlantId, date, context) {
        const { user, domain } = context.state;
        let inverterYearlyData = await (0, eaas_inverter_model_1.getInverterSeperateYearlyByPowerPlantId)(powerPlantId, date);
        return inverterYearlyData;
    }
    async inverterSummaryBetweenDate(powerPlantId, startDate, endDate, context) {
        const { user, domain } = context.state;
        const inverterDailyAverageGenerationTime = await (0, eaas_inverter_model_1.getInverterSummaryBetweenDate)(powerPlantId, startDate, endDate);
        const sum = inverterDailyAverageGenerationTime.reduce((acc, curr) => Number(acc) + Number(curr.generationTime), 0);
        const count = inverterDailyAverageGenerationTime.length;
        return {
            dailyAverageGenerationTime: count > 0 ? sum / count : 0
        };
    }
    // 인버터 아이디 no라는 키를 기준으로 인버터
    async bindingInverterData(inverterDailyData, powerPlantId) {
        const inverterInfos = await (0, eaas_inverter_model_1.getInverterInfosByPowerPlantId)(powerPlantId);
        return inverterDailyData.map(data => {
            const inverterInfo = inverterInfos.find(info => info.no == data.no);
            return { ...data, description: inverterInfo?.description };
        });
    }
    async inverterSummaryDailyByPowerPlantId(powerPlantId, date, context) {
        const { user, domain } = context.state;
        if (sunflow_entity_1.IS_DEV) {
            date = sunflow_entity_1.DEV_TODAY;
        }
        const inverterDailyData = await (0, eaas_inverter_model_1.getInverterSummaryDailyByPowerPlantId)(powerPlantId, date);
        return inverterDailyData;
    }
    async inverterSummaryMonthlyByPowerPlantId(powerPlantId, yearMonth, context) {
        const { user, domain } = context.state;
        if (sunflow_entity_1.IS_DEV) {
            yearMonth = (0, moment_timezone_1.default)(sunflow_entity_1.DEV_TODAY).format('YYYY-MM');
        }
        const inverterMonthlyData = await (0, eaas_inverter_model_1.getInverterSummaryMonthlyByPowerPlantId)(powerPlantId, yearMonth);
        return inverterMonthlyData;
    }
    async inverterSummaryYearlyByPowerPlantId(powerPlantId, date, context) {
        const { user, domain } = context.state;
        if (sunflow_entity_1.IS_DEV) {
            date = sunflow_entity_1.DEV_TODAY;
        }
        const inverterYearlyData = await (0, eaas_inverter_model_1.getInverterSummaryYearlyByPowerPlantId)(powerPlantId, date);
        return inverterYearlyData;
    }
    async inverterHistoriesByDate(powerPlantId, inverterId, dateType, date, page, limit, context) {
        const { user, domain } = context.state;
        if (sunflow_entity_1.IS_DEV) {
            date = sunflow_entity_1.DEV_TODAY;
        }
        let { items, total } = await (0, eaas_inverter_model_1.getInverterHistoriesByDate)(powerPlantId, inverterId, dateType, date, page, limit);
        items = items.map(item => {
            const data = JSON.parse(item.data) || {};
            return {
                no: item.no,
                PF: data?.PF || 0, // 역률
                DC_V: data?.DC_V || 0, // DC 전압
                DC_I: data?.DC_I || 0, // DC 전류
                DC_P: data?.DC_P || 0, // DC 전력
                IV_R: data?.IV_R || 0, // AC 전압 R-S
                IV_S: data?.IV_S || 0, // AC 전압 S-T
                IV_T: data?.IV_T || 0, // AC 전압 T-R
                IC_R: data?.IC_R || 0, // AC 전류 R
                IC_S: data?.IC_S || 0, // AC 전류 S
                IC_T: data?.IC_T || 0, // AC 전류 T
                AC_P: data?.AC_P || 0, // AC 전력
                dailyGeneration: data?.ACC_P, // 일일 누적 발전량
                totalGeneration: data?.DS, // 총 누적 발전량
                collectedAt: item.collectedAt, // 타임스탬프 (타임존이 서울로 들어가있어서 날짜가 깨짐.)
                DATE_INFO: data?.DATE_INFO // 시간
            };
        });
        return { items, total };
    }
    async allInverterHistoriesByDate(powerPlantId, date, context) {
        const { user, domain } = context.state;
        const inverterHistories = await (0, eaas_inverter_model_1.getAllInverterHistoriesByDate)(powerPlantId, date);
        const items = inverterHistories.map(item => {
            const data = JSON.parse(item.data) || {};
            return {
                no: item.no,
                PF: data?.PF || 0, // 역률
                DC_V: data?.DC_V || 0, // DC 전압
                DC_I: data?.DC_I || 0, // DC 전류
                DC_P: data?.DC_P || 0, // DC 전력
                IV_R: data?.IV_R || 0, // AC 전압 R-S
                IV_S: data?.IV_S || 0, // AC 전압 S-T
                IV_T: data?.IV_T || 0, // AC 전압 T-R
                IC_R: data?.IC_R || 0, // AC 전류 R
                IC_S: data?.IC_S || 0, // AC 전류 S
                IC_T: data?.IC_T || 0, // AC 전류 T
                AC_P: data?.AC_P || 0, // AC 전력
                dailyGeneration: data?.ACC_P, // 일일 누적 발전량
                totalGeneration: data?.DS, // 총 누적 발전량
                collectedAt: item.collectedAt, // 타임스탬프 (타임존이 서울로 들어가있어서 날짜가 깨짐.)
                DATE_INFO: data?.DATE_INFO // 시간
            };
        });
        return items;
    }
};
exports.EaasInverterQuery = EaasInverterQuery;
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => [inverter_1.InverterStatus], { nullable: true, description: '' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('powerPlantId')),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], EaasInverterQuery.prototype, "inverterStatusListByPowerPlantId", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => [dataset_1.DataSensor], { nullable: true, description: '' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('powerPlantId')),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], EaasInverterQuery.prototype, "inverterRtuBindingListByPowerPlantId", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => eaas_inverter_type_1.EaasInverterSummary, { nullable: true, description: '' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('powerPlantId')),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], EaasInverterQuery.prototype, "inverterSummaryByPowerPlantId", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => [eaas_inverter_type_1.EaasInverterDetail], { nullable: true, description: '' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('powerPlantId')),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], EaasInverterQuery.prototype, "inverterDetailByPowerPlantId", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => [eaas_inverter_type_1.EaasInverterData], { nullable: true, description: '' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('powerPlantId')),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('date')),
    tslib_1.__param(2, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], EaasInverterQuery.prototype, "inverterSeperateDailyByPowerPlantId", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => [eaas_inverter_type_1.EaasInverterData], { nullable: true, description: '' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('powerPlantId')),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('date')),
    tslib_1.__param(2, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], EaasInverterQuery.prototype, "inverterSeperateMonthlyByPowerPlantId", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => [eaas_inverter_type_1.EaasInverterData], { nullable: true, description: '' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('powerPlantId')),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('date')),
    tslib_1.__param(2, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], EaasInverterQuery.prototype, "inverterSeperateYearlyByPowerPlantId", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => eaas_inverter_type_1.EaasInverterSummaryBetweenDate, { nullable: true, description: '' }),
    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.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], EaasInverterQuery.prototype, "inverterSummaryBetweenDate", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => [eaas_inverter_type_1.EaasInverterPeriodData], { nullable: true, description: '' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('powerPlantId')),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('date')),
    tslib_1.__param(2, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], EaasInverterQuery.prototype, "inverterSummaryDailyByPowerPlantId", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => [eaas_inverter_type_1.EaasInverterPeriodData], { nullable: true, description: '' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('powerPlantId')),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('yearMonth')),
    tslib_1.__param(2, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], EaasInverterQuery.prototype, "inverterSummaryMonthlyByPowerPlantId", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => [eaas_inverter_type_1.EaasInverterPeriodData], { nullable: true, description: '' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('powerPlantId')),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('date')),
    tslib_1.__param(2, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], EaasInverterQuery.prototype, "inverterSummaryYearlyByPowerPlantId", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => eaas_inverter_type_1.EaasInverterHistoryList, { nullable: true, description: '' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('powerPlantId')),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('inverterId')),
    tslib_1.__param(2, (0, type_graphql_1.Arg)('dateType')),
    tslib_1.__param(3, (0, type_graphql_1.Arg)('date')),
    tslib_1.__param(4, (0, type_graphql_1.Arg)('page')),
    tslib_1.__param(5, (0, type_graphql_1.Arg)('limit')),
    tslib_1.__param(6, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, String, String, Number, Number, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], EaasInverterQuery.prototype, "inverterHistoriesByDate", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => [eaas_inverter_type_1.EaasInverterHistory], { nullable: true, description: '' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('powerPlantId')),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('date')),
    tslib_1.__param(2, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], EaasInverterQuery.prototype, "allInverterHistoriesByDate", null);
exports.EaasInverterQuery = EaasInverterQuery = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(shell_1.Domain)
], EaasInverterQuery);
//# sourceMappingURL=eaas-inverter-query.js.map