package com.yihu.jw.im.dao;

import com.yihu.jw.entity.base.im.ConsultTeamDo;
import org.hibernate.validator.constraints.LuhnCheck;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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;

import java.util.Date;
import java.util.List;

/**
 * 咨询扩展表DAO类
 * @author huangwenjie
 */
public interface ConsultTeamDao  extends PagingAndSortingRepository<ConsultTeamDo, String>, JpaSpecificationExecutor<ConsultTeamDo> {
	// 根據consult查詢咨询记录
	ConsultTeamDo findByConsult(String consult);
	
//	// 关闭咨询
//	@Modifying
//	@Query("update ConsultTeam a set a.status = 1 where a.consult = ?1")
//	int updateStatusByConsult(String consult);
//
//	@Query("select a from ConsultTeamDo a,ConsultTeamDoctor b where a.consult = b.consult and a.patient = ?1 and b.to = ?2 and a.del = '1' and a.type<>8 and a.status = 0")
//	List<ConsultTeamDo> findUnfinishedConsultType(String patient,String doctor);

	@Query("select count(id) from  ConsultTeamDo a where a.type = ?1 and a.doctor = ?2 and a.czrq >=?3 and a.czrq <=?4")
	Integer  countByTime(int type, String doctor, Date startTime, Date endTime);

	ConsultTeamDo queryByRelationCode(String relationCode);

	List<ConsultTeamDo> findByTypeAndDel(Integer type,String del);

	// 患者咨询列表(未结束)
	@Query("select a from ConsultTeamDo a where a.patient = ?1 and a.status=0 and a.del = '1'")
	Page<ConsultTeamDo> findNotFinishedBypatient(String patient, Pageable pageRequest);

	@Query("select a from ConsultTeamDo a where a.patient = ?1 and a.id = ?2 and a.status=0 and a.del = '1' and a.type<>8")
	Page<ConsultTeamDo> findNotFinishedBypatient(String patient, String  id, Pageable pageRequest);

	// 患者咨询列表(已结束)
	@Query("select a from ConsultTeamDo a where a.patient=?1 and a.status=1 and a.del = '1' and a.type<>8")
	Page<ConsultTeamDo> findFinishedBypatient(String patient, Pageable pageRequest);

	@Query("select a from ConsultTeamDo a where a.patient=?1 and a.id = ?2 and a.status=1 and a.del = '1' and a.type<>8")
	Page<ConsultTeamDo> findFinishedBypatient(String patient, String id, Pageable pageRequest);

	// 患者咨询列表(已取消)
	@Query("select a from ConsultTeamDo a where a.patient=?1 and a.status=-1 and a.del = '1' and a.type<>8")
	Page<ConsultTeamDo> findCancelBypatient(String patient, Pageable pageRequest);

	@Query("select a from ConsultTeamDo a where a.patient=?1 and a.id =?2 and a.status=-1 and a.del = '1' and a.type<>8")
	Page<ConsultTeamDo> findCancelBypatient(String patient, String id, Pageable pageRequest);
	// 取消三师咨询
	@Modifying
	@Query("update ConsultTeamDo a set a.status = -1 where a.consult = ?1 and a.status <> 1")
	int cancel(String consult);
}
