package com.uas.platform.b2b.dao;

import com.uas.platform.b2b.model.User;
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.jpa.repository.QueryHints;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.persistence.QueryHint;
import java.util.List;

@Repository
public interface UserDao extends JpaSpecificationExecutor<User>, JpaRepository<User, Long> {

	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value = "true") })
	public List<User> findUserByUserUU(Long userUU);

	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value = "true") })
	public List<User> findUserByUserEmail(String userEmail);

	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value = "true") })
	public List<User> findUserByUserTel(String userTel);

	/**
	 * 按企业ID和个人ID查找个人
	 * 
	 * @param enUU
	 * @param userUU
	 * @return
	 */
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value = "true") })
	@Query("select u from User u inner join u.enterprises as e where e.uu = :enUU and u.userUU = :userUU")
	public List<User> findByEnUUAndUserUU(@Param("enUU") long enUU, @Param("userUU") long userUU);

	/**
	 * 按企业ID和查找个人
	 * 
	 * @param enUU
	 * @return
	 */
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value = "true") })
	@Query("select u from User u inner join u.enterprises as e where e.uu = :enUU")
	public List<User> findByEnUU(@Param("enUU") long enUU);

	/**
	 * 按角色ID查找该角色下的用户
	 * 
	 * @param roleId
	 * @return
	 */
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value = "true") })
	@Query("select u from User u inner join u.roles as r where r.id = :roleId")
	public List<User> findByRole(@Param("roleId") long roleId);

	/**
	 *  分页查询用户信息
	 *
	 * @param page
	 * @return
	 */
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value = "true") })
	@Query(value = "select u from User u inner join u.enterprises as e where e.uu = :enUU",
			countQuery = "select count(1) from User u inner join u.enterprises as e where e.uu = :enUU")
	Page<User> findEnUUByAndPage(@Param("enUU")Long enUU, Pageable page);

	/**
	 * 按企业ID和个人ID查找个人
	 *
	 * @param enUU
	 * @param userUU
	 * @return
	 */
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value = "true") })
	@Query(value = "select u from User u inner join u.enterprises as e where e.uu = :enUU and u.userUU = :userUU",
			countQuery = "select count(1) from User u inner join u.enterprises as e where e.uu = :enUU and u.userUU = :userUU")
	public Page<User> findByEnUUAndUserUU(@Param("enUU") long enUU, @Param("userUU") long userUU, Pageable page);

	/**
	 * 按企业ID和个人ID查找个人
	 *
	 * @author hejq
	 * @date 2018-01-16 17:09
	 * @param enUU 企业UU
	 * @param keyword 关键词
	 * @return
	 */
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value = "true") })
	@Query(value = "select u from User u inner join u.enterprises as e where e.uu = :enUU and " +
			"(u.userName like %:keyword% or u.userTel like %:keyword% or u.userEmail like %:keyword%)",
			countQuery = "select count(1) from User u inner join u.enterprises as e where e.uu = :enUU and " +
					"(u.userName like %:keyword% or u.userTel like %:keyword% or u.userEmail like %:keyword%)")
	public Page<User> findByEnUUAndKeyword(@Param("enUU") long enUU, @Param("keyword") String keyword, Pageable page);

    /**
     * 按企业ID和个人ID查找个人
     *
     * @author hejq
     * @date 2018-01-16 17:09
     * @param enUU 企业UU
     * @param keyword 关键词
     * @return
     */
    @QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value = "true") })
    @Query(value = "select u from User u inner join u.enterprises as e where e.uu = :enUU and " +
            "(u.userUU like :useruu or u.userName like %:keyword% or u.userTel like %:keyword% or u.userEmail like %:keyword%)",
            countQuery = "select count(1) from User u inner join u.enterprises as e where e.uu = :enUU and " +
                    "(u.userUU like :useruu or u.userName like %:keyword% or u.userTel like %:keyword% or u.userEmail like %:keyword%)")
    public Page<User> findByEnUUAndKeyword(@Param("enUU") long enUU, @Param("keyword") String keyword, @Param("useruu") Long useruu, Pageable page);
}
