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

import com.cm.common.article.dao.articlecategory.IArticleCategoryDao;
import com.cm.common.article.pojo.dtos.articlecategory.ArticleCategoryDTO;
import com.cm.common.article.pojo.vos.articlecategory.ArticleCategoryVO;
import com.cm.common.article.service.articlecategory.IArticleCategoryService;
import com.cm.common.base.AbstractService;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResult;
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: ArticleCategoryServiceImpl
 * @Description: 文章类别
 * @Author: WenG
 * @Date: 2020-04-03 15:20
 * @Version: 1.0
 **/
@Service
public class ArticleCategoryServiceImpl extends AbstractService implements IArticleCategoryService {

    @Autowired
    private IArticleCategoryDao articleCategoryDao;

    @Override
    public SuccessResult saveArticleCategory(ArticleCategoryVO articleCategoryVO) throws Exception {
        saveArticleCategoryInfo(null, articleCategoryVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult saveArticleCategoryByToken(String token, ArticleCategoryVO articleCategoryVO) throws Exception {
        saveArticleCategoryInfo(token, articleCategoryVO);
        return new SuccessResult();
    }

    /**
     * 新增文章类别
     *
     * @param token
     * @param articleCategoryVO
     * @throws Exception
     */
    private void saveArticleCategoryInfo(String token, ArticleCategoryVO articleCategoryVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(articleCategoryVO);
        params.put("articleCategoryId", UUIDUtil.getUUID());
        if (token != null) {
            setSaveInfo(token, params);
        } else {
            setSaveInfo(params);
        }
        articleCategoryDao.saveArticleCategory(params);
    }

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

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

    /**
     * 删除文章类别
     *
     * @param token
     * @param ids
     */
    private void removeArticleCategoryInfo(String token, String ids) {
        Map<String, Object> params = getHashMap(3);
        params.put("articleCategoryIds", Arrays.asList(ids.split("_")));
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        articleCategoryDao.removeArticleCategory(params);
    }

    @Override
    public SuccessResult updateArticleCategory(String articleCategoryId, ArticleCategoryVO articleCategoryVO) throws Exception {
        updateArticleCategoryInfo(null, articleCategoryId, articleCategoryVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateArticleCategoryByToken(String token, String articleCategoryId, ArticleCategoryVO articleCategoryVO) throws Exception {
        updateArticleCategoryInfo(token, articleCategoryId, articleCategoryVO);
        return new SuccessResult();
    }

    /**
     * 修改文章类别
     *
     * @param token
     * @param articleCategoryId
     * @param articleCategoryVO
     */
    private void updateArticleCategoryInfo(String token, String articleCategoryId, ArticleCategoryVO articleCategoryVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(articleCategoryVO);
        params.put("articleCategoryId", articleCategoryId);
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        articleCategoryDao.updateArticleCategory(params);
    }

    @Override
    public ArticleCategoryDTO getArticleCategoryById(String articleCategoryId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("articleCategoryId", articleCategoryId);
        return articleCategoryDao.getArticleCategory(params);
    }

    @Override
    public List<ArticleCategoryDTO> listArticleCategory(Map<String, Object> params) throws SearchException {
        return articleCategoryDao.listArticleCategory(params);
    }

    @Override
    public SuccessResultList<List<ArticleCategoryDTO>> listPageArticleCategory(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<ArticleCategoryDTO> articleCategoryDTOs = articleCategoryDao.listArticleCategory(page.getParams());
        PageInfo<ArticleCategoryDTO> pageInfo = new PageInfo<>(articleCategoryDTOs);
        return new SuccessResultList<>(articleCategoryDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

}
