package com.ym.mec.cms.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.mec.cms.dal.dao.HelpCenterCatalogDao;
import com.ym.mec.cms.dal.entity.HelpCenterCatalog;
import com.ym.mec.cms.service.HelpCenterCatalogService;
import com.ym.mec.cms.service.HelpCenterContentService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;

@Service
public class HelpCenterCatalogServiceImpl extends BaseServiceImpl<Integer, HelpCenterCatalog> implements HelpCenterCatalogService {

	@Autowired
	private HelpCenterCatalogDao helpCenterCatalogDao;

	@Autowired
	private HelpCenterContentService helpCenterContentService;

	@Override
	public BaseDAO<Integer, HelpCenterCatalog> getDAO() {

		return helpCenterCatalogDao;
	}

	@Override
	public List<HelpCenterCatalog> queryByParentId(Integer parentId, Integer status) {

		List<HelpCenterCatalog> dataList = helpCenterCatalogDao.queryByParentId(parentId, status);
		if (dataList != null) {
			for (HelpCenterCatalog sub : dataList) {
				List<HelpCenterCatalog> subList = helpCenterCatalogDao.queryByParentId(sub.getId(), status);
				if (subList == null) {
					continue;
				}
				sub.setChildren(subList);
				queryByParentId(sub.getId(), status);
			}
		}
		return dataList;
	}

	public boolean updateCatalog(HelpCenterCatalog catalog) {
		return helpCenterCatalogDao.update(catalog) > 0 && helpCenterContentService.updateContentByCatalog(catalog.getId(), catalog.getStatus());
	}

	@Override
	public boolean deleteCatalog(Integer id) {
		return helpCenterCatalogDao.delete(id) > 0 && helpCenterContentService.deleteContentByCatalog(id);
	}

}
