package com.yihu.wlyy.repository.discussion;

import java.util.List;

import com.yihu.wlyy.entity.discussion.WlyyTalkGroupMember;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.yihu.wlyy.entity.discussion.WlyyTalkGroup;

public interface DiscussionGroupDao extends PagingAndSortingRepository<WlyyTalkGroup, Long>{
	
	/**
	 * 查询医生对应讨论组
	 * 
	 * @param doctor
	 * @return
	 */
	@Query("select tg from WlyyTalkGroup tg, WlyyTalkGroupMember tgm where tg.code = tgm.groupCode and tgm.memberCode = ?1")
	List<WlyyTalkGroup> findDoctorTalkGroups(String doctor);

	@Query("select tg from WlyyTalkGroup tg,WlyyTalkGroupMember tgm where tg.code = tgm.groupCode and tgm.memberName like ?1 and tg.code in (?2)")
	List<WlyyTalkGroup> findByDoctorName(String doctorName,String groupCode);
	
	/**
	 * 根据咨询查询讨论组
	 * 
	 * @param consultCode
	 * @return
	 */
	List<WlyyTalkGroup> findByConsultCodeAndType(String consultCode,Integer type);
	
	/**
	 * 获取医生对应病人讨论组
	 * 
	 * @param doctor
	 * @param patient
	 * @return
	 */
	@Query("select tg from WlyyTalkGroup tg,WlyyTalkGroupMember tgm where tg.code = tgm.groupCode and tgm.memberCode = ?1 and tg.patient = ?2")
	List<WlyyTalkGroup> findByDoctorAndPatient(String doctor,String patient);
	
	WlyyTalkGroup findByCode(String code);

	@Query("from WlyyTalkGroup tg where tg.consultCode=?1  ")
	List<WlyyTalkGroup> findByConsult(String consult);

	@Query("from WlyyTalkGroup tg where tg.consultCode=?1 and tg.type=?2 ")
	WlyyTalkGroup findByConsultAndType(String consult, int type);

	@Query("select tgm from WlyyTalkGroup tg,WlyyTalkGroupMember tgm where tg.code = tgm.groupCode and tg.creator=?1 and tg.type=2 and tgm.memberCode = ?2")
	WlyyTalkGroupMember findCreateAndDoctor(String create, String doctorCode);
}
