package ink.wgink.module.article.service.category.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.module.article.dao.category.ICategoryDao;
import ink.wgink.module.article.pojo.dtos.category.CategoryDTO;
import ink.wgink.module.article.pojo.vos.category.CategoryVO;
import ink.wgink.module.article.service.category.ICategoryService;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.SearchException;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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 CategoryServiceImpl extends DefaultBaseService implements ICategoryService {

    @Autowired
    private ICategoryDao categoryDao;

    @Override
    public void save(CategoryVO categoryVO) {
        saveInfo(null, categoryVO);
    }

    @Override
    public void saveByToken(String token, CategoryVO categoryVO) {
        saveInfo(token, categoryVO);
    }

    /**
     * 新增文章类别
     *
     * @param token
     * @param categoryVO
     * @throws Exception
     */
    private void saveInfo(String token, CategoryVO categoryVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(categoryVO);
        params.put("categoryId", UUIDUtil.getUUID());
        if (token != null) {
            setAppSaveInfo(token, params);
        } else {
            setSaveInfo(params);
        }
        categoryDao.save(params);
    }

    @Override
    public void remove(List<String> ids) {
        removeInfo(null, ids);
    }

    @Override
    public void removeByToken(String token, List<String> ids) {
        removeInfo(token, ids);
    }

    /**
     * 删除文章类别
     *
     * @param token
     * @param ids
     */
    private void removeInfo(String token, List<String> ids) {
        Map<String, Object> params = getHashMap(3);
        params.put("categoryIds", ids);
        if (token != null) {
            setAppUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        categoryDao.remove(params);
    }

    @Override
    public void update(String categoryId, CategoryVO categoryVO) {
        updateInfo(null, categoryId, categoryVO);
    }

    @Override
    public void updateByToken(String token, String categoryId, CategoryVO categoryVO) {
        updateInfo(token, categoryId, categoryVO);
    }

    /**
     * 修改文章类别
     *
     * @param token
     * @param categoryId
     * @param categoryVO
     */
    private void updateInfo(String token, String categoryId, CategoryVO categoryVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(categoryVO);
        params.put("categoryId", categoryId);
        if (token != null) {
            setAppUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        categoryDao.update(params);
    }

    @Override
    public CategoryDTO get(String categoryId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("categoryId", categoryId);
        return categoryDao.get(params);
    }

    @Override
    public List<CategoryDTO> list(Map<String, Object> params) throws SearchException {
        return categoryDao.list(params);
    }

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

}
