package org.jebot.repository.jebot;

import org.jebot.models.jebot.BotGroup;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.util.List;

@Component
public interface BotGroupRepository extends JpaRepository<BotGroup, Long> {

    //通过群组 ID 查询群组
    @Query("select g from bot_group g where g.groupId =:groupId")
    List<BotGroup> findByGroupId(@Param("groupId") Long groupId);

    //通过data ID 和数据类型查询群组
    BotGroup findBotGroupByDataIdAndDataType(@Param("dataId") Long dataId, @Param("dataType") String dataType);


    BotGroup findBotGroupByGroupIdAndDataType(@Param("groupId") Long groupId, @Param("dataType") String dataType);

    //通过群组 ID 和数据类型查询群组列表
    List<BotGroup> findAllByGroupIdAndDataType(@Param("groupId") Long groupId, @Param("dataType") String dataType);

    //通过dataId和数据类型删除群组
    @Modifying
    @Transactional
    @Query("delete from bot_group g where g.dataId =:dataId and g.dataType =:dataType")
    void deleteByDataIDAndDataType(@Param("dataId") Long dataId, @Param("dataType") String dataType);


    @Modifying
    @Transactional
    @Query("UPDATE bot_group g SET g.paymentWarningThreshold = :paymentWarningThresholdNew WHERE g.id = :id")
    void updatePaymentWarningThresholdByIdAndPaymentWarningThreshold(@Param("paymentWarningThresholdNew") double paymentWarningThresholdNew, @Param("id") Long id);

    @Modifying
    @Transactional
    @Query("UPDATE bot_group g SET g.ustdUrl = :ustdUrl WHERE g.id = :id")
    void updateUrlById(@Param("ustdUrl") String ustdUrl, @Param("id") Long id);

    @Modifying
    @Transactional
    @Query("UPDATE bot_group g SET g.agentWarningThreshold =:agentWarningThresholdNew WHERE g.id =:id ")
    void updateAgentWarningThresholdByIdAndAgentWarningThreshold(@Param("agentWarningThresholdNew") double agentWarningThresholdNew, @Param("id") Long id);

    @Query("SELECT g FROM bot_group g WHERE g.agentWarningThreshold >=0 or g.paymentWarningThreshold >=0")
    List<BotGroup> findAllByAgentWarningThresholdOrPaymentWarningThresholdGreaterThanZero();

}
