package sgc.portal.com.com.service;

import sgc.portal.base.exception.UnsupportedGirdStatusException;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.websquare.GridRowStatus;
import sgc.portal.com.com.model.Menu;
import sgc.portal.com.com.persistence.ComMenuMngMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**********************************************************
 * 시스템명  	: 시스템관리(COM)
 * 프로그램ID 	: ComMenuMngService
 * 프로그램명   	: 메뉴 관리 Service
 * @author	: SBK
 * @version	: 2023-07-25
 * @since JDK1.8
 * @see
 * 1. 최초 작성
**********************************************************/

@Transactional
@Service
public class ComMenuMngService
{

    @Autowired
    private ComMenuMngMapper comMenuMngMapper;

    public  List<EasyHashMap<String, Object>>  selectComMenuMngList(Menu mn) {
        return comMenuMngMapper.selectComMenuMngList(mn);
    }

    public  List<EasyHashMap<String, Object>>  selectComMenuMng(Menu mn) {
        return comMenuMngMapper.selectComMenuMng(mn);
    }

    public  void  saveComMenuMng(List<Menu> mns) {
	    if (CollectionUtils.isNotEmpty(mns)) {
	        for(Menu mn : mns){
	            if("".equals(mn.getMenuId()) || null == mn.getMenuId()){
	                mn.setRowStatus(GridRowStatus.CREATED);
	            }else{
	                mn.setRowStatus(GridRowStatus.UPDATED);
	            }
	            switch (mn.getRowStatus()) {
                case GridRowStatus.CREATED:
                    comMenuMngMapper.saveComMenuMngInsert(mn);
                    break;
                case GridRowStatus.UPDATED:
                    comMenuMngMapper.saveComMenuMngUpdate(mn);
                    break;
                default:
                    throw new UnsupportedGirdStatusException();
                }
	        }
	    }
    }

    public Map<String,String> deleteComMenuMng(Menu mn) {
        Map<String,String> result = new HashMap<>();
        if(comMenuMngMapper.beforeDeleteComMenuMng(mn)!=0){
            result.put("result", "N");
        }else{
            comMenuMngMapper.deleteComMenuMng(mn);
            result.put("result", "Y");
        }
        return result;
    }
}
