package com.slodon.b2b2c.model.cms;

import com.slodon.b2b2c.cms.dto.ArticleCategoryAddDTO;
import com.slodon.b2b2c.cms.dto.ArticleCategoryUpdateDTO;
import com.slodon.b2b2c.cms.example.HelpArticleCategoryExample;
import com.slodon.b2b2c.cms.example.HelpArticleExample;
import com.slodon.b2b2c.core.constant.ArticleConst;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.dao.read.cms.HelpArticleCategoryReadMapper;
import com.slodon.b2b2c.dao.read.cms.HelpArticleReadMapper;
import com.slodon.b2b2c.dao.write.cms.HelpArticleCategoryWriteMapper;
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;

/**
 * 帮助文章管理的model
 */
@Component
@Slf4j
public class HelpArticleCategoryModel {

        @Resource
        private HelpArticleCategoryReadMapper helpArticleCategoryReadMapper;
        @Resource
        private HelpArticleCategoryWriteMapper helpArticleCategoryWriteMapper;
        @Resource
        private HelpArticleReadMapper helpArticleReadMapper;

        /**
         * 新增文章分类
         *
         * @param
         * @return
         */
        public Integer saveArticleCategory(StoreCmsArticleCategory storeCmsArticleCategory) {
        int count = helpArticleCategoryWriteMapper.insert(storeCmsArticleCategory);
        if (count == 0) {
            throw new MallException("添加文章分类失败，请重试");
        }
        return count;
    }

        /**
         * 新增文章分类
         *
         * @param articleCategoryAddDTO
         * @param admin
         * @return
         */
        public Integer saveArticleCategory(ArticleCategoryAddDTO articleCategoryAddDTO, Admin admin) throws Exception {
        //判断分类名称是否重复
        HelpArticleCategoryExample articleCategoryExample = new HelpArticleCategoryExample();
        //去掉头尾空格字符
        articleCategoryExample.setCategoryName(StringUtil.trim(articleCategoryAddDTO.getCategoryName()));
        List<StoreCmsArticleCategory> list = helpArticleCategoryReadMapper.listByExample(articleCategoryExample);
        if (!CollectionUtils.isEmpty(list)) {
            throw new MallException("分类名称重复，请重新输入");
        }

        StoreCmsArticleCategory articleCategoryInsert = new StoreCmsArticleCategory();
        articleCategoryInsert.setCreateId(admin.getAdminId());
        articleCategoryInsert.setCategoryName(articleCategoryAddDTO.getCategoryName());
        articleCategoryInsert.setSort(articleCategoryAddDTO.getSort());
        articleCategoryInsert.setIsShow(ArticleConst.STATE_YES);
        articleCategoryInsert.setCreateTime(new Date());
        int count = helpArticleCategoryWriteMapper.insert(articleCategoryInsert);
        if (count == 0) {
            throw new MallException("添加文章分类失败，请重试");
        }
        return articleCategoryInsert.getCategoryId();
    }

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

        /**
         * 批量删除文章分类
         *
         * @param categoryIds categoryIds
         * @return
         */
        public Integer batchDeleteArticle(String categoryIds) {
        String[] idStr = categoryIds.split(",");
        for (String categoryId : idStr) {
            //判断分类下是否有文章
            HelpArticleExample articleExample = new HelpArticleExample();
            articleExample.setCategoryId(Integer.parseInt(categoryId));
            List<StoreCmsArticle> list = helpArticleReadMapper.listByExample(articleExample);
            if (!CollectionUtils.isEmpty(list)) {
                StoreCmsArticleCategory category = helpArticleCategoryReadMapper.getByPrimaryKey(categoryId);
                if (category == null) {
                    continue;
                }
                throw new MallException("请先删除该分类[" + category.getCategoryName() + "]下的所有文章");
            }
        }
        HelpArticleCategoryExample example = new HelpArticleCategoryExample();
        example.setCategoryIdIn(categoryIds);
        int count = helpArticleCategoryWriteMapper.deleteByExample(example);
        if (count == 0) {
            log.error("根据categoryIds：" + categoryIds + "批量删除文章分类失败");
            throw new MallException("删除文章分类失败,请重试");
        }
        return count;
    }

        /**
         * 根据categoryId更新文章分类
         *
         * @param articleCategory
         * @return
         */
        public Integer updateArticleCategory(StoreCmsArticleCategory articleCategory) {
        if (StringUtils.isEmpty(articleCategory.getCategoryId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = helpArticleCategoryWriteMapper.updateByPrimaryKeySelective(articleCategory);
        if (count == 0) {
            log.error("根据categoryId：" + articleCategory.getCategoryId() + "更新文章分类失败");
            throw new MallException("更新文章分类失败,请重试");
        }
        return count;
    }

        /**
         * 根据categoryId更新文章分类
         *
         * @param articleCategoryUpdateDTO
         * @return
         */
        public Integer updateArticleCategory(ArticleCategoryUpdateDTO articleCategoryUpdateDTO) throws Exception {
        if (StringUtils.isEmpty(articleCategoryUpdateDTO.getIsShow())) {
            if (StringUtils.isEmpty(articleCategoryUpdateDTO.getCategoryName())) {
                throw new MallException("请填写分类名称");
            }

            //判断分类名称是否重复
            HelpArticleCategoryExample articleCategoryExample = new HelpArticleCategoryExample();
            articleCategoryExample.setCategoryIdNotEquals(articleCategoryUpdateDTO.getCategoryId());
            articleCategoryExample.setCategoryName(StringUtil.trim(articleCategoryUpdateDTO.getCategoryName()));
            List<StoreCmsArticleCategory> list = helpArticleCategoryReadMapper.listByExample(articleCategoryExample);
            if (!CollectionUtils.isEmpty(list)) {
                throw new MallException("分类名称重复，请重新输入");
            }
        }
        StoreCmsArticleCategory articleCategoryUpdate = new StoreCmsArticleCategory();
        PropertyUtils.copyProperties(articleCategoryUpdate, articleCategoryUpdateDTO);
        articleCategoryUpdate.setUpdateTime(new Date());
        int count = helpArticleCategoryWriteMapper.updateByPrimaryKeySelective(articleCategoryUpdate);
        if (count == 0) {
            log.error("根据categoryId：" + articleCategoryUpdate.getCategoryId() + "更新文章分类失败");
            throw new MallException("更新文章分类失败,请重试");
        }
        return count;
    }

        /**
         * 根据categoryId获取文章分类详情
         *
         * @param categoryId categoryId
         * @return
         */
        public StoreCmsArticleCategory getArticleCategoryByCategoryId(Integer categoryId) {
        return helpArticleCategoryReadMapper.getByPrimaryKey(categoryId);
    }

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


}
