package cn.com.liandisys.ycjt.ggcx.admin.service.cms;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

import cn.com.liandisys.ycjt.ggcx.admin.dao.cms.ArticleDao;
import cn.com.liandisys.ycjt.ggcx.admin.dao.cms.ArticleDataDao;
import cn.com.liandisys.ycjt.ggcx.admin.model.cms.Article;
import cn.com.liandisys.ycjt.ggcx.admin.model.cms.ArticleData;
import cn.com.liandisys.ycjt.ggcx.admin.utils.CacheUtils;
import cn.com.liandisys.ycjt.ggcx.admin.utils.CmsUtils;
import cn.com.liandisys.ycjt.ggcx.common.util.StringUtils;
import cn.com.liandisys.ycjt.ggcx.common.util.UserUtils;

/**
 * 文章service
 * @author jiangchao
 * @version 2016-11-29
 */
@Service
@Transactional(readOnly = true)
public class ArticleService {

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private ArticleDataDao articleDataDao;

    public Article findOne(String id) {

        return articleDao.findOne(id);
    }

    public List<Article> findAll() {

        return articleDao.findAll();
    }

    public Page<Article> findAll(Specification<Article> spec, Pageable pageable) {

        return articleDao.findAll(spec, pageable);
    }

    //声明service的方法需要事务管理
    @Transactional(readOnly = false)
    public void save(Article article) throws UnsupportedEncodingException {

        if (article.getArticleData().getContent() != null) {
            String content = article.getArticleData().getContent();
            content = content.replaceAll("&nbsp;", "&amp;nbsp;");
            article.getArticleData().setContent(StringEscapeUtils.unescapeHtml4(content));
        }
        if (StringUtils.isBlank(article.getId())) {
            article.setCreateBy(UserUtils.getPrincipal());
            article.setCreateDate(new Date());
        }
        article.setUpdateBy(UserUtils.getPrincipal());
        article.setUpdateDate(new Date());
        if(article.getDelFlag()==null) {
        article.setDelFlag(Article.DEL_FLAG_NORMAL);
        }
        ArticleData articleData = article.getArticleData();
        articleData = articleDataDao.save(articleData);
        articleDao.save(article);
        updateArticleCache(article);
    }

    /**
     * 更新缓存
     * @param article
     */
    private void updateArticleCache(Article article) {

        CacheUtils.remove(CmsUtils.CACHE_ARTICLE_LIST + article.getCategory().getId(),
                CmsUtils.CACHE_ARTICLE_LIST_BELONG_ID + article.getCategory().getId());
        List<Article> newCacheList =
                CmsUtils.getArticleList(article.getCategory().getId(), CmsUtils.CMS_INDEX_ARTICLE_SIZE, null);
        CacheUtils.put(CmsUtils.CACHE_ARTICLE_LIST + article.getCategory().getId(),
                CmsUtils.CACHE_ARTICLE_LIST_BELONG_ID + article.getCategory().getId(), newCacheList);
    }

    @Transactional(readOnly = false)
    public void delete(Article article) {

        if (article.getArticleData() != null) {
            articleDataDao.delete(article.getArticleData());
        }
        articleDao.delete(article);
        updateArticleCache(article);
    }

    /**
     * 通过编号获取内容标题
     * @return new Object[]{栏目Id,文章Id,文章标题}
     */
    public List<Object[]> findByIds(String ids) {

        if (ids == null) {
            return new ArrayList<Object[]>();
        }
        List<Object[]> list = Lists.newArrayList();
        String[] idss = StringUtils.split(ids, ",");
        Article e = null;
        for (int i = 0; (idss.length - i) > 0; i++) {
            e = articleDao.findOne(idss[i]);
            list.add(new Object[] { e.getCategory().getId(), e.getId(), StringUtils.abbr(e.getTitle(), 50) });
        }
        return list;
    }

    @Transactional(readOnly = false)
    public void updateHitsAddOne(Article article) {

        article.setHits(article.getHits() + 1);
        articleDao.save(article);
    }

    public List<Article> findByCategoryId(String categoryId) {

        return articleDao.findByCategoryId(categoryId);
    }

}
