lrf 2 năm trước cách đây
mục cha
commit
10ba3596c8
1 tập tin đã thay đổi với 12 bổ sung7 xóa
  1. 12 7
      src/interface/SearchBase.ts

+ 12 - 7
src/interface/SearchBase.ts

@@ -6,20 +6,22 @@ import _ = require('lodash');
  * @param {Array} like_prop 范围查询字段数组
  */
 export class SearchBase {
-  constructor({ like_prop = [], props = [] }) {
+  constructor({ like_prop = [], props = [], mapping = {} }) {
     this.like_prop = like_prop;
     this.props = props;
+    this.mapping = mapping;
   }
   /** 模糊查询字段数组 */
   like_prop: Array<string>;
   /** class定义字段 */
   props: Array<string>;
-
+  /**字段映射:key:DTO接收字段; value:服务查询字段 */
+  mapping: object;
   /**
    * 此处将字段转换为使用的方式
    */
   getFilter() {
-    const others = _.omit(this, ['like_prop', 'props']);
+    const others = _.omit(this, ['like_prop', 'props', 'mapping']);
     let keys = Object.keys(others);
     keys = keys.filter(f => this.props.includes(f));
     const result = {};
@@ -32,22 +34,25 @@ export class SearchBase {
       }
       if (this.like_prop.includes(i)) {
         // 处理模糊查询
-        result[i] = new RegExp(value);
+        const propName = _.get(this.mapping, i, i);
+        result[propName] = new RegExp(value);
       } else if (i.includes('@')) {
         // 处理范围查询
         const arr = i.split('@');
         const prop = _.head(arr);
+        const propName = _.get(this.mapping, prop, prop);
         const pos = _.last(arr);
-        const pd = _.get(result, prop, {});
+        const pd = _.get(result, propName, {});
         if (pos === 'start') {
           Object.assign(pd, { $gte: value });
         } else if (pos === 'end') {
           Object.assign(pd, { $lte: value });
         } else continue;
-        result[prop] = pd;
+        result[propName] = pd;
       } else {
         // 正常处理
-        result[i] = value;
+        const propName = _.get(this.mapping, i, i);
+        result[propName] = value;
       }
     }
     return result;