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.DoorAccessControlConfigState;
import com.yuchen.saas.park.api.entity.door.config.DoorAccessControlTimeConfigDetails;
import lombok.val;
import org.apache.ibatis.annotations.Mapper;

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

@Mapper
public interface DoorAccessControlTimeConfigDetailsRepository extends BaseMapper<DoorAccessControlTimeConfigDetails> {
    default Optional<DoorAccessControlTimeConfigDetails> getByTargetAndParentId(DoorAccessControlConfigTarget target, @Positive long doorAccessControlMappingId) {
        val o = Wrappers.<DoorAccessControlTimeConfigDetails>lambdaQuery()
          .eq(DoorAccessControlTimeConfigDetails::getUserId, target.getUserId())
          .eq(DoorAccessControlTimeConfigDetails::getDoorAccessControlId, target.getDoorAccessControlId())
          .eq(DoorAccessControlTimeConfigDetails::getDoorAccessControlMappingId, doorAccessControlMappingId);

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

    default List<DoorAccessControlTimeConfigDetails> listByTarget(DoorAccessControlConfigTarget target) {
        val o = Wrappers.<DoorAccessControlTimeConfigDetails>lambdaQuery()
          .eq(DoorAccessControlTimeConfigDetails::getUserId, target.getUserId())
          .eq(DoorAccessControlTimeConfigDetails::getDoorAccessControlId, target.getDoorAccessControlId());

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

    default void deleteByParentIds(@NotEmpty Set<@Positive Long> doorAccessControlMappingIds) {
        val o = Wrappers.<DoorAccessControlTimeConfigDetails>lambdaQuery()
          .in(DoorAccessControlTimeConfigDetails::getDoorAccessControlMappingId, doorAccessControlMappingIds);

        this.delete(o);
    }

    default void expiredByIds(@NotEmpty Set<@Positive Long> ids) {
        val o = Wrappers.<DoorAccessControlTimeConfigDetails>lambdaUpdate()
          .in(DoorAccessControlTimeConfigDetails::getId, ids)
          .set(DoorAccessControlTimeConfigDetails::getState, DoorAccessControlConfigState.EXPIRED.getCode());

        this.update(null, o);
    }

    default List<DoorAccessControlTimeConfigDetails> listByParentIds(@NotEmpty Set<@Positive Long> doorAccessControlMappingIds) {
        val o = Wrappers.<DoorAccessControlTimeConfigDetails>lambdaQuery().in(DoorAccessControlTimeConfigDetails::getDoorAccessControlMappingId, doorAccessControlMappingIds);
        return Optional.ofNullable(this.selectList(o)).orElse(List.of());
    }

    default List<DoorAccessControlTimeConfigDetails> listByParentId(@NotEmpty Long doorAccessControlMappingId) {
        val o = Wrappers.<DoorAccessControlTimeConfigDetails>lambdaQuery().eq(DoorAccessControlTimeConfigDetails::getDoorAccessControlMappingId, doorAccessControlMappingId);
        return Optional.ofNullable(this.selectList(o)).orElse(List.of());
    }
}
