package com.ruoyi.liandao.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.query.LambdaQueryWrapperX;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.liandao.domain.ArticleContent;
import com.ruoyi.liandao.domain.ArticleType;
import com.ruoyi.liandao.dto.articleContent.ArticleContentAddReqDto;
import com.ruoyi.liandao.dto.articleContent.ArticleContentEditReqDto;
import com.ruoyi.liandao.dto.articleContent.ArticleContentPageReqDto;
import com.ruoyi.liandao.mapper.ArticleContentMapper;
import com.ruoyi.liandao.service.ArticleContentService;
import com.ruoyi.liandao.service.ArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author admin
 * @description 针对表【ld_article_content(文章内容)】的数据库操作Service实现
 * @createDate 2024-03-14 10:51:08
 */
@Service
@DataSource(DataSourceType.MASTER)
public class ArticleContentServiceImpl extends ServiceImpl<ArticleContentMapper, ArticleContent>
        implements ArticleContentService {

    @Autowired
    private ArticleTypeService articleTypeService;

    @Override
    public IPage<ArticleContent> getPage(ArticleContentPageReqDto dto) {
        List<Long> typeIdList = articleTypeService.getChildTypeIdList(dto.getArticleTypeId());
        return page((IPage<ArticleContent>) dto.getPage(), new LambdaQueryWrapperX<ArticleContent>()
                .inIfPresent(ArticleContent::getArticleTypeId, typeIdList)
                .likeIfPresent(ArticleContent::getAuthor, dto.getAuthor())
                .likeIfPresent(ArticleContent::getTittle, dto.getTittle())
                .betweenIfPresent(ArticleContent::getSubmitTime, dto.getStartTime(), dto.getEndTime())
                .eqIfPresent(ArticleContent::getLanguage, dto.getLanguage())
                .select(ArticleContent::getId,
                        ArticleContent::getArticleTypeId,
                        ArticleContent::getArticleType,
                        ArticleContent::getTittle,
                        ArticleContent::getAuthor,
                        ArticleContent::getSubmitTime,
                        ArticleContent::getImg,
                        ArticleContent::getExcerpt,
                        ArticleContent::getLanguage)
                .orderByDesc(ArticleContent::getUpdateTime));
    }

    @Override
    public void add(ArticleContentAddReqDto dto) {
        ArticleType articleType = articleTypeService.getById(dto.getArticleTypeId());
        ArticleContent content = Convert.convert(ArticleContent.class, dto);
        content.setArticleType(articleType.getName());
        content.setSubmitTime(DateUtils.getNowDate());
        save(content);
//        addArticlesInForeignLanguage(content);
    }

    @Override
    public void edit(ArticleContentEditReqDto dto) {
        ArticleType articleType = articleTypeService.getById(dto.getArticleTypeId());
        ArticleContent content = Convert.convert(ArticleContent.class, dto);
        content.setArticleType(articleType.getName());
        updateById(content);
    }

    private void addArticlesInForeignLanguage(ArticleContent content){
        String tittle = content.getTittle();
        Long articleTypeId = content.getArticleTypeId();

        List<ArticleContent> zhArticle = list(new LambdaQueryWrapperX<ArticleContent>()
                .eq(ArticleContent::getTittle, tittle)
                .eq(ArticleContent::getArticleTypeId, articleTypeId)
                .eq(ArticleContent::getLanguage, "zh"));
        List<ArticleContent> jpArticle = list(new LambdaQueryWrapperX<ArticleContent>()
                .eq(ArticleContent::getTittle, tittle)
                .eq(ArticleContent::getArticleTypeId, articleTypeId)
                .eq(ArticleContent::getLanguage, "jp"));
        List<ArticleContent> enArticle = list(new LambdaQueryWrapperX<ArticleContent>()
                .eq(ArticleContent::getTittle, tittle)
                .eq(ArticleContent::getArticleTypeId, articleTypeId)
                .eq(ArticleContent::getLanguage, "en"));
        List<ArticleContent> koArticle = list(new LambdaQueryWrapperX<ArticleContent>()
                .eq(ArticleContent::getTittle, tittle)
                .eq(ArticleContent::getArticleTypeId, articleTypeId)
                .eq(ArticleContent::getLanguage, "ko"));
        if(CollUtil.isEmpty(zhArticle)){
            content.setId(null);
            content.setLanguage("zh");
            save(content);
        }
        if(CollUtil.isEmpty(jpArticle)){
            content.setId(null);
            content.setLanguage("jp");
            save(content);
        }
        if(CollUtil.isEmpty(enArticle)){
            content.setId(null);
            content.setLanguage("en");
            save(content);
        }
        if(CollUtil.isEmpty(koArticle)){
            content.setId(null);
            content.setLanguage("ko");
            save(content);
        }
    }
}




