package com.cm.common.article.service.articlecontent.impl;

import com.cm.common.article.dao.articlecontent.IArticleContentDao;
import com.cm.common.article.pojo.dtos.articlecontent.ArticleContentDTO;
import com.cm.common.article.pojo.vos.articlecontent.ArticleContentVO;
import com.cm.common.article.service.articlecontent.IArticleContentService;
import com.cm.common.base.AbstractService;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.exception.UpdateException;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultData;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.HashMapUtil;
import com.cm.common.utils.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ArticleContentServiceImpl
 * @Description: 文章内容
 * @Author: WenG
 * @Date: 2020-04-03 15:37
 * @Version: 1.0
 **/
@Service
public class ArticleContentServiceImpl extends AbstractService implements IArticleContentService {

    @Autowired
    private IArticleContentDao articleContentDao;

    @Override
    public SuccessResult saveArticleContent(ArticleContentVO articleContentVO) throws Exception {
        saveArticleContentInfo(null, articleContentVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult saveArticleContentByToken(String token, ArticleContentVO articleContentVO) throws Exception {
        saveArticleContentInfo(token, articleContentVO);
        return new SuccessResult();
    }

    /**
     * 新增文章内容
     *
     * @param token
     * @param articleContentVO
     * @throws Exception
     */
    private void saveArticleContentInfo(String token, ArticleContentVO articleContentVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(articleContentVO);
        params.put("articleContentId", UUIDUtil.getUUID());
        if (token != null) {
            setSaveInfo(token, params);
        } else {
            setSaveInfo(params);
        }
        articleContentDao.saveArticleContent(params);
    }

    @Override
    public SuccessResult removeArticleContent(String ids) throws RemoveException {
        removeArticleContentInfo(null, ids);
        return new SuccessResult();
    }

    @Override
    public SuccessResult removeArticleContentByToken(String token, String ids) throws RemoveException {
        removeArticleContentInfo(token, ids);
        return new SuccessResult();
    }

    /**
     * 删除文章内容
     *
     * @param token
     * @param ids
     */
    private void removeArticleContentInfo(String token, String ids) {
        Map<String, Object> params = getHashMap(3);
        params.put("articleContentIds", Arrays.asList(ids.split("_")));
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        articleContentDao.removeArticleContent(params);
    }

    @Override
    public SuccessResult updateArticleContent(String articleContentId, ArticleContentVO articleContentVO) throws Exception {
        updateArticleContentInfo(null, articleContentId, articleContentVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateArticleContentByToken(String token, String articleContentId, ArticleContentVO articleContentVO) throws Exception {
        updateArticleContentInfo(token, articleContentId, articleContentVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateArticleContentPublishStatus(String articleContentId, Integer isPublish) throws UpdateException {
        Map<String, Object> params = getHashMap(2);
        params.put("articleContentId", articleContentId);
        params.put("isPublish", isPublish);
        setUpdateInfo(params);
        articleContentDao.updateArticleContent(params);
        return new SuccessResult();
    }

    /**
     * 修改文章内容
     *
     * @param token
     * @param articleContentId
     * @param articleContentVO
     */
    private void updateArticleContentInfo(String token, String articleContentId, ArticleContentVO articleContentVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(articleContentVO);
        params.put("articleContentId", articleContentId);
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        articleContentDao.updateArticleContent(params);
    }

    @Override
    public ArticleContentDTO getArticleContentById(String articleContentId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("articleContentId", articleContentId);
        return articleContentDao.getArticleContent(params);
    }

    @Override
    public List<ArticleContentDTO> listArticleContent(Map<String, Object> params) throws SearchException {
        return articleContentDao.listArticleContent(params);
    }

    @Override
    public SuccessResultList<List<ArticleContentDTO>> listPageArticleContent(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<ArticleContentDTO> articleContentDTOs = articleContentDao.listArticleContent(page.getParams());
        PageInfo<ArticleContentDTO> pageInfo = new PageInfo<>(articleContentDTOs);
        return new SuccessResultList<>(articleContentDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public SuccessResultData<Integer> countArticleCount(Map<String, Object> params) throws SearchException {
        Integer countArticleCount = articleContentDao.countArticleCount(params);
        return new SuccessResultData<>(countArticleCount == null ? 0 : countArticleCount);
    }

    @Override
    public List<ArticleContentDTO> listArticleContentAll(Map<String, Object> params) throws SearchException {
        return articleContentDao.listArticleContentAll(params);
    }
}
