package com.vote.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vctgo.common.core.text.Convert;
import com.vctgo.common.core.utils.ServletUtils;
import com.vctgo.common.mybatisplus.constant.MybatisPageConstants;
import com.vote.system.domain.VoteEventNews;
import com.vote.system.domain.dto.VoteEventNewsDto;
import com.vote.system.domain.eunm.VoteEventType;
import com.vote.system.service.VoteEventNewsService;
import com.vote.system.mapper.VoteEventNewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author MacBook
 * @description 针对表【vote_event_news(活动资讯表)】的数据库操作Service实现
 * @createDate 2024-08-11 17:57:11
 */
@Service
public class VoteEventNewsServiceImpl extends ServiceImpl<VoteEventNewsMapper, VoteEventNews>
        implements VoteEventNewsService {

    @Autowired
    private VoteEventNewsMapper voteEventNewsMapper;


    /**
     * 分页查询 活动资讯
     *
     * @param voteEventNewsDto
     * @return
     */
    @Override
    public IPage<VoteEventNews> selectByPage(VoteEventNewsDto voteEventNewsDto) {
        // 提取并转换分页参数
        long pageNum = Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM), 1L);
        long pageSize = Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE), 10L);
        // 构建查询条件
        LambdaQueryWrapper<VoteEventNews> wrapper = new LambdaQueryWrapper<>();
        if (voteEventNewsDto.getKeyword() != null) {
            wrapper.like(VoteEventNews::getTitle, voteEventNewsDto.getKeyword());
        }
        if (voteEventNewsDto.getStatus() != null) {
            wrapper.eq(VoteEventNews::getStatus, voteEventNewsDto.getStatus());
        }
        wrapper.eq(VoteEventNews::getEventId, voteEventNewsDto.getEventId());
        wrapper.orderByDesc(VoteEventNews::getCreatedAt);
        // 查询结果
        List<VoteEventNews> eventNews = voteEventNewsMapper.selectList(wrapper);
        if (eventNews != null && !eventNews.isEmpty()) {
            // 置顶新闻列表
            List<VoteEventNews> topNews = new ArrayList<>();
            // 非置顶新闻列表
            List<VoteEventNews> normalNews = new ArrayList<>();
            // 将新闻分类为置顶和非置顶
            eventNews.forEach(item -> {
                item.setCode(item.getId().toString());
                if ("0".equals(item.getIsTop())) {
                    topNews.add(item);
                } else {
                    normalNews.add(item);
                }
            });
            // 按创建时间降序排列置顶新闻
            topNews.sort(Comparator.comparing(VoteEventNews::getCreatedAt).reversed());
            // 合并置顶和非置顶新闻
            topNews.addAll(normalNews);
            eventNews = topNews;
        }
        // 分页处理
        Page<VoteEventNews> page = new Page<>(pageNum, pageSize);
        // 分页的实际数据集合
        List<VoteEventNews> pagedEventNews = eventNews.stream()
                .skip((pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());
        page.setRecords(pagedEventNews);
        page.setTotal(eventNews.size());
        return page;
    }


    /**
     * 添加 活动资讯
     */
    @Override
    @Transactional
    public int addEventsNews(VoteEventNews voteEventNews) {
        System.out.println(voteEventNews);
        return voteEventNewsMapper.insert(voteEventNews);
    }


    @Override
    public VoteEventNews selectById(Long id) {
        VoteEventNews voteEventNews = voteEventNewsMapper.selectById(id);
        return voteEventNews;
    }

    @Override
    public List<VoteEventNews> getNewsTop(Long eventId) {
        LambdaQueryWrapper<VoteEventNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VoteEventNews::getEventId, eventId);
        wrapper.eq(VoteEventNews::getIsTop, "0");
        return voteEventNewsMapper.selectList(wrapper);
    }

    /**
     * 添加置顶
     */
    @Override
    @Transactional
    public int top(Long id) {
        VoteEventNews eventNews = new VoteEventNews();
        eventNews.setIsTop("0")
                .setId(id);
        return voteEventNewsMapper.updateById(eventNews);
    }


}




