import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import { ShopGoodsRoleCommission, ShopGoodsRoleCommissionParam } from '@/api/shop/shopGoodsRoleCommission/model';

/**
 * 分页查询商品绑定角色的分润金额
 */
export async function pageShopGoodsRoleCommission(params: ShopGoodsRoleCommissionParam) {
  const res = await request.get<ApiResult<PageResult<ShopGoodsRoleCommission>>>(
    '/shop/shop-goods-role-commission/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询商品绑定角色的分润金额列表
 */
export async function listShopGoodsRoleCommission(params?: ShopGoodsRoleCommissionParam) {
  const res = await request.get<ApiResult<ShopGoodsRoleCommission[]>>(
    '/shop/shop-goods-role-commission',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加商品绑定角色的分润金额
 */
export async function addShopGoodsRoleCommission(data: ShopGoodsRoleCommission) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-goods-role-commission',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改商品绑定角色的分润金额
 */
export async function updateShopGoodsRoleCommission(data: ShopGoodsRoleCommission) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-goods-role-commission',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除商品绑定角色的分润金额
 */
export async function removeShopGoodsRoleCommission(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-goods-role-commission/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除商品绑定角色的分润金额
 */
export async function removeBatchShopGoodsRoleCommission(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-goods-role-commission/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询商品绑定角色的分润金额
 */
export async function getShopGoodsRoleCommission(id: number) {
  const res = await request.get<ApiResult<ShopGoodsRoleCommission>>(
    '/shop/shop-goods-role-commission/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
