package com.goafanti.news.service;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.NewsMapper;
import com.goafanti.common.model.News;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.common.utils.LoggerUtils;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.news.bo.NewsPortalList;
import com.goafanti.news.bo.NewsSummary;

@Service
@Transactional
public class NewsService extends BaseMybatisDao<NewsMapper> {
	@Autowired
	NewsMapper newsMapper;
	@Value("${spring.profiles.active}")
	private String env;

	private static final Logger logger = LoggerFactory.getLogger(NewsService.class);

	@Cacheable(value = "IndexNewsListCache", key = "'NewsList:Page:'+#pageNo+'Type:'+#type+'Size:'+#pageSize")
	public List<NewsSummary> findIndexNewsList(Integer pageNo, Integer type, Integer pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("type", type);
		params.put("pageSize", pageSize);
		params.put("pageNo", pageNo);
		return newsMapper.findIndexNewsLists(params);
	}

	@CacheEvict(value = "IndexNewsListCache", allEntries = true)
	public void cleanIndexNewsList() {
		LoggerUtils.debug(logger, "清除首页新闻列表缓存");
	}

	@Cacheable(value = "NewsCache", key = "'News:'+#id")
	public News selectById(Long id) {
		return newsMapper.selectByPrimaryKey(id);
	}

	@Cacheable(value = "NewsListCache", key = "'NewsList:Page:'+#pageNo+'Type:'+#type+'Size:'+#pageSize+'domainName:'+#domainName+'picReq:'+#picRequired")
	public List<NewsSummary> findNewsList(Integer pageNo, Integer type, Integer pageSize, String domainName,
			Boolean picRequired) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("type", type);
		params.put("pageSize", pageSize);
		params.put("pageNo", pageNo);
		if (!StringUtils.equals(env, "dev") && !StringUtils.equals(env, "local")) {
			params.put("website", domainName);
		}
		if (picRequired) {
			params.put("picRequired", picRequired);
		}
		return newsMapper.findList(params);
	}

	@CacheEvict(value = "NewsListCache", allEntries = true)
	public void cleanList() {
		LoggerUtils.debug(logger, "清除新闻列表缓存");
	}

	@CacheEvict(value = "NewsCache", key = "'News:'+#id")
	public void clean(String id) {
		LoggerUtils.debug(logger, "清除新闻缓存:[%s]", id);
	}

	@CachePut(value = "NewsCache", key = "'News:'+#id")
	public News save(Long id, News news) {
		news.setId(id);
		newsMapper.insert(news);
		return news;
	}

	@CachePut(value = "NewsCache", key = "'News:'+#id")
	public News update(long id, News news) {
		newsMapper.updateByPrimaryKeyWithBLOBs(news);
		return news;
	}

	@SuppressWarnings("unchecked")
	public Pagination<News> listNews(Integer type, String title, String author, String startCreateTime,
			String endCreateTime, String source, Integer hot, Integer pNo, Integer pSize,String auditStatus,String releaseStatus) {
		Map<String, Object> params = new HashMap<>();
		if (null != auditStatus) {
			params.put("auditStatus", auditStatus);
		}
		
		if (null != releaseStatus) {
			params.put("releaseStatus", releaseStatus);
		}
		
		if (null != type) {
			params.put("type", type);
		}

		if (StringUtils.isNotBlank(title)) {
			params.put("title", title);
		}

		if (StringUtils.isNotBlank(author)) {
			params.put("author", author);
		}

		if (StringUtils.isNotBlank(startCreateTime)) {
			try {
				params.put("sDate", DateUtils.parseDate(startCreateTime, AFTConstants.YYYYMMDD));
			} catch (ParseException e) {
			}
		}

		if (StringUtils.isNotBlank(endCreateTime)) {
			try {
				params.put("eDate", DateUtils.addDays(DateUtils.parseDate(endCreateTime, AFTConstants.YYYYMMDD), 1));
			} catch (ParseException e) {
			}
		}

		if (StringUtils.isNotBlank(source)) {
			params.put("source", source);
		}

		if (null != hot) {
			params.put("hot", hot);
		}

		if (pNo == null || pNo < 0) {
			pNo = 1;
		}

		if (pSize == null || pSize < 0 || pSize > 10) {
			pSize = 10;
		}
		return (Pagination<News>) findPage("findNewsListByPage", "findNewsCount", params, pNo, pSize);
	}

	public News findNewsDetail(Long id) {
		return newsMapper.selectByPrimaryKey(id);
	}

	public int batchDeleteByPrimaryKey(List<String> id) {
		return newsMapper.batchDeleteByPrimaryKey(id);
	}

	@SuppressWarnings("unchecked")
	public Pagination<NewsPortalList> portalNewsList(Integer pSize, Integer pNo, Integer type, Integer hot,
			Integer provinceId, String keyword) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (null != hot) {
			params.put("hot", hot);
		}
		if (null != type) {
			params.put("type", type);
		}
		if (null != provinceId) {
			params.put("provinceId", provinceId);
		}
		if (null != keyword) {
			params.put("keyword", keyword);
		}
		if (pNo == null || pNo < 0) {
			pNo = 1;
		}
		if (pSize == null || pSize < 0 || pSize > 10) {
			pSize = 10;
		}
		return (Pagination<NewsPortalList>) findPage("portalNewsList", "portalNewsCount", params, pNo, pSize);
	}

	@CacheEvict(value = "NewsPortalListCache", allEntries = true)
	public void cleanPortalList() {
		LoggerUtils.debug(logger, "清除门户端新闻列表缓存");

	}

	public List<NewsSummary> findJmrhNewsList(String type) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("type", type);
		return newsMapper.findJmrhNewsList(params);
	}

	public List<NewsPortalList> portalNewsPolicy(){
		
		return newsMapper.portalNewsPolicy();
	}
	public List<NewsPortalList> portalNewsQuestion(){
		
		return newsMapper.portalNewsQuestion();
	}
	@SuppressWarnings("unchecked")
	public Pagination<NewsPortalList> appListNews(String param,Integer pNo,Integer pSize){
		Map<String, Object> params=new HashMap<>();
		if (StringUtils.isNotBlank(param)) {
			params.put("param", param);
		}
		if (pNo == null || pNo < 0) {
			pNo = 1;
		}
		if (pSize == null || pSize < 0 || pSize > 10) {
			pSize = 10;
		}
		return (Pagination<NewsPortalList>) findPage("appNewsList", "appNewsCount", params, pNo, pSize);
	}
	
}