package com.yihu.wlyy.repository.deviece;

import com.yihu.wlyy.entity.device.PatientHealthTime;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

/**
 * Created by Reece on 2017/5/11.
 */
public interface PatientHealthTimeDao extends PagingAndSortingRepository<PatientHealthTime, Long> {
    //    根据居民和设备sn码查询自定义的血糖时间段
    @Query(value = "select t.* from wlyy_patient_health_time t where t.del = 1 and t.user = ?1 and t.device_sn = ?2 ", nativeQuery = true)
    PatientHealthTime findByUserAndSN(String user, String deviceSN);

    //    根据居民和设备sn码修改自定义的血糖时间段
    @Modifying
    @Query("update PatientHealthTime t set t.fastingStart = ?1,t.fastingEnd = ?2,t.afterBreakfastStart = ?3,t.afterBreakfastEnd = ?4," +
            "t.beforeLunchStart = ?5 ,t.beforeLunchEnd = ?6 ,t.afterLunchStart = ?7,t.afterLunchEnd = ?8 ,t.beforeDinnerStart = ?9,t.beforeDinnerEnd = ?10," +
            "t.afterDinnerStart = ?11,t.afterDinnerEnd = ?12,t.beforeSleepStart = ?13,t.beforeSleepEnd = ?14 where t.del = 1 and t.user = ?15 and t.deviceSN = ?16 ")
    int modifyByUserAndSN(String fastingStart, String fastingEnd, String afterBreakfastStart, String afterBreakfastEnd, String beforeLunchStart, String beforeLunchEnd,
                          String afterLunchStart, String afterLunchEnd, String beforeDinnerStart, String beforeDinnerEnd, String afterDinnerStart, String afterDinnerEnd,
                          String beforeSleepStart, String beforeSleepEnd, String user, String deviceSN);

    //    根据居民和设备sn码删除自定义的血糖时间段
    @Modifying
    @Query("update PatientHealthTime t set t.del = 0 where t.user = ?1 and t.deviceSN = ?2 ")
    int deleteByUserAndSN(String user, String deviceSN);

    //    更换个人sv码
    @Modifying
    @Query("update PatientHealthTime t set t.deviceSN = ?3 where t.user = ?1 and t.deviceSN = ?2 ")
    int updateDeviceSN(String user, String deviceSN,String newDeviceSN );

}
