fengxiang
2018-07-04 7ca521e4267b987270f6ccbb9a6c076aeb467d96
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
import { _HttpClient } from '@delon/theme';
import { environment } from 'environments/environment';
import { Injectable } from '@angular/core';
import { ResultBean, PageBean } from '@business/entity/grid';
import { Observable } from 'rxjs/Observable';
import { ExampleService, Criteria } from '@business/services/util/example.service';
import { Device } from '@business/entity/data';
 
@Injectable()
export class DeviceService {
  private urls = {
    list: environment.SERVER_BASH_URL + 'device/page-list',
    save: environment.SERVER_BASH_URL + 'device/add-or-modify',
    delete: environment.SERVER_BASH_URL + 'device/delete-by-ids',
    count: environment.SERVER_BASH_URL + 'device/count-by-example'
  };
  public getListUrl () {
    return this.urls.list;
  }
  public getSqlParams(queryMap: {[key: string]: number|string}) {
    const example = new ExampleService();
    const orgId = !!queryMap.orgId ? queryMap.orgId : null;
    const mpointId = !!queryMap.mpointId ? queryMap.mpointId : null;
    const devMacOrName = !!queryMap.devMacOrName && !!(<string>queryMap.devMacOrName).trim() ? queryMap.devMacOrName : null;
    let  criWithMac: Criteria = null;
    let  criWithName: Criteria  = null;
    if (!!devMacOrName) {
      criWithName = example.or().andLike({name: 'name', value: '%' + devMacOrName + '%'});
      criWithMac = example.or().andLike({name: 'mac', value: '%' + devMacOrName + '%'});
    }
    if (!!mpointId) {
        if (!!devMacOrName) {
          criWithName.andEqualTo({name: 'monitorPointId', value: mpointId});
          criWithMac.andEqualTo({name: 'monitorPointId', value: mpointId});
        }else {
          example.or().andEqualTo({name: 'monitorPointId', value: mpointId});
        }
    } else if (!!orgId) {
      if (!!devMacOrName) {
        criWithName.andCondition(`monitor_point_id in (select id from monitor_point where organization_id = ${orgId})`);
        criWithMac.andCondition(`monitor_point_id in (select id from monitor_point where organization_id = ${orgId})`);
      }else {
        example.or().andCondition(`monitor_point_id in (select id from monitor_point where organization_id = ${orgId})`);
      }
    }
    return example.getSqlParam();
  }
  constructor(private http: _HttpClient) { }
  delete(...ids: number[]): Observable< ResultBean<any> > {
      return this.http.post(this.urls.delete, ids);
  }
  public save(data: Device): Observable<any> {
    return this.http.post(this.urls.save, data);
  }
  public countByExample(example: ExampleService): Observable<ResultBean<number>> {
    return this.http.get(this.urls.count, { queryParams: example.getSqlParam()});
  }
  public getPageByExample(page: PageBean, example: ExampleService): Observable<PageBean> {
    let orderByClause = '';
    const _queryParams = !!example ? example.getSqlParam() : '';
    if (!!page) {
       if ( page.getOrderByClause != null && page.getOrderByClause instanceof Function) {
          orderByClause = page.getOrderByClause();
          }
    } else {
          page =  {pageIndex: 0, pageSize: 20};
    }
    const param: PageBean = {pageSize: page.pageSize, pageIndex: page.pageIndex, 
        queryParams: _queryParams, orderByClause: orderByClause};
        return this.http.get(this.urls.list, param);
 }
}