package sgc.portal.cw.cw1506.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.cw1506.model.CW15060001U;
import sgc.portal.cw.cw1506.dto.CW15060001USearch;
import sgc.portal.cw.cw1506.persistence.CW15060001UMapper;

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

@Service
public class CW15060001UService {
    private final CW15060001UMapper mapper;

    public CW15060001UService(CW15060001UMapper mapper) {
        this.mapper = mapper;
    }

    public List<EasyHashMap<String, Object>> getList(CW15060001USearch search) {
        return mapper.getList(search);
    }

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

    private void insert(CW15060001U cW15060001U) {
        mapper.insert(cW15060001U);
    }

    private void update(CW15060001U cW15060001U) {
        mapper.update(cW15060001U);
    }
}
