package com.slodon.b2b2c.model.cms;

import com.slodon.b2b2c.cms.dto.ArticleAddDTO;
import com.slodon.b2b2c.cms.dto.ArticleUpdateDTO;
import com.slodon.b2b2c.cms.example.HelpArticleExample;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.read.cms.HelpArticleCategoryReadMapper;
import com.slodon.b2b2c.dao.read.cms.HelpArticleReadMapper;
import com.slodon.b2b2c.dao.write.cms.HelpArticleWriteMapper;
import com.slodon.b2b2c.seller.pojo.StoreCmsArticle;
import com.slodon.b2b2c.seller.pojo.StoreCmsArticleCategory;
import com.slodon.b2b2c.system.pojo.Admin;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class HelpArticleModel {

    @Resource
    private HelpArticleReadMapper helpArticleReadMapper;
    @Resource
    private HelpArticleWriteMapper helpArticleWriteMapper;
    @Resource
    private HelpArticleCategoryReadMapper helpArticleCategoryReadMapper;

    /**
     * 新增文章
     *
     * @param article
     * @return
     */
    public Integer saveArticle(StoreCmsArticle article) {
        int count = helpArticleWriteMapper.insert(article);
        if (count == 0) {
            throw new MallException("添加文章失败，请重试");
        }
        return article.getArticleId();
    }

    /**
     * 新增文章
     *
     * @param articleAddDTO
     * @param admin
     * @return
     */
    public Integer saveArticle(ArticleAddDTO articleAddDTO, Admin admin) throws Exception {
        //根据文章标题查重
        HelpArticleExample articleExample = new HelpArticleExample();
        articleExample.setWebSite("1");
        articleExample.setTitle(articleAddDTO.getTitle());
        List<StoreCmsArticle> list = helpArticleReadMapper.listByExample(articleExample);
        if (!CollectionUtils.isEmpty(list)) {
            throw new MallException("文章标题重复，请重新填写");
        }
        StoreCmsArticle articleInsert = new StoreCmsArticle();
        PropertyUtils.copyProperties(articleInsert, articleAddDTO);
        articleInsert.setWebSite("1");
        articleInsert.setCreateTime(new Date());
        articleInsert.setCreateId(admin.getAdminId());
        int count = helpArticleWriteMapper.insert(articleInsert);
        if (count == 0) {
            throw new MallException("添加文章失败，请重试");
        }
        return articleInsert.getArticleId();
    }

    /**
     * 根据articleId删除文章
     *
     * @param articleId articleId
     * @return
     */
    public Integer deleteArticle(Integer articleId) {
        if (StringUtils.isEmpty(articleId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = helpArticleWriteMapper.deleteByPrimaryKey(articleId);
        if (count == 0) {
            log.error("根据articleId：" + articleId + "删除文章失败");
            throw new MallException("删除文章失败,请重试");
        }
        return count;
    }

    /**
     * 批量删除文章
     *
     * @param ids ids
     * @return
     */
    public Integer batchDeleteArticle(String ids) {
        HelpArticleExample example = new HelpArticleExample();
        example.setArticleIdIn(ids);
        int count = helpArticleWriteMapper.deleteByExample(example);
        if (count == 0) {
            log.error("根据ids：" + ids + "批量删除文章失败");
            throw new MallException("删除文章失败,请重试");
        }
        return count;
    }

    /**
     * 根据articleId更新文章
     *
     * @param article
     * @return
     */
    public Integer updateArticle(StoreCmsArticle article) {
        if (StringUtils.isEmpty(article.getArticleId())) {
            throw new MallException("请选择要修改的数据");
        }

        int count = helpArticleWriteMapper.updateByPrimaryKeySelective(article);
        if (count == 0) {
            log.error("根据articleId：" + article.getArticleId() + "更新文章失败");
            throw new MallException("更新文章失败,请重试");
        }
        return count;
    }

    /**
     * 根据articleId更新文章
     *
     * @param articleUpdateDTO
     * @return
     */
    public Integer updateArticle(ArticleUpdateDTO articleUpdateDTO) throws Exception {
        if (StringUtils.isEmpty(articleUpdateDTO.getArticleId())) {
            throw new MallException("请选择要修改的数据");
        }
        //根据文章标题查重
        HelpArticleExample articleExample = new HelpArticleExample();
        articleExample.setArticleIdNotEquals(articleUpdateDTO.getArticleId());
        articleExample.setWebSite("1");
        articleExample.setTitle(articleUpdateDTO.getTitle());
        List<StoreCmsArticle> list = helpArticleReadMapper.listByExample(articleExample);
        if (!CollectionUtils.isEmpty(list)) {
            throw new MallException("文章标题重复，请重新填写");
        }
        StoreCmsArticle articleUpdate = new StoreCmsArticle();
        PropertyUtils.copyProperties(articleUpdate, articleUpdateDTO);
        articleUpdate.setWebSite("1");
        articleUpdate.setUpdateTime(new Date());
        int count = helpArticleWriteMapper.updateByPrimaryKeySelective(articleUpdate);
        if (count == 0) {
            log.error("根据articleId：" + articleUpdateDTO.getArticleId() + "更新文章失败");
            throw new MallException("更新文章失败,请重试");
        }
        return count;
    }

    /**
     * 根据articleId获取文章详情
     *
     * @param articleId articleId
     * @return
     */
    public StoreCmsArticle getArticleByArticleId(Integer articleId) {
        StoreCmsArticle article = helpArticleReadMapper.getByPrimaryKey(articleId);
        if (article == null) {
            throw new MallException("获取文章详情为空，请重试");
        }
        StoreCmsArticleCategory category = helpArticleCategoryReadMapper.getByPrimaryKey(article.getCategoryId());
        article.setCategoryName(category.getCategoryName());
        return article;
    }

    /**
     * 根据条件获取文章列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<StoreCmsArticle> getArticleList(HelpArticleExample example, PagerInfo pager) {
        List<StoreCmsArticle> articleList;
        if (pager != null) {
            pager.setRowsCount(helpArticleReadMapper.countByExample(example));
            articleList = helpArticleReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            articleList = helpArticleReadMapper.listByExample(example);
        }
        return articleList;
    }
}