package com.vote.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.vctgo.common.core.exception.ServiceException;
import com.vote.system.domain.VoteEventNews;
import com.vote.system.domain.VoteEventNewsCarousels;
import com.vote.system.mapper.VoteEventNewsMapper;
import com.vote.system.service.VoteEventNewsCarouselsService;
import com.vote.system.mapper.VoteEventNewsCarouselsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author MacBook
 * @description 针对表【vote_event_news_carousels(活动资讯轮播图表)】的数据库操作Service实现
 * @createDate 2024-08-11 17:57:14
 */
@Service
public class VoteEventNewsCarouselsServiceImpl extends ServiceImpl<VoteEventNewsCarouselsMapper, VoteEventNewsCarousels>
        implements VoteEventNewsCarouselsService {

    @Autowired
    private VoteEventNewsCarouselsMapper voteEventNewsCarouselsMapper;


    @Autowired
    private VoteEventNewsMapper voteEventNewsMapper;

    @Override
    public List<VoteEventNewsCarousels> chartList(VoteEventNewsCarousels voteEventNewsCarousels) {
        LambdaQueryWrapper<VoteEventNewsCarousels> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VoteEventNewsCarousels::getEventId, voteEventNewsCarousels.getEventId());
        wrapper.orderByDesc(VoteEventNewsCarousels::getSortOrder);
        List<VoteEventNewsCarousels> newsCarousels = voteEventNewsCarouselsMapper.selectList(wrapper);
        LambdaQueryWrapper<VoteEventNews> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VoteEventNews::getEventId, voteEventNewsCarousels.getEventId());
        List<VoteEventNews> eventNews = voteEventNewsMapper.selectList(queryWrapper);
        // 创建一个Map，用于快速查找eventNews的title
        Map<Long, String> eventNewsMap = eventNews.stream().collect(Collectors.toMap(VoteEventNews::getId, VoteEventNews::getTitle));
        // 对比主键并设置title
        for (VoteEventNewsCarousels carousel : newsCarousels) {
            if (eventNewsMap.containsKey(carousel.getNewsId())) {
                carousel.setTitle(eventNewsMap.get(carousel.getNewsId()));
            }
        }
        return newsCarousels;
    }

    @Override
    @Transactional
    public int saveCarousels(VoteEventNewsCarousels voteEventNewsCarousels) {
        LambdaQueryWrapper<VoteEventNewsCarousels> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VoteEventNewsCarousels::getNewsId, voteEventNewsCarousels.getNewsId());
        List<VoteEventNewsCarousels> newsCarousels = voteEventNewsCarouselsMapper.selectList(wrapper);
        if (newsCarousels.size() > 1) {
            throw new ServiceException("该资讯已经添加轮播图,不能继续添加");
        }
        return voteEventNewsCarouselsMapper.insert(voteEventNewsCarousels);
    }

}




