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

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.common.business.dto.cmd.IdLongCmd;
import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.command.dynamic.DynamicBlacklistAddCmdExe;
import com.red.circle.other.app.command.dynamic.DynamicBlacklistDelCmdExe;
import com.red.circle.other.app.command.dynamic.DynamicCheckSendPermissionCmdExe;
import com.red.circle.other.app.command.dynamic.DynamicCommentCmdExe;
import com.red.circle.other.app.command.dynamic.DynamicCommentLikeCmdExe;
import com.red.circle.other.app.command.dynamic.DynamicLikeCmdExe;
import com.red.circle.other.app.command.dynamic.DynamicMessageUpdateReadCmdExe;
import com.red.circle.other.app.command.dynamic.DynamicReportCmdExe;
import com.red.circle.other.app.command.dynamic.DynamicTagCmdExe;
import com.red.circle.other.app.command.dynamic.add.DynamicContentAddCmdExe;
import com.red.circle.other.app.command.dynamic.delete.DynamicCommentDelCmdExe;
import com.red.circle.other.app.command.dynamic.delete.DynamicContentDelCmdExe;
import com.red.circle.other.app.command.dynamic.delete.DynamicMessageAllDelCmdExe;
import com.red.circle.other.app.command.dynamic.delete.DynamicMessageDelCmdExe;
import com.red.circle.other.app.command.dynamic.query.DynamicChildCommentQryExe;
import com.red.circle.other.app.command.dynamic.query.DynamicCommentQryExe;
import com.red.circle.other.app.command.dynamic.query.DynamicGiftQryExe;
import com.red.circle.other.app.command.dynamic.query.DynamicLikeQryExe;
import com.red.circle.other.app.command.dynamic.query.DynamicDetailsQryExe;
import com.red.circle.other.app.command.dynamic.query.DynamicFollowQryExe;
import com.red.circle.other.app.command.dynamic.query.DynamicLatestQryExe;
import com.red.circle.other.app.command.dynamic.query.DynamicListQryByTagExe;
import com.red.circle.other.app.command.dynamic.query.DynamicMessageQryExe;
import com.red.circle.other.app.command.dynamic.query.DynamicMyQryExe;
import com.red.circle.other.app.command.dynamic.query.DynamicPopularQryExe;
import com.red.circle.other.app.command.dynamic.query.DynamicTagQryExe;
import com.red.circle.other.app.dto.clientobject.dynamic.*;
import com.red.circle.other.app.dto.cmd.dynamic.*;

import java.util.List;

import com.red.circle.other.app.util.DistributedLockUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 动态 .
 * </p>
 *
 * @author pengshigang
 * @since 2022-4-14
 */
@Service
@RequiredArgsConstructor
public class DynamicServiceImpl implements DynamicService {

  private final DynamicTagCmdExe dynamicTagExe;
  private final DynamicLikeCmdExe dynamicLikeExe;
  private final DynamicMyQryExe dynamicMyQryExe;
  private final DynamicTagQryExe dynamicTagQryExe;
  private final DynamicCommentCmdExe dynamicCommentExe;
  private final DynamicLatestQryExe dynamicLatestQryExe;
  private final DynamicFollowQryExe dynamicFollowQryExe;
  private final DynamicReportCmdExe dynamicReportCmdExe;
  private final DynamicMessageQryExe dynamicMessageQryExe;
  private final DynamicPopularQryExe dynamicPopularQryExe;
  private final DynamicDetailsQryExe dynamicDetailsQryExe;
  private final DynamicCommentQryExe dynamicCommentQryExe;
  private final DynamicChildCommentQryExe dynamicChildCommentQryExe;
  private final DynamicLikeQryExe dynamicLikeQryExe;
  private final DynamicGiftQryExe dynamicGiftQryExe;
  private final DynamicCommentDelCmdExe dynamicCommentDelExe;
  private final DynamicCommentLikeCmdExe dynamicCommentLikeExe;
  private final DynamicContentDelCmdExe dynamicContentDelExe;
  private final DynamicListQryByTagExe dynamicListQryByTagExe;
  private final DynamicMessageDelCmdExe dynamicMessageDelCmdExe;
  private final DynamicContentAddCmdExe dynamicContentAddCmdExe;
  private final DynamicMessageAllDelCmdExe dynamicMessageAllDelCmdExe;
  private final DynamicMessageUpdateReadCmdExe dynamicMessageUpdateReadCmdExe;
  private final DynamicCheckSendPermissionCmdExe dynamicCheckSendPermissionCmdExe;
  private final DynamicBlacklistAddCmdExe dynamicBlacklistAddCmdExe;
  private final DynamicBlacklistDelCmdExe dynamicBlacklistDelCmdExe;
  private final DistributedLockUtil distributedLockUtil;

  private static final String LIKE_LOCK_PREFIX = "dynamic:like:lock";
  private static final String COMMENT_LIKE_LOCK_PREFIX = "dynamic:comment:like:lock";
  private static final String COMMENT_LOCK_PREFIX = "dynamic:comment:lock";
  private static final String CREATE_LOCK_PREFIX = "dynamic:create:lock";
  private static final String DELETE_LOCK_PREFIX = "dynamic:delete:lock";
  private static final String DELETE_COMMENT_LOCK_PREFIX = "dynamic:comment:delete:lock";
  private static final String REPORT_LOCK_PREFIX = "dynamic:report:lock";

  @Override
  public DynamicListCO createDynamic(DynamicContentAddCmd cmd) {
    String lockKey = DistributedLockUtil.buildLockKey(CREATE_LOCK_PREFIX, cmd.getReqUserId());
    return distributedLockUtil.executeWithLock(lockKey, () -> dynamicContentAddCmdExe.execute(cmd));
  }

  @Override
  public void deleteDynamic(IdLongCmd cmd) {
    String lockKey = DistributedLockUtil.buildLockKey(DELETE_LOCK_PREFIX, cmd.getReqUserId(), cmd.getId());
    distributedLockUtil.executeWithLock(lockKey, () -> dynamicContentDelExe.execute(cmd));
  }

  @Override
  public PageResult<DynamicCommentListCO> listDynamicComment(DynamicCommentLimitCmd cmd) {
    return dynamicCommentQryExe.execute(cmd);
  }

  @Override
  public PageResult<DynamicCommentListCO> listDynamicChildComment(DynamicChildCommentLimitCmd cmd) {
    return dynamicChildCommentQryExe.execute(cmd);
  }

  @Override
  public PageResult<DynamicLikeListCO> listDynamicLike(DynamicLikeLimitCmd cmd) {
    return dynamicLikeQryExe.execute(cmd);
  }

  @Override
  public PageResult<DynamicGiftListCO> listDynamicGift(DynamicGiftLimitCmd cmd) {
    return dynamicGiftQryExe.execute(cmd);
  }

  @Override
  public DynamicListCO getDynamicDetails(IdLongCmd cmd) {
    return dynamicDetailsQryExe.execute(cmd);
  }

  @Override
  public PageResult<DynamicListCO> listDynamicFollow(DynamicLimitCmd cmd) {
    return dynamicFollowQryExe.execute(cmd);
  }

  @Override
  public PageResult<DynamicListCO> listMyDynamic(DynamicMyLimitCmd cmd) {
    return dynamicMyQryExe.execute(cmd);
  }

  @Override
  public PageResult<DynamicListCO> listDynamicLatest(DynamicLimitCmd cmd) {
    return dynamicLatestQryExe.execute(cmd);
  }

  @Override
  public List<DynamicListCO> listDynamicPopular(DynamicLimitCmd cmd) {
    return dynamicPopularQryExe.execute(cmd);
  }

  @Override
  public List<DynamicListCO> listDynamicByTagId(DynamicByTagLimitCmd cmd) {
    return dynamicListQryByTagExe.execute(cmd);
  }

  @Override
  public List<DynamicTagCO> listDynamicTag(AppExtCommand cmd) {
    return dynamicTagQryExe.execute(cmd);
  }

  @Override
  public Long deleteComment(AppIdLongCmd cmd) {
    String lockKey = DistributedLockUtil.buildLockKey(DELETE_COMMENT_LOCK_PREFIX, cmd.getReqUserId(), cmd.getId());
    return distributedLockUtil.executeWithLock(lockKey, () -> dynamicCommentDelExe.execute(cmd));
  }

  @Override
  public DynamicCommentListCO comment(DynamicCommentCmd cmd) {
    String lockKey = DistributedLockUtil.buildLockKey(COMMENT_LOCK_PREFIX, cmd.getReqUserId(), cmd.getId());
    return distributedLockUtil.executeWithLock(lockKey, () -> dynamicCommentExe.execute(cmd));
  }

  @Override
  public Long likeComment(DynamicLikeCmd cmd) {
    String lockKey = DistributedLockUtil.buildLockKey(COMMENT_LIKE_LOCK_PREFIX, cmd.getReqUserId(), cmd.getCommentId());
    return distributedLockUtil.executeWithLock(lockKey, () -> dynamicCommentLikeExe.execute(cmd));
  }

  @Override
  public Long likeDynamic(DynamicLikeCmd cmd) {
    String lockKey = DistributedLockUtil.buildLockKey(LIKE_LOCK_PREFIX, cmd.getReqUserId(), cmd.getDynamicId());
    return distributedLockUtil.executeWithLock(lockKey, () -> dynamicLikeExe.execute(cmd));
  }

  @Override
  public PageResult<DynamicMessageListCO> listDynamicMessage(DynamicLimitCmd cmd) {
    return dynamicMessageQryExe.execute(cmd);
  }

  @Override
  public void deleteMessageAll(AppExtCommand cmd) {
    dynamicMessageAllDelCmdExe.execute(cmd);
  }

  @Override
  public void updateReadMessage(AppExtCommand cmd) {
    dynamicMessageUpdateReadCmdExe.execute(cmd);
  }

  @Override
  public void deleteMessage(IdLongCmd cmd) {
    dynamicMessageDelCmdExe.execute(cmd);
  }

  @Override
  public DynamicTagCO getDynamicTag(IdLongCmd cmd) {
    return dynamicTagExe.execute(cmd);
  }

  @Override
  public void report(DynamicReportCmd cmd) {
    String lockKey = DistributedLockUtil.buildLockKey(REPORT_LOCK_PREFIX, cmd.getReqUserId(), cmd.getDynamicContentId());
    distributedLockUtil.executeWithLock(lockKey, () -> dynamicReportCmdExe.execute(cmd));
  }

  @Override
  public DynamicSendRestrictCO checkSendPermission(AppExtCommand cmd) {
    return dynamicCheckSendPermissionCmdExe.execute(cmd);
  }

  @Override
  public Boolean checkSendPermission2(AppExtCommand cmd) {
    return dynamicCheckSendPermissionCmdExe.execute2(cmd);
  }

  @Override
  public void addBlacklist(DynamicBlacklistAddCmd cmd) {
    dynamicBlacklistAddCmdExe.execute(cmd);
  }

  @Override
  public void deleteBlacklist(DynamicBlacklistDelCmd cmd) {
    dynamicBlacklistDelCmdExe.execute(cmd);
  }

  @Override
  public void addApprovalDynamic(DynamicContentAddCmd cmd) {
    dynamicContentAddCmdExe.addApprovalDynamic(cmd);
  }

}
