package sgc.portal.cw.cw0701.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.cw0701.dto.CW07010001USearch;
import sgc.portal.cw.cw0701.model.CW07010001U;
import sgc.portal.cw.cw0701.persistence.CW07010001UMapper;

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

@Service
public class CW07010001UService {
    private final CW07010001UMapper mapper;

    public CW07010001UService(CW07010001UMapper mapper) {
        this.mapper = mapper;
    }
    
    //잉여 자재(비품)정보 공유 조회
    public List<EasyHashMap<String, Object>> selectZehrIsExtraMaterial(CW07010001USearch search) {
        return mapper.selectZehrIsExtraMaterial(search);
    }
    
    //잉여 자재(비품)정보 공유 추가 수정 삭제
    @Transactional
    public Map<String, String> saveZehrIsExtraMaterial (List<CW07010001U> CW07010001U) {
    	
    	Map<String,String> result = new HashMap<>();

        for (CW07010001U cW07010001U : CW07010001U) {
        	
            switch (cW07010001U.getRowStatus()) {
            
                case GridRowStatus.CREATED:
                	mapper.insertZehrIsExtraMaterial(cW07010001U);
                    break;
                    
                case GridRowStatus.UPDATED:
                	mapper.updateZehrIsExtraMaterial(cW07010001U);
                    break;
                    
                case GridRowStatus.DELETED:
                	
                	int count1 = mapper.selectAtchfileIdCount(cW07010001U);
                	if(count1 != 0) {
                		mapper.deleteZehrIsExtraMaterialAtchfileId(cW07010001U);	
                	}
                	mapper.deleteZehrIsExtraMaterial(cW07010001U);
                    break;
                default:
                    break;
            }
        }
        return result;
    }
}

