package com.cf.imes.module.system.dal.mysql.tokenconfig;


import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.module.system.controller.admin.tokenconfig.vo.TokenConfigPageReqVO;
import com.cf.imes.module.system.dal.dataobject.tokenconfig.TokenConfigDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;

import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getUserOrganId;



/**
 * @author token 配置管理mapper
 */

@Mapper
public interface TokenConfigMapper extends BaseMapperX<TokenConfigDO> {


    default PageResult<TokenConfigDO> selectConfigPage(TokenConfigPageReqVO pageReqVO, LocalDateTime[] localDateTimes) {
        return selectPage(pageReqVO, new LambdaQueryWrapperX<TokenConfigDO>()
                .eq(TokenConfigDO::getOrganId,getUserOrganId())
                .eq(TokenConfigDO::getDeleted,false)
                .likeIfPresent(TokenConfigDO::getAppToken,pageReqVO.getAppToken())
                .likeIfPresent(TokenConfigDO::getAppName,pageReqVO.getAppName())
                .betweenIfPresent(TokenConfigDO::getExpiresTime,localDateTimes)
                .orderByDesc(TokenConfigDO::getId));
    }


    List<Integer> selectAppTypeByOrganId(@Param("organId") Long organId);


    default List<TokenConfigDO> selectConfigByOrganId(Long organId) {
        return selectList(new LambdaQueryWrapperX<TokenConfigDO>()
                .eq(TokenConfigDO::getOrganId,organId)
                .eq(TokenConfigDO::getDeleted,false));
    }


    default TokenConfigDO selectByToken(String token) {
        return selectOne(new LambdaQueryWrapperX<TokenConfigDO>()
                .eq(TokenConfigDO::getAppToken,token));
    }

    void deleteTokenConfig(@Param("id") Long id);
}
