package com.huimv.admin.repo.primary;


import com.huimv.admin.domain.primary.SysUserEntity;
import com.huimv.admin.group.UserAndAccountGroup;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface SysUserEntityRepository extends JpaRepository<SysUserEntity, Integer>, JpaSpecificationExecutor<SysUserEntity> {
    @Query(nativeQuery = true, value = "SELECT * FROM sys_user WHERE instr(user_name, ?1) > 0 OR instr(mobile, ?1) > 0 order by ?#{#pageable}")
    Page<SysUserEntity> findByUserNameOOrMobile(String searchStr, Pageable pageable);

//    Page<SysUserEntity> findAllUser(Pageable pageable);

//    @Query(nativeQuery = true, value = "SELECT user.*,account.account_name,account.account_status FROM sys_user user LEFT JOIN sys_account account ON user.user_id=account.user_id WHERE user.user_name=?1  OR user.mobile=?1 order by ?#{#pageable}")
    @Query(nativeQuery = true, value = "SELECT user.*,account.account_name,account.account_status FROM sys_user user LEFT JOIN sys_account account ON user.user_id=account.user_id WHERE user.user_name=?1  OR user.mobile=?1 order by id desc limit ?2,?3")
    List<Object[]> findUserAndAccountByUserName(String searchStr, int start, int end);

    @Query(nativeQuery = true, value="SELECT count(*) FROM sys_user  WHERE user_name=?1")
    Integer countByUserName(String userName);

//    @Query(nativeQuery = true, value = "SELECT user.*,account.account_name,account.account_status FROM sys_user user LEFT JOIN sys_account account ON user.user_id=account.user_id order by ?#{#pageable}")
    @Query(nativeQuery = true, value = "SELECT user.*,account.account_name,account.account_status FROM sys_user user LEFT JOIN sys_account account ON user.user_id=account.user_id order by id desc limit ?1,?2")
    List<Object[]> findUserAndAccount(int start, int pageSize);

    @Query(nativeQuery = true, value = "SELECT * FROM sys_user WHERE instr(user_name, ?1) > 0 OR instr(mobile, ?1) > 0 order by ?#{#pageable}")
    Page<SysUserEntity> findUserAndAccount2(@Param("searchStr") String searchStr, Pageable pageable);

    @Query(nativeQuery = true, value = "SELECT user.*,account.account_name,account.account_status FROM sys_user user LEFT JOIN sys_account account ON user.user_id=account.user_id WHERE user.user_name=?1  OR user.mobile=?1 order by ?#{#pageable}")
    Page<UserAndAccountGroup> yxxList(String searchStr, Pageable pageable);
}