package com.huimv.admin.repo.primary;

import com.huimv.admin.domain.primary.SysAccountEntity;
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 org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface SysAccountEntityRepository extends JpaRepository<SysAccountEntity, Integer>, JpaSpecificationExecutor<SysAccountEntity> {
    @Query(nativeQuery = true , value = "SELECT * FROM sys_account WHERE account_name=?1 AND password=?2")
    SysAccountEntity findByAccountName(String accountName, String password);

    @Transactional
    @Modifying(clearAutomatically = true, flushAutomatically = true)
    @Query(nativeQuery = true,value = "UPDATE sys_account SET account_status=?2 WHERE user_id=(SELECT user_id FROM sys_user WHERE id=?1)")
    int updateAccountStatus(Integer id, Integer status);

    @Transactional
    @Modifying(clearAutomatically = true, flushAutomatically = true)
    @Query(nativeQuery = true,value = "DELETE FROM sys_account WHERE user_id=?1")
    void deleteByUserId(Long userId);

    @Query(nativeQuery = true,value = "SELECT * FROM sys_account WHERE user_id=?1")
    List<SysAccountEntity> findByUserId(Long userId);
}