package sgc.portal.cw.cw0621.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.cw.cw0621.dto.CW06210001USearch;
import sgc.portal.cw.cw0621.model.CW06210001U;
import sgc.portal.cw.cw0621.persistence.CW06210001UMapper;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CW06210001UService {
    private final CW06210001UMapper mapper;

    public CW06210001UService(CW06210001UMapper mapper) {
        this.mapper = mapper;
    }

    public List<EasyHashMap<String, Object>> getList(CW06210001USearch search) {
        return mapper.getList(search);
    }
    
    @Transactional
    public Map<String, String> save(List<CW06210001U> CW06210001U) {
    	
    	Map<String,String> result = new HashMap<>();

        for (CW06210001U cW06210001U : CW06210001U) {

            switch (cW06210001U.getRowStatus()) {

            	case GridRowStatus.CREATED:
    				insert(cW06210001U);
    				
                default:
                    break;
            }
        }
        
        return result;
    }
    
    @Transactional
    public Map<String, String> updateCre(List<CW06210001U> CW06210001U) {
        Map<String, String> result = new HashMap<>();

        for (CW06210001U cW06210001U : CW06210001U) {
        	
            mapper.updateCre(cW06210001U);
            String errCode = cW06210001U.getErrCode(); // 출력 파라미터 값 가져오기
            String errMsg = cW06210001U.getErrMsg();

            if (errCode.equals("X")) {
                result.put("errCode", errCode);
                result.put("errMsg", errMsg);
                return result;
            }
        }

        return result;
    }
    
    @Transactional
    public Map<String, String> updatePrev(List<CW06210001U> CW06210001U) {
    	
    	Map<String,String> result = new HashMap<>();

        for (CW06210001U cW06210001U : CW06210001U) {

            switch (cW06210001U.getRowStatus()) {

            	case GridRowStatus.UPDATED:
            		updatePrev(cW06210001U);
    				
                default:
                    break;
            }
        }
        
        return result;
    }
    
    @Transactional
    public Map<String, String> updateNext(List<CW06210001U> CW06210001U) {
    	
    	Map<String,String> result = new HashMap<>();

        for (CW06210001U cW06210001U : CW06210001U) {

            switch (cW06210001U.getRowStatus()) {

            	case GridRowStatus.UPDATED:
            		updateNext(cW06210001U);
    				
                default:
                    break;
            }
        }
        
        return result;
    }

    private void insert(CW06210001U cW06210001U) {
        mapper.insert(cW06210001U);
    }
    
    
    public void updatePrev(CW06210001U cW06210001U) {
    	
    	mapper.updatePrev(cW06210001U);
    }
    
    public void updateNext(CW06210001U cW06210001U) {
    	
    	mapper.updateNext(cW06210001U);
    }
}
