package com.ruoyi.ss.article.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.ArticleClassify;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ss.article.domain.Article;
import com.ruoyi.ss.articleClassify.mapper.ArticleClassifyMapper;
import com.ruoyi.ss.article.mapper.ArticleMapper;
import com.ruoyi.ss.article.service.IArticleService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 文章Service业务层处理
 *
 * @author qiuzhenzhao
 * @date 2023-12-06
 */
@Service
public class ArticleServiceImpl implements IArticleService
{
    @Resource
    private ArticleMapper rlArticleMapper;

    @Resource
    private ArticleClassifyMapper articleClassifyMapper;
//
//    @Resource
//    private EtOperatingAreaMapper etOperatingAreaMapper;
//
//    @Autowired
//    private IWxPayService wxPayService;

    /**
     * 查询文章
     *
     * @param articleId 文章主键
     * @return 文章
     */
    @Override
    public Article selectAsArticleByArticleId(Long articleId)
    {
        Article etArticle = rlArticleMapper.selectAsArticleByArticleId(articleId);
        if(ObjectUtils.isNotEmpty(etArticle)){
            etArticle.setFormatCreateTime(DateUtils.getYYYY_MM_DD(etArticle.getCreateTime()));
        }
        return etArticle;
    }

    /**
     * 查询文章列表
     *
     * @param etArticle 文章
     * @return 文章
     */
    @Override
    @DataScope(userAlias = "u")
    public List<Article> selectAsArticleListByIsolate(Article etArticle)
    {
        List<Article> etArticles = rlArticleMapper.selectAsArticleList(etArticle);
        return etArticles;
    }

    /**
     * 查询文章列表
     *
     * @param etArticle 文章 隔离
     * @return 文章
     */
    @Override
    public List<Article> selectAsArticleList(Article etArticle)
    {
        List<Article> etArticles = rlArticleMapper.selectAsArticleList(etArticle);
        for (Article etArticle1 : etArticles) {
            etArticle.setFormatCreateTime(DateUtils.getYYYY_MM_DD(etArticle1.getCreateTime()));
        }
        return etArticles;
    }

    /**
     * 查询文章列表
     *
     * @param etArticle 文章 隔离
     * @return 文章
     */
    @Override
    public List<Article> selectAsArticleListByApp(Article etArticle)
    {
        List<Article> etArticles = rlArticleMapper.selectAsArticleListByApp(etArticle);
        List<Article> mergedArticles = new ArrayList<>();
        Set<String> seenArticleIds = new HashSet<>();

        for (Article etArticle1 : etArticles) {
            etArticle1.setFormatCreateTime(DateUtils.getYYYY_MM_DD(etArticle1.getCreateTime()));
            if (seenArticleIds.add(etArticle1.getArticleId().toString())) {
                mergedArticles.add(etArticle1);
            }
        }

        for (Article etArticle1 : etArticles) {
            if (etArticle1.getClassifyId().equals("111")) {
                List<ArticleClassify> classifies = articleClassifyMapper.selectChildrenClassifyById(Long.parseLong(etArticle1.getClassifyId()));
                for (ArticleClassify classify : classifies) {
                    Article etArticle2 = new Article();
                    etArticle2.setClassifyId(classify.getClassifyId().toString());
                    List<Article> etArticles1 = rlArticleMapper.selectAsArticleList(etArticle2);
                    for (Article etArticle2Item : etArticles1) {
                        if (seenArticleIds.add(etArticle2Item.getArticleId().toString())) {
                            mergedArticles.add(etArticle2Item);
                        }
                    }
                }
            }
        }
        etArticles = mergedArticles;
        return etArticles;
    }

    /**
     * 新增文章
     *
     * @param etArticle 文章
     * @return 结果
     */
    @Override
    public int insertAsArticle(Article etArticle)
    {
        etArticle.setCreateTime(DateUtils.getNowDate());
        return rlArticleMapper.insertAsArticle(etArticle);
    }

    /**
     * 修改文章
     *
     * @param etArticle 文章
     * @return 结果
     */
    @Override
    public int updateAsArticle(Article etArticle)
    {
        etArticle.setUpdateTime(DateUtils.getNowDate());
        return rlArticleMapper.updateAsArticle(etArticle);
    }

    /**
     * 批量删除文章
     *
     * @param articleIds 需要删除的文章主键
     * @return 结果
     */
    @Override
    public int deleteAsArticleByArticleIds(Long[] articleIds)
    {
        return rlArticleMapper.deleteAsArticleByArticleIds(articleIds);
    }

    /**
     * 删除文章信息
     *
     * @param articleId 文章主键
     * @return 结果
     */
    @Override
    public int deleteAsArticleByArticleId(Long articleId)
    {
        return rlArticleMapper.deleteAsArticleByArticleId(articleId);
    }

    /**
     * 根据文章id获取标签列表
     *
     * @param articleId 文章主键
     * @return 结果
     */
    @Override
    public String[] getTagList(Long articleId) {
        Article etArticle = rlArticleMapper.selectAsArticleByArticleId(articleId);
        String tag = etArticle.getTag();
        String[] split = tag.split(",");
        return split;
    }

//    /**
//     * 查询动态分类及数量
//     *
//     * @param
//     * @return 结果
//     */
//    @Override
//    public List<DynamicClassify> selectDynamicClassifyList() {
//        List<DynamicClassify> classifies = new ArrayList<>();
//        SysDictData dictData = new SysDictData();
//        dictData.setDictType("dynamic_classify");
//        List<SysDictData> sysDictData = dictDataMapper.selectDictDataList(dictData);
//        for (SysDictData one :sysDictData) {
//            DynamicClassify dynamicClassify = new DynamicClassify();
//            String dictValue = one.getDictValue();
//            dynamicClassify.setName(one.getDictLabel());
//            dynamicClassify.setValue(dictValue);
//            int classify = asArticleMapper.countDynamicClassify(dictValue);
//            dynamicClassify.setNum(classify);
//            classifies.add(dynamicClassify);
//        }
//        return classifies;
//    }
}
