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

import com.samsung.core.entity.PagingResult;
import com.samsung.sample.approval.knox.dto.KnoxApprovalSampleSearchDto;
import com.samsung.sample.approval.knox.entity.KnoxApprovalSampleDocument;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

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

@Repository
public class KnoxApprovalSampleDao {
	
    final
    SqlSession sqlSession;

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

    public PagingResult<KnoxApprovalSampleDocument> getSampleApprovalDocumentPagingList(KnoxApprovalSampleSearchDto sampleSearchDto) {
    	
        sampleSearchDto.setStartDate(sampleSearchDto.getStartDate().replace("-", ""));
        sampleSearchDto.setEndDate(sampleSearchDto.getEndDate().replace("-", ""));
    	
        List<KnoxApprovalSampleDocument> sampleApprovalDocuments = sqlSession.selectList("sampleApprovalKnox.selectSampleApprovalDocumentPagingList", sampleSearchDto);

        int cnt = sqlSession.selectOne("sampleApprovalKnox.countSampleApprovalDocument", sampleSearchDto);
        return new PagingResult<>(sampleSearchDto.getPageIndex(), cnt, sampleSearchDto.getPageSize(), sampleSearchDto.getPageUnit(), sampleApprovalDocuments);
    }
    
    public KnoxApprovalSampleDocument getSampleApprovalDocument(String docId) {
        return sqlSession.selectOne("sampleApprovalKnox.selectSampleApprovalDocument", Map.of("docId", docId));
    }
    
    public void insertSampleApprovalDocument(KnoxApprovalSampleDocument sampleApprovalDocument) {
        sqlSession.insert("sampleApprovalKnox.insertSampleApprovalDocument", sampleApprovalDocument);
    }
    
    public void modifySampleApprovalDocument(KnoxApprovalSampleDocument sampleApprovalDocument) {
        sqlSession.update("sampleApprovalKnox.modifySampleApprovalDocument", sampleApprovalDocument);
    }
    
    public void deleteSampleApprovalDocument(KnoxApprovalSampleDocument sampleApprovalDocument) {
        sqlSession.update("sampleApprovalKnox.deleteSampleApprovalDocument", sampleApprovalDocument);
    }
    
    public void updateSampleApprovalDocument(Map<String, Object> condition) {
        sqlSession.update("sampleApprovalKnox.updateSampleApprovalDocument", condition);
    }
}