zhuoyuan.wang
2024-06-19 15ebe96f28cadec6a726c5324593a40bbf56205f
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const removeEmptyProperties = (obj) => {
  for (const key in obj) {
    if (obj.hasOwnProperty(key) && (obj[key] === "" || obj[key] === undefined)) {
      delete obj[key];
    }
  }
  return obj;
};
 
export const dataFilter = (data, filters) => {
  const andLikeParams = {};
  const andEqParams = {};
  Object.keys(data).forEach(key => {
    if (data[key]) {
      filters.forEach(field => {
        if (field.prop === key) {
          if (field.type === 'el-select') {
            andEqParams[key] = data[key];
          }
          if (field.type === 'el-input') {
            andLikeParams[key] = data[key];
          }
        }
      });
    }
  })
 
  return convertFilter({andLikeParams, andEqParams})
}
 
export const convertFilter = ({
                                orLikeParams = {},
                                andLikeParams = {},
                                orEqParams = {},
                                andEqParams = {},
                                orInParams = {},
                                andInParams = {}
                              }) => {
  let params = "1=1 ";
 
  /**
   * 1=1%20and%20province_code%20=%20'500000'%20and%20%201=1%20%20and%20customer_eas_code%20like%20'%252312411000%25'%20and%20customer_name%20like%20'%252312411000%25'
   */
  const orLikeKeys = Object.keys(removeEmptyProperties(orLikeParams));
 
  if (orLikeKeys.length > 0) {
    params += 'and ( '
  }
 
  orLikeKeys.forEach((key, index) => {
    params += `${key} like ` + "'" + `%${orLikeParams[key]}%` + "'" +
      `${index < orLikeKeys.length - 1 ? ' or ' : ''}`;
  });
 
  if (orLikeKeys.length > 0) {
    params += ') '
  }
 
  const orEqKeys = Object.keys(removeEmptyProperties(orEqParams));
 
  if (orEqKeys.length > 0) {
    params += 'and ( '
  }
 
  orEqKeys.forEach((key, index) => {
    params += `${key} = ` + "'" + `${orEqParams[key]}` + "'" +
      `${index < orEqKeys.length - 1 ? ' or ' : ''}`;
  });
 
  if (orEqKeys.length > 0) {
    params += ') '
  }
 
  const andLikeKeys = Object.keys(removeEmptyProperties(andLikeParams));
 
  if (andLikeKeys.length > 0) {
    params += ''
  }
 
  andLikeKeys.forEach(key => {
    params += ` and ${key} like ` + "'" + `%${andLikeParams[key]}%` + "' ";
  });
 
  if (andLikeKeys.length > 0) {
    params += ''
  }
 
 
  const andEqKeys = Object.keys(removeEmptyProperties(andEqParams));
 
  if (andEqKeys.length > 0) {
    params += ''
  }
 
  andEqKeys.forEach(key => {
    params += ` and ${key} = ` + "'" + `${andEqParams[key]}` + "' ";
  });
 
  if (andEqKeys.length > 0) {
    params += ''
  }
 
  const orInKeys = Object.keys(removeEmptyProperties(orInParams));
  if (orInKeys.length > 0) {
    params += " and ( "
  }
  for (var key in orInParams) {
    if (typeof (orInParams[key]) == "object") {
      const orInKeysChildren = Object.keys(removeEmptyProperties(orInParams[key]));
      if (orInKeysChildren.length > 0) {
        params += " or "
        params += key + ' in ('
        orInKeysChildren.forEach((key1, index) => {
          params += "'" + `${orInParams[key][key1]}` + "'" + ' , ';
        })
        //去除逗号
        params = params.substring(0, params.length - 2)
        params += ") "
 
 
      }
 
    }
  }
  if (orInKeys.length > 0) {
    params += " ) "
  }
 
  const andInKeys = Object.keys(removeEmptyProperties(andInParams));
  if (andInKeys.length > 0) {
    params += ""
  }
 
  for (const key in andInParams) {
    if (typeof (andInParams[key]) == "object") {
      const andInKeysChildren = Object.keys(removeEmptyProperties(andInParams[key]));
      if (andInKeysChildren.length > 0) {
        params += " and "
        params += key + ' in ('
        andInKeysChildren.forEach((key1, index) => {
          params += "'" + `${andInParams[key][key1]}` + "'" + ' , ';
        })
        //去除逗号
        params = params.substring(0, params.length - 2)
        params += ") "
 
      }
 
    }
  }
 
  if (andInKeys.length > 0) {
    params += ""
  }
 
  return params;
};