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

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.dto.cmd.user.user.UserShieldBlackUserCmd;
import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheService;
import com.red.circle.other.infra.database.mongo.entity.live.RoomProfile;
import com.red.circle.other.infra.database.mongo.entity.live.RoomUserBlacklist;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.infra.database.mongo.service.live.RoomUserBlacklistService;
import com.red.circle.other.infra.database.rds.entity.user.user.BaseInfo;
import com.red.circle.other.infra.database.rds.service.user.user.BaseInfoService;
import com.red.circle.other.infra.database.rds.service.user.user.RelationshipFriendService;
import com.red.circle.other.infra.database.rds.service.user.user.ShieldBlackService;
import com.red.circle.other.inner.enums.config.EnumConfigKey;
import com.red.circle.other.inner.enums.live.RoomUserRolesEnum;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.text.StringUtils;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 屏蔽拉黑记录.
 *
 * @author pengliang on 2020/11/4
 */
@Component
@RequiredArgsConstructor
public class UserShieldBlackAddCmdExe {

  private final BaseInfoService baseInfoService;
  private final ShieldBlackService shieldBlackService;
  private final EnumConfigCacheService enumConfigCacheService;
  private final RoomUserBlacklistService roomUserBlacklistService;
  private final RoomProfileManagerService roomProfileManagerService;
  private final RelationshipFriendService relationshipFriendService;

  public void execute(UserShieldBlackUserCmd cmd) {

    String superAdminIds = enumConfigCacheService.getValue(EnumConfigKey.ASWAT_ADMINS,
        cmd.requireReqSysOrigin());
    // 权限不足
    ResponseAssert.isFalse(CommonErrorCode.INSUFFICIENT_PERMISSION,
        StringUtils.isNotBlank(superAdminIds) && superAdminIds
            .contains(String.valueOf(cmd.getShieldUserId())));

    // 拉黑用户
    shieldBlackService.saveShieldBlack(cmd.requiredReqUserId()
        , cmd.getShieldUserId());

    if (shieldBlackService.checkShieldBlack(cmd.getShieldUserId(), cmd.requiredReqUserId())) {
      relationshipFriendService.removeFriend(cmd.requiredReqUserId(), cmd.getShieldUserId());
    }

    // 拉黑房间
    BaseInfo beBlackUser = baseInfoService.getById(cmd.getShieldUserId());
    ResponseAssert.notNull(UserErrorCode.USER_INFO_NOT_FOUND, beBlackUser);

    RoomProfile beBlackRoom =
        roomProfileManagerService.getProfileByUserId(cmd.requiredReqUserId());

    if (Objects.nonNull(beBlackRoom)) {
      roomUserBlacklistService.add(new RoomUserBlacklist()
          .setTimingId(IdWorker.getId())
          .setSysOrigin(cmd.requireReqSysOrigin())
          .setUserId(beBlackUser.getId())
          .setRoomId(beBlackRoom.getId())
          .setCreateUserRoles(RoomUserRolesEnum.HOMEOWNER.name())
          .setCreateUser(cmd.requiredReqUserId())
          .setExpiredTime(TimestampUtils.nowPlusYear(1))
      );
    }

  }

}
