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

import com.yihu.jw.entity.care.device.BaseSleepPlanDetail;
import com.yihu.jw.entity.hospital.message.SystemMessageDO;
import org.hibernate.annotations.SQLUpdate;
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;

/**
 * @author zmk
 * @vsrsion 1.0
 * Created at 2019/11/13
 */
public interface SystemMessageDao extends PagingAndSortingRepository<SystemMessageDO, String>, JpaSpecificationExecutor<SystemMessageDO> {


    @Query("from SystemMessageDO p where p.id = ?1")
    SystemMessageDO queryById(String id);

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

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

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

    @Modifying
    @Query("delete from SystemMessageDO a  where a.receiver = ?1 and a.relationCode = ?2 and a.type =?3")
    int orderMessageDelByType(String doctor, String orderId,String type);

    SystemMessageDO findByRelationCodeAndReceiver(String relationCode,String receiver);

    @Query("update SystemMessageDO a set a.isRead=0,a.over = '0' where a.id = ?1")
    @Modifying
    Integer setSpecialistById(String id);


    @Query("update SystemMessageDO a set a.isRead = 0 where a.id = ?1" )
    @Modifying
    Integer setSpecialistReadById(String id);


    @Query("update SystemMessageDO a set a.isRead = 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("select a from SystemMessageDO a where a.receiver=?1 and a.type=?2 and  a.del='1'")
    List<SystemMessageDO> getByReceiverAndType(String doctor,String type);

    @Query("select a from SystemMessageDO a where  a.type=?1 and  a.del='1'")
    Page<SystemMessageDO> getByType(String type, Pageable pageRequest);

    @Modifying
    @Query("update SystemMessageDO a set a.del=0 where a.id in (?1)")
    Integer delMessage(String[] ids);

    @Modifying
    @Query("DELETE FROM SystemMessageDO where type='43' and relationCode in(?1) ")
    void delMessageByRelationCode(List<String> relationCode);

}
