package sgc.portal.base.service;

import sgc.portal.base.exception.UnsupportedGirdStatusException;
import sgc.portal.base.model.Yfvrprg;
import sgc.portal.base.persistence.ComBookMarkPopMapper;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.websquare.GridRowStatus;
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.List;

/**
 * The type Com book mark pop service.
 *
 * @author mir(khjang @ sgc.co.kr)
 * @version 1.0.0
 * @since 2020. 8. 2.
 */
@Transactional
@Service
public class ComBookMarkPopService {

    @Autowired
    private ComBookMarkPopMapper comBookMarkPopMapper;

    /**
     * Select book mark menu list.
     *
     * @param yfvrprgs the yfvrprgs
     * @return the list
     */
    //조회
    public List<EasyHashMap<String, Object>> selectBookMarkMenu(Yfvrprg yfvrprgs) {
        return comBookMarkPopMapper.selectBookMarkMenu(yfvrprgs);
    }

    /**
     * Save book mark menu.
     *
     * @param yfvrprgs the yfvrprgs
     */
    //저장
    public void saveBookMarkMenu(List<Yfvrprg> yfvrprgs) {
        if (CollectionUtils.isNotEmpty(yfvrprgs)) {
            for (Yfvrprg yfvrprg : yfvrprgs) {
                switch (yfvrprg.getRowStatus()) {
                    case GridRowStatus.CREATED:
                    	if(yfvrprg.getPrgGb().equals("M")) {
                    		comBookMarkPopMapper.insertBookMarkMenu(yfvrprg);
                    	} else {
                    		comBookMarkPopMapper.insertBookMarkPrg(yfvrprg);
                    	}
                        break;
                    case GridRowStatus.UPDATED:
                    	if(yfvrprg.getPrgGb().equals("M")) {
                    		comBookMarkPopMapper.updateBookMarkMenu(yfvrprg);
                    	} else {
                    		comBookMarkPopMapper.updateBookMarkPrg(yfvrprg);
                    	}
                        break;
                    case GridRowStatus.DELETED:
                    	if(yfvrprg.getPrgGb().equals("M")) {
                    		comBookMarkPopMapper.deleteBookMarkPrg(yfvrprg);
                    		comBookMarkPopMapper.deleteBookMarkMenu(yfvrprg);
                    	} else {
                    		comBookMarkPopMapper.deleteBookMarkPrg(yfvrprg);
                    	}
                        break;
                    default:
                        throw new UnsupportedGirdStatusException();
                }
            }
        }
    }
}
