package sgc.portal.cs.cs0101.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.cs.cs0101.persistence.CS01010001UMapper;
import sgc.portal.cs.cs0101.dto.CS01010001USearch;
import sgc.portal.cs.cs0101.model.CS01010001U;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CS01010001UService {
    
	private final CS01010001UMapper mapper;

    public CS01010001UService(CS01010001UMapper mapper) {
        this.mapper = mapper;
    }
    
    //공지사항 조회
    public List<EasyHashMap<String, Object>> selectTcsNotiList(CS01010001USearch search) {
        return mapper.selectTcsNotiList(search);
    }
    
    //공지사항 조회 팝업
    public EasyHashMap<String, Object> selectTcsNotiPopList(CS01010001USearch search) {
        return mapper.selectTcsNotiPopList(search);
    }
    
    //첨부파일 조회
    public List<EasyHashMap<String, Object>> selectFileList(CS01010001USearch search) {
        return mapper.selectFileList(search);
    }
    
    //공지사항 등록 변경 
    @Transactional
    public Map<String, String>  saveTcsNotiList(CS01010001U cS01010001U) {
    	
    	Map<String, String> result = new HashMap<>();
    	
    	int count = mapper.selectTcsNotiCount(cS01010001U);
    	
    	if(count == 0) {
    		
    		String NotiSeq = mapper.selectTcsNotiSeq(cS01010001U);
    		cS01010001U.setNotiSeq(NotiSeq);
    		mapper.insertTcsNoti(cS01010001U);

    		result.put("count", "0");
			result.put("notiSeq", NotiSeq);			
    	}else {
    		mapper.updateTcsNoti(cS01010001U);	
    	}
    	
    	return result;
    }
    
    //공지사항 삭제
    public void deleteTcsNotiList(CS01010001U cS01010001U) {
    		
    	int count = mapper.selectTcsNotiNotiAflIdCount(cS01010001U);
    	if(count > 0) {
    		mapper.deleteTcsNotiNotiAflId(cS01010001U);
    	}
    	mapper.deleteTcsNoti(cS01010001U);		
    }
    
    //공지사항 첨부파일 변경 
    @Transactional
    public Map<String, String>  updateAflId(CS01010001U cS01010001U) {
    	
    	Map<String, String> result = new HashMap<>();
    	mapper.updateAflId(cS01010001U);
    	
    	return result;
    }
}