package sgc.portal.qc.qc0103.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.websquare.GridRowStatus;
import sgc.portal.qc.qc0103.persistence.QC01030001UMapper;
import sgc.portal.qc.qc0103.dto.QC01030001USearch;
import sgc.portal.qc.qc0103.model.QC01030001U;

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

@Service
public class QC01030001UService {
    
	private final QC01030001UMapper mapper;

    public QC01030001UService(QC01030001UMapper mapper) {
        this.mapper = mapper;
    }
    
    //문서관리 조회
    public List<EasyHashMap<String, Object>> selectQcOutcoDocGrd(QC01030001USearch search) {
        return mapper.selectQcOutcoDocGrd(search);
    }
    
    //문서관리 조회
    public List<EasyHashMap<String, Object>> selectCommonCode(QC01030001USearch search) {
        return mapper.selectCommonCode(search);
    }
    
    //문서관리 수정
    @Transactional
    public Map<String, String> saveQcOutcoDoc(List<QC01030001U> QC01030001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
        for (QC01030001U qC01030001U : QC01030001U) {
        	
            switch (qC01030001U.getRowStatus()) {
            
                case GridRowStatus.CREATED:
                	insertQcOutcoDoc(qC01030001U);
                    break;
                    
                case GridRowStatus.UPDATED:
                	updateQcOutcoDoc(qC01030001U);
                    break;
                    
                case GridRowStatus.DELETED:
                    deleteQcOutcoDoc(qC01030001U);
                    break;
                default:
                    break;
            }
        }
        return result;
    }

    private void insertQcOutcoDoc(QC01030001U qC01030001U) {
    	
    	if(qC01030001U.getDocDistGbCd().equals("D01")) {
    		qC01030001U.setOutcoId(mapper.selectOutcoId(qC01030001U));
    		qC01030001U.setSndRcvSeq(mapper.selectSndRcvSeq(qC01030001U));
    		qC01030001U.setDocClsNo(mapper.selectDocClsNo(qC01030001U));
    		mapper.insertQcOutcoDoc(qC01030001U);
    	}
    	if(qC01030001U.getDocDistGbCd().equals("D02")) {
    		qC01030001U.setSndRcvSeq(mapper.selectSndRcvSeq(qC01030001U));
    		mapper.insertQcOutcoDoc(qC01030001U);
    	}
		if(qC01030001U.getDocDistGbCd().equals("D03")) {
			qC01030001U.setSndRcvSeq(mapper.selectSndRcvSeq(qC01030001U));
			mapper.insertQcOutcoDoc(qC01030001U);
		}
    }

    private void updateQcOutcoDoc(QC01030001U qC01030001U) {
    	//qC01030001U.setDocClsNo(mapper.selectDocClsNoUpdate(qC01030001U));
        mapper.updateQcOutcoDoc(qC01030001U);
    }

    private void deleteQcOutcoDoc(QC01030001U qC01030001U) {
    	mapper.deleteQcOutcoDoc(qC01030001U);
    }
}