package com.ywt.outpatient.domain.entity.center;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface TreatmentRepository extends JpaRepository<Treatment, Integer> {
    @Query(value = "select * from treatment where doctor_id = ? and patient_id = ? and is_deleted = 0 order by create_time desc limit 1", nativeQuery = true)
    Treatment getLastTreatment(int doctorId, int patientId);

    @Query(value = "select * from treatment where treatment_status & 128 <> 128 and treatment_status & 32 = 32 and is_deleted = 0 order by create_time desc;", nativeQuery = true)
    List<Treatment> getTreatmentList();

    @Modifying
    @Transactional
    @Query(value = "update treatment set order_id = ?1, order_no = ?2 where id = ?3", nativeQuery = true)
    void updateOrderIdAndOrderNoById(int orderId, String orderNo, int id);

    Treatment getByOrderId(int orderId);

    /**
     * 查看某个患者当前有没有正在进行的远程会诊订单
     *
     * @param doctorId
     * @param patientId
     * @return
     */
    @Query(value = "select count(1) from treatment where doctor_id = ? and patient_id = ? and is_deleted = 0 and treatment_status in (1, 2, 3);", nativeQuery = true)
    int countOngoingTreatment(int doctorId, int patientId);


    /**
     * 根据ID查找记录数量
     *
     * @param userId
     * @return
     */
    @Query(value = "SELECT count(1) FROM treatment WHERE  user_id = ? and is_deleted = 0;",nativeQuery = true)
    int getCountByUserId(int userId);

    /**
     * 根据ID查找记录
     *
     * @param userId
     * @return
     */
    @Query(value = "SELECT * FROM treatment WHERE  user_id = ? and is_deleted = 0 order by id desc limit ?",nativeQuery = true)
    List<Treatment> getTreatmentListByUserId(int userId, int count);


}
