// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "username": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "用户名",
    "label": "用户名"
  },
  "password_secret_version": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "passwordSecret",
    "label": "passwordSecret"
  },
  "nickname": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "昵称",
    "label": "昵称"
  },
  "gender": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "未知",
            "value": 0
          },
          {
            "text": "男",
            "value": 1
          },
          {
            "text": "女",
            "value": 2
          }
        ]
      }
    ],
    "title": "性别",
    "defaultValue": 0,
    "label": "性别"
  },
  "status": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "正常",
            "value": 0
          },
          {
            "text": "禁用",
            "value": 1
          },
          {
            "text": "审核中",
            "value": 2
          },
          {
            "text": "审核拒绝",
            "value": 3
          }
        ]
      }
    ],
    "title": "用户状态",
    "defaultValue": 0,
    "label": "用户状态"
  },
  "mobile": {
    "rules": [
      {
        "format": "string"
      },
      {
        "pattern": "^\\+?[0-9-]{3,20}$"
      }
    ],
    "title": "手机号码",
    "label": "手机号码"
  },
  "mobile_confirmed": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "未验证",
            "value": 0
          },
          {
            "text": "已验证",
            "value": 1
          }
        ]
      }
    ],
    "title": "手机号验证状态",
    "defaultValue": 0,
    "label": "手机号验证状态"
  },
  "email": {
    "rules": [
      {
        "format": "string"
      },
      {
        "format": "email"
      }
    ],
    "title": "邮箱",
    "label": "邮箱"
  },
  "email_confirmed": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "未验证",
            "value": 0
          },
          {
            "text": "已验证",
            "value": 1
          }
        ]
      }
    ],
    "title": "邮箱验证状态",
    "defaultValue": 0,
    "label": "邮箱验证状态"
  },
  "avatar": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "头像地址",
    "label": "头像地址"
  },
  "avatar_file": {
    "rules": [
      {
        "format": "file"
      }
    ],
    "title": "头像文件",
    "label": "头像文件"
  },
  "d_ids": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "部门",
    "label": "部门"
  },
  "e_ids": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "企业",
    "label": "企业"
  },
  "wx_unionid": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "ali_openid": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "apple_openid": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "dcloud_appid": {
    "rules": [
      {
        "format": "array"
      }
    ]
  },
  "comment": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "备注",
    "label": "备注"
  },
  "score": {
    "rules": [
      {
        "format": "int"
      }
    ]
  },
  "last_login_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ]
  },
  "last_login_ip": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "token": {
    "rules": [
      {
        "format": "array"
      }
    ]
  },
  "inviter_uid": {
    "rules": [
      {
        "format": "array"
      }
    ]
  },
  "invite_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ]
  },
  "my_invite_code": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "identities": {
    "rules": [
      {
        "format": "array"
      }
    ]
  }
}

const enumConverter = {
  "gender_valuetotext": {
    "0": "未知",
    "1": "男",
    "2": "女"
  },
  "status_valuetotext": {
    "0": "正常",
    "1": "禁用",
    "2": "审核中",
    "3": "审核拒绝"
  },
  "mobile_confirmed_valuetotext": {
    "0": "未验证",
    "1": "已验证"
  },
  "email_confirmed_valuetotext": {
    "0": "未验证",
    "1": "已验证"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
