package com.keao.edu.cms.service.impl;

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

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

import com.keao.edu.cms.controller.queryinfo.NewsInformationQueryInfo;
import com.keao.edu.cms.dal.dao.SysNewsInformationDao;
import com.keao.edu.cms.dal.entity.NewsStatusEnum;
import com.keao.edu.cms.dal.entity.SysNewsInformation;
import com.keao.edu.cms.service.SysNewsInformationService;
import com.keao.edu.common.dal.BaseDAO;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.common.page.QueryInfo;
import com.keao.edu.common.service.impl.BaseServiceImpl;
import com.keao.edu.util.collection.MapUtil;

@Service
public class SysNewsInformationServiceImpl extends BaseServiceImpl<Long, SysNewsInformation> implements SysNewsInformationService {

	@Autowired
	private SysNewsInformationDao sysNewsInformationDao;
	
	@Override
	public BaseDAO<Long, SysNewsInformation> getDAO() {
		return sysNewsInformationDao;
	}

	@Override
	public List<SysNewsInformation> queryByType(Integer type) {
		return sysNewsInformationDao.queryByType(type);
	}

	@Override
	public boolean deleteWithLogical(Long id) {
		sysNewsInformationDao.deleteWithLogical(id);
		return true;
	}

	@Override
	public Map<String,PageInfo<SysNewsInformation>> getHomeList(NewsInformationQueryInfo queryInfo) {
		//1精彩活动，2热门资讯,3banner类型
		Map<String,PageInfo<SysNewsInformation>> homeList = new HashMap<>();
		queryInfo.setStatus(NewsStatusEnum.SHOW);
		
		String memo = queryInfo.getMemo();
		
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);

		queryInfo.setType(3); 
		int count = sysNewsInformationDao.queryHomeCount(params);
		if(count == 0){
			queryInfo.setMemo(null);
		}
		PageInfo<SysNewsInformation> pageInfos = queryHomePage(queryInfo);

		homeList.put("banner",pageInfos);
		
		queryInfo.setMemo(memo);
		queryInfo.setType(6);
		count = sysNewsInformationDao.queryHomeCount(params);
		if(count == 0){
			queryInfo.setMemo(null);
		}
		homeList.put("app",queryHomePage(queryInfo));
		
		queryInfo.setMemo(null);
		queryInfo.setType(1);
		homeList.put("active",queryHomePage(queryInfo));
		queryInfo.setType(2);
		homeList.put("hot",queryHomePage(queryInfo));
		
		queryInfo.setType(5);
		pageInfos = new PageInfo<SysNewsInformation>();
		SysNewsInformation info = new SysNewsInformation();
		info.setOrder(1);
		pageInfos = queryHomePage(queryInfo);
		homeList.put("flash",pageInfos);
		return homeList;
	}
	
	private PageInfo<SysNewsInformation> queryHomePage(QueryInfo queryInfo) {
		PageInfo<SysNewsInformation> pageInfo = new PageInfo<SysNewsInformation>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);
		
		List<SysNewsInformation> dataList = null;
		int count = sysNewsInformationDao.queryHomeCount(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = sysNewsInformationDao.queryHomePage(params);
		}
		if (count == 0) {
			dataList = new ArrayList<SysNewsInformation>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

}