fengxiang
2018-03-08 efe936a73370a55d4c3336fb9973a92fcf87efff
src/app/routes/analysis/query/query.component.ts
@@ -1,11 +1,17 @@
import { LineChartCriteria, TimePeriod } from '@business/entity/data';
import { PageBean } from '@business/entity/grid';
import { ExampleService } from '@business/services/util/example.service';
import { MonitorPointService } from '@business/services/http/monitor-point.service';
import { AreacodeService } from '@business/services/http/areacode.service';
import { DataCondition } from './../../../business/entity/data';
import { LineChartCriteria, TimePeriod, Organization, MonitorPoint } from '@business/entity/data';
import { PageBean, ResultBean } from '@business/entity/grid';
import { Sensor } from '@business/entity/data';
import { SensorsService } from '@business/services/http/sensors.service';
import { Component, OnInit } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { TimeUnits } from '@business/enum/types.enum';
import { TimeUnits, AreaRange, ResultCode } from '@business/enum/types.enum';
import * as moment from 'moment';
import { CascaderOption } from 'ng-zorro-antd/src/cascader/nz-cascader.component';
import { environment } from '@env/environment';
@Component({
  selector: 'app-query',
@@ -13,79 +19,322 @@
  styleUrls: [ './query.component.less' ]
})
export class QueryComponent implements OnInit {
    public sensorList:Sensor[];
    public timeUnits:{ label: string, value: TimeUnits, disabled: boolean }[] = [];
    // public timePeriod:TimePeriod = {startTime:null,endTime:null};
    public lineChartCriteria:LineChartCriteria = {sensorKey:'e1', timePeriod:{startTime:null,endTime:null},dataConditions:[]};
    get lineChartSensorName():String{
        return !!this.sensorList?this.sensorList.find( sensor => sensor.sensorKey==this.lineChartCriteria.sensorKey).name:null;
    public sensorList: Sensor[];
    public timeUnits: { label: string, value: TimeUnits, disabled: boolean }[] = [];
    public lineChartCriteria: LineChartCriteria = {sensorKey: 'e1', timePeriod: {startTime: null, endTime: null}, dataConditions: []};
    _areas: {label: any, value: any};
    _monitorPoint;
    public monitorPoints: MonitorPoint[];
    get lineChartSensorName(): String {
        return !!this.sensorList ? this.sensorList.find( sensor => sensor.sensorKey === this.lineChartCriteria.sensorKey).name : null;
    }
    private _timePeriodUnitValue:TimeUnits=TimeUnits.DAY;
    public set timePeriodUnitValue(value:TimeUnits){
    public _timePeriodUnitValue: TimeUnits = TimeUnits.DAY;
    public set timePeriodUnitValue(value: TimeUnits) {
        this._timePeriodUnitValue = value;
        switch(value){
            //YYYY-MM-DD HH:mm:ss
        switch (value) {
            // YYYY-MM-DD HH:mm:ss
            case TimeUnits.MONTH: 
            this.timeType.dateFormat ="YYYY-MM";
            this.timeType.mode = "month";
            this.timeType.dateFormat = 'YYYY-MM';
            this.timeType.mode = 'month';
            this.timeType.timePickerOption = null;
            break;
            case TimeUnits.DAY: 
            this.timeType.dateFormat ="YYYY-MM-DD";
            this.timeType.mode = "day";
            this.timeType.dateFormat = 'YYYY-MM-DD';
            this.timeType.mode = 'day';
            this.timeType.timePickerOption = null;
            break;
            case TimeUnits.HOUR: 
            this.timeType.dateFormat ="YYYY-MM-DD HH 时";
            this.timeType.timePickerOption = {nzFormat:"HH:mm"};
            this.timeType.dateFormat = 'YYYY-MM-DD HH 时';
            this.timeType.mode = 'day';
            this.timeType.timePickerOption = true;
            break;
            case TimeUnits.MINUTE:
            this.timeType.dateFormat = 'YYYY-MM-DD HH:mm';
            this.timeType.mode = 'day';
            this.timeType.timePickerOption = true;
            break;
        }
    }
    public timeType:{dateFormat?:string,mode?:'year'|'month'|'day',timePickerOption?:{}}
    = {dateFormat:"YYYY-MM-DD",mode:"day",timePickerOption:null};
    get timePeriodUnitName():String {
        return this.timeUnits.find( item => item.value == this._timePeriodUnitValue).label;
    public timeType: {dateFormat?: string, mode?: 'month'|'day', timePickerOption?: boolean}
    = {dateFormat: 'YYYY-MM-DD', mode: 'day', timePickerOption: null};
    get timePeriodUnitName(): String {
        return this.timeUnits.find( item => item.value === this._timePeriodUnitValue).label;
    }
    constructor(
        private sensorsService:SensorsService
    ) {
        this.initPage();
        private sensorsService: SensorsService,
        private areacodeService: AreacodeService,
        private http: _HttpClient,
        private monitorPointService: MonitorPointService,
    ) {
    }
    private initPage(){
        this.sensorsService.getPagingList(null,null).subscribe(
    private initPage() {
        this.sensorsService.getPagingList(null, null).subscribe(
            (res: PageBean)  => {
                this.sensorList = res.data;
            }
        );
        this.timeUnits.push(
            {label:'年',value:TimeUnits.YEAR,disabled:false},
            {label:'月',value:TimeUnits.MONTH,disabled:false},
            {label:'日',value:TimeUnits.DAY,disabled:false},
            {label:'时',value:TimeUnits.HOUR,disabled:false},
            {label:'分',value:TimeUnits.MINUTE,disabled:false}
            {label: '年', value: TimeUnits.YEAR, disabled: false},
            {label: '月', value: TimeUnits.MONTH, disabled: false},
            {label: '日', value: TimeUnits.DAY, disabled: false},
            {label: '时', value: TimeUnits.HOUR, disabled: false},
            {label: '分', value: TimeUnits.MINUTE, disabled: false}
        );
        const now = new Date();
        this.lineChartCriteria.timePeriod.startTime = now;
        this.lineChartCriteria.timePeriod.endTime = new Date(now.getTime());
        // 省市区 初始值
        this.http.get(environment.SERVER_BASH_URL + 'organization/get-my-org').subscribe(
           ( res: ResultBean<Organization>) => {
              if (res.code === ResultCode.SUCCESS) {
                    const org = res.data;
                    if (!!org.areaNames) {
                        const areaNames = org.areaNames;
                        this._areas = {label: null, value: null};
                        this._areas.label = Object.values(areaNames).filter(
                            val => !!val
                        ).join('/');
                        if (areaNames.areaName != null) {
                            this._areas.value = org.areaCode;
                            this.dataConditionTemp.areaRange = AreaRange.AREA;
                            this.dataConditionTemp.areaRangeId = org.areaCode;
                        } else if (areaNames.cityName != null) {
                            this._areas.value = org.cityCode;
                            this.dataConditionTemp.areaRange = AreaRange.CITY;
                            this.dataConditionTemp.areaRangeId = org.cityCode;
                        } else {
                            this._areas.value = org.provinceCode;
                            this.dataConditionTemp.areaRange = AreaRange.PROVINCE;
                            this.dataConditionTemp.areaRangeId = org.provinceCode;
                        }
                    } else {
                        this._areas = {label: '江苏省/苏州市/昆山市', value: 320583};
                        this.dataConditionTemp.areaRange = AreaRange.AREA;
                        this.dataConditionTemp.areaRangeId = 320583;
                      }
              }
           }
        );
    }
    monitorPointChange(text) {
        const pageBean: PageBean = {pageIndex: 0, pageSize: 20};
        const example = new ExampleService();
        let areaName = '';
        switch (this.dataConditionTemp.areaRange) {
            case AreaRange.PROVINCE:
            areaName = 'provinceCode'; break;
            case AreaRange.CITY:
            areaName = 'cityCode'; break;
            case AreaRange.AREA:
            areaName = 'areaCode'; break;
        }
        if (!!text) {
            example.or().andLike({name: 'name', value: '%' + text + '%'})
            .andEqualTo({name: areaName, value: this.dataConditionTemp.areaRangeId});
        } else {
            example.or()
            .andEqualTo({name: areaName, value: this.dataConditionTemp.areaRangeId});
        }
        this.monitorPointService.getPageByExample(pageBean, example).subscribe(
          (res: PageBean) => {
               if (!!res && !!res.data) {
                   this.monitorPoints = res.data;
               }
          }
       );
       }
    ngOnInit() {
        this.initPage();
    }
    get _isSameDay() {
        console.log(this.lineChartCriteria.timePeriod.startTime && this.lineChartCriteria.timePeriod.endTime && moment(this.lineChartCriteria.timePeriod.startTime).isSame(this.lineChartCriteria.timePeriod.endTime, 'day'));
        return this.lineChartCriteria.timePeriod.startTime && this.lineChartCriteria.timePeriod.endTime && moment(this.lineChartCriteria.timePeriod.startTime).isSame(this.lineChartCriteria.timePeriod.endTime, 'day');
    }
    _disabledStartDate = (startValue) => {
    get _startShowTime() {
        return this.timeType.timePickerOption ?
           {nzHideDisabledOptions: true,
            nzDisabledHours: () => {
                const endTime = this.lineChartCriteria.timePeriod.endTime;
                const hours = this.newArray(50);
                return this._isSameDay && !!endTime ? hours.slice(endTime.getHours() + 1, 24) : [];
            },
            nzDisabledMinutes: (h) => {
                if (this._timePeriodUnitValue === TimeUnits.MINUTE) {
                    const endTime = this.lineChartCriteria.timePeriod.endTime;
                    if (this._isSameDay && h === endTime.getHours()) {
                        const minutes = this.newArray(122);
                        return minutes.slice(endTime.getMinutes() + 1, 60);
                    } else {
                        return [];
                    }
                } else {
                    return this.newArray(60).slice(1);
                }
            },
            nzDisabledSeconds: () => {
                return this.newArray(60).slice(1);
            }
            } : false;
    }
    get _endShowTime() {
        return this.timeType.timePickerOption ?
           {nzHideDisabledOptions: true,
            nzDisabledHours: () => {
                const startTime = this.lineChartCriteria.timePeriod.startTime;
                return this._isSameDay && !!startTime ? this.newArray(startTime.getHours()) : [];
            },
            nzDisabledMinutes: (h) => {
                if (this._timePeriodUnitValue === TimeUnits.MINUTE) {
                    const startTime = this.lineChartCriteria.timePeriod.startTime;
                    if (this._isSameDay && h === startTime.getHours()) {
                        return this.newArray(startTime.getMinutes());
                    } else {
                        return [];
                    }
                } else {
                    return this.newArray(60).slice(1);
                }
            },
            nzDisabledSeconds: () => {
                return this.newArray(60).slice(1);
            }
            } : false;
    }
    newArray = (len) => {
        const result = [];
        for (let i = 0; i < len; i++) {
          result.push(i);
        }
        return result;
      }
    _disabledStartDate = (startValue: Date) => {
        if (!startValue || !this.lineChartCriteria.timePeriod.endTime) {
          return false;
        }
        console.log(startValue);
        console.log("getTime:"+startValue.getTime());
        let _startValue = moment(startValue,this.timeType.dateFormat);
        console.log("valueOf:"+_startValue.valueOf());
        return _startValue.valueOf() > this.lineChartCriteria.timePeriod.endTime;
      };
      _disabledEndDate = (endValue) => {
        let format = this.timeType.dateFormat;
        // 单位为时,开始日期中 日默认时间设置为 00.00.00
        if (format.indexOf('H', 11)) {
            format = format.replace(/HH.*/, '');
        }
        const _startValue = moment(moment(startValue).format(format), format);
        return _startValue.valueOf() > this.lineChartCriteria.timePeriod.endTime.getTime();
      }
      _disabledEndDate = (endValue: Date) => {
        if (!endValue || !this.lineChartCriteria.timePeriod.startTime) {
          return false;
        }
        return endValue.getTime() < this.lineChartCriteria.timePeriod.startTime;
      };
        const _endValue: Date = new Date(endValue.getTime());
        _endValue.setHours(23);
        _endValue.setMinutes(59);
        _endValue.setSeconds(59);
        return _endValue.getTime() < this.lineChartCriteria.timePeriod.startTime.getTime();
      }
      _timeChange = (type: string) => {
        const startTime = this.lineChartCriteria.timePeriod.startTime;
        const endTime = this.lineChartCriteria.timePeriod.endTime;
        if (type === 'start') {
            !!endTime ? '' : this.lineChartCriteria.timePeriod.endTime = startTime;
        } else {
            !!startTime ? '' : this.lineChartCriteria.timePeriod.startTime === endTime;
        }
      }
      get startYear(): number {
          !!this.lineChartCriteria.timePeriod.startTime ?
          '' : this.lineChartCriteria.timePeriod.startTime = new Date();
          return this.lineChartCriteria.timePeriod.startTime.getFullYear();
      }
      set startYear(value) {
        !!this.lineChartCriteria.timePeriod.startTime ?
        '' : this.lineChartCriteria.timePeriod.startTime = new Date();
        if (this.startYear === this.endYear && value < this.startYear) {
            // 异步调用防止循环
            setTimeout(() => {
                this.endYear += 1;
            }, 1);
        }
        this.lineChartCriteria.timePeriod.startTime.setFullYear(value);
      }
      get minStartYear(): number {
        const now = new Date();
        return now.getFullYear() - 50;
      }
      get maxStartYear(): number {
        return this.endYear;
      }
      get endYear(): number {
        !!this.lineChartCriteria.timePeriod.endTime ?
        '' : this.lineChartCriteria.timePeriod.endTime = new Date();
        return this.lineChartCriteria.timePeriod.endTime.getFullYear();
     }
     set endYear(value) {
        !!this.lineChartCriteria.timePeriod.endTime ?
        '' : this.lineChartCriteria.timePeriod.endTime = new Date();
        if (this.startYear === this.endYear && value > this.startYear) {
            // 异步调用防止循环
            setTimeout(() => {
                this.startYear -= 1;
            }, 1);
        }
        this.lineChartCriteria.timePeriod.endTime.setFullYear(value);
     }
     get minEndYear(): number {
        return this.startYear;
      }
      get maxEndYear(): number {
        const now = new Date();
        return now.getFullYear() + 50;
      }
      dataConditionTemp: DataCondition = {};
      areaLazyLoad(event: { option: CascaderOption, index: number, resolve: (children: CascaderOption[]) => void, reject: () => void }) {
        const index = event['index'];
        const option = event.option;
        switch (index) {
           case -1:
           this.areacodeService.getProvinces().subscribe(
             (res: {label: string, value: string}[]) => {
                 event.resolve( res );
             }
           ); break;
           case 0:
           this.areacodeService.getCities(option.value).subscribe(
             (res: {label: string, value: string}[]) => {
                 event.resolve( res );
             }
           ); break;
           case 1:
           this.areacodeService.getAreas(option.value).subscribe(
             (res: {label: string, value: string}[]) => {
                 event.resolve( res );
             }
           ); break;
        }
      }
      setAreaCodes(codes: string[]) {
         if (!!codes && !!codes.length) {
             const n = codes.length;
             switch (n) {
                case 1:
                this.dataConditionTemp.areaRangeId = Number(codes[0]);
                this.dataConditionTemp.areaRange = AreaRange.PROVINCE;
                break;
                case 2:
                this.dataConditionTemp.areaRangeId = Number(codes[1]);
                this.dataConditionTemp.areaRange = AreaRange.CITY;
                break;
                case 3:
                this.dataConditionTemp.areaRangeId = Number(codes[2]);
                this.dataConditionTemp.areaRange = AreaRange.AREA;
                break;
             }
             this._monitorPoint = 1;
         }
      }
      setMonitorPoint(value) {
          console.log(value);
          this.dataConditionTemp.areaRangeId = value;
          this.dataConditionTemp.areaRange = AreaRange.MONITORPOINT;
      }
}