package com.vote.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vctgo.common.core.web.domain.AjaxResult;
import com.vote.system.domain.VoteEvents;
import com.vote.system.domain.vo.VoteEventsVo;
import com.vote.system.service.VoteEventsService;
import com.vote.system.util.ConstantUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author feng
 * @date 2024/8/11
 */
@Validated
@RestController
@RequestMapping("/events")
public class VoteEventsController {

    @Autowired
    private VoteEventsService voteEventsService;


    /**
     * 添加活动信息
     *
     * @param voteEvents
     */
    @PostMapping
    public AjaxResult addEvents(@RequestBody @Valid VoteEvents voteEvents) {
        return AjaxResult.success(voteEventsService.addEvents(voteEvents));
    }

    /**
     * 分页查询
     *
     * @param voteEvents
     * @return
     */
    @GetMapping("/page")
    public AjaxResult getEventsList(VoteEvents voteEvents) {
        IPage<VoteEventsVo> page = voteEventsService.getEventsList(voteEvents);
        return AjaxResult.success(page);
    }


    /**
     * 根据id查询活动信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getEventsById(@PathVariable("id") Long id) {
        return AjaxResult.success(voteEventsService.getEventsById(id));
    }


    /**
     * 修改
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/editStatus")
    public AjaxResult editEvents(@RequestParam("id") Long id, @RequestParam("status") String status) {
        boolean result = voteEventsService.changeStatus(id, status);
        if (result) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteEvents(@PathVariable("id") Long id) {
        return AjaxResult.success(voteEventsService.deleteEvents(id));
    }

    /**
     * 编辑活动设置
     * @param voteEvents
     * @return
     */
    @PutMapping("/edit")
    public AjaxResult editEvents(@RequestBody @Valid VoteEvents voteEvents) {
        return AjaxResult.success(voteEventsService.edit(voteEvents));
    }

}
