package com.ruoyi.system.service.impl;

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

import com.ruoyi.brewery.domain.WyBeer;
import com.ruoyi.brewery.service.IWyBeerService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WyPlatformActivity;
import com.ruoyi.system.domain.WyPlatformActivityBeer;
import com.ruoyi.system.domain.WyPlatformFeatureBeer;
import com.ruoyi.system.service.IWyPlatformActivityService;
import com.ruoyi.system.service.IWyPlatformFeatureBeerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WyPlatformFeaturePageMapper;
import com.ruoyi.system.domain.WyPlatformFeaturePage;
import com.ruoyi.system.service.IWyPlatformFeaturePageService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 专题页Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-06
 */
@Service
public class WyPlatformFeaturePageServiceImpl implements IWyPlatformFeaturePageService {
    @Resource
    private WyPlatformFeaturePageMapper wyPlatformFeaturePageMapper;

    @Resource
    private IWyPlatformFeatureBeerService wyPlatformFeatureBeerService;
    @Resource
    private IWyBeerService wyBeerService;

    @Autowired
    private IWyPlatformActivityService wyPlatformActivityService;

    /**
     * 查询专题页
     *
     * @param id 专题页主键
     * @return 专题页
     */
    @Override
    public WyPlatformFeaturePage selectWyPlatformFeaturePageById(Long id) {
//        final List<WyPlatformFeatureBeer> wyPlatformFeatureBeers = wyPlatformFeatureBeerService.selectWyPlatformFeatureBeerByPageId(id);
        final WyPlatformFeaturePage platformFeaturePage = wyPlatformFeaturePageMapper.selectWyPlatformFeaturePageById(id);
        if (platformFeaturePage == null) {
            return null;
        }
        final WyPlatformActivity platformActivity = wyPlatformActivityService.selectWyPlatformActivityById(platformFeaturePage.getPlatformActivityId());
        if (platformActivity == null) {
            return null;
        }
        platformFeaturePage.setActivity(platformActivity);
        platformFeaturePage.setBeers(platformActivity.getBeers());
        return platformFeaturePage;
    }

    /**
     * 查询专题页列表
     *
     * @param wyPlatformFeaturePage 专题页
     * @return 专题页
     */
    @Override
    public List<WyPlatformFeaturePage> selectWyPlatformFeaturePageList(WyPlatformFeaturePage wyPlatformFeaturePage) {
        final List<WyPlatformFeaturePage> wyPlatformFeaturePages = wyPlatformFeaturePageMapper.selectWyPlatformFeaturePageList(wyPlatformFeaturePage);
        wyPlatformFeaturePages.forEach(page -> {
            WyPlatformActivity platformActivity = wyPlatformActivityService.selectWyPlatformActivityById(page.getPlatformActivityId());
            page.setActivity(platformActivity);
            if (platformActivity != null) {
                page.setBeers(platformActivity.getBeers());
            }
        });
        return wyPlatformFeaturePages;
    }

    /**
     * 新增专题页
     *
     * @param wyPlatformFeaturePage 专题页
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWyPlatformFeaturePage(WyPlatformFeaturePage wyPlatformFeaturePage) {
//        if(wyPlatformFeaturePage.getBeerIds() == null){
//           throw new RuntimeException("beerIds不能为空");
//        }
        if (wyPlatformFeaturePage.getPlatformActivityId() == null) {
            throw new RuntimeException("活动id不能为空");
        }
        if (wyPlatformFeaturePage.getCover() == null) {
            throw new RuntimeException("封面图片不能为空");
        }
        wyPlatformFeaturePage.setCreateTime(DateUtils.getNowDate());
        wyPlatformFeaturePageMapper.insertWyPlatformFeaturePage(wyPlatformFeaturePage);
//        wyPlatformFeaturePage.getBeerIds().forEach(beerId -> {
//            final WyPlatformFeatureBeer wyPlatformFeatureBeer = new WyPlatformFeatureBeer();
//            wyPlatformFeatureBeer.setBeerId(beerId);
//            wyPlatformFeatureBeer.setPageId(wyPlatformFeaturePage.getId());
//            wyPlatformFeatureBeerService.insertWyPlatformFeatureBeer(wyPlatformFeatureBeer);
//        });
        return 1;
    }

    /**
     * 修改专题页
     *
     * @param wyPlatformFeaturePage 专题页
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWyPlatformFeaturePage(WyPlatformFeaturePage wyPlatformFeaturePage) {
        if (wyPlatformFeaturePage.getPlatformActivityId() == null) {
            throw new RuntimeException("活动id不能为空");
        }
        if (wyPlatformFeaturePage.getCover() == null) {
            throw new RuntimeException("封面图片不能为空");
        }
//        final WyPlatformFeatureBeer wyPlatformFeatureBeer = new WyPlatformFeatureBeer();
//        final List<WyPlatformFeatureBeer> wyPlatformFeatureBeers = wyPlatformFeatureBeerService.selectWyPlatformFeatureBeerList(wyPlatformFeatureBeer);
//        if(wyPlatformFeaturePage.getBeerIds() == null && wyPlatformFeatureBeers==null){
//            throw new RuntimeException("beerIds不能为空");
//        }
        wyPlatformFeaturePage.setUpdateTime(DateUtils.getNowDate());
        wyPlatformFeaturePageMapper.updateWyPlatformFeaturePage(wyPlatformFeaturePage);
//        wyPlatformFeatureBeerService.deleteWyPlatformFeatureBeerByPageId(wyPlatformFeaturePage.getId()); //删除原有关联
//        wyPlatformFeaturePage.getBeerIds().forEach(beerId -> {
//            final WyPlatformFeatureBeer platformFeatureBeer = new WyPlatformFeatureBeer();
//            platformFeatureBeer.setBeerId(beerId);
//            platformFeatureBeer.setPageId(wyPlatformFeaturePage.getId());
//            wyPlatformFeatureBeerService.insertWyPlatformFeatureBeer(platformFeatureBeer);
//        });
        return 1;
    }

    /**
     * 批量删除专题页
     *
     * @param ids 需要删除的专题页主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWyPlatformFeaturePageByIds(Long[] ids) {
        for (Long id : ids) {
            wyPlatformFeatureBeerService.deleteWyPlatformFeatureBeerByPageId(id); //删除原有关联
        }
        return wyPlatformFeaturePageMapper.deleteWyPlatformFeaturePageByIds(ids);
    }

    /**
     * 删除专题页信息
     *
     * @param id 专题页主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWyPlatformFeaturePageById(Long id) {
        wyPlatformFeatureBeerService.deleteWyPlatformFeatureBeerByPageId(id); //删除原有关联
        return wyPlatformFeaturePageMapper.deleteWyPlatformFeaturePageById(id);
    }
}
