package incheon.ags.mrb.analysis.mapper;

import incheon.ags.mrb.analysis.vo.AnlsHstryDtlVO;
import incheon.ags.mrb.analysis.vo.AnlsHstryJoinVO;
import incheon.ags.mrb.analysis.vo.AnlsHstryVO;
import incheon.com.config.annotation.MainDB;
import org.egovframe.rte.psl.dataaccess.mapper.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;
/**
 * 공간 분석 이력 매퍼 인터페이스
 */
@org.egovframe.rte.psl.dataaccess.mapper.Mapper @incheon.com.config.annotation.MainDB
public interface AnalysisHistoryMapper {

    /**
     * 분석 이력 단건 조회
     */
    AnlsHstryVO selectHstryById(@Param("anlsHstryId") int anlsHstryId);

    /**
     * 사용자 분석 이력 리스트 조회
     */
    List<AnlsHstryVO> selectMyHstry(
            @Param("userId") String userId,
            @Param("offset") int offset,
            @Param("limit") int limit
    );
    
    /**
     * 전체 분석 이력 조회 (페이징)
     */
    List<AnlsHstryVO> selectAllHstry(@Param("offset") int offset, @Param("limit") int limit);

    /**
     * 분석 이력 등록
     */
    int insertHstry(AnlsHstryVO anlsHstryVO);

    /**
     * 분석 이력 수정
     */
    int updateHstry(AnlsHstryVO anlsHstryVO);

    /**
     * 분석 이력 삭제
     */
    int deleteHstry(@Param("anlsHstryId") int anlsHstryId);

    /**
     * 분석 이력 상세 단건 조회
     */
    AnlsHstryDtlVO selectHstryDtlById(@Param("anlsHstryId") int anlsHstryId);

    /**
     * 분석 이력 상세 등록
     */
    int insertHstryDtl(AnlsHstryDtlVO anlsHstryDtlVO);

    /**
     * 분석 이력 상세 수정
     */
    int updateHstryDtl(AnlsHstryDtlVO anlsHstryDtlVO);

    /**
     * 분석 이력 상세 삭제
     */
    int deleteHstryDtl(@Param("anlsHstryId") int anlsHstryId);

    /**
     * 분석 이력, 상세 조인 조회
     */
    AnlsHstryJoinVO selectHstryWithDtl(@Param("anlsHstryId") int anlsHstryId);

    /**
     * 분석 이력, 상세 리스트 조회
     */
    List<AnlsHstryJoinVO> selectMyHstryWithDtl(
            @Param("userId") String userId,
            @Param("offset") int offset,
            @Param("limit") int limit,
            @Param("analysisType") String analysisType,
            @Param("status") String status,
            @Param("sortOrder") String sortOrder
    );

    /**
     * 분석 이력 총 개수 조회 (필터 적용)
     */
    int countMyHstry(
            @Param("userId") String userId,
            @Param("analysisType") String analysisType,
            @Param("status") String status
    );

    /**
     * 레이어 ID로 분석 이력 ID 목록 조회
     */
    List<Integer> selectHstryIdsByUserLyrId(@Param("userLyrId") Long userLyrId);

    /**
     * 레이어 ID로 연관된 분석 이력 삭제 (상태를 DELETED로 변경하고 user_lyr_id를 NULL로 설정)
     */
    int softDeleteHstriesByUserLyrId(@Param("userLyrId") Long userLyrId);

    /**
     * 대기/기동 중 상태 분석 이력을 실패 상태로 초기화
     */
    void markPendingOrRunningAsFailed();
}
