package cn.ysk.cashier.repository;

import cn.ysk.cashier.pojo.TbVersion;
import org.apache.ibatis.annotations.Param;
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;

/**
 * @author ww
 * @website https://eladmin.vip
 * @date 2024-03-29
 **/
public interface TbVersionRepository extends JpaRepository<TbVersion, Integer>, JpaSpecificationExecutor<TbVersion> {

    @Query("SELECT count(1) FROM TbVersion WHERE source = :source AND type =:type AND version =:version")
    int isExist(@Param("source") String source, @Param("type") String type, @Param("version") String version);

    @Query("SELECT count(1) FROM TbVersion WHERE source = :source AND version =:version")
    int isExist(@Param("source") String source, @Param("version") String version);

    @Modifying
    @Query("update TbVersion version set version.sel=CASE" +
            " WHEN version.source=:source AND version.id=:id THEN 1 " +
            " WHEN version.source=:source AND version.id!=:id THEN 0 " +
            " ELSE version.sel END " +
            " WHERE version.source = :source")
    int updateSelBySource(@Param("source") String source, @Param("id") Integer id);
}
