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

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.manage.user.api.entity.User;
import com.yuchen.saas.park.api.commons.IdName;
import com.yuchen.saas.park.api.commons.IdTree;
import com.yuchen.saas.park.api.commons.IdTreeCount;
import com.yuchen.saas.park.service.mapper.domain.adapter.DoorAccessControlTemplateIdNoRo;
import com.yuchen.saas.park.service.mapper.domain.config.details.DoorAccessControlCondition;
import com.yuchen.saas.park.service.mapper.domain.config.details.DoorAccessControlRO;
import com.yuchen.saas.park.service.service.door.communication.domain.CommonsParameter;
import lombok.val;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nullable;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Positive;
import java.util.*;
import java.util.stream.Collectors;

@Mapper
public interface AdapterRepository extends BaseMapper<User> {
    /**
     * 查询部门id和父类id
     * @return
     */
    @Nullable
    List<IdTree> listAllDepartmentId();

    default Map<@Positive Long, String> listFacialByUserIds(@NotEmpty Set<@Positive Long> userIds) {
        val query = Wrappers.<User>lambdaQuery().in(User::getId, userIds);

        val list = this.selectList(query);
        if (CollectionUtils.isEmpty(list)) {
            return Map.of();
        }

        return list.stream().collect(Collectors.toMap(User::getId, User::getFaceImgUrl));
    }

    default Optional<String> getFacialByUserId(@Positive long userId) {
        val query = Wrappers.<User>lambdaQuery().eq(User::getId, userId);

        return Optional.ofNullable(this.selectOne(query)).map(User::getFaceImgUrl);
    }

    @Nullable
    List<IdTree> listUserIdByDepartmentIds(@Param("ids") Set<@Positive Long> ids);

    @Nullable
    List<IdTree> listUserIdByUserGroupIds(@Param("ids") Set<@Positive Long> ids);

    @Nullable
    List<IdTree> listDoorAccessControlIdByGroupIds(@Param("ids") Set<@Positive Long> ids);

    IPage<DoorAccessControlRO> pageDoorAccessControl(IPage<?> page, @Param("o") DoorAccessControlCondition o);

    @Nullable
    List<IdName> listNameByUserIds(@Param("ids") @NotEmpty Set<@Positive Long> ids);

    @Nullable
    List<IdName> listDepartmentNameByUserIds(@Param("ids") @NotEmpty Set<@Positive Long> ids);

    @Nullable
    List<IdTreeCount> listDoorAccessControlGroupCountByGroupIds(@Param("ids") @NotEmpty Set<@Positive Long> ids);

    @Nullable
    List<DoorAccessControlTemplateIdNoRo> listAllDoorAccessControlTemplateIdNo();

    default Optional<CommonsParameter> getCommonsParameterByDoorAccessControlId(@Positive long id) {
        val list = this.listCommonsParameterByDoorAccessControlIds(Set.of(id));
        return Optional.ofNullable(list).stream().flatMap(Collection::stream).findFirst();
    }

    @Nullable
    List<CommonsParameter> listCommonsParameterByDoorAccessControlIds(@NotEmpty Set<@Positive Long> ids);
}
