package com.yihu.jw.message.dao;

import com.yihu.jw.entity.base.message.BaseMessageDO;
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;

/**
 * @author liuwenbin on 2018/11/28.
 */
public interface MessageDao extends PagingAndSortingRepository<BaseMessageDO, String>, JpaSpecificationExecutor<BaseMessageDO> {

    @Query("select m from BaseMessageDO m where m.platform=?1 and m.receiver=?2  and m.del=1 and m.msgTypeCode like ?3  order by m.readState desc")
    List<BaseMessageDO> messageList(Integer platform, String code,String msgTypeCode);

    @Query("select count(m.id) from BaseMessageDO m where m.platform=?1 and m.receiver=?2  and m.del=1 and m.msgTypeCode like ?3  and m.readState=0")
    Integer countMessage(Integer platform, String code,String msgTypeCode);

    BaseMessageDO findById(Integer id);

    @Modifying
    @Query("update BaseMessageDO t set t.readState = ?1 where t.id=?2 ")
    int updateStatusById(Integer readState,Integer id);

    @Query("select m from BaseMessageDO m where m.receiver=?1 and m.sender=?2 and m.msgTypeCode like ?3 and m.platform=?4 and m.del='1'")
    List<BaseMessageDO> getAllByPatient(String receiver,String sender,String msgTypeCode,Integer platform,Pageable pageRequest);

    @Query("select m from BaseMessageDO m where m.receiver=?1  and m.msgTypeCode like ?2 and m.platform=?3 and m.del='1'")
    List<BaseMessageDO> getAllByPatient(String receiver,String msgTypeCode,Integer platform,Pageable pageRequest);

}
