package com.salesmanager.core.business.repositories.user;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.salesmanager.core.model.user.User;

public interface UserRepository extends JpaRepository<User, Long>, UserRepositoryCustom {

	@Query("select u from User as u left join fetch u.groups ug join fetch u.merchantStore um left join fetch u.defaultLanguage ul where u.adminName = ?1")
	User findByUserName(String userName);

	@Query("select u from User as u left join fetch u.groups ug join fetch u.merchantStore um left join fetch u.defaultLanguage ul where u.id = ?1")
	User findOne(Long id);
	
	@Query("select u from User as u left join fetch u.groups ug join fetch u.merchantStore um left join fetch u.defaultLanguage ul order by u.id")
	List<User> findAll();
	
	@Query("select distinct u from User as u left join fetch u.groups ug join fetch u.merchantStore um left join fetch u.defaultLanguage ul where um.id = ?1 order by u.id")
	List<User> findByStore(Integer storeId);
	
	@Query("select u from User as u left join fetch u.groups ug join fetch u.merchantStore um left join fetch u.defaultLanguage ul where u.id= ?1 and um.code = ?2 order by u.id")
	User findByUserAndStore(Long userId, String storeCode);
}

@@ -339,32 +339,41 @@
 %09@Query(%22select 
+distinct 
 u from User as u
@@ -541,32 +541,41 @@
 %09@Query(%22select 
+distinct 
 u from User as u
@@ -722,32 +722,41 @@
 %09@Query(%22select 
+distinct 
 u from User as u

fix return multipule user when left join with more than one groups. this will fix shopizer-ecommerce/shopizer#340