package com.red.circle.other.app.command.material.query;

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.infra.database.mongo.entity.live.RoomProfile;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.infra.database.rds.service.props.PropsRoomLockService;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import com.red.circle.tool.core.date.TimestampUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 获取房间锁过期时间.
 *
 * @author pengliang on 2021/6/3
 */
@Component
@RequiredArgsConstructor
public class PropsRoomLockExpiredTimeQryExe {

  private final PropsRoomLockService propsRoomLockService;
  private final RoomProfileManagerService roomProfileManagerService;

  public Timestamp execute(AppExtCommand cmd) {
    Timestamp timestamp = propsRoomLockService.getExpireTime(cmd.requiredReqUserId());
    List<RoomProfile> list = roomProfileManagerService.listProfileByUserIds(Set.of(cmd.requiredReqUserId()));
    if (Objects.nonNull(timestamp)){
      if (TimestampUtils.now().compareTo(timestamp) > 0){
        timestamp =Timestamp.valueOf("0");
        roomProfileManagerService.updatePassword(list.get(0).getId(),"");
      }
    }
    return timestamp;
  }

}
