package com.yihu.jw.base.dao.device;

import com.yihu.jw.entity.care.device.DevicePatientDevice;
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 PatientDeviceDao extends PagingAndSortingRepository<DevicePatientDevice, Long>, JpaSpecificationExecutor<DevicePatientDevice> {

    @Query("select a from DevicePatientDevice a where a.user = ?1 ")
    Iterable<DevicePatientDevice> findByUser(String user);

    @Query("select a from DevicePatientDevice a where a.del = 0 ")
    List<DevicePatientDevice> findByAll();

    @Query("select a from DevicePatientDevice a where a.user = ?1 ")
    List<DevicePatientDevice> findByPatient(String user);

    @Query("select a from DevicePatientDevice a where a.user = ?1 and a.del=0 and a.deviceName like '%药盒%'")
    List<DevicePatientDevice> findByPatientAndDel(String user);

    @Query("select a from DevicePatientDevice a where a.user = ?1 and a.deviceSn=?2 ")
    List<DevicePatientDevice> findByPatientAndDeviceSn(String user, String deviceSn);

    List<DevicePatientDevice> findByUser(String user, Pageable pageRequest);

    List<DevicePatientDevice> findByUserAndDoctor(String user, String doctor, Pageable pageRequest);

    List<DevicePatientDevice> findByDeviceSnAndCategoryCode(String deviceSn, String categoryCode);

    DevicePatientDevice findByDeviceSnAndCategoryCodeAndUserType(String deviceSn, String categoryCode, String userType);

    DevicePatientDevice findByDeviceIdAndDeviceSnAndUserType(Long deviceId, String deviceSn, String userType);

    DevicePatientDevice findByDeviceSnAndUserType(String deviceSn, String userType);

    @Query("select pd from DevicePatientDevice pd where pd.deviceSn = ?1 and pd.del=0")
    List<DevicePatientDevice> findByDeviceSn(String deviceSn);

    //更换患者绑定的血糖仪
    @Modifying
    @Query("update DevicePatientDevice t set t.deviceSn = ?3 , t.userType = ?4,t.sim=?5,t.hospital=?6,t.hospitalName=?7,t.checkDoctorName=?8 where t.user = ?1 and t.deviceSn = ?2 ")
    int updatePatientDevice(String user, String deviceSN, String newDeviceSN, String userType, String sim, String hospital, String hospitalName, String checkDoctorName);

    //管理员解绑居民设备
    @Modifying
    @Query("update DevicePatientDevice t set t.del = 1  where t.deviceSn = ?1 and t.del =0  ")
    int updatePatientDevice(String deviceSN);

    @Query("select a from DevicePatientDevice a")
    List<DevicePatientDevice> findAll();

    @Query("select a from DevicePatientDevice a group by a.user")
    List<DevicePatientDevice> findAllGroupUser();

    @Query("select a from DevicePatientDevice a where a.categoryCode = ?1 and a.del=0 group by a.user")
    List<DevicePatientDevice> findAllByCategoryCode(String categoryCode);

    @Modifying
    @Query("delete DevicePatientDevice a where a.deviceSn = ?1")
    int deleteByDeviceSn(String deviceSN);

    @Query("select a from DevicePatientDevice a where a.user=?1 and a.del=0")
    List<DevicePatientDevice> findAllByUser(String user);

    @Query("select a from DevicePatientDevice a where a.user=?1 and a.categoryCode =?2 and a.del=0")
    List<DevicePatientDevice> findAllByUserAndCategoryCode(String user,String categoryCode);

    @Query("select a from DevicePatientDevice a where a.user=?1 and a.categoryCode =?2 and a.del=0")
    List<DevicePatientDevice> findByUserAndCategoryCode(String user,String categoryCode);
}
