package com.ssj.dao.weixin.user.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.ssj.bean.weixin.user.domain.User;

import java.util.List;

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

	public Page<User> findAll(Specification<User> specification, Pageable page);
	
	public User findByAccount(String account);

	public User findByUserName(String userName);
	   /**
     * 修改关注状态
     * @param openId
     */
	 @Query(nativeQuery = true, value = "update tb_user set subscribe = ?2   where wx_account = ?1 ")
	 @Modifying
     public void updateSubscribe(String openId, Integer subscribe);
	 @Query(nativeQuery = true, value = "update tb_user set wx_account = ?2   where id = ?1 ")
	 @Modifying
	 public void updateAccount(String id, String account);

	@Query(nativeQuery = true, value = "update tb_user set unionid = ?2   where id = ?1 ")
	@Modifying
	public void updateUnionid(String id, String unionid);

	 
	@Query(nativeQuery = true, value = "select wx_account  from tb_user  where id =?1  ")
	public String getAccount(String id);
	
	@Query(nativeQuery = true, value = "select id  from tb_user  where unionid =?1 ORDER BY wx_account desc  limit 1 ")
	public String findUserByUnionid(String unionid);
	@Query(nativeQuery = true, value = "select *  from tb_user  where unionid =?1 ORDER BY wx_account desc   ")
	public List<User>  getUserByUnionid(String unionid);
	@Query(nativeQuery = true, value = "select *  from tb_user  where unionid =?1  limit 1 ")
	public User  getUserByUnionidLimit(String unionid);
	
	@Query(nativeQuery = true, value = "select id  from tb_user  where wx_account =?1  ")
	public String findUserById(String wx_account);
	
	@Query(nativeQuery = true, value = "select subscribe  from tb_user  where id =?1  ")
	public String getSubscribe(String userId);
	
	
	@Query(nativeQuery = true, value = "select *   from tb_user  where wx_account is not null   LIMIT ?1,?2 ")
	public List<User> getByUserLimit(int start, int end);
	@Query(nativeQuery = true, value = "select count(1)   from tb_user  where wx_account is not null   ")
	public Long getCount();
	
	public List<User> findByIdIn(List<String> ids);
	
	public User findByEasemobName(String easemobName);
	
	public User findByMobile(String mobile);


	@Query(value = "select * from ssj_parent_v where user_name like concat('%', ?1, '%') or (?1 = '' and  user_name is null) ORDER BY ?#{#pageable}", nativeQuery = true)
	Page<User> getParent(String userName, Pageable pageable);
	
	public List<User> findByEasemobNameIsNotNull();
	
	@Query(value = "select * from tb_user t where t.id in("
			+ " select child_user_id from tb_lib_vip where (user_id is null or user_id = '') and child_user_id is not null "
			+ " ) and t.easemob_name is not null", nativeQuery = true)
	public List<User> findNoUserEasemobName();
	
	
	 @Query(nativeQuery = true, value = "update tb_user set `password` = ?2 , pwd_salt=?3  where id = ?1 ")
	 @Modifying
     public void updateByPassword(String userId, String passwrod,String pwdSalt);

	@Query(nativeQuery = true, value = "update tb_user set mobile = ?2  where id = ?1 ")
	@Modifying
	void updateUserPhone(String id, String phone);

	@Modifying
	@Query(nativeQuery = true,value = "update tb_user set mobile=null,wx_name=null,photo=null,weixin_photo=null,user_name=null,unionid=null,wx_account=null,create_time=null,password=null,pwd_salt=null where id=?1")
	void updateById(String id);
}
