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

import com.backendsys.exception.CustException;
import com.backendsys.modules.cms.article.entity.Article;
import com.backendsys.modules.cms.banner.dao.BannerDao;
import com.backendsys.modules.cms.banner.dao.BannerI18nDao;
import com.backendsys.modules.cms.banner.entity.Banner;
import com.backendsys.modules.cms.banner.entity.BannerI18n;
import com.backendsys.modules.cms.banner.service.BannerService;
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 BannerServiceImpl implements BannerService {

    @Autowired
    private BannerDao bannerDao;
    @Autowired
    private BannerI18nDao bannerI18nDao;

    /**
     * 获取幻灯片列表
     */
    @Override
    public PageEntity selectBannerList(Banner banner) {
        PageUtils.startPage();  // 分页
        List<Banner> list = bannerDao.selectBannerList(banner);
        return new PageInfoResult(list).toEntity();
    }

    /**
     * 获取幻灯片详情
     */
    @Override
    public Map<String, Object> selectBannerDetail(Banner banner) {
        return bannerDao.selectBannerDetail(banner);
    }


    /**
     * 创建幻灯片
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertBanner(Banner banner) {

        // 插入
        bannerDao.insert(banner);

        // 批量插入 (翻译字段)
        Long banner_id = banner.getId();
        List<BannerI18n> translations = banner.getTranslations().stream()
            .peek(item -> { ValidationUtil.validateObject(item, BannerI18n.Create.class); })
            .map(item -> { item.setBanner_id(banner_id); return item; })
            .collect(Collectors.toList());
        bannerI18nDao.insertBatch(translations);

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

    /**
     * 编辑幻灯片
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> updateBanner(Banner banner) {

        System.out.println(banner);

        Long banner_id = banner.getBanner_id();

        Banner detail = bannerDao.selectById(banner_id);
        if (detail == null) throw new CustException("幻灯片不存在");

        // 更新
        banner.setId(banner_id);
        bannerDao.updateById(banner);

        // 批量更新 (翻译字段)
        List<BannerI18n> translations = banner.getTranslations().stream()
            .peek(item -> { ValidationUtil.validateObject(item, BannerI18n.Create.class); })
            .map(item -> { item.setBanner_id(banner_id); return item; })
            .collect(Collectors.toList());

        System.out.println(translations);

        bannerI18nDao.updateBatch(translations);

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

    /**
     * 删除幻灯片
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> deleteBanner(Banner banner) {

        Long banner_id = banner.getBanner_id();

        Banner detail = bannerDao.selectById(banner_id);
        if (detail == null) throw new CustException("幻灯片不存在");

        bannerDao.deleteById(banner_id);
        bannerI18nDao.delete(new QueryWrapper<BannerI18n>().eq("banner_id", banner_id));

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

}
