/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.yihu.jw.sign.dao;


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

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

public interface MessagesDao extends PagingAndSortingRepository<Message, Long>, JpaSpecificationExecutor<Message> {

    @Query("select count(1) from Message a where a.type in(1,101) and a.read= 1 and a.receiver=?1 ")
    Integer amountUnreadByReceiver(String doctor);

    @Query("select a from Message a where a.type in(1,101) and a.read= 1 and a.receiver=?1 order by a.czrq desc")
    Page<Message> amountUnreadLastByReceiver(String doctor, Pageable pageRequest);

    @Query("select count(1) from Message a where a.type =2 and a.read= 1 and a.receiver=?1 ")
    Integer amountUnreadHealthByReceiver(String doctor);

    @Query("select a from Message a where a.type =2 and a.read= 1 and a.receiver=?1 order by a.czrq desc")
    List<Message> amountUnreadHealthLastByReceiver(String doctor, Pageable pageRequest);

    @Modifying
    @Query("update Message a set a.read = 0,a.over='0' where a.id = ?1")
    int read(long msgid);

    @Modifying
    @Query("update Message a set a.read = 0 where a.id = ?1")
    int updateReadById(long msgid);

    @Modifying
    @Query("update Message a set a.read = 0,a.over='1' where a.code = ?1")
    int read(String msgCode);

    @Query("SELECT a FROM Message a WHERE a.sender=?1 AND a.receiver = ?2 and a.signStatus = ?3 and a.read=1 ORDER BY czrq DESC")
    List<Message> findUnreadSign(String sender, String receiver, String signStatus);

    @Query(" from Message a where a.read = 1 and over ='1' and a.sender = ?1 and a.receiver=?2 and a.signStatus='1'")
    List<Message> findByPatient(String patient, String doctor);

    @Query(" from Message a where a.read = 1 and over ='1' and a.sender = ?1 and a.receiver=?2 ")
    List<Message> findByPatientRenew(String patient, String doctor);

    @Query("select a from Message a where a.receiver = ?1 and a.sender=?2 and a.tzType=?3  and a.over=1 order by a.createTime desc")
    List<Message> getHealthIndexMessageByPatient(String doctor, String patient, String type, Pageable pageRequest);

    //查询医生下的签约居民未读体征异常消息
    @Query("SELECT a FROM Message a WHERE a.sender=?1 AND a.receiver = ?2 AND a.type = 2 and a.del = '1' and a.read=1 ORDER BY czrq DESC")
    List<Message> findUnreadHealthIndex(String sender, String receiver);

    @Modifying
    @Query("update Message a set a.read = 0 where a.receiver = ?1 and a.sender=?2 and a.tzType=?3")
    int updateHealthIndexMessageByPatient(String doctor, String patient, String type);

    @Query("select a from Message a where a.read= 1 and a.receiver = ?1 and a.type not in (1,2,6,7,12,101,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,32,33,301,401,402,403,407,408,409,430,431,432,434,435) and (a.over = 1 or a.over is null) order by a.czrq desc")
    List<Message> getSystemMessageUnread(String doctor);

    @Query("select a from Message a where a.receiver = ?1 and a.prescriptionStatus=?2 and a.type in (6,7,33) order by a.createTime desc")
    List<Message> getSysTemMessageByPrescription(String doctor, String prescriptionStatus);

    @Query("select a from Message a where a.receiver = ?1 and a.prescriptionStatus=?2 and a.type =31 order by a.createTime desc")
    List<Message> getSysTemMessageByExamination(String doctor, String prescriptionStatus);

    @Query("select a from Message a where a.receiver = ?1 and a.type not in (1,2,6,7,12,101,14,15,301,16,17,18,19,20,21,22,23,24,25,26,27,28,29,32,33,401,402,403,407,408,409,430,431,432,434,435) and (a.del = '1' or a.del is null) and (a.over = 1 or a.over is null) ")
    List<Message> getSystemMessage(String doctor, Pageable pageRequest);

    @Query("select a from Message a where a.receiver = ?1 and ((a.type=?2 and a.prescriptionStatus='0') or a.type = 33 )")
    List<Message> getPrescriptionMessage(String doctor, Integer type, Pageable pageRequest);

    @Query("select count(a) from Message a where a.read = 0 and a.over ='0'  and a.receiver=?1 and a.type=?2 ")
    int findMessageNum(String doctor, Integer type);

    @Modifying
    @Query("update Message a set a.prescriptionStatus=?2 where a.relationCode = ?1 and a.type=?3 ")
    int updatePreScriptionMessage(String relationCode, String prescriptionStatus, Integer type);

    @Query("update Message a set a.read = 0,a.over = '0' where a.relationCode = ?1 and a.type = ?2")
    @Modifying
    @Transactional
    int updateMessageByRelationCode(String relationCode, Integer type);

    @Query("update Message a set a.read = 0,a.over = '0' where a.receiver = ?1 and a.type = ?2")
    @Modifying
    int setMessageReaded(String doctor, Integer type);

    @Query("update Message a set a.read = 0,a.over = '0' where a.receiver = ?1 and a.id = ?2")
    @Modifying
    int setMessageReadedById(String doctor, Long id);

    @Query("update Message a set a.read = 0 where a.receiver = ?1 and a.id = ?2")
    @Modifying
    int setMessageById(String doctor, Long id);

    @Query("update Message a set a.read = 0,a.over = '0' where a.receiver = ?1 and a.type not in (1,2,6,7)")
    @Modifying
    int setSysMessageReaded(String doctor);

    @Query("from Message a where a.receiver = ?1 and relationCode = ?2 and a.type=?3 ")
    List<Message> findByReceiverAndRelationCode(String receiver, String relationCode, Integer type);

    //查询患者最近7天的体征异常记录
    @Query(value = "SELECT COUNT(1) FROM wlyy_message WHERE type = '2' and sender = ?1 and create_time>=date_add(now(),interval -7 day) ", nativeQuery = true)
    int findTzMessage(String patient);

    //根据续方关联code查询团队长分配健管师消息
    @Query("from Message a where a.type = 8 and a.state=0 and a.del='1' and a.over='0' and relationCode = ?1  ")
    Message findByRelationCode(String relationCode);

    @Query("from Message a where a.type IN(24,25,26,27,12) and a.del='1' and a.over='1' and receiver = ?1  order by a.createTime desc")
    List<Message> findByReceiverCallService(String doctor);

    @Query("select a from Message a where a.receiver = ?1 and a.type=?2 and a.over='1' and a.read=1 order by a.createTime desc")
    List<Message> getMessageByType(String doctor, Integer type, Pageable pageRequest);

    @Query("from Message a where a.type = 12 and a.del='1' and a.over='1' and relationCode = ?1  ")
    List<Message> findByCallServiceRelationCode(String relationCode);

    @Query(" select a from Message a where a.type IN (15,30) and a.del='1' and a.read=1 and a.receiver=?1 and a.over='1' order by a.createTime desc ")
    List<Message> findEduArticle1(String receiver);

    @Query(" select a from Message a where a.type=?2 and a.del='1' and a.read=1 and a.receiver=?1 and a.over='1' order by a.createTime desc ")
    List<Message> findEduArticle(String receiver, Integer type);

    @Query(" select a from Message a where a.type=?2 and a.del='1'  and a.receiver=?1 ")
    List<Message> findEduArticle(String receiver, Integer type, Pageable pageRequest);

    @Query(" select a from Message a where a.type IN (15,30)and a.del='1'  and a.receiver=?1 ")
    List<Message> findEduArticle1(String receiver, Pageable pageRequest);

    @Modifying
    @Query("delete from Message a   where a.receiver = ?1 and a.data = ?2 and a.type in (14,15,30) ")
    int setMessageDel(String doctor, String articleCode);

    @Modifying
    @Query("delete from Message a   where  a.data = ?1 and a.type in (14,15,30) ")
    int setMessageDel1(String articleCode);

    @Modifying
    @Query("delete from Message a   where a.receiver = ?1 and a.relationCode = ?2 and a.type in (14,15,30) ")
    int setMessageDel2(String doctor, String refinementCode);

    @Modifying
    @Query("delete from Message a   where a.receiver = ?1 and a.relationCode = ?2 and a.type ='407'")
    int orderMessageDel(String doctor, String orderId);

    @Query(" select a from Message a where a.type=?2 and a.del='1' and a.read=1 and a.receiver=?1 and a.over='1' order by a.createTime desc ")
    List<Message> findUnReadByReceiverAndType(String receiver, Integer type);

    @Query("select a from Message a where a.read = 1 and a.receiver = ?1 and a.state = 1 and a.over = ?2 and a.type in (16,20,22,23) order by a.czrq desc")
    List<Message> getSpecialistApply(String receiver, String over);

    @Query("select a from Message a where a.receiver = ?1 and a.state = 1 and a.type = 17 order by a.czrq desc")
    List<Message> getFamilyTransfer(String receiver);

    @Query("select a from Message a where a.read = 1 and a.receiver = ?1 and a.state = 1 and a.type = 18 order by a.czrq desc")
    List<Message> getFamilySign(String receiver);

    @Query("select a from Message a where a.receiver = ?1 and a.state = 1 and a.type in(18,19,20,21,22,23,29)")
    List<Message> selectFamilySign(String receiver, Pageable pageableRequest);

    @Query("select a from Message a where a.read= ?2 and a.receiver = ?1 and a.state = 1 and a.over = ?3 and a.type = 17 order by a.czrq desc")
    List<Message> getFamilyTransferReadAndOver(String receiver, Integer read, String over);

    @Query("select a from Message a where a.receiver = ?1 and a.type=31 and a.read= ?2 and a.over = ?3 and a.prescriptionStatus='0' ")
    List<Message> getExaminationMessage(String doctor, Integer read, String over);

    @Query("select a from Message a where a.receiver = ?1 and a.state = 1 and a.over = ?2 and a.type in (16,20,22,23) order by a.czrq desc")
    List<Message> getSpecialistUntreated(String receiver, String over, Pageable pageable);

    @Query("select a from Message a where  a.receiver = ?1 and a.state = 1 and a.over = ?2 and a.type in (19,21,29) order by a.czrq desc")
    List<Message> getSpecialistMessages(String receiver, String over, Pageable pageable);

    @Query("select a from Message a where a.read= ?2 and a.receiver = ?1 and a.state = 1 and a.over = ?3 and a.type in (17) order by a.czrq desc")
    List<Message> getTransferMessage(String receiver, Integer read, String over, Pageable pageable);

    @Query("select a from Message a where a.code = ?1")
    Message findByCode(String code);

    @Query("select a from Message a where a.relationCode = ?1 and a.receiver = ?2 and a.type = ?3")
    Message findOldMessage(String orderId, String receiver, int type);

    @Query("select a from Message a where a.read = 1 and a.receiver = ?1 and a.state = 1 and a.type in (19,21,29) order by a.czrq desc")
    List<Message> getSpecialistUnRead(String receiver);

    @Query("select a from Message a where a.read = 1 and a.receiver = ?1 and a.state = 1 and a.type in (18,19,20,21,22,23,29) order by a.czrq desc")
    List<Message> getSpecialistUnRead2(String receiver);


    @Query("update Message a set a.read=0,a.over = '0' where a.id = ?1")
    @Modifying
    int setSpecialistById(Long id);

    @Query("update Message a set a.read = 0 where a.id = ?1")
    @Modifying
    int setSpecialistReadById(Long id);

    @Query("select a from Message a where a.read = 1 and a.receiver = ?1 and a.state = 1 and a.type in (24,25,26,27,12,32) order by a.czrq desc")
    List<Message> getSynergyService(String receiver);


    @Query("select a from Message a where  a.receiver = ?1 and a.state = 1 and a.type in (12,32,24,25,26,27) order by a.czrq desc")
    List<Message> getSynergyMessages(String receiver, Pageable pageable);

    @Query("update Message a set a.read = 0 where a.receiver = ?1 and a.type in (12,32,24,25,26,27)")
    @Modifying
    int setSynergyMessagesByRead(String doctor);

    @Query("select a from Message a where  a.receiver = ?1 and a.type = 28  and a.state = 1 and a.del = '1' and a.createTime > ?2 order by a.czrq desc")
    List<Message> findBirthdayReminder(String doctor, Date now);

    @Query("select a from Message a where  a.receiver = ?1 and a.over = 1 and a.type in (17) order by a.czrq desc")
    List<Message> getTransferMessageByPCIM(String receiver, Pageable pageable);

    @Query("select a from Message a where  a.receiver = ?1 and a.over = 1 and a.type in(?2)  order by a.czrq desc")
    List<Message> getPCIMFamilyServer(String receiver, String types, Pageable pageable);

    @Query(" select a from Message a where a.type IN (401,402) and a.del='1' and a.read=1 and a.receiver=?1 and a.over='1' order by a.createTime desc ")
    List<Message> findDoorOrder(String receiver);

    @Query(" select a from Message a where a.type IN (408,407,403,409) and a.del='1' and a.receiver=?1 and a.over='1' order by a.createTime desc ")
    List<Message> findDoorByOrder(String receiver);

    @Query("select a from Message a where  a.receiver = ?1 and a.state = 1 and a.over = ?2 and a.type in (?3) order by a.czrq desc")
    List<Message> findWaitingMessages(String receiver, String over, List<Integer> typeList, Pageable pageable);

    //获取资质申请审核有效的未结束已发送的消息
    @Query("select a from Message a where a.type in (401,408) and a.del='1' and a.over='1' and a.state=1  and relationCode = ?1  ")
    List<Message> findMessageByRelationCode(String relationCode);

/*    //获取资质申请审核有效的未结束已发送的消息
    @Query("select a from Message a where a.type = 401 and a.del='1' and a.over='1' and a.state=1  and a.relationCode = ?1 ")
    List<Message> findMessageByRelationCodeAndReceiver(String relationCode);*/

    List<Message> queryByRelationCodeAndTypeIn(String relationCode, Integer[] type);

    @Query("select a from Message a where a.receiver = ?1 and a.type=?2 and  a.over='1' order by a.createTime desc")
    List<Message> getMessageByReceiverByType(String receiver, Integer type, Pageable pageRequest);

    @Query("update Message a set a.read = 0,a.over = '0' where a.receiver = ?1 and a.type = ?2 and (a.createTime > ?3 and a.createTime< ?4)")
    @Modifying
    void setMessageOverByType(String doctor, Integer type, Date start, Date end);

    @Query("update Message a set a.read = 0,a.over = '0' where a.receiver = ?1 and a.relationCode = ?2 and a.type=?3 ")
    @Modifying
    void updateByReceiverAndRelationCode(String receiver, String relationCode, Integer type);
}
