package com.red.circle.other.app.service.user.relation;

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.common.business.dto.cmd.UserIdCmd;
import com.red.circle.common.business.dto.cmd.UserIdCommonCmd;
import com.red.circle.other.app.command.user.DismissCpApplyCmdExe;
import com.red.circle.other.app.command.user.LoveLetterQueryCmdExe;
import com.red.circle.other.app.command.user.ProcessCpApplyCmd;
import com.red.circle.other.app.command.user.SendCpApplyCmdExe;
import com.red.circle.other.app.command.user.SendLoveLetterCmdExe;
import com.red.circle.other.app.command.user.query.CpApplyIdQryExe;
import com.red.circle.other.app.command.user.query.UserCpPairUserProfileQryExe;
import com.red.circle.other.app.command.user.query.UserCpRelationQryExe;
import com.red.circle.other.app.command.user.query.UserCpRelationQueryV4Exe;
import com.red.circle.other.app.dto.clientobject.user.relation.cp.CpCabinUserProfileCO;
import com.red.circle.other.app.dto.clientobject.user.relation.cp.CpPairUserProfileCO;
import com.red.circle.other.app.dto.clientobject.user.relation.cp.CpUserProfileCO;
import com.red.circle.other.domain.model.cp.loveletter.LoveLetterDetailCO;
import com.red.circle.other.domain.model.cp.loveletter.LoveLetterWallCO;
import com.red.circle.other.app.dto.cmd.user.relation.cp.CpApplyCmd;
import com.red.circle.other.app.dto.cmd.user.relation.cp.CpApplyDismissCmd;
import com.red.circle.other.app.dto.cmd.user.relation.cp.ProcessApplyCmd;
import com.red.circle.other.app.dto.cmd.user.relation.cp.loveletter.SendLoveLetterCmd;
import com.red.circle.other.app.util.DistributedLockUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户cp关系实现.
 *
 * @author pengliang on 2021/4/13
 */
@Service
@RequiredArgsConstructor
public class UserCpRelationServiceImpl implements UserCpRelationService {

  private final CpApplyIdQryExe cpApplyIdQryExe;
  private final SendCpApplyCmdExe sendCpApplyCmdExe;
  private final ProcessCpApplyCmd processCpApplyCmd;
  private final DismissCpApplyCmdExe dismissCpApplyCmdExe;
  private final UserCpRelationQryExe userCpRelationQryExe;
  private final UserCpPairUserProfileQryExe userCpPairUserProfileQryExe;
  private final UserCpRelationQueryV4Exe userCpRelationQueryV4Exe;
  private final DistributedLockUtil distributedLockUtil;
  private final SendLoveLetterCmdExe sendLoveLetterCmdExe;
  private final LoveLetterQueryCmdExe loveLetterQueryCmdExe;

  @Override
  public CpUserProfileCO getCpUser(UserIdCmd cmd) {
    return userCpRelationQryExe.execute(cmd);
  }

  @Override
  public List<CpPairUserProfileCO> getCpPairUserProfileCO(AppExtCommand cmd) {
    return userCpPairUserProfileQryExe.execute(cmd);
  }

  @Override
  public void sendApply(CpApplyCmd cmd) {
    String key = "SEND_CP_APPLY:" + cmd.getReqUserId() + "_" + cmd.getAcceptApplyUserId();
    distributedLockUtil.executeWithLock(key, () -> {
      sendCpApplyCmdExe.execute(cmd);
    });
  }

  @Override
  public void processApply(ProcessApplyCmd cmd) {
    String key = "PROCESS_CP_APPLY:" + cmd.getApplyId();
    distributedLockUtil.executeWithLock(key, () -> {
      processCpApplyCmd.execute(cmd);
    });
  }

  @Override
  public void dismissApply(CpApplyDismissCmd cmd) {
    dismissCpApplyCmdExe.execute(cmd);
  }

  @Override
  public String getCpApplyId(UserIdCmd cmd) {
    return cpApplyIdQryExe.execute(cmd);
  }

  @Override
  public CpCabinUserProfileCO getCpUserV4(UserIdCommonCmd cmd) {
    return userCpRelationQueryV4Exe.execute(cmd);
  }

  @Override
  public void sendLoveLetter(SendLoveLetterCmd cmd) {
    String key = "SEND_LOVE_LETTER:" + cmd.getReqUserId();
    distributedLockUtil.executeWithLock(key, () -> {
      sendLoveLetterCmdExe.execute(cmd);
    });

  }

}
