package com.goldenwater.slgc.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goldenwater.common.utils.StringUtils;
import com.goldenwater.common.utils.SecurityUtils;
import com.goldenwater.common.utils.uuid.IdUtils;
import com.goldenwater.slgc.domain.CmsArticle;
import com.goldenwater.slgc.domain.CmsArticleFile;
import com.goldenwater.slgc.mapper.CmsArticleFileMapper;
import com.goldenwater.slgc.mapper.CmsArticleMapper;
import com.goldenwater.slgc.service.CmsArticleService;

/** 内容文章 业务实现 */
@Service
public class CmsArticleServiceImpl implements CmsArticleService {
    @Autowired
    private CmsArticleMapper cmsArticleMapper;
    @Autowired
    private CmsArticleFileMapper cmsArticleFileMapper;

    @Override
    public List<CmsArticle> selectCmsArticleList(CmsArticle cmsArticle) {
        return cmsArticleMapper.selectCmsArticleList(cmsArticle);
    }

    @Override
    public CmsArticle selectCmsArticleById(String aid) {
        CmsArticle article = cmsArticleMapper.selectCmsArticleById(aid);
        if (article != null) {
            CmsArticleFile query = new CmsArticleFile();
            query.setAid(aid);
            article.setArticleFiles(cmsArticleFileMapper.selectCmsArticleFileList(query));
        }
        return article;
    }

    @Override
    public int deleteCmsArticleByIds(String[] aids) {
        return cmsArticleMapper.deleteCmsArticleByIds(aids);
    }

    @Override
    public int insertCmsArticle(CmsArticle cmsArticle) {
        if (cmsArticle.getAid() == null || cmsArticle.getAid().isEmpty()) {
            cmsArticle.setAid(IdUtils.fastSimpleUUID());
        }
        // 作者为空时默认当前登录用户
        if (cmsArticle.getPubUser() == null || cmsArticle.getPubUser().isEmpty()) {
            cmsArticle.setPubUser(SecurityUtils.getUsername());
        }
        return cmsArticleMapper.insertCmsArticle(cmsArticle);
    }

    @Override
    public int updateCmsArticle(CmsArticle cmsArticle) {
        return cmsArticleMapper.updateCmsArticle(cmsArticle);
    }

    @Override
    @org.springframework.transaction.annotation.Transactional
    public int batchInsert(List<CmsArticle> list) {
        for (CmsArticle article : list) {
            if (article.getAid() == null || article.getAid().isEmpty()) {
                article.setAid(IdUtils.fastSimpleUUID());
            }
        }
        return cmsArticleMapper.batchInsert(list);
    }

    @Override
    @org.springframework.transaction.annotation.Transactional
    public int batchUpdate(List<CmsArticle> list) {
        return cmsArticleMapper.batchUpdate(list);
    }

    @Override
    public int selectTodayPublishedCount() {
        return cmsArticleMapper.selectTodayPublishedCount();
    }

    @Override
    public String importCmsArticle(List<CmsArticle> articleList, String cateId) {
        if (com.goldenwater.common.utils.StringUtils.isNull(articleList) || articleList.size() == 0) {
            throw new RuntimeException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CmsArticle article : articleList) {
            try {
                if (com.goldenwater.common.utils.StringUtils.isEmpty(article.getTitle())) {
                    throw new RuntimeException("标题不能为空");
                }
                // 自动生成AID
                if (article.getAid() == null || article.getAid().isEmpty()) {
                    article.setAid(IdUtils.fastSimpleUUID());
                }
                // 指定了目标分类则统一归类（如法规导入到"法律法规"分类）
                if (com.goldenwater.common.utils.StringUtils.isNotEmpty(cateId)) {
                    article.setCateId(cateId);
                }
                // 默认值：状态空则草稿，发布日期空则当前时间，作者空则当前登录用户
                if (article.getStatus() == null || article.getStatus().isEmpty()) {
                    article.setStatus("0");
                }
                if (article.getPubDate() == null) {
                    article.setPubDate(new Date());
                }
                if (article.getPubUser() == null || article.getPubUser().isEmpty()) {
                    article.setPubUser(SecurityUtils.getUsername());
                }
                article.setIsDel("0");
                cmsArticleMapper.insertCmsArticle(article);
                successNum++;
                successMsg.append("<br/>" + successNum + "、文章 " + article.getTitle() + " 导入成功");
            } catch (Exception e) {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、文章 " + article.getTitle() + " 导入失败：" + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new RuntimeException(failureMsg.toString());
        }
        return successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：").toString();
    }
}
