package com.storlead.user.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.storlead.framework.auth.vo.LoginUser;
import com.storlead.framework.mybatis.mapper.MyBaseMapper;
import com.storlead.user.pojo.entity.UserEntity;
import com.storlead.user.model.UserBaseInfo;
import com.storlead.user.model.UserInfo;
import com.storlead.user.model.UserQueryModel;
import com.storlead.user.pojo.vo.UserVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;
import java.util.Set;

public interface UserMapper extends MyBaseMapper<UserEntity> {

    @Select("select u.id,u.user_name,u.password,u.avatar,u.sex,u.nick_name,u.real_name,u.mobile,u.email,u.job_id," +
            "u.dept_id,1 AS test,u.sub_company_id,u.company_id,u.enabled,u.status,u.is_leader,u.manager_id,u.managers,r.data_scope from user u left join user_role ur on ur.user_id = u.id left join role r on r.id = ur.role_id  where user_name = #{username} limit 1")
    UserEntity getUserByName(String username);

    @Select("select id,user_name,avatar,sex,nick_name,real_name,mobile,email,job_id,dept_id,sub_company_id,company_id,enabled,status from user")
    List<UserEntity> getUserList();

    UserBaseInfo queryByUserId(Long id);

//    List<UserVo> list(IPage<UserVo> page, @Param(Constants.WRAPPER) Wrapper<UserVo> wrapper);

    List<UserVo> searchHomeUserList(IPage<UserVo> page, @Param(Constants.WRAPPER) Wrapper<UserVo> wrapper);


    List<UserBaseInfo> queryByUserIdList(@Param("ids") Collection<Long> ids);

    List<UserInfo> queryList(@Param("query") UserQueryModel userQueryModel);

    List<Long> getListByUserRole(LoginUser loginUser);

    /**
    * 获取所有下级id
    * @Author: xiaojiawei
    * @Date: 2022/5/7
    */
    List<Long> getAllSubordinate(@Param("currentUserId") Long currentUserId);

    @Select("select u.id,u.user_name,u.password,u.avatar,u.sex,u.nick_name,u.real_name,u.mobile,u.email,u.job_id," +
            "u.dept_id,1 AS test,u.sub_company_id,u.company_id,u.enabled,u.status,u.is_leader,u.manager_id,u.managers,r.data_scope from user u left join user_role ur on ur.user_id = u.id left join role r on r.id = ur.role_id  where mobile = #{mobile} limit 0,1")
    UserEntity getUserByMobile(String mobile);

    UserVo getUserInfoById(@Param("userId") Long userId);

    void addUserAvatar();

    Set<Long> selectUserRoleIdsByApiUrl(@Param("userId") Long userId, @Param("apiUrl") String apiUrl);

    Set<Long> selectUserRoleIdsByMenuId(@Param("userId") Long userId, @Param("menuId") String menuId);

    Set<Long> selectUserRoleIdsByCommonScopeType(@Param("userId") Long userId, @Param("commonScopeType") String commonScopeType);

    /**
     * 根据部门id获取用户id
     * @param deptIds
     * @return
     */
    Set<Long> getUserIdsByDeptIds(@Param("deptIds") List<Long> deptIds);

    void updateLastFollowUpTime(@Param("ownerByIds") List<Long> ownerByIds);
}
