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

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.backendsys.exception.CustException;
import com.backendsys.modules.cms.article.entity.ArticleCategoryI18n;
import com.backendsys.modules.cms.navigation.dao.NavigationDao;
import com.backendsys.modules.cms.navigation.dao.NavigationI18nDao;
import com.backendsys.modules.cms.navigation.entity.Navigation;
import com.backendsys.modules.cms.navigation.entity.NavigationI18n;
import com.backendsys.modules.cms.navigation.service.NavigationService;
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 NavigationServiceImpl implements NavigationService {

    @Autowired
    private NavigationDao navigationDao;
    @Autowired
    private NavigationI18nDao navigationI18nDao;

    /**
     * 获取导航列表
     */
    @Override
    public PageEntity selectNavigationList(Navigation navigation) {
        PageUtils.startPage();  // 分页
        List<Navigation> list = navigationDao.selectNavigationList(navigation);
        return new PageInfoResult(list).toEntity();
    }
    /**
     * 获取导航列表 (模板引擎)
     */
    public List<Navigation> selectNavigationTemplate(String lang) {
        Navigation navigation = new Navigation();
        navigation.setLang(lang);
        return navigationDao.selectNavigationList(navigation);
    }

    /**
     * 获取导航详情
     */
    @Override
    public Map<String, Object> selectNavigationDetail(Navigation navigation) {
        Map<String, Object> detail = navigationDao.selectNavigationDetail(navigation);
        if (detail == null) throw new CustException("导航不存在");
        return detail;
    }

    /**
     * 创建导航
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertNavigation(Navigation navigation) {

        // 插入
        navigationDao.insert(navigation);
        Long navigation_id = navigation.getId();

        // 批量插入 (翻译字段)
        List<NavigationI18n> translations = navigation.getTranslations().stream()
            .peek(item -> { ValidationUtil.validateObject(item, NavigationI18n.Create.class); })
            .map(item -> { item.setNavigation_id(navigation_id); return item; })
            .collect(Collectors.toList());
        navigationI18nDao.insertBatch(translations);

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

    /**
     * 编辑导航
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> updateNavigation(Navigation navigation) {
        Long navigation_id = navigation.getNavigation_id();

        Navigation navigationDetail = navigationDao.selectById(navigation_id);
        if (navigationDetail == null) throw new CustException("导航不存在");

        // 更新
        if (ObjectUtil.isEmpty(navigation.getParent_id())) navigation.setParent_id(-1L);
        navigation.setId(navigation_id);
        navigationDao.updateById(navigation);

        // 批量更新 (翻译字段)
        List<NavigationI18n> translations = navigation.getTranslations().stream()
            .peek(item -> { ValidationUtil.validateObject(item, NavigationI18n.Update.class); })
            .map(item -> { item.setNavigation_id(navigation_id); return item; })
            .collect(Collectors.toList());
        navigationI18nDao.updateBatch(translations);

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

    /**
     * 删除导航
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> deleteNavigation(Navigation navigation) {
        Long navigation_id = navigation.getNavigation_id();

        Navigation navigationDetail = navigationDao.selectById(navigation_id);
        if (navigationDetail == null) throw new CustException("导航不存在");

        navigationDao.deleteById(navigation_id);
        navigationI18nDao.delete(new QueryWrapper<NavigationI18n>().eq("navigation_id", navigation_id));

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

    /**
     * 删除导航 (批量)
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> deleteNavigationBatch(Navigation navigation) {

        List<Long> navigation_ids = navigation.getNavigation_ids();

        navigationDao.deleteBatchIds(navigation_ids);
        navigationI18nDao.delete(new QueryWrapper<NavigationI18n>().in("navigation_id", navigation_ids));

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

}
