import {Component, ViewEncapsulation} from '@angular/core'; import {ApiService} from '../api.service'; import { ChartsConfigService } from './chartConfigs.service'; interface IDistrictWeek { name: String; callsCount: Number; claimsCount: Number; planCallsCount: Number; planClaimsCount: Number; } interface IClaimStatistics { total: number; overdue: number; inProgress: number; } interface IPieServiceClaimsAll { service: string; claimsCount: number; } interface IChart { chart: Object; title: Object; series: Array; setTitle?: any; } @Component({ selector: 'housing', encapsulation: ViewEncapsulation.None, styles: [require('./housing.scss')], template: require('./housing.html') }) export class Housing { private CHANGE_CHART_AND_TABLE_TIME: number = 4000; private PIE_CHANGE_MAXIMUM: number = 4; private CLAIMS_CHANGE_MAXIMUM: number = 13; private CLAIMS_CHANGE_CHART_TYPE_STEP: number = 11; chart: HighchartsChartObject; options: HighchartsOptions; EmergencyClaimDetail : Object[] = []; Top3ProblemBuildings : Object[] = []; Top3ProblemElevators : Object[] = []; Top5ProblemHousing : Object[] = []; Top5ProblemClaims : Object[] = []; AvgQueueTime : Object[] = []; DistrictsWithoutNow : Object[] = []; DistrictsWithoutYesterday : Object[] = []; DistrictsWithoutToShow: Object[] = []; DistrictsWithoutNowSum: Object[] = []; DistrictsWithoutYesterdaySum: Object[] = []; DistrictsWithoutToShowSum: Object[] = []; ClaimsChartToShow: Object; CallsChartToShow: Object; ClaimsByDistrictsChartTypeLine: Object; ClaimsByDistrictsChartTypeBar: Object; Pie: Object; CallsByDistrictsChartTypeBar: Object; CallsByDistrictsChartTypeArea: Object; DistrictNames : String[] = []; CallsCounts : Number[] = []; ClaimsCounts : Number[] = []; PlanCallsCounts : Number[] = []; PlanClaimsCounts : Number[] = []; DistrictLine: Object[] = []; DistrictWeek: IDistrictWeek[] = []; DistrictWeekArray: Array = []; DistrictArrayClaimsCount: Array = []; DistrictArrayCallsCount: Array = []; DistrictArrayPlanClaimsCount: Array = []; DistrictArrayPlanCallsCount: Array = []; DistrictClaimsToShow: Array = []; DistrictPlanClaimsToShow: Array = []; DistrictDaysToShow: Array = []; DistrictsToShow: any = []; PieServiceClaimsEmergencies: Array = []; SumClaimsEmergencies : number = 0; EmergenciesTitleName : String = ''; TitleValueEmergencies: string | number; ClaimStatisticsTotal: number = 0; ClaimStatisticsOverdue: number = 0; ClaimStatisticsToShow: string | number; ClaimStatisticsInRows: number = 0; public pieState : number = 1; public claimsState : number = 2; public callsState: string = 'area'; public switchTablesInfoState: string = 'now'; dayState: String; chartClaimsState: IChart; chartCallsState: IChart; chartPieState: IChart; constructor(private _chartService: ApiService, private _chartsConfigService: ChartsConfigService) { for (let i = 0; i < 10; i++) { this.DistrictWeekArray.push(this.DistrictWeek); this.DistrictWeekArray[i] = []; this.DistrictLine.push([]); } setInterval(() => { this.changePie(); }, this.CHANGE_CHART_AND_TABLE_TIME); setInterval(() => { if (this.switchTablesInfoState == 'now') { this.switchTablesInfoState = 'yesterday'; this.DistrictsWithoutToShow = this.DistrictsWithoutNow; this.DistrictsWithoutToShowSum = this.DistrictsWithoutNowSum; this.dayState = 'на сьогодні'; this.ClaimStatisticsToShow = (this.ClaimStatisticsOverdue / this.ClaimStatisticsTotal * 100).toFixed(2) + '%'; } else { this.switchTablesInfoState = 'now'; this.DistrictsWithoutToShow = this.DistrictsWithoutYesterday; this.DistrictsWithoutToShowSum = this.DistrictsWithoutYesterdaySum; this.dayState = 'за вчора'; this.ClaimStatisticsToShow = this.ClaimStatisticsOverdue; } }, this.CHANGE_CHART_AND_TABLE_TIME); let getPiePromise: [Promise, Promise] = [this._chartService.getClaimStatistics(), this._chartService.getEmergencies()]; Promise.all(getPiePromise) .then((result: any[]) => { let pieServiceClaimsAll: IPieServiceClaimsAll[] = JSON.parse(result[1]._body); let claimState: IClaimStatistics = JSON.parse(result[0]._body); this.ClaimStatisticsTotal = claimState.total; this.ClaimStatisticsOverdue = claimState.overdue; this.ClaimStatisticsInRows = claimState.total - claimState.inProgress; this.ClaimStatisticsToShow = claimState.overdue; pieServiceClaimsAll.forEach(item => { this.PieServiceClaimsEmergencies.push([item.service, item.claimsCount]); this.SumClaimsEmergencies += item.claimsCount; }); }) .then(() => { this.EmergenciesTitleName = this.PieServiceClaimsEmergencies[0][0]; this.TitleValueEmergencies = (this.PieServiceClaimsEmergencies[0][1] * 100 / this.SumClaimsEmergencies).toFixed(2); }) .then(() => { this.Pie = this._chartsConfigService.getPieConfig(this.EmergenciesTitleName, this.TitleValueEmergencies, this.PieServiceClaimsEmergencies, this.ClaimStatisticsOverdue, this.ClaimStatisticsInRows); }); } updatePie(chartInstance) { this.chartPieState = chartInstance; } updateClaims(chartInstance) { this.chartClaimsState = chartInstance; } updateCalls(chartInstance) { this.chartCallsState = chartInstance; } changePie() { this.chartPieState.setTitle({ text : this.PieServiceClaimsEmergencies[this.pieState][0] + ' ' + (this.PieServiceClaimsEmergencies[this.pieState][1]*100 / this.SumClaimsEmergencies).toFixed(2) + '%'}); let serie = this.chartPieState.series[0]; let prevPoint = serie.points[this.pieState]; prevPoint.setState(''); let point = serie.points[this.pieState]; point.setState('hover'); serie.chart.hoverSeries = serie; this.pieState++; this.pieState == this.PIE_CHANGE_MAXIMUM ? this.pieState = 0: false; } changeClaims() { if (this.claimsState == 1) { this.DistrictDaysToShow = this.DistrictsToShow.map((item) => new Date(Date.parse(item.date)).toLocaleString('uk-UA', { weekday: 'short' })); this.ClaimsByDistrictsChartTypeLine = this._chartsConfigService.getBaseLineConfig(this.DistrictsToShow[0].districtName, this.DistrictDaysToShow, this.DistrictClaimsToShow, this.DistrictPlanClaimsToShow); this.ClaimsChartToShow = this.ClaimsByDistrictsChartTypeLine; } if (this.claimsState < this.CLAIMS_CHANGE_CHART_TYPE_STEP) { this.DistrictsToShow = this.DistrictLine[this.claimsState - 1]; this.chartClaimsState.setTitle({ text: this.DistrictsToShow[0].districtName + ' за тиждень' }); this.chartClaimsState.series[0].update({ data: this.DistrictsToShow.map((item) => item.claimsCount) }); this.chartClaimsState.series[1].update({ data: this.DistrictsToShow.map((item) => item.planClaimsCount) }); } else if (this.claimsState == this.CLAIMS_CHANGE_CHART_TYPE_STEP) { this.ClaimsChartToShow = this.ClaimsByDistrictsChartTypeBar; } else { this.chartClaimsState.series[0].update({ type: 'line' }); this.chartClaimsState.series[1].update({ type: 'line' }); } this.claimsState++; this.claimsState == this.CLAIMS_CHANGE_MAXIMUM ? this.claimsState = 1: false; } changeCalls() { if (this.callsState == 'bar') { this.CallsChartToShow = this.CallsByDistrictsChartTypeBar; this.callsState = 'area'; } else { this.CallsChartToShow = this.CallsByDistrictsChartTypeArea; this.callsState = 'bar'; } } getDistrictInfo(parent, data, info) { parent.push(info); if (!data.name) data.name = info.districtName; data.callsCount ? data.callsCount += info.callsCount : data.callsCount = info.callsCount; data.claimsCount ? data.claimsCount += info.claimsCount : data.claimsCount = info.claimsCount; data.planCallsCount ? data.planCallsCount += info.planCallsCount : data.planCallsCount = info.planCallsCount; data.planClaimsCount ? data.planClaimsCount += info.planClaimsCount : data.planClaimsCount = info.planClaimsCount; } getWithoutSum(data, property) { data.forEach((item) => { property.gvp ? property.gvp += item.gvp : property.gvp = item.gvp; property.hvp ? property.hvp += item.hvp : property.hvp = item.hvp; property.co ? property.co += item.co : property.co = item.co; property.electricity ? property.electricity += item.electricity : property.electricity = item.electricity; }) } ngOnInit() { this._chartService.getCallsAndClaims() .then(data=> { data.forEach(item => { this.DistrictNames.push(item.districtName); this.CallsCounts.push(item.callsCount); this.ClaimsCounts.push(item.claimsCount); this.PlanCallsCounts.push(item.planCallsCount); this.PlanClaimsCounts.push(item.planClaimsCount); this.getDistrictInfo(this.DistrictLine[item.districtId - 1], this.DistrictWeekArray[item.districtId - 1], item); }); }) .then(() => { let i = this.DistrictNames.length; this.DistrictNames.sort(); while (i--) { if (this.DistrictNames[i] == this.DistrictNames[i-1]) { this.DistrictNames.splice(i, 1); } } this.DistrictsToShow = this.DistrictLine[0]; let districtLength = this.DistrictWeekArray.length; for (let i = 0; i < districtLength; i++) { this.DistrictArrayClaimsCount.push(this.DistrictWeekArray[i].claimsCount); this.DistrictArrayCallsCount.push(this.DistrictWeekArray[i].callsCount); this.DistrictArrayPlanClaimsCount.push(this.DistrictWeekArray[i].planClaimsCount); this.DistrictArrayPlanCallsCount.push(this.DistrictWeekArray[i].planCallsCount); } this.DistrictClaimsToShow = this.DistrictsToShow.map((item) => item.claimsCount); this.DistrictPlanClaimsToShow = this.DistrictsToShow.map((item) => item.planClaimsCount); this.DistrictDaysToShow = this.DistrictsToShow.map((item) => new Date(Date.parse(item.date)).toLocaleString('uk-UA', { weekday: 'short' })); this.ClaimsByDistrictsChartTypeLine = this._chartsConfigService.getBaseLineConfig(this.DistrictsToShow[0].districtName, this.DistrictDaysToShow, this.DistrictClaimsToShow, this.DistrictPlanClaimsToShow); this.ClaimsByDistrictsChartTypeBar = this._chartsConfigService.getBarLineConfig(this.DistrictNames, this.DistrictArrayClaimsCount, this.DistrictArrayPlanClaimsCount); this.CallsByDistrictsChartTypeBar = this._chartsConfigService.getBarConfig(this.DistrictNames, this.DistrictArrayCallsCount, this.DistrictArrayPlanCallsCount); this.CallsByDistrictsChartTypeArea = this._chartsConfigService.getAreaConfig(this.DistrictNames, this.DistrictArrayCallsCount, this.DistrictArrayPlanCallsCount); }) .then(() => { this.CallsChartToShow = this.CallsByDistrictsChartTypeBar; this.ClaimsChartToShow = this.ClaimsByDistrictsChartTypeLine; }) .then(() => { setInterval(() => { this.changeClaims(); this.changeCalls(); }, this.CHANGE_CHART_AND_TABLE_TIME); }); this._chartService.getEmergencyClaimDetail().then(data=> { this.EmergencyClaimDetail = data; }); this._chartService.getTop3ProblemBuildings().then(data=> { this.Top3ProblemBuildings = data; }); this._chartService.getTop3ProblemElevators().then(data=> { this.Top3ProblemElevators = data; }); this._chartService.getTop5ProblemHousing().then(data=> { this.Top5ProblemHousing = data; }); this._chartService.getTop5ProblemClaims().then(data=> { this.Top5ProblemClaims = data; }); this._chartService.getAvgQueueTime().then(data=> { this.AvgQueueTime = data; }); this._chartService.getDistrictsWithoutNow().then(data=> { this.DistrictsWithoutNow = data; this.getWithoutSum(data, this.DistrictsWithoutNowSum); }); this._chartService.getDistrictsWithoutYesterday().then(data=> { this.DistrictsWithoutYesterday = data; this.getWithoutSum(data, this.DistrictsWithoutYesterdaySum); this.DistrictsWithoutToShowSum = this.DistrictsWithoutYesterdaySum; }); } }