package com.yuchen.saas.park.service.mapper.door.card;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuchen.saas.park.api.entity.door.card.DoorAccessControlCard;
import com.yuchen.saas.park.service.mapper.domain.*;
import com.yuchen.saas.park.service.mapper.domain.config.details.UserDoorAccessControlCondition;
import com.yuchen.saas.park.service.mapper.domain.config.details.UserDoorAccessControlRO;
import com.yuchen.saas.park.service.service.domain.DoorAccessControlCardUserCondition;
import lombok.val;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Positive;
import java.util.List;
import java.util.Optional;
import java.util.Set;

@Mapper
public interface DoorAccessControlCardRepository extends BaseMapper<DoorAccessControlCard> {
    default List<DoorAccessControlCard> listByUserId(@Positive long userId) {
        val query = Wrappers.<DoorAccessControlCard>lambdaQuery().eq(DoorAccessControlCard::getUserId, userId);

        return Optional.ofNullable(this.selectList(query)).orElse(List.of());
    }

    default Optional<DoorAccessControlCard> getByNo(@NotBlank String no) {
        return Optional.ofNullable(this.selectOne(Wrappers.<DoorAccessControlCard>lambdaQuery().eq(DoorAccessControlCard::getNo, no)));
    }

    default List<DoorAccessControlCard> listByNos(Set<@NotBlank String> nos) {
        return this.selectList(Wrappers.<DoorAccessControlCard>lambdaQuery().eq(DoorAccessControlCard::getNo, nos));
    }

    IPage<DoorAccessControlCardUserAggregationRO> pageForUser(IPage<?> page, @Param("o") DoorAccessControlCardConditionRO ro);

    IPage<DoorAccessControlCardRO> page(IPage<?> page, @Param("o") DoorAccessControlCardUserCondition o);

    List<DoorAccessControlCardStateAggregationRO> groupByLossReport(@Param("o") DoorAccessControlCardStateAggregationCondition o);

    IPage<UserDoorAccessControlRO> pageUserDoorAccessControl(IPage<?> page, @Param("o") UserDoorAccessControlCondition o);
}
