/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.yihu.wlyy.statistics.dao;

import com.yihu.wlyy.statistics.model.consult.ConsultTeam;
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.List;

public interface ConsultTeamDao extends PagingAndSortingRepository<ConsultTeam, Long>, JpaSpecificationExecutor<ConsultTeam> {
	// 根據consult查詢咨询记录
	ConsultTeam findByConsult(String consult);

	@Query("select a from ConsultTeam a where a.patient = ?1 and a.del = '1' and a.status = 0")
	List<ConsultTeam> findUnfinishedConsult(String patient);

	// 统计未完成的数量
	@Query("select count(1) from ConsultTeam a where a.patient = ?1 and a.status = 0 and a.del = '1'and a.type=?2")
	int countByPatient(String patient, Integer signType);

	// 医生关闭三师咨询
	@Modifying
	@Query("update ConsultTeam a set a.status = 1 where a.consult = ?1")
	int updateStatusByConsult(String consult);

	@Modifying
	@Query("update ConsultTeam a set a.doctorRead = 0 where a.consult = ?1")
	int updateReadedByConsult(String consult);

	/**
	 * 解约时结束家庭咨询
	 *
	 * @param patient
	 * @return
	 */
	@Modifying
	@Query("update ConsultTeam a set a.status = 1 where a.patient =?1 and a.type = 2")
	int updateStatusByPatient(String patient);

	// 取消三师咨询
	@Modifying
	@Query("update ConsultTeam a set a.status = -1 where a.consult = ?1 and a.status <> 1")
	int cancel(String consult);

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

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

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

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

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

	@Query("select a from ConsultTeam a where a.patient=?1 and a.id < ?2 and a.status=-1 and del = '1'")
	Page<ConsultTeam> findCancelBypatient(String patient, long id, Pageable pageRequest);

	// 医生未读数量+1
	@Modifying
	@Query("update ConsultTeam a set a.doctorRead = a.doctorRead + 1 where a.consult = ?1")
	int increaseDoctorRead(String consult);

	// 患者未读数量+1
	@Modifying
	@Query("update ConsultTeam a set a.patientRead = a.patientRead + 1 where a.consult = ?1")
	int increasePatientRead(String consult);

	// 清空医生未读数量
	@Modifying
	@Query("update ConsultTeam a set a.doctorRead = 0 where a.consult = ?1")
	int clearDoctorRead(String consult);

	// 清空患者未读数量
	@Modifying
	@Query("update ConsultTeam a set a.patientRead = 0 where a.consult = ?1")
	int clearPatientRead(String consult);

	// 更新患者评价标识
	@Modifying
	@Query("update ConsultTeam a set a.commentContent = ?2, a.commentStar = ?3 where a.consult = ?4")
	int updateComment(String commentContent, double commentStar, String consult);

	//找出当日的咨询量
	@Query("SELECT a FROM ConsultTeam a WHERE a.type=2 and unix_timestamp(a.czrq)>=unix_timestamp(?1) and unix_timestamp(a.czrq)<unix_timestamp(?2) ")
	List<ConsultTeam> findByCzrqyYesterday(String yesterday, String now);
	//查找
	@Query("SELECT a FROM ConsultTeam a WHERE a.patient=?1 and a.status=0 and a.del=1 and a.type=6")
	ConsultTeam findFamousConsultByPatient(String uid);
	@Query("select a from ConsultTeam a where  a.type=?3  and a.del = '1' and a.patient=?1 and a.doctor=?2 and  a.status=0 ")
	ConsultTeam findByParientCodeAndSignTypeAndDoctor(String patientCode, String doctor, int s);

    // 更新超过24小时未回复的项目，多加一分钟，防止过于频繁更新
    @Modifying
    @Query("UPDATE ConsultTeam a set a.status = -2 where TIMESTAMPDIFF(SECOND, a.czrq, CURRENT_TIME) > 87840 and status = 0")
    void updateUnresponsedConsultOver24Hours();

    // 最近24小时内未回复的项目
    @Query("FROM ConsultTeam a where TIMESTAMPDIFF(SECOND, a.czrq, CURRENT_TIME) < 86400 and a.status = 0 order by a.czrq desc")
    Page<ConsultTeam> getUnresponsedConsultIn24Hours(Pageable pageable);

	@Query("FROM ConsultTeam a where a.type=?1 and a.adminTeamCode is null")
	List<ConsultTeam> findByTypeAndAdminTeamIdIsNull(Integer s);
}
