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

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.commons.enums.DoorAccessControlConfigMappingType;
import com.yuchen.saas.park.api.entity.door.config.DoorAccessControlConfigMapping;
import com.yuchen.saas.park.service.mapper.domain.config.mapping.*;
import lombok.val;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import javax.validation.constraints.Positive;
import java.util.Optional;

@Mapper
public interface DoorAccessControlConfigMappingRepository extends BaseMapper<DoorAccessControlConfigMapping> {
    default Optional<DoorAccessControlConfigMapping> getByTypeAndRefId(
      DoorAccessControlConfigMappingType type,
      @Positive long userRefId,
      boolean recursive,
      @Positive long doorAccessControlRefId,
      @Positive long timeConfigId
    ) {
        val o = Wrappers.<DoorAccessControlConfigMapping>lambdaQuery()
          .eq(DoorAccessControlConfigMapping::getType, type.getCode())
          .eq(DoorAccessControlConfigMapping::getUserRefId, userRefId)
          .eq(DoorAccessControlConfigMapping::isRecursive, recursive)
          .eq(DoorAccessControlConfigMapping::getDoorAccessControlRefId, doorAccessControlRefId)
          .eq(DoorAccessControlConfigMapping::getTimeConfigId, timeConfigId);

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

    IPage<DoorAccessControlConfigMappingDepartmentGroupRO> pageForDepartmentGroup(IPage<?> page, @Param("o") DoorAccessControlConfigMappingDepartmentGroupCondition o);

    IPage<DoorAccessControlConfigMappingDepartmentPersonalRO> pageForDepartmentPersonal(IPage<?> page, @Param("o") DoorAccessControlConfigMappingDepartmentPersonalCondition o);

    IPage<DoorAccessControlConfigMappingGroupGroupRO> pageForGroupGroup(IPage<?> page, @Param("o") DoorAccessControlConfigMappingGroupGroupCondition o);

    IPage<DoorAccessControlConfigMappingGroupPersonalRO> pageForGroupPersonal(IPage<?> page, @Param("o") DoorAccessControlConfigMappingGroupPersonalCondition o);

    IPage<DoorAccessControlConfigMappingPersonalGroupRO> pageForPersonalGroup(IPage<?> page, @Param("o") DoorAccessControlConfigMappingPersonalGroupCondition o);

    IPage<DoorAccessControlConfigMappingPersonalPersonalRO> pageForPersonalPersonal(IPage<?> page, @Param("o") DoorAccessControlConfigMappingPersonalPersonalCondition o);
}
