package com.fdkanfang.dao.backend;

import com.fdkanfang.dao.backend.provider.UserProvider;
import com.fdkanfang.domain.backend.UserEntity;
import com.fdkanfang.domain.response.UserResponse;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.springframework.stereotype.Component;

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

@Component
@Mapper
public interface UserMapper extends IBaseMapper<UserEntity, Long> {

    @SelectProvider(type = UserProvider.class, method = "findAllBySearchKey")
    List<UserResponse> findAllBySearchKey(String searchKey);

    @Select(value = "select * from tb_user where user_name= #{userName}")
    UserEntity findByUserName(String userName);

    @Select(value = "delete from tb_user_role where user_id = #{id}")
    void deleteUserRoleByUserId(Long id);

    @Select(value = "insert into tb_user_role (user_id, role_id) values (#{userId}, #{roleId})")
    void saveUserRole(Long userId, Long roleId);

    /**
     * 根据用户id查角色名称
     */
    @Select(value = "select z.role_key from tb_user_role ur left join tb_role z on z.id = ur.role_id where ur.user_id = #{userId}")
    Set<String> findRoleByUserId(Long userId);

    @Select(value = "select z.id from tb_user_role ur left join tb_role z on z.id = ur.role_id where ur.user_id = #{userId}")
    Set<Integer> findRoleIdByUserId(Long userId);

    @Select(value = "select * from tb_user where phone = #{phone}")
    UserEntity findByPhone(String phone);
}