package com.backendsys.modules.cms.article.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.backendsys.exception.CustException;
import com.backendsys.modules.cms.article.dao.ArticleCategoryDao;
import com.backendsys.modules.cms.article.dao.ArticleCategoryI18nDao;
import com.backendsys.modules.cms.article.entity.ArticleCategory;
import com.backendsys.modules.cms.article.entity.ArticleCategoryI18n;
import com.backendsys.modules.cms.article.service.ArticleCategoryService;
import com.backendsys.modules.common.utils.ValidationUtil;
import com.backendsys.utils.response.PageEntity;
import com.backendsys.utils.response.PageInfoResult;
import com.backendsys.utils.v2.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ArticleCategoryServiceImpl implements ArticleCategoryService {

    @Autowired
    private ArticleCategoryDao articleCategoryDao;
    @Autowired
    private ArticleCategoryI18nDao articleCategoryI18nDao;

    /**
     * 获取资讯分类列表
     */
    @Override
    public PageEntity selectArticleCategoryList(ArticleCategory articleCategory) {
        PageUtils.startPage();  // 分页
        List<ArticleCategory> list = articleCategoryDao.selectArticleCategoryList(articleCategory);
        return new PageInfoResult(list).toEntity();
    }

    @Override
    public List<ArticleCategory> selectArticleCategoryListPublic(ArticleCategory articleCategory) {
        return articleCategoryDao.selectArticleCategoryList(articleCategory);
    }

    /**
     * 获取资讯列表 (视图)
     */
    @Override
    public List<ArticleCategory> selectArticleCategory(ArticleCategory articleCategory) {
        return articleCategoryDao.selectArticleCategoryList(articleCategory);
    }

    /**
     * 获取资讯分类详情
     */
    @Override
    public Map<String, Object> selectArticleCategoryDetail(ArticleCategory article) {
        Map<String, Object> detail = articleCategoryDao.selectArticleCategoryDetail(article);
        if (detail == null) throw new CustException("资讯分类不存在");
        return detail;
    }

    /**
     * 创建资讯分类
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertArticleCategory(ArticleCategory articleCategory) {

        System.out.println(articleCategory);

        if (articleCategory.getParent_id() == null) articleCategory.setParent_id(-1L);

        // 插入
        articleCategoryDao.insert(articleCategory);

        Long article_category_id = articleCategory.getId();

        // 批量插入 (翻译字段)
        List<ArticleCategoryI18n> translations = articleCategory.getTranslations().stream()
            .peek(item -> { ValidationUtil.validateObject(item, ArticleCategoryI18n.Create.class); })
            .map(item -> { item.setArticle_category_id(article_category_id); return item; })
            .collect(Collectors.toList());

        System.out.println(translations);

        articleCategoryI18nDao.insertBatch(translations);

        return Map.of("article_category_id", article_category_id);
    }

    /**
     * 编辑资讯分类
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> updateArticleCategory(ArticleCategory articleCategory) {

        Long article_category_id = articleCategory.getArticle_category_id();

        ArticleCategory detail = articleCategoryDao.selectById(article_category_id);
        if (detail == null) throw new CustException("资讯分类不存在");

        // 更新
        if (ObjectUtil.isEmpty(articleCategory.getParent_id())) articleCategory.setParent_id(-1L);
        articleCategory.setId(article_category_id);
        articleCategoryDao.updateById(articleCategory);

        // 批量更新 (翻译字段)
        List<ArticleCategoryI18n> translations = articleCategory.getTranslations().stream()
            .peek(item -> { ValidationUtil.validateObject(item, ArticleCategoryI18n.Update.class); })
            .map(t -> { t.setArticle_category_id(article_category_id); return t; })
            .collect(Collectors.toList());
        articleCategoryI18nDao.updateBatch(translations);

        return Map.of("article_category_id", article_category_id);
    }

    /**
     * 删除资讯分类
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> deleteArticleCategory(ArticleCategory articleCategory) {

        Long article_category_id = articleCategory.getArticle_category_id();

        ArticleCategory detail = articleCategoryDao.selectById(article_category_id);
        if (detail == null) throw new CustException("资讯分类不存在");

        articleCategoryDao.deleteById(article_category_id);
        articleCategoryI18nDao.delete(new QueryWrapper<ArticleCategoryI18n>().eq("article_category_id", article_category_id));

        return Map.of("article_category_id", article_category_id);
    }

}
