package com.red.circle.other.app.listener.strategy;

import com.red.circle.mq.business.model.event.approval.ApprovalContent;
import com.red.circle.mq.business.model.event.approval.ApprovalEvent;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.tool.core.text.StringUtils;
import com.red.circle.tool.core.tuple.ImmutableKeyValuePair;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author pengliang on 2020/9/21
 */
@Component("NICKNAME_APPROVAL")
@RequiredArgsConstructor
public class NicknameApprovalStrategy implements ApprovalStrategy {

  private final UserProfileGateway userProfileGateway;

  @Override
  public void doOperation(ApprovalEvent param) {

    if (param.approvalNotPass()) {
      // 修改用户昵称
      userProfileGateway.updateUserNicknameBatch(toApprovalData(
          param.getWaitApprovalUser()));
    }
  }

  private List<ImmutableKeyValuePair<Long, String>> toApprovalData(
      List<ApprovalContent> waitApprovalUser) {
    return waitApprovalUser.stream().map(wait -> {
      String nickname = "st_" + StringUtils.getRandomString(13);
      if (StringUtils.isNotBlank(wait.getContent())) {
        wait.setContent(wait.getContent() + " #替换# " + nickname);
      }
      return ImmutableKeyValuePair.of(wait.getUserId(), nickname);
    }).toList();
  }

}
