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

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.dto.clientobject.dynamic.DynamicSendRestrictCO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.model.user.UserConsumptionLevel;
import com.red.circle.other.infra.database.cache.service.other.DynamicCacheService;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicBlacklistService;
import com.red.circle.other.inner.asserts.DynamicErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 校验发送动态权限.
 * </p>
 *
 * @author pengshigang
 * @since 2022-06-30
 */
@Component
@RequiredArgsConstructor
public class DynamicCheckSendPermissionCmdExe {

  private final UserProfileGateway userProfileGateway;
  private final DynamicCacheService dynamicCacheService;
  private final DynamicBlacklistService dynamicBlacklistService;

  public DynamicSendRestrictCO execute(AppExtCommand cmd) {

    //该用户为动态黑名单用户
    ResponseAssert.isFalse(CommonErrorCode.INSUFFICIENT_PERMISSION,
        dynamicBlacklistService.exist(cmd.getReqUserId()));

    //当前用户今天已发动态数量
//    Long userSentTodayCount = getUserTodayDynamicCount(cmd);

    //限制用户每天发送动态的数量
//    Long quantityLimit = dynamicCacheService.getSendQuantityLimit();

    return new DynamicSendRestrictCO();
  }

  /**
   * 校验用户财富，魅力等级
   */
  private void checkUserLevel(AppExtCommand cmd) {

    //获得用户魅力,财富等级限制
    Long limitLevel = dynamicCacheService.getSendLevelLimit();
    UserConsumptionLevel userLevel = getConsumptionLevel(cmd);

    ResponseAssert.isTrue(DynamicErrorCode.WEALTH_AND_CHARM_INSUFFICIENT_ERROR,
        userLevel.getCharmLevel() >= limitLevel && userLevel.getWealthLevel() >= limitLevel);
  }

  private UserConsumptionLevel getConsumptionLevel(AppExtCommand cmd) {
    return userProfileGateway.getUserConsumptionLevel(cmd.requireReqSysOriginEnum(),
        cmd.requiredReqUserId());
  }

  private Long getUserTodayDynamicCount(AppExtCommand cmd) {
    return dynamicCacheService.getUserTodayDynamicCount(cmd.getReqUserId());
  }

  public Boolean execute2(AppExtCommand cmd) {
    return !dynamicBlacklistService.exist(cmd.getReqUserId());
  }

}
