package com.samsung.sample.approval.internal.dao;

import com.samsung.core.entity.PagingResult;
import com.samsung.sample.approval.internal.dto.InternalSampleSearchDto;
import com.samsung.sample.approval.internal.entity.InternalApprovalSampleDocument;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class InternalApprovalSampleDao {
	
    final
    SqlSession sqlSession;

    public InternalApprovalSampleDao(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    public PagingResult<InternalApprovalSampleDocument> getSampleApprovalDocumentPagingList(InternalSampleSearchDto internalSampleSearchDto) {
    	
        internalSampleSearchDto.setStartDate(internalSampleSearchDto.getStartDate().replace("-", ""));
        internalSampleSearchDto.setEndDate(internalSampleSearchDto.getEndDate().replace("-", ""));
    	
        List<InternalApprovalSampleDocument> internalApprovalSampleDocuments = sqlSession.selectList("sampleApprovalInternal.selectSampleApprovalDocumentPagingList", internalSampleSearchDto);

        int cnt = sqlSession.selectOne("sampleApprovalInternal.countSampleApprovalDocument", internalSampleSearchDto);
        return new PagingResult<>(internalSampleSearchDto.getPageIndex(), cnt, internalSampleSearchDto.getPageSize(), internalSampleSearchDto.getPageUnit(), internalApprovalSampleDocuments);
    }
    
    public InternalApprovalSampleDocument getSampleApprovalDocument(String docId) {
        return sqlSession.selectOne("sampleApprovalInternal.selectSampleApprovalDocument", Map.of("docId", docId));
    }
    
    public void insertSampleApprovalDocument(InternalApprovalSampleDocument internalApprovalSampleDocument) {
        sqlSession.insert("sampleApprovalInternal.insertSampleApprovalDocument", internalApprovalSampleDocument);
    }
    
    public void modifySampleApprovalDocument(InternalApprovalSampleDocument internalApprovalSampleDocument) {
        sqlSession.update("sampleApprovalInternal.modifySampleApprovalDocument", internalApprovalSampleDocument);
    }
    
    public void deleteSampleApprovalDocument(InternalApprovalSampleDocument internalApprovalSampleDocument) {
        sqlSession.update("sampleApprovalInternal.deleteSampleApprovalDocument", internalApprovalSampleDocument);
    }
    
    public void updateSampleApprovalDocument(Map<String, Object> condition) {
        sqlSession.update("sampleApprovalInternal.updateSampleApprovalDocument", condition);
    }
}