package sgc.portal.cw.cw0702.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.cw0702.dto.CW07020001USearch;
import sgc.portal.cw.cw0702.model.CW07020001U;
import sgc.portal.cw.cw0702.persistence.CW07020001UMapper;

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

@Service
public class CW07020001UService {
	
    private final CW07020001UMapper mapper;

    public CW07020001UService(CW07020001UMapper mapper) {
        this.mapper = mapper;
    }
    
    //잉여 자재(비품)정보 공유 (관리자) 조회
    public List<EasyHashMap<String, Object>> selectZehrIsExtraMaterialAdmin(CW07020001USearch search) {
        return mapper.selectZehrIsExtraMaterialAdmin(search);
    }
    
    //잉여 자재(비품)정보 공유 (관리자) 추가 수정 삭제
    @Transactional
    public Map<String, String> saveZehrIsExtraMaterialAdmin (List<CW07020001U> CW07020001U) {
    	
    	Map<String,String> result = new HashMap<>();

        for (CW07020001U cW07020001U : CW07020001U) {
        	
            switch (cW07020001U.getRowStatus()) {
            
                case GridRowStatus.CREATED:
                	mapper.insertZehrIsExtraMaterialAdmin(cW07020001U);
                    break;
                    
                case GridRowStatus.UPDATED:
                	mapper.updateZehrIsExtraMaterialAdmin(cW07020001U);
                    break;
                    
                case GridRowStatus.DELETED:
                	
                	int count1 = mapper.selectAtchfileIdAdminCount(cW07020001U);
                	if(count1 != 0) {
                		mapper.deleteZehrIsExtraMaterialAtchfileIdAdmin(cW07020001U);	
                	}
                	mapper.deleteZehrIsExtraMaterialAdmin(cW07020001U);
                    break;
                default:
                    break;
            }
        }
        return result;
    }
}