fengxiang
2018-01-26 0d8b1828eea6bb027bbf71ddcd9c28a7bcf0ca3b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { zip } from 'rxjs/observable/zip';
import { DeviceAdjustValueService } from '@business/services/http/device-adjust-value.service';
import { NzModalSubject, NzMessageService } from 'ng-zorro-antd';
import { SensorsService } from '@business/services/http/sensors.service';
import { Component, OnInit } from '@angular/core';
import { DeviceAdjustValue, Sensor, Device } from '@business/entity/data';
import { Grid, PageBean, ResultBean } from '@business/entity/grid';
import { ContentChild } from '@angular/core/src/metadata/di';
import swal, { SweetAlertType } from 'sweetalert2';
 
@Component({
  selector: 'app-adjust-config',
  templateUrl: './adjust-config.component.html',
  styles: []
})
export class AdjustConfigComponent implements OnInit {
  record:Device;
  _dataValue:{[s:string]:number} = {};
  data:DeviceAdjustValue;
  deviceVersionId: number;
  isSaving = false;
  grid: Grid<Sensor> = new Grid(null);
  private initPage() {    
    const sensor: Sensor = {
      name: {
        text: '名称',
        width: '200px'
      },
      sensorKey: {
        text: '键值',
        width: '60px'
      },
      lower: {
        text: '下限值',
        width: '90px'
      },
      upper: {
        text: '上限值',
        width: '90px'
      },
      unit: {
        text: '单位',
        width: '100px'
      },
      description: {
        text: '描述'
      }
    };
    this.grid.title = '传感器';
    this.grid.setColumns(sensor);
    this.grid.pageSize = 0;
  }
  constructor(
    private subject: NzModalSubject,
    private sensorsService: SensorsService,
    private adjustValueService:DeviceAdjustValueService,
    public msgSrv: NzMessageService,
  ) { }
 
  ngOnInit() {
    if(this.record.deviceVersionId==null){
      swal(`请先选择设备型号`, '设备校准值到传感器类型由所属设备型号决定', 'info');
      this.subject.destroy();    
    }
    this.initPage();
    this.load();
  }
  load() {
    // 延时加载避免ExpressionChangedAfterItHasBeenCheckedError
    setTimeout(() => {
      this.grid.loading = true;
    }, 1);
    zip(
      this.adjustValueService.getByDid(this.record.id),
      this.sensorsService.getPageByVersionId(this.record.deviceVersionId)
    ).subscribe(
        ([adjustRes,sensorsRes]) => {
            if(adjustRes!=null && adjustRes.code==1 && sensorsRes != null && sensorsRes.data != null){
                this.data = {
                  deviceId:this.record.id,
                  value:{}
                };
                if(adjustRes.data!=null){
                  // 存储修改前到值
                  Object.assign(this._dataValue,adjustRes.data.value);
                  this.data['id'] =adjustRes.data.id;
                }                
               this.grid.initData(sensorsRes);
               sensorsRes.data.forEach(
                (item:Sensor) => {
                  this.data.value[item.sensorKey] = this._dataValue[item.sensorKey] == null?0:this._dataValue[item.sensorKey];                
                  }
              );
              this.grid.refreshStatus();
              setTimeout(() => {
                this.grid.loading = false;
              }, 1);
            }
        }
    );
  }
  close() {
    this.subject.destroy();
  }
  save($event) {
    // $event.preventDefault();
    let isModify = Object.keys(this._dataValue).length != Object.keys(this.data.value).length;
    if(!isModify){
      isModify = Object.keys(this.data.value).some(
        key => {
            return this.data.value[key] !== this._dataValue[key];
        }
      );
    }
    debugger;
    if(isModify){
      this.adjustValueService.save(this.data).subscribe(
        (res:ResultBean<any>) =>{
               if(res!=null&&res.code==1){
                   this.subject.destroy();                   
                   this.msgSrv.success('校准值配置成功');
               }
        }
    );
    }else{
        this.subject.destroy();
        this.msgSrv.success('校准值配置未改变');
    }
  }
}