package com.izouma.nineth.repo;

import com.izouma.nineth.domain.AppVersion;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;

public interface AppVersionRepo extends JpaRepository<AppVersion, Long>, JpaSpecificationExecutor<AppVersion> {
    /**
     * 查找是否需要强制更新
     * @param platForm 渠道
     * @param appVersion APP版本
     * @return
     */
    @Query(value = "select id from app_version where os = ?1 and update_type ='STRONG_UPDATE' and version > ?2 and start_update = 1  and del = 0 order by version desc limit 1",nativeQuery = true)
    Long strongUpdateVersion(String platForm, String appVersion);

    @Query(value = "select id from app_version where os = ?1 and update_type != 'HOTSWAP' and start_update = 1  and del = 0 order by version desc limit 1",nativeQuery = true)
    Long lastUpdateVersion(String platForm);


    @Query(value = "select id from app_version where os = ?1 and version > ?2 and start_update = 1  and del = 0 order by version desc limit 1",nativeQuery = true)
    Long hasNewVersion(String platForm, String appVersion);

    @Query(value = "select id from app_version where os = ?1 and start_update = 1  and del = 0 order by version desc limit 1",nativeQuery = true)
    Long lastVersion(String platForm);

    @Query("update AppVersion t set t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDelete(Long id);
}
