package com.its.op.dao.repository.its.oper;

import com.its.op.entity.its.oper.TbUserCnncHs;
import com.its.op.entity.its.oper.TbUserCnncHsKey;
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.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface TbUserCnncHsRepository extends JpaRepository<TbUserCnncHs, TbUserCnncHsKey>, JpaSpecificationExecutor<TbUserCnncHs> {

    @Query("select p from TbUserCnncHs p")
    List<TbUserCnncHs> findAll();

/*
    @Query("select new com.its.api.op.model.dto.oper.TbUserCnncHsDto(p.loginHms, p.userId, p.logoutHms) from TbUserCnncHs p where p.loginHms between :fromDt and :toDt")
    List<TbUserCnncHsDto> findAllByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt);
*/
    @Query("select p from TbUserCnncHs p where p.loginHms between :fromDt and :toDt")
    List<TbUserCnncHs> findAllByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

    @Query("select p from TbUserCnncHs p where p.loginHms between :fromDt and :toDt")
    List<TbUserCnncHs> findAllByDateRangeEntity(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

    @Transactional
    @Modifying
    @Query(value = "insert into tb_usercnnc_hs(login_hms, user_id, logout_hms) values(:loginHms, :userId, :logoutHms)", nativeQuery = true)
    Integer insertData(@Param("loginHms") String loginHms, @Param("userId") String userId, @Param("logoutHms") String logoutHms);

    @Transactional
    @Modifying
    @Query(value =
            "update tb_usercnnc_hs set logout_hms = :logoutHms " +
            " where login_hms = :loginHms and user_id = :userId",
            nativeQuery = true)
    Integer updateData(@Param("loginHms") String loginHms, @Param("userId") String userId, @Param("logoutHms") String logoutHms);
}
