package com.ruoyi.frequency.vote.controller;

import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.frequency.vote.entity.Vote;
import com.ruoyi.frequency.vote.model.result.VoteCount;
import com.ruoyi.frequency.vote.service.VoteService;
import com.ruoyi.frequency.works.entity.Works;
import com.ruoyi.frequency.works.model.result.WorksCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 投票活动Controller
 *
 * @author liwenlong
 * @date 2023-10-12
 */
@RestController
@RequestMapping("/frequency/vote")
public class VoteController extends BaseController
{
    @Autowired
    private VoteService voteService;

    /**
     * 查询投票活动列表
     */
    //@PreAuthorize("@ss.hasPermi('frequency:vote:list')")
    @PostMapping("/list")
    public TableDataInfo<Vote> list(@RequestBody Vote vote)
    {
        startPage();
        List<Vote> list = voteService.selectVoteList(vote);
        return getDataTable(list);
    }

    /**
     * 统计
     */
    @PostMapping("/statistics")
    public AjaxResult statistics(@RequestBody Vote vote)
    {
        VoteCount count = voteService.statistics(vote);
        return AjaxResult.success(count);
    }

    /**
     * 导出投票活动列表
     */
    //@PreAuthorize("@ss.hasPermi('frequency:vote:export')")
    @Log(title = "投票活动", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Vote vote)
    {
        List<Vote> list = voteService.selectVoteList(vote);
        ExcelUtil<Vote> util = new ExcelUtil<Vote>(Vote.class);
        return util.exportExcel(list, "投票活动数据");
    }

    /**
     * 获取投票活动详细信息
     */
    //@PreAuthorize("@ss.hasPermi('frequency:vote:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult<Vote> getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(voteService.selectVoteById(id));
    }

    /**
     * 新增投票活动
     */
    //@PreAuthorize("@ss.hasPermi('frequency:vote:add')")
    @Log(title = "投票活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Vote vote)
    {
        return toAjax(voteService.insertVote(vote));
    }

    /**
     * 修改投票活动
     */
    //@PreAuthorize("@ss.hasPermi('frequency:vote:edit')")
    @Log(title = "投票活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Vote vote)
    {
        return toAjax(voteService.updateVote(vote));
    }

    /**
     * 删除投票活动
     */
    //@PreAuthorize("@ss.hasPermi('frequency:vote:remove')")
    @Log(title = "投票活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}/{delFlag}")
    public AjaxResult remove(@PathVariable("ids") Long[] ids,@PathVariable("delFlag") Integer delFlag)
    {
        return toAjax(voteService.deleteVoteByIds(ids,delFlag));
    }

    /**
     * 终止活动
     */
    @Log(title = "终止活动", businessType = BusinessType.UPDATE)
    //@PreAuthorize("@ss.hasPermi('frequency:vote:stop')")
    @PutMapping("/stop/{id}")
    public AjaxResult stop(@PathVariable Long id)
    {
        Vote vote = new Vote();
        vote.setId(id);
        vote.setDelFlag(PublicCommon.禁用);
        return toAjax(voteService.updateVote(vote));
    }
}
