package com.red.circle.wallet.inner.endpoint.diamond;

import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.framework.web.spring.RequestCallable;
import com.red.circle.wallet.inner.endpoint.diamond.api.SalaryDiamondBalanceClientApi;
import com.red.circle.wallet.inner.model.cmd.UserSalaryDiamondBalanceQryCmd;
import com.red.circle.wallet.inner.model.cmd.UserSalaryDiamondCmd;
import com.red.circle.wallet.inner.model.dto.SalaryDiamondBalanceDTO;
import com.red.circle.wallet.inner.model.dto.UserSalaryDiamondRunningWaterDTO;
import com.red.circle.wallet.inner.service.diamond.SalaryDiamondBalanceClientService;
import com.red.circle.wallet.inner.service.diamond.SalaryDiamondRunningWaterClientService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * 用户工资钻石
 *
 * @author zongpubin on 2024/3/14
 */
@Validated
@RestController
@RequestMapping(value = SalaryDiamondBalanceClientApi.API_PREFIX, produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class SalaryDiamondBalanceClientEndpoint implements SalaryDiamondBalanceClientApi {

  private final SalaryDiamondBalanceClientService salaryDiamondBalanceClientService;
  private final SalaryDiamondRunningWaterClientService salaryDiamondRunningWaterClientService;

  @Override
  public ResultResponse<PageResult<SalaryDiamondBalanceDTO>> listUserSalaryDiamondBalance(
      UserSalaryDiamondBalanceQryCmd query) {
    return ResultResponse.success(
        salaryDiamondBalanceClientService.listUserSalaryDiamondBalance(query));
  }

  @Override
  public ResultResponse<Boolean> exists(Long userId) {
    return ResultResponse.success(salaryDiamondBalanceClientService.exists(userId));
  }

  @Override
  public ResultResponse<Map<Long, BigDecimal>> mapBalanceBySysOrigin(String sysOrigin) {
    return ResultResponse.success(salaryDiamondBalanceClientService.mapBalanceBySysOrigin(sysOrigin));
  }

  @Override
  public ResultResponse<Boolean> incr(UserSalaryDiamondCmd param) {
    return ResultResponse.success(salaryDiamondBalanceClientService.incr(param));
  }

  @Override
  public ResultResponse<SalaryDiamondBalanceDTO> getBalance(Long userId) {
    return ResultResponse.success(salaryDiamondBalanceClientService.getBalance(userId));
  }

  @Override
  public ResultResponse<SalaryDiamondBalanceDTO> decr(UserSalaryDiamondCmd param) {
    return ResultResponse.success(salaryDiamondBalanceClientService.decr(param));
  }

  @Override
  public CompletableFuture<ResultResponse<SalaryDiamondBalanceDTO>> changeSalaryBalanceAsync(UserSalaryDiamondCmd cmd) {
    return RequestCallable.supplyAsync(() -> {
      salaryDiamondBalanceClientService.incr(cmd);
      return ResultResponse.success();
    });
  }

}
