package com.yihu.wlyy.statistics.dao;

import com.yihu.wlyy.statistics.model.team.DoctorTeam;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface DoctorTeamDao extends
PagingAndSortingRepository<DoctorTeam, Long>,
JpaSpecificationExecutor<DoctorTeam> {

	DoctorTeam findByCode(String code);

	//@Query("select dt from DoctorTeam dt,DoctorTeamMember dtm  where dt.code=dtm.team and dt.del=1 and dtm.del=1 and dtm.memberCode= ?1 and dtm.type= 5 and dtm.signType='2'")
	@Query("select dtt from DoctorTeam dtt where dtt.id = (select max(dt.id) from DoctorTeam dt,DoctorTeamMember dtm  where dt.code=dtm.team and dt.del=1 and dtm.del=1 and dtm.memberCode= ?1 and dtm.type= 5 and dtm.signType='2')")
	DoctorTeam findByParientCode(String parientCode);

	@Query("select dt from DoctorTeam dt,DoctorTeamMember dtm  where dt.code=dtm.team and dt.del=1 and dtm.del=1 and dtm.memberCode= ?1 and dtm.type= 5 and dtm.signType='1'")
	DoctorTeam findBySanshiParientCode(String parientCode);

	//@Query("select dt from DoctorTeam dt,DoctorTeamMember dtm  where dt.code=dtm.team and dt.del=1 and dtm.del=1 and dtm.memberCode= ?1 and dtm.type= 5 and dtm.signType=?2 order by dtm.id limit 1")
	@Query("select dtt from DoctorTeam dtt where dtt.id = (select max(dt.id) from DoctorTeam dt,DoctorTeamMember dtm  where dt.code=dtm.team and dt.del=1 and dtm.del=1 and dtm.memberCode= ?1 and dtm.type= 5 and dtm.signType=?2)")
	DoctorTeam findByParientCodeAndSignType(String parientCode, String type);

	@Modifying
	@Query("update DoctorTeam a set a.del =0  where a.code = ?1 and signType='2'")
	void deleteTeam(String teamCode);
}
