package kr.iotdoor.boot.mapper;

import kr.iotdoor.boot.model.user.UserAccount;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserMapper {

    @Select("""
        SELECT
            USER_IDNTF_NO,
            USER_ID,
            PSWD,
            USER_NM,
            EML_ADDR,
            USER_STTS_CD,
            USER_TYPE_CD,
            JOIN_DT,
            INDVDL_INFO_AGRE_DT,
            LOCK_YN,
            LGN_FAIL_NMTM,
            LOCK_DT,
            LAST_LGN_DT
        FROM TN_USER
        WHERE USER_ID = #{userId}
        """)
    UserAccount selectByUserId(@Param("userId") String userId);

    @Select("""
        SELECT COUNT(*)
        FROM TN_USER
        WHERE USER_ID = #{userId}
        """)
    int countByUserId(@Param("userId") String userId);

    @Insert("""
        INSERT INTO TN_USER (
            USER_IDNTF_NO,
            USER_ID,
            PSWD,
            USER_NM,
            EML_ADDR,
            USER_STTS_CD,
            USER_TYPE_CD,
            JOIN_DT,
            INDVDL_INFO_AGRE_DT,
            LOCK_YN,
            LGN_FAIL_NMTM,
            LOCK_DT,
            LAST_LGN_DT
        ) VALUES (
            #{userIdntfNo},
            #{userId},
            #{pswd},
            #{userNm},
            #{emlAddr},
            #{userSttsCd},
            #{userTypeCd},
            #{joinDt},
            #{indvdlInfoAgreDt},
            #{lockYn},
            #{lgnFailNmtm},
            #{lockDt},
            #{lastLgnDt}
        )
        """)
    int insertUser(UserAccount userAccount);

    @Update("""
        UPDATE TN_USER
        SET LAST_LGN_DT = CURRENT_TIMESTAMP
        WHERE USER_IDNTF_NO = #{userIdntfNo}
        """)
    int updateLastLoginDt(@Param("userIdntfNo") String userIdntfNo);
}
