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

import com.yihu.wlyy.entity.message.Message;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
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 MessageDao extends PagingAndSortingRepository<Message, Long>, JpaSpecificationExecutor<Message> {

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

    @Query("select a from Message a where a.type =1 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);

    @Query("SELECT a FROM Message a WHERE a.sender=?1 AND a.receiver = ?2 and a.signStatus = ?3 and a.read=1")
    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'")
    Message findByPatient(String patient,String doctor);

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

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

    @Query("select a from Message a where a.receiver = ?1 and a.type not in (1,2)")
    List<Message> getSystemMessage(String doctor,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);

    @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.type not in (1,2)")
    @Modifying
    int setSysMessageReaded(String doctor);
}
