/// import {Component, ViewEncapsulation} from '@angular/core'; import {ApiService} from '../api.service'; import { ChartsConfigService } from './chartConfigs.service'; import 'rxjs/add/operator/map'; interface IDistrictWeek { name: string; claimsCount: number; planClaimsCount: number; } interface IMayorTask { releaseDate: string; responsiblePerson: string; department: string; subject: string; id: number; createdAt: string; overdue?: number; } interface IMayorRaiting { districtId: number; districtName: string; previousValue: number; previousPlace: number; currentValue: number; currentPlace: number; ratingType: string; id: number; createdAt: string; } interface IChart { chart: Object; title: Object; series: Array; setTitle: any; } @Component({ selector: 'mayorService', encapsulation: ViewEncapsulation.None, styles: [require('./mayorService.scss')], template: require('./mayorService.html') }) export class MayorService { private CHANGE_CHART_AND_TABLE_TIME: number = 4000; private MAYOR_TASK_CHANGE_MAXUMUM: number = 4; private CLAIMS_CHANGE_MAXIMUM: number = 13; private CLAIMS_CHANGE_CHART_TYPE_STEP: number = 11; MayorCallAndClaims: Object[] = []; getMayorDistrictClaims: Object[] = []; MayorDepartamentClaims: Object[] = []; MayorDistrictRating: Array = [[],[]]; MayorGrowQuestions: Object[] = []; MayorFallQuestions: Object[] = []; MayorTasks: IMayorTask[] = []; MayorTasksToShow: IMayorTask; MayorClaimsStatistics: Object[] = []; MayorStatistDicsistrictNames: Object[] = []; MayorStatisticsClaimsProcessed: Object[] = []; MayorStatisticsClaimsClarified: Object[] = []; DistrictsStatisticChart: Object; DistrictsCallsAndClaimsChart: Object; DistrictClaimsWeeklyChart: Object; BarLine: Object; DistrictLine: Object[] = []; DistrictsToShow: any = []; DistrictWeek: IDistrictWeek[] = []; DistrictWeekArray: Array = []; DistrictNames: String[] = []; ClaimsCounts: Number[] = []; PlanClaimsCounts: Number[] = []; DistrictArrayClaimsCount: Array = []; DistrictArrayPlanClaimsCount: Array = []; DistrictClaimsToShow: Array = []; DistrictPlanClaimsToShow: Array = []; DistrictDaysToShow: Array = []; MayorDistrictRatingToShow: IMayorRaiting[] = []; tableSwitcher: Boolean = true; ratingType: string = ''; public claimsState: number = 2; public tasksState: number = 0; chartClaims: IChart; updateClaims(chartInstance) { this.chartClaims = chartInstance; } 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([]); } this.MayorTasksToShow = { releaseDate: '', responsiblePerson: '', department: '', subject: '', id: 0, createdAt: '', overdue: 0 }; setInterval(() => { if (this.tableSwitcher) { this.tableSwitcher = !this.tableSwitcher; this.MayorDistrictRatingToShow = this.MayorDistrictRating[0]; } else { this.tableSwitcher = !this.tableSwitcher; this.MayorDistrictRatingToShow = this.MayorDistrictRating[1]; } this.ratingType = this.MayorDistrictRatingToShow[0].ratingType; this.changeTask(); }, this.CHANGE_CHART_AND_TABLE_TIME); } changeTask() { this.MayorTasksToShow = this.MayorTasks[this.tasksState]; this.MayorTasksToShow.overdue = this.getDayMinusDate(this.MayorTasksToShow.releaseDate); this.tasksState++; this.tasksState == this.MAYOR_TASK_CHANGE_MAXUMUM ? this.tasksState = 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.DistrictClaimsWeeklyChart = this._chartsConfigService.getBaseLineConfig(this.DistrictsToShow[0].districtName, this.DistrictDaysToShow, this.DistrictClaimsToShow, this.DistrictPlanClaimsToShow); this.DistrictsCallsAndClaimsChart = this.DistrictClaimsWeeklyChart; } if (this.claimsState < this.CLAIMS_CHANGE_CHART_TYPE_STEP) { this.DistrictsToShow = this.DistrictLine[this.claimsState - 1]; this.chartClaims.setTitle({ text: this.DistrictsToShow[0].districtName + ' за тиждень' }); this.chartClaims.series[0].update({ data: this.DistrictsToShow.map((item) => item.claimsCount) }); this.chartClaims.series[1].update({ data: this.DistrictsToShow.map((item) => item.planClaimsCount) }); } else if (this.claimsState == this.CLAIMS_CHANGE_CHART_TYPE_STEP) { this.DistrictsCallsAndClaimsChart = this.BarLine; } else { this.chartClaims.series[0].update({ type: 'line' }); this.chartClaims.series[1].update({ type: 'line' }); } this.claimsState++; this.claimsState == this.CLAIMS_CHANGE_MAXIMUM ? this.claimsState = 1 : false; } getDistrictInfo(parent, data, info) { parent.push(info); if (!data.name) data.name = info.districtName; data.claimsCount ? data.claimsCount += info.claimsCount : data.claimsCount = info.claimsCount; data.planClaimsCount ? data.planClaimsCount += info.planClaimsCount : data.planClaimsCount = info.planClaimsCount; } getDayMinusDate(date) { let today = new Date().getTime(); return (today - Date.parse(date)) / 86400000; } ngOnInit() { this._chartService.getMayorCallAndClaims() .then(data => { data.forEach(item => { this.DistrictNames.push(item.districtName); this.ClaimsCounts.push(item.claimsCount); 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.DistrictArrayPlanClaimsCount.push(this.DistrictWeekArray[i].planClaimsCount); } 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.DistrictClaimsWeeklyChart = this._chartsConfigService.getBaseLineConfig(this.DistrictsToShow[0].districtName, this.DistrictDaysToShow, this.DistrictClaimsToShow, this.DistrictPlanClaimsToShow); this.BarLine = this._chartsConfigService.getBarLineConfig(this.DistrictNames, this.DistrictArrayClaimsCount, this.DistrictArrayPlanClaimsCount); }) .then(() => { this.DistrictsCallsAndClaimsChart = this.DistrictClaimsWeeklyChart; }) .then(() => { setInterval(() => { this.changeClaims(); }, this.CHANGE_CHART_AND_TABLE_TIME); }); this._chartService.getMayorDistrictClaims(3).then(data => { this.getMayorDistrictClaims = data; }); this._chartService.getMayorDepartamentClaims(3).then(data => { this.MayorDepartamentClaims = data; }); this._chartService.getMayorDistrictRating().then(data => { this.MayorDistrictRating[0] = data.splice(0, 10); this.MayorDistrictRating[1] = data; this.MayorDistrictRatingToShow = this.MayorDistrictRating[0]; }); this._chartService.getMayorGrowQuestions(3).then(data => { this.MayorGrowQuestions = data; }); this._chartService.getMayorFallQuestions(3).then(data => { this.MayorFallQuestions = data; }); this._chartService.getMayorTasks(4).then(data => { this.MayorTasks = data; this.MayorTasksToShow = this.MayorTasks[0]; this.MayorTasksToShow.overdue = this.getDayMinusDate(this.MayorTasksToShow.releaseDate); }); this._chartService.getMayorClaimsStatistics().then(data => { data.forEach(item => { this.MayorStatistDicsistrictNames.push(item.districtName); this.MayorStatisticsClaimsProcessed.push(item.claimsProcessed); this.MayorStatisticsClaimsClarified.push(item.claimsClarified); }) }) .then(() => { this.DistrictsStatisticChart = this._chartsConfigService.getStatisticChart(this.MayorStatistDicsistrictNames, this.MayorStatisticsClaimsProcessed, this.MayorStatisticsClaimsClarified); }); } }