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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.config.DoorAccessControlUserPermissions;
import com.yuchen.saas.park.service.mapper.domain.DoorAccessControlBlacklistedCondition;
import com.yuchen.saas.park.service.mapper.domain.DoorAccessControlBlacklistedRO;
import lombok.val;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.nest.springwrap.core.tool.utils.Func;

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

@Mapper
public interface DoorAccessControlUserPermissionsRepository extends BaseMapper<DoorAccessControlUserPermissions> {
    default Optional<DoorAccessControlUserPermissions> getByUserId(@Positive long userId) {
        return Optional.ofNullable(this.selectOne(Wrappers.<DoorAccessControlUserPermissions>lambdaQuery().eq(DoorAccessControlUserPermissions::getUserId, userId)));
    }

    default void updateBlacklistedByUserIds(@NotEmpty Set<@Positive Long> userIds, boolean blacklisted) {
        val o = Wrappers.lambdaUpdate(DoorAccessControlUserPermissions.class)
          .in(DoorAccessControlUserPermissions::getUserId, userIds)
          .set(DoorAccessControlUserPermissions::isBlacklisted, blacklisted)
          .set(DoorAccessControlUserPermissions::getUpdateTime, LocalDateTime.now());

        this.update(null, o);
    }

    IPage<DoorAccessControlBlacklistedRO> page(IPage<DoorAccessControlBlacklistedRO> page, @Param("o") DoorAccessControlBlacklistedCondition o);

    default void batchSetUserState(@NotEmpty List<DoorAccessControlUserPermissions> permissionList){
        if(Func.isNotEmpty(permissionList)){
            for (DoorAccessControlUserPermissions permissions : permissionList) {
                long userId = permissions.getUserId();
                List<DoorAccessControlUserPermissions> list = this.selectList(Wrappers.lambdaQuery(DoorAccessControlUserPermissions.class).eq(DoorAccessControlUserPermissions::getUserId, userId));
                if(Func.isNotEmpty(list)){
                    for (DoorAccessControlUserPermissions userPermissions : list) {
                        userPermissions.setFrozen(permissions.isFrozen());
                        userPermissions.setBlacklisted(permissions.isBlacklisted());
                        userPermissions.setDeleted(permissions.isDeleted());
                        this.updateById(userPermissions);
                    }
                }else{
                    DoorAccessControlUserPermissions permissionsNew =  new DoorAccessControlUserPermissions();
                    permissionsNew.setUserId(permissions.getUserId());
                    permissionsNew.setFrozen(permissions.isFrozen());
                    permissionsNew.setBlacklisted(permissions.isBlacklisted());
                    permissionsNew.setDeleted(permissions.isDeleted());
                    this.insert(permissionsNew);
                }
            }
        }
    }
}
