package com.malk.guangming.repository.dao;

import com.malk.guangming.repository.entity.primary.DingTalkAppMappingPo;
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.transaction.annotation.Transactional;

import java.util.Optional;

public interface DingTalkAppMappingDao extends JpaRepository<DingTalkAppMappingPo, Long> {

    Optional<DingTalkAppMappingPo> findByAppKey(String appKey);

    Optional<DingTalkAppMappingPo> findFirstByAgentId(String agentId);

    Optional<DingTalkAppMappingPo> findFirstByRoleGroupIdIsNotNull();

    @Modifying
    @Transactional
    @Query("update DingTalkAppMappingPo p set p.roleGroupId = :groupId where p.roleGroupId is null")
    int updateAllRoleGroupId(@Param("groupId") String groupId);
}
