package sgc.portal.cw.cw1601.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.cw1601.model.CW16010001U;
import sgc.portal.cw.cw1601.dto.CW16010001USearch;
import sgc.portal.cw.cw1601.persistence.CW16010001UMapper;

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

@Service
public class CW16010001UService {
    private final CW16010001UMapper mapper;

    public CW16010001UService(CW16010001UMapper mapper) {
        this.mapper = mapper;
    }

    public List<EasyHashMap<String, Object>> getList(CW16010001USearch search) {
        return mapper.getList(search);
    }
    
    @Transactional
    public Map<String, String> save(List<CW16010001U> CW16010001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (CW16010001U cW16010001U : CW16010001U) {
            switch (cW16010001U.getRowStatus()) {
                case GridRowStatus.CREATED:
        	        insert(cW16010001U);
                    break;
                case GridRowStatus.UPDATED:
                    update(cW16010001U);
                    break;
                case GridRowStatus.DELETED:
                	int count1 = mapper.selectAtchfileIdCount(cW16010001U);
                	if(count1 != 0) {
                		mapper.deleteAtchfileId(cW16010001U);	
                	}
                    mapper.delete(cW16010001U);
                    break;
                default:
                    break;
            }
        }
    	return result;
    }

    private void insert(CW16010001U cW16010001U) {
        mapper.insert(cW16010001U);
    }

    private void update(CW16010001U cW16010001U) {
        mapper.update(cW16010001U);
    }
    
    @Transactional
    public Map<String, String> updateStatus(List<CW16010001U> CW16010001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
        for (CW16010001U cW16010001U : CW16010001U) {
        	updateStatus(cW16010001U);
        }
        return result;
    }
    
    private void updateStatus(CW16010001U cW16010001U) {
        mapper.updateStatus(cW16010001U);
    }
}
