package com.yonge.cooleshow.cms.service.impl;

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

import com.yonge.cooleshow.cms.dto.SysNewsTypeDto;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.base.page.QueryInfo;
import com.yonge.toolset.mybatis.service.impl.BaseServiceImpl;
import com.yonge.toolset.utils.collection.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonge.cooleshow.cms.dal.dao.SysNewsTypeDao;
import com.yonge.cooleshow.cms.dal.entity.SysNewsType;
import com.yonge.cooleshow.cms.dal.entity.SysNewsTypeTree;
import com.yonge.cooleshow.cms.service.SysNewsTypeService;
import com.yonge.toolset.mybatis.dal.BaseDAO;

@Service
public class SysNewsTypeServiceImpl extends BaseServiceImpl<Long, SysNewsType> implements SysNewsTypeService {
	
	@Autowired
	private SysNewsTypeDao sysNewsTypeDao;

	@Override
	public BaseDAO<Long, SysNewsType> getDAO() {
		return sysNewsTypeDao;
	}

	@Override
	public List<SysNewsTypeTree> queryTree(Long parentId) {
		if(parentId == null){
			parentId = 0L ;
		}
		List<SysNewsTypeTree> sysNewsTypeList = queryByParentId(parentId);
		if(sysNewsTypeList != null && sysNewsTypeList.size() > 0){
			for(SysNewsTypeTree type : sysNewsTypeList){
				type.setSubTypeList(queryTree(type.getId()));
			}
		}
		
		return sysNewsTypeList;
	}

	@Override
	public List<SysNewsTypeTree> queryByParentId(Long parentId) {
		if(parentId == null){
			parentId = 0L ;
		}
		return sysNewsTypeDao.queryByParentId(parentId);
	}

    @Override
    public PageInfo<SysNewsTypeDto> appPage(QueryInfo query) {
		PageInfo<SysNewsTypeDto> pageInfo = new PageInfo<>(query.getPage(), query.getRows());
		Map<String, Object> params = new HashMap<>();
		MapUtil.populateMap(params, query);

		List<SysNewsTypeDto> dataList = null;
		int count = sysNewsTypeDao.queryAppCount(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = sysNewsTypeDao.queryAppPage(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;

    }

}