package com.ssj.dao.conch.conch.dao;

import com.ssj.bean.conch.conch.domain.ConchDerver;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/** 
 * @author  ZhangCaibao 
 * @version 2018年4月11日 下午3:34:38 
 * 类说明   小塾数据访问接口
 */
@Repository
public interface ConchDerverDao  extends JpaRepository<ConchDerver, String>{
    /**
     * 查询设备号
    * @param derver
    * @return
    * 创建人：ZhangCaibao 
    * 2018年4月12日 上午10:38:06 
    * @ConchDerver
     */
	public ConchDerver findByDerver(String derver);
	
	@Query(nativeQuery = true, value = "SELECT * from conch_derver where user_id = ?1 ")
	public ConchDerver findByUserId(String userId);
	
	public ConchDerver findByVipId(String vipId);
	
	
	 @Query(nativeQuery = true, value = "update conch_derver set vip_id =null , is_service =0 ,user_id =null  where derver = ?1 ")
	@Modifying
	public void updateByDerver(String derver);

	 @Query(nativeQuery = true, value = "update conch_derver set simcard =?2  where derver = ?1 ")
	 @Modifying
	 public void updateSimcardByDerver(String derver,String simcard);
	 @Transactional  
	 @Query(nativeQuery = true, value = "update conch_derver set  is_service =1  where id = ?1 ")
	 @Modifying
	 public void updateByIsService(String derverId);

	 public List<ConchDerver> findByDerverIn(List<String> dervers);
	 
	 public ConchDerver findByQrCode(String qrCode);

	/**
	 * 根据条件过滤查询，并且分页
	 * @param specification 过滤条件
	 * @param pageable 分页信息
	 */
	Page<ConchDerver> findAll(Specification<ConchDerver> specification, Pageable pageable);
	
	@Query(nativeQuery = true, value = "SELECT * from conch_derver t where t.imei=?1 LIMIT 1 ")
	public ConchDerver getConchDerverByImei(String imei);

	@Query(nativeQuery = true, value = "select * from conch_derver where created_date<=DATE_SUB(NOW(),INTERVAL 28 DAY)  " +
			"and user_id is null and type =1  " )
	public List<ConchDerver> findDerverList();

	public List<ConchDerver> findByImei(String imei);

	/**
	 * 根据sim卡号更新小塾sim
	 * @param simcard sim卡号
	 * @param sim sim
	 */
	@Modifying
	@Query(nativeQuery = true, value = "update conch_derver set sim = ?2 where simcard = ?1")
	int updateSimBySimcard(String simcard, String sim);
}
