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

import com.backendsys.exception.CustException;
import com.backendsys.modules.cms.page.dao.PageDao;
import com.backendsys.modules.cms.page.dao.PageI18nDao;
import com.backendsys.modules.cms.page.entity.Page;
import com.backendsys.modules.cms.page.entity.PageI18n;
import com.backendsys.modules.cms.page.service.PageService;
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.LambdaQueryWrapper;
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 PageServiceImpl implements PageService {

    @Autowired
    private PageDao pageDao;
    @Autowired
    private PageI18nDao pageI18nDao;

    /**
     * 获取单页列表
     */
    @Override
    public PageEntity selectPageList(Page page) {
        PageUtils.startPage();  // 分页
        List<Page> list = pageDao.selectPageList(page);
        return new PageInfoResult(list).toEntity();
    }

    /**
     * 获取单页详情
     */
    @Override
    public Map<String, Object> selectPageDetail(Page page) {
        Map<String, Object> detail = pageDao.selectPageDetail(page);
        if (detail == null) throw new CustException("单页不存在");
        return detail;
    }

    /**
     * 获取单页详情 (公开)
     */
    @Override
    public Map<String, Object> selectPageDetailPublic(Page page) {
        Map<String, Object> detail = pageDao.selectPageDetailFull(page);
        if (detail == null) throw new CustException("单页不存在");
        return detail;
    }

    /**
     * 获取单页详情 (视图)
     */
    @Override
    public Page selectPage(String lang, String page_sign) {

        // [Get] 详情
        Page detail = pageDao.selectOne(new LambdaQueryWrapper<Page>().eq(Page::getPage_sign, page_sign));
        if (detail == null) return null;

        // [Get] 翻译
        LambdaQueryWrapper<PageI18n> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PageI18n::getLanguage, lang);
        wrapper.eq(PageI18n::getPage_sign, detail.getPage_sign());
        PageI18n i18nDetail = pageI18nDao.selectOne(wrapper);
        if (i18nDetail == null) return null;

        // [拼接] 详情 + 翻译
        detail.setTitle(i18nDetail.getTitle());
        detail.setDescription(i18nDetail.getDescription());
        detail.setContent(i18nDetail.getContent());
        detail.setMeta_keyword(i18nDetail.getMeta_keyword());
        detail.setMeta_description(i18nDetail.getMeta_description());

        return detail;

    }

    /**
     * 创建单页
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertPage(Page page) {

        Boolean isExist = pageDao.exists(new LambdaQueryWrapper<Page>().eq(Page::getPage_sign, page.getPage_sign()));
        if (isExist) throw new CustException("单页标识: " + page.getPage_sign() + " 已存在");

        // 插入
        pageDao.insert(page);
        // 批量插入 (翻译字段)
        List<PageI18n> translations = page.getTranslations().stream()
            .peek(item -> { ValidationUtil.validateObject(item, PageI18n.Create.class); })
            .map(item -> { item.setPage_sign(page.getPage_sign()); return item; })
            .collect(Collectors.toList());
        pageI18nDao.insertBatch(translations);
        return Map.of("page_sign", page.getPage_sign());
    }

    /**
     * 编辑单页
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> updatePage(Page page) {

        Boolean isExist = pageDao.exists(new LambdaQueryWrapper<Page>().eq(Page::getPage_sign, page.getPage_sign()));
        if (!isExist) throw new CustException("单页不存在");

        // 更新
        pageDao.update(page, new LambdaQueryWrapper<Page>().eq(Page::getPage_sign, page.getPage_sign()));
        // 批量更新 (翻译字段)
        List<PageI18n> translations = page.getTranslations().stream()
            .peek(item -> { ValidationUtil.validateObject(item, PageI18n.Update.class); })
            .map(t -> { t.setPage_sign(page.getPage_sign()); return t; })
            .collect(Collectors.toList());
        pageI18nDao.updateBatch(translations);

        return Map.of("page_sign", page.getPage_sign());
    }

    /**
     * 删除单页
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> deletePage(Page page) {

        Boolean isExist = pageDao.exists(new LambdaQueryWrapper<Page>().eq(Page::getPage_sign, page.getPage_sign()));
        if (!isExist) throw new CustException("单页不存在");

        pageDao.delete(new LambdaQueryWrapper<Page>().eq(Page::getPage_sign, page.getPage_sign()));
        pageI18nDao.delete(new LambdaQueryWrapper<PageI18n>().eq(PageI18n::getPage_sign, page.getPage_sign()));

        return Map.of("page_sign", page.getPage_sign());
    }

}
