package com.ssj.dao.weixin.sales.dao;

import java.util.List;

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.sales.domain.LibStaffing;


/** 
 * @author  ZhangCaibao 
 * @version 2017年4月24日 上午11:13:35 
 * 类说明 
 */
@Repository
public interface LibStaffingDao  extends JpaRepository<LibStaffing, String>{
  
	 public Page<LibStaffing> findAll(Specification<LibStaffing> specification, Pageable page);
	 
	 @Query(nativeQuery = true, value = " delete from  tb_lib_staffing  where lib_id = ?1 and user_id = ?2 ")
	 @Modifying
	 public void deleteIn(String libId,String userId);
	 
	 
	 @Query(nativeQuery = true, value = " update   tb_lib_staffing  set stat =?2  where id =?1 ")
	 @Modifying
	 public void updateStaffingById(String id,Integer stat);
	 @Query(nativeQuery = true, value = " update   tb_lib_staffing  set stat =?2 ,create_time=now()  where user_id =?1 and type ='s_sd' ")
	 @Modifying
	 public void updateStatByUserId(String userId,Integer stat);
	 
	 @Query(nativeQuery = true, value = " select  count(1) from  tb_lib_staffing  where lib_id = ?1 and user_id = ?2 and type = ?3 and stat=1 ")
	 public Integer  getLibStaffingCount(String libId,String userId,String type);
	 
	 @Query(nativeQuery = true, value = " select  * from  tb_lib_staffing  where lib_id = ?1 and user_id = ?2 and type = ?3 limit 1 ")
	 public LibStaffing  getLibStaffingByLibIdAndUserIdAndType(String libId,String userId,String type);
	 
	 @Query(nativeQuery = true, value = " select id  from  tb_lib_join  where org_code = ?1 limit 1")
	 public String getLibJoinId(String orgCode); 
	 
	 @Query(nativeQuery = true, value = " select  * from  tb_lib_staffing  where lib_id = ?1 and  stat = 1 ")
	 public List<LibStaffing> findByLibId(String libId); 
	 
	 @Query(nativeQuery = true, value = " select  * from  tb_lib_staffing  where user_id = ?1 and stat=1 ")
	 public List<LibStaffing> findByUserId(String userId); 
	 
	 @Query(nativeQuery = true, value = "SELECT  DISTINCT u.wx_account from  tb_lib_staffing  t  JOIN  tb_user  u\n" +
			 " on  t.user_id = u.id \n" +
			 " where  t.lib_id = ?1 and  t.type=?2 and stat=1 ")
	 public List<String> findByCDAndOpenId(String libId,String type); 
	 
	 @Query(nativeQuery = true, value = " select *  from  tb_lib_staffing  where lib_id = ?1 and user_id = ?2 and stat=1  limit 1")
	 public LibStaffing findByLibIdAndUserId(String libId,String userId);
	 
	 @Query(nativeQuery = true, value = " select  count(1) from  tb_lib_staffing  where lib_id = ?1 and  type = 's_cd' and stat=1 ")
	 public Integer fingByCDcount(String libId);

	 @Query(nativeQuery = true, value = " select *  from  tb_lib_staffing  where  user_id = ?1 and type=?2  and stat=1 order by create_time DESC limit 1")
	 public LibStaffing findByUserIdAndType(String userId, String type);
	 
	 @Query(nativeQuery = true, value = "SELECT  DISTINCT u.wx_account from  tb_lib_staffing  t LEFT JOIN  tb_user  u\n" +
			 " on  t.user_id = u.id \n" +
			 " where  t.lib_id =?1  AND (t.type=?2 OR t.type=?3) and t.stat = 1 ")
	 public List<String> findByCDSDAndOpenId(String libId,String type,String types);

	 @Query(nativeQuery = true, value = " select *  from  tb_lib_staffing  where  user_id = ?1 and lib_id=?2 and stat=1 limit 1")
	 public LibStaffing findByUserIdAndLib(String userid, String libId);

	 @Query(nativeQuery = true, value = " select *  from  tb_lib_staffing  where lib_id = ?1 and type = ?2 and stat=1  limit 1")
	 public LibStaffing findOnebyLibIdAndType(String libId, String type); 
}
