package com.red.circle.wallet.app.command.gold.query;

import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.framework.mybatis.mybatisplus.LambdaQueryWrapperChain;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.DateUtils;
import com.red.circle.tool.core.text.StringUtils;
import com.red.circle.tool.core.tuple.PennyAmount;
import com.red.circle.wallet.inner.model.tmp.AssetRecordRunningWaterCO;
import com.red.circle.wallet.app.dto.cmd.GoldRunningWaterQueryCmd;
import com.red.circle.wallet.infra.database.rds.entity.WalletGoldAssetRecord;
import com.red.circle.wallet.infra.database.rds.service.WalletGoldAssetRecordService;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 金币流水查询. ·
 *
 * @author pengliang on 2023/7/5
 */
@Component
@RequiredArgsConstructor
public class WalletAssetRecordGoldQryExe {

  private final WalletGoldAssetRecordService walletGoldAssetRecordService;

  public List<AssetRecordRunningWaterCO> execute(GoldRunningWaterQueryCmd cmd) {
    List<String> querySysOrigin = querySysOrigin(cmd);

    LambdaQueryWrapperChain<WalletGoldAssetRecord> queryChain = walletGoldAssetRecordService.query();

    if (CollectionUtils.isEmpty(querySysOrigin) || querySysOrigin.size() <= 1) {
      queryChain.eq(WalletGoldAssetRecord::getSysOrigin,
          querySysOrigin.get(0));
    } else {
      queryChain.in(WalletGoldAssetRecord::getSysOrigin, querySysOrigin);
    }

    List<WalletGoldAssetRecord> assetRecords = walletGoldAssetRecordService.query()
        .eq(WalletGoldAssetRecord::getUserId, cmd.requiredReqUserId())
        .eq(Objects.nonNull(cmd.getType()), WalletGoldAssetRecord::getType, cmd.getType())
        .lt(Objects.nonNull(cmd.getLastId()), WalletGoldAssetRecord::getId, cmd.getLastId())
        .ge(WalletGoldAssetRecord::getCreateTime,
            DateUtils.setDateTime(DateUtils.firstDayOfMonth(), 0, 0, 0))
        .le(WalletGoldAssetRecord::getCreateTime, DateUtils.now())
        .last(PageConstant.formatLimit(20))
        .orderByDesc(WalletGoldAssetRecord::getId)
        .list();

    if (CollectionUtils.isEmpty(assetRecords)) {
      return CollectionUtils.newArrayList();
    }

    return assetRecords.stream().map(assetRecord -> new AssetRecordRunningWaterCO()
        .setId(assetRecord.getId())
        .setTitle(
            assetRecord.getEventDesc().contains("Game") ? "Other" : assetRecord.getEventDesc())
        .setType(assetRecord.getType())
        .setQuantity(PennyAmount.ofPenny(assetRecord.getPennyAmount()).getDollarAmount())
        .setCreateTime(assetRecord.getCreateTime())
    ).collect(Collectors.toList());
  }

  private List<String> querySysOrigin(GoldRunningWaterQueryCmd cmd) {
    List<String> sysOrigins = StringUtils.isBlank(cmd.requireReqSysOrigin())
        ? null
        : Arrays.stream(cmd.requireReqSysOrigin().split(",")).map(StringUtils::trim)
            .filter(StringUtils::isNotBlank).toList();
    return CollectionUtils.isNotEmpty(sysOrigins)
        ? sysOrigins
        : Arrays.stream(SysOriginPlatformEnum.values()).map(Objects::toString).toList();
  }

}
