package com.red.circle.other.app.command.activity;

import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.dto.clientobject.activity.LotteryWithdrawCO;
import com.red.circle.other.app.dto.cmd.activity.LotteryWithdrawQryCmd;
import com.red.circle.other.infra.database.rds.entity.activity.LotteryWithdraw;
import com.red.circle.other.infra.database.rds.service.activity.LotteryWithdrawService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 * 查询抽奖提现记录.
 *
 * @author system
 * @since 2025-10-21
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class LotteryWithdrawQryExe {

  private final LotteryWithdrawService lotteryWithdrawService;

  public PageResult<LotteryWithdrawCO> execute(LotteryWithdrawQryCmd cmd) {
    PageResult<LotteryWithdraw> pageResult = lotteryWithdrawService.query()
        .eq(cmd.getUserId() != null, LotteryWithdraw::getUserId, cmd.getUserId())
        .eq(cmd.getActivityId() != null, LotteryWithdraw::getActivityId, cmd.getActivityId())
        .eq(cmd.getStatus() != null, LotteryWithdraw::getStatus, cmd.getStatus())
        .orderByDesc(LotteryWithdraw::getCreateTime)
        .page(cmd.getPageQuery());

    return pageResult.convert(this::convertToCO);
  }

  private LotteryWithdrawCO convertToCO(LotteryWithdraw withdraw) {
    LotteryWithdrawCO co = new LotteryWithdrawCO();
    BeanUtils.copyProperties(withdraw, co);
    co.setStatusText(getStatusText(withdraw.getStatus()));
    return co;
  }

  private String getStatusText(Integer status) {
    if (status == null) {
      return "未知";
    }
    return switch (status) {
      case 0 -> "待审核";
      case 1 -> "已通过";
      case 2 -> "已拒绝";
      case 3 -> "已完成";
      default -> "未知";
    };
  }

}
