fengxiang
2017-12-28 55da782025f5728051fea9fff49f9e6b6f602a1e
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
 
import { Injectable } from '@angular/core';
 
export class  Criteria{
    private static CONDITION_SPLIT = '||';
     private conditions: string[] = [];
     public getConditions(): string[]{
       return this.conditions;
     }
 
     public addCondition(condition: string,colName:string,...values: any[]){
        const split = Criteria.CONDITION_SPLIT; // '||'
        this.conditions.push(condition+split+colName+split+ values.join(split));
     }
     public andLike(col: { name: string, value: any}): Criteria{
         this.addCondition('andLike',col.name,col.value);
         return this;
     }
     public andEqualTo(col: { name: string, value: any}): Criteria{
        this.addCondition('andEqualTo',col.name,col.value);
        return this;
     }
}
 
@Injectable()
export class ExampleService {
  private static OR_SPLIT = "or|";
  private static  CRITERIA_SPLIT = "|||";
  private criterion: Criteria[]=[];
  
  public getSqlParam():string{
      let whereSql = '';
     for(let cri of this.criterion){
            const conditions = cri.getConditions();
            whereSql += ExampleService.OR_SPLIT+conditions.join(ExampleService.CRITERIA_SPLIT);
     }
     return encodeURI(whereSql);
  }
  constructor() { }
    public or(){
        const cri = new Criteria();
        this.criterion.push(cri);
        return cri;
    }
}