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

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuchen.saas.park.api.commons.domain.DoorAccessControlConfigTarget;
import com.yuchen.saas.park.api.commons.enums.DoorAccessControlConfigType;
import com.yuchen.saas.park.api.entity.door.config.DoorAccessControlConfigTrigger;
import lombok.val;
import org.apache.ibatis.annotations.Mapper;

import javax.validation.constraints.Max;
import javax.validation.constraints.PositiveOrZero;
import java.util.List;
import java.util.Optional;

@Mapper
public interface DoorAccessControlConfigTriggerRepository extends BaseMapper<DoorAccessControlConfigTrigger> {
    default Optional<DoorAccessControlConfigTrigger> getByTargetAndType(DoorAccessControlConfigTarget target, DoorAccessControlConfigType type) {
        val o = Wrappers.<DoorAccessControlConfigTrigger>lambdaQuery()
          .eq(DoorAccessControlConfigTrigger::getUserId, target.getUserId())
          .eq(DoorAccessControlConfigTrigger::getDoorAccessControlId, target.getDoorAccessControlId())
          .eq(DoorAccessControlConfigTrigger::getType, type.getCode());

        return Optional.ofNullable(this.selectOne(o));
    }

    //default IPage<DoorAccessControlConfigTrigger> pageUnprocessed(IPage<DoorAccessControlConfigTrigger> page) {
    //    val o = Wrappers.<DoorAccessControlConfigTrigger>lambdaQuery()
    //      .eq(DoorAccessControlConfigTrigger::isProcessed, false)
    //      .orderBy(true, true, DoorAccessControlConfigTrigger::getId);
    //
    //    return this.selectPage(page, o);
    //}

    default List<DoorAccessControlConfigTrigger> listUnprocessed(@Max(20) @PositiveOrZero int size) {
        val o = Wrappers.<DoorAccessControlConfigTrigger>lambdaQuery()
          .eq(DoorAccessControlConfigTrigger::isProcessed, false)
          .orderByAsc(DoorAccessControlConfigTrigger::getId)
          .last("LIMIT " + size);

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