import request from '@/utils/request';

/**
 * 分页查询用户
 * @param params 查询条件
 */
export async function pageMembers(params) {
  const res = await request.get('/sylive/members/page', {
    params
  });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改马甲
 * @param userId 用户id
 * @param maJiaId 马甲id
 */
export async function updateMembersMaJia(userId, maJiaId) {
  const res = await request.put('/sylive/members/majia', {
    userId,
    maJiaId
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询用户列表
 * @param params 查询条件
 */
export async function listMembers(params) {
  const res = await request.post('/sylive/members/list', params);
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询用户
 * @param id 用户id
 */
export async function getMembers(id) {
  const res = await request.get('/sylive/members/' + id);
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加用户
 * @param data 用户信息
 */
export async function addMembers(data) {
  const res = await request.post('/sylive/members', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改用户
 * @param data 用户信息
 */
export async function updateMembers(data) {
  const res = await request.put('/sylive/members', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除用户
 * @param id 用户id
 */
export async function removeMembers(id) {
  return removeMembersBatch([id]);
}

/**
 * 批量删除用户
 * @param ids 用户id集合
 */
export async function removeMembersBatch(ids) {
  const res = await request.delete('/sylive/members/batch', {
    data: { ids }
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改用户状态
 * @param userId 用户id
 * @param status 状态
 */
export async function updateMembersStatus(userId, status) {
  const res = await request.put('/sylive/members/status', {
    userId,
    status
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 重置用户密码
 * @param membersId 用户id
 * @param password 密码
 * @returns {Promise<string>}
 */
export async function updateMembersPassword(membersId, password = '123456') {
  const res = await request.put('/sylive/members/password', {
    membersId,
    password
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导入用户
 * @param file excel文件
 */
export async function importMembers(file) {
  const formData = new FormData();
  formData.append('file', file);
  const res = await request.post('/sylive/members/import', formData);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 检查用户是否存在
 * @param field 检查的字段
 * @param value 字段的值
 * @param organizationId 机构id
 */
export async function checkExistence(field, value, organizationId) {
  const res = await request.get('/sylive/members/existence', {
    params: { field, value, organizationId }
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
