import { request } from '@/utils/request'

// User 类型定义
export interface User {
  id?: number

  wuid?:  string

  openid?:  string

  unionid?:  string

  phone?:  string

  nickName?:  string

  avatarUrl?:  string

  name?:  string

  gender?: number

  gzhId?:  string

  gzhOpenid?:  string

  assetsUrl?:  string

  assetsState?: number

  bankCard?:  string

  bankOpenName?:  string

  createTime?:  string

  updateTime?:  string

  createBy?: number

  updateBy?: number

  status?: number

  deleted?: number

}

// User API
export const userApi = {
  // 分页查询
  page(params: { page: number; pageSize: number; id?: number; name?:  string; status?: number }) {
    return request({ url: '/biz/user/page', method: 'get', params })
  },

  // 获取详情
  detail(id: number) {
    return request({ url: `/biz/user/${id}`, method: 'get' })
  },

  // 新增
  create(data: User) {
    return request({ url: '/biz/user', method: 'post', data })
  },

  // 修改
  update(data: User) {
    return request({ url: '/biz/user', method: 'put', data })
  },

  // 删除
  delete(ids: number[]) {
    return request({ url: `/biz/user/${ids.join(',')}`, method: 'delete' })
  },

  // 导出
  export(params?: { ids?: number[]; id?: number; name?:  string; status?: number }) {
    const p: Record<string, any> = {}
    if (params?.ids?.length) p.ids = params.ids.join(',')
    if (params?.id !== undefined && params?.id !== null) p.id = params.id
    if (params?.name !== undefined && params?.name !== null) p.name = params.name
    if (params?.status !== undefined && params?.status !== null) p.status = params.status
    return request({ url: `/biz/user/export`, method: 'get', params: p, responseType: 'blob' })
  },

  // 导入
  importData(file: File) {
    const formData = new FormData()
    formData.append('file', file)
    return request<{ success: number; fail: number; errors: string[] }>({
      url: `/biz/user/import`,
      method: 'post',
      data: formData,
      headers: { 'Content-Type': 'multipart/form-data' }
    })
  },

  // 下载导入模板
  downloadTemplate() {
    return request({ url: `/biz/user/template`, method: 'get', responseType: 'blob' })
  }
}
