package com.vote.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vctgo.common.core.web.domain.AjaxResult;
import com.vctgo.common.security.annotation.RequiresPermissions;
import com.vote.system.domain.VoteRule;
import com.vote.system.domain.VoteScoringItem;
import com.vote.system.domain.dto.VoteScoringItemDto;
import com.vote.system.service.VoteRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
@Validated
@RestController
@RequestMapping("/vote_rule")
public class VoteRuleController {

    @Autowired
    private VoteRuleService voteRuleService;

    /**
     * 查询报名用户分页列表
     */
    @RequiresPermissions("vote:rule:page")
    @GetMapping("/page")
    public AjaxResult list(VoteRule voteRule) {
        IPage<VoteRule> ruleIPage = voteRuleService.selectRulePage(voteRule);
        return AjaxResult.success(ruleIPage);
    }


    /**
     * 查看详情
     *
     * @param ruleId 规则主键
     * @return 实体
     */
    @GetMapping("/{ruleId}")
    public AjaxResult queryOne(@PathVariable() Integer ruleId) {
        VoteScoringItemDto voteRule = voteRuleService.selectByRuleId(ruleId);
        return AjaxResult.success(voteRule);
    }

    /**
     * 修改
     *
     * @param voteRule
     * @return
     */
    @RequiresPermissions("vote:rule:edit")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody @Valid VoteRule voteRule) {
        return AjaxResult.success(voteRuleService.updateRule(voteRule));
    }

    /**
     * 添加
     *
     * @param voteRule
     * @return
     */
    @PostMapping
    public AjaxResult add(@RequestBody @Valid VoteRule voteRule) {
        return AjaxResult.success(voteRuleService.insertRule(voteRule));
    }

    /**
     * 删除
     * @param ruleId
     * @return
     */
    @DeleteMapping("/{ruleId}")
    public AjaxResult abandon(@PathVariable Integer ruleId){
        return AjaxResult.success(voteRuleService.deleteRuleByRuleIds(ruleId));
    }

    /**
     * 下拉框回显
     * @param searchValue
     * @return
     */
    @GetMapping("/search")
    public AjaxResult searchRule(String searchValue){
        return AjaxResult.success(voteRuleService.selectSearch(searchValue));
    }
}
