package com.vote.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vctgo.common.core.exception.ServiceException;
import com.vctgo.common.core.web.domain.AjaxResult;
import com.vote.system.domain.VoteEventNews;
import com.vote.system.domain.dto.VoteEventNewsDto;
import com.vote.system.service.VoteEventNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author feng
 * @date 2024/8/11
 */
@RestController
@RequestMapping("/eventsNews")
public class VoteEventNewsController {

    @Autowired
    private VoteEventNewsService voteEventNewsService;


    @GetMapping("/page")
    public AjaxResult selectByPage(VoteEventNewsDto voteEventNewsDto) {
        IPage<VoteEventNews> page = voteEventNewsService.selectByPage(voteEventNewsDto);
        return AjaxResult.success(page);
    }

    /**
     * 添加 事件新闻
     */
    @PostMapping
    public AjaxResult addNews(@RequestBody VoteEventNews voteEventNews) {
        if (voteEventNews.getEventId() == null || voteEventNews.getEventId().toString().isEmpty()) {
            return AjaxResult.error("该咨询未关联活动，添加失败");
        }
            return AjaxResult.success(voteEventNewsService.save(voteEventNews));
    }

    /**
     * 查看详情
     */
    @GetMapping("/{id}")
    public AjaxResult getDetail(@PathVariable("id") Long id) {
        return AjaxResult.success(voteEventNewsService.selectById(id));
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public AjaxResult updateById(@RequestBody VoteEventNews voteEventNews) {
        return AjaxResult.success(voteEventNewsService.updateById(voteEventNews));
    }

    /**
     * 显示 隐藏
     */
    @PutMapping("/show")
    public AjaxResult show(@RequestBody VoteEventNews voteEventNews) {
        return AjaxResult.success(voteEventNewsService.updateById(voteEventNews));
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        return AjaxResult.success(voteEventNewsService.removeById(id));
    }

    /*
     * 资讯置顶管理
     */
    @GetMapping("/newsTop")
    public AjaxResult getNewsTop(Long eventId) {
        return AjaxResult.success(voteEventNewsService.getNewsTop(eventId));
    }

    @PutMapping("/cancel")
    public AjaxResult cancel(@RequestBody VoteEventNews voteEventNews) {
        return AjaxResult.success(voteEventNewsService.updateById(voteEventNews));
    }

    /**
     * 添加置顶
     */
    @PostMapping("/{id}")
    public AjaxResult top(@PathVariable("id") Long id) {
        return AjaxResult.success(voteEventNewsService.top(id));
    }
}
