import request from '@/utils/request';

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

/**
 * 查询用户列表
 * @param params 查询条件
 */
export async function listGroupsUser(params) {
  const res = await request.get('/sylive/groupsUser', {
    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 getGroupsUser(id) {
  const res = await request.get('/sylive/groupsUser/' + id);
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

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

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

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

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

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

/**
 * 同步原始门店用户
 * @param groupsId 分组id
 */
export async function synchronousGroupsUser(groupsId) {
  const res = await request.get('/sylive/groupsUser/synchronous', {
    params: { groupsId }
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
