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

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ss.article.domain.SmArticle;
import com.ruoyi.ss.article.domain.SmArticleQuery;
import com.ruoyi.ss.article.domain.enums.LicenceType;
import com.ruoyi.ss.article.mapper.SmArticleMapper;
import com.ruoyi.ss.article.service.ISmArticleService;
import com.ruoyi.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 文章Service业务层处理
 *
 * @author qiuzhenzhao
 * @date 2023-12-06
 */
@Service
@Slf4j
public class SmArticleServiceImpl implements ISmArticleService
{
    @Resource
    private SmArticleMapper articleMapper;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询文章
     *
     * @param articleId 文章主键
     * @return 文章
     */
    @Override
    public SmArticle selectSmArticleByArticleId(Long articleId)
    {
        SmArticle smArticle = articleMapper.selectSmArticleByArticleId(articleId);
        if(ObjectUtils.isNotEmpty(smArticle)){
            smArticle.setFormatCreateTime(DateUtils.getYYYY_MM_DD(smArticle.getCreateTime()));
        }
        return smArticle;
    }

    /**
     * 查询文章列表
     *
     * @param smArticle 文章
     * @return 文章
     */
    @Override
    public List<SmArticle> selectSmArticleList(SmArticleQuery smArticle)
    {
        List<SmArticle> smArticles = articleMapper.selectSmArticleList(smArticle);
        for (SmArticle smArticle1 : smArticles) {
            smArticle.setFormatCreateTime(DateUtils.getYYYY_MM_DD(smArticle1.getCreateTime()));
        }
        return smArticles;
    }

    /**
     * 新增文章
     *
     * @param smArticle 文章
     * @return 结果
     */
    @Override
    public int insertSmArticle(SmArticle smArticle)
    {
        smArticle.setCreateTime(DateUtils.getNowDate());
        return articleMapper.insertSmArticle(smArticle);
    }

    /**
     * 修改文章
     *
     * @param smArticle 文章
     * @return 结果
     */
    @Override
    public int updateSmArticle(SmArticle smArticle)
    {
        smArticle.setUpdateTime(DateUtils.getNowDate());
        return articleMapper.updateSmArticle(smArticle);
    }

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

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

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

    /**
     * 根据类型获取协议
     *
     * @param type 协议类型
     */
    @Override
    public SmArticle selectLicenceByType(LicenceType type) {
        if (type == null) {
            return null;
        }
        // 查询对应ID
        String idStr = configService.selectConfigByKey(type.getKey());
        if (!StringUtils.hasText(idStr)) {
            log.warn("key {} not has text, so return null.", type.getKey());
            return null;
        }
        // 查询ID对应的文章
        return this.selectSmArticleByArticleId(Long.parseLong(idStr));
    }
}
