package com.yihu.jw.hospital.prescription.dao;

import com.yihu.jw.entity.hospital.prescription.WlyyOutpatientDO;
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 org.springframework.transaction.annotation.Transactional;

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

/**
 * Created by Trick on 2019/6/3.
 */
public interface OutpatientDao extends PagingAndSortingRepository<WlyyOutpatientDO, String>, JpaSpecificationExecutor<WlyyOutpatientDO> {

    @Query("from WlyyOutpatientDO a where a.patient = ?1 and a.outpatientType <> '3' and a.outpatientType <> '4' and a.status in(0,1,2)")
    List<WlyyOutpatientDO> findByPatientList(String patient);
    @Query("from WlyyOutpatientDO a where a.patient = ?1 and  a.outpatientType = '3' and a.status in(0,1,2)")
    List<WlyyOutpatientDO> findByPatientAndDoctorList(String patient);

    @Query("from WlyyOutpatientDO a where a.patient = ?1 and a.outpatientType <> '3' and a.status in(3) order by a.createTime desc ")
    List<WlyyOutpatientDO> findByPatientListByStatus(String patient);
    @Query("from WlyyOutpatientDO a where a.patient = ?1 and a.outpatientType = ?2  and a.status in(3) order by a.createTime desc ")
    List<WlyyOutpatientDO> findByPatientListByStatusAndType(String patient,String outpatientType);

    @Query("from WlyyOutpatientDO a where a.patient = ?1 and a.hisStatus=1 and a.createTime>=?2 and a.createTime<=?3")
    List<WlyyOutpatientDO> findByPatientListNoStatus(String patient,Date startDate,Date endDate);

    List<WlyyOutpatientDO> findByDoctorAndStatus(String doctor,String status);

    @Query("from WlyyOutpatientDO a where a.doctor = ?1 and a.status in(2,3)")
    List<WlyyOutpatientDO> findByDoctorList(String doctor);

    @Query("from WlyyOutpatientDO a where a.registerNo = ?1 ")
    List<WlyyOutpatientDO> findByRegisterNoList(String registerNo);

    @Query("from WlyyOutpatientDO a where a.realOrder = ?1 ")
    WlyyOutpatientDO findByRealOrder(String realOrder);

    Integer countByDoctorAndStatusAndOutpatientType(String doctor,String status,String outpatientType);

    List<WlyyOutpatientDO> findByDoctor(String doctor);

    WlyyOutpatientDO findById(String id);
    List<WlyyOutpatientDO> findByAdmNo(String admNo);

    @Query("from WlyyOutpatientDO a where a.generalDoctor = ?1 and a.registerDate >=?2 and a.registerDate <=?3 and a.doctor is not null order by registerDate ASC")
    List<WlyyOutpatientDO> findByGeneralDoctor(String generalDoctor, Date startDate,Date endDate);

    @Query("from WlyyOutpatientDO a where a.generalDoctor = ?1 and a.registerDate >=?2 and a.registerDate <=?3 and a.status = ?4 and a.doctor is not null order by registerDate ASC")
    List<WlyyOutpatientDO> findByGeneralDoctor(String generalDoctor, Date startDate,Date endDate,String status);

    @Query("from WlyyOutpatientDO a where a.generalDoctor = ?1 and a.registerDate >=?2 and a.registerDate <=?3 and a.status in(?4) and a.doctor is not null order by registerDate ASC")
    List<WlyyOutpatientDO> findByGeneralDoctorAndStatusAndDate(String generalDoctor, Date startDate,Date endDate,String status);

    @Query("from WlyyOutpatientDO a where a.generalDoctor = ?1 and a.status in(?2) and a.doctor is not null order by registerDate ASC")
    List<WlyyOutpatientDO> findByGeneralDoctorAndStatus(String generalDoctor,String status);


    @Query("from WlyyOutpatientDO a where a.status in(1,2) and a.payStatus = 1")
    List<WlyyOutpatientDO> findByStatus();

    @Query("from WlyyOutpatientDO a where a.status in(1,2) and a.payStatus = 1 and a.outpatientType=?1 and a.registerDate <=?2")
    List<WlyyOutpatientDO> findOutpatientListByOutpatientType(String outpatientType,Date endDate);

    @Query("from WlyyOutpatientDO a where a.status in(1,2) and a.payStatus = 1 and a.createTime<'2020-08-31 23:59:59'")
    List<WlyyOutpatientDO> findPreviousByStatus();
    List<WlyyOutpatientDO> findByDoctorAndCreateTimeAndPatientCancelRemark(String doctor,Date createTime,String patientCancelRemark);

    @Query("select count(id) from  WlyyOutpatientDO a where a.outpatientType = ?1 and a.doctor = ?2 and a.admDate >=?3 and a.admDate <=?4")
    Integer countByTime(String type,String doctor,Date startTime,Date endTime);
    @Query("from WlyyOutpatientDO a where a.status = 0 and a.payStatus = 1 and (a.remindCount<?1 or a.remindCount is null)")
    List<WlyyOutpatientDO> findWaitingOutpatient(Integer remindCount);
    @Query("from WlyyOutpatientDO a where a.status = 0")
    List<WlyyOutpatientDO> findAllWaitingOutpatient();

    /*@Query("from WlyyOutpatientDO a where a.patient = ?1 and a.outpatientType = 1")
    List<WlyyOutpatientDO> findOutpatientByPatientAndStatus(String patient);*/

    @Transactional
    @Modifying
    @Query("update WlyyOutpatientDO p set p.hisStatus=?2 where p.id=?1")
    void updateHisStatusById(String id,Integer hisStatus);

    @Transactional
    @Modifying
    @Query("update WlyyOutpatientDO p set p.channelId=?2 where p.id=?1")
    void updateChannelId(String id,String channelId);

    @Query("from WlyyOutpatientDO a where a.consumer = ?1 and a.status=1 and a.payStatus =1 order by a.createTime desc")
    List<WlyyOutpatientDO> findWlyyOutpatientDO(String patient);

    @Query("from WlyyOutpatientDO a where a.status=0 and a.payStatus =1 order by a.createTime desc")
    List<WlyyOutpatientDO> findDoctorRemindMessage();

    @Query("from WlyyOutpatientDO a where a.status = 0 and a.payStatus =1 and a.doctor=?1 and a.outpatientType =?2 and a.patient=?3")
    List<WlyyOutpatientDO> findByDoctorAndOutpatientType(String doctor,String outpatientType,String patient);


}
