package com.zd.exam.controller;

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.exam.domain.ElRepo;
import com.zd.exam.domain.vo.ElRepoVO;
import com.zd.exam.service.ElRepoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 题库(ElRepo)表控制层
 *
 * @author liubo
 * @since 2021-08-30 14:45:55
 */
@RestController
@RequestMapping("/repo")
@Api(tags = {"题库管理"})
public class ElRepoController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private ElRepoService elRepoService;

    /**
     * 主键查询  "exam:repo:query"
     */
    @PreAuthorize(hasPermi = PerPrefix.EXAM_REPO+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据主键查询题库明细",notes="权限字符："+PerPrefix.EXAM_REPO+ PerFun.QUERY)
    public AjaxResult selectOne(@PathVariable("id") Long id) {
        return AjaxResult.success(this.elRepoService.queryById(id));
    }

    /**
     * 获取题库列表  "exam:repo:list"
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询题库列表")
    @PreAuthorize(hasPermi =PerPrefix.EXAM_REPO+ PerFun.LIST)
    public TableDataInfo list(ElRepoVO repovo) {
        startPage();
        List<ElRepo> list = elRepoService.queryList(repovo);
        return getDataTable(list);
    }

    /**
     * 获取题库下拉列表
     */
    @PostMapping("/queryOption")
    @ApiOperation(value = "查询题库下拉列表")
    public AjaxResult queryOption(@RequestBody ElRepoVO repovo) {
        List<ElRepo> list = elRepoService.queryList(repovo);
        return AjaxResult.success(list);
    }

    /**
     * 添加题库  "exam:repo:add"
     */
    @PreAuthorize(hasPermi =PerPrefix.EXAM_REPO+ PerFun.ADD)
    @Log(title = "题库", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "添加题库" ,notes="权限字符："+PerPrefix.EXAM_REPO+ PerFun.ADD)
    public AjaxResult add(@RequestBody @Valid ElRepo repo) {
        return toAjax(elRepoService.insert(repo));
    }

    /**
     * 修改题库  "exam:repo:edit"
     */
    @PreAuthorize(hasPermi =PerPrefix.EXAM_REPO+ PerFun.EDIT)
    @Log(title = "题库", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改题库" ,notes="权限字符："+PerPrefix.EXAM_REPO+ PerFun.EDIT)
    public AjaxResult edit(@RequestBody @Valid ElRepo repo) {
        return toAjax(elRepoService.update(repo));
    }

    /**
     * 批量删除  "exam:repo:remove"
     */
    @PreAuthorize(hasPermi =PerPrefix.EXAM_REPO+ PerFun.REMOVE)
    @Log(title = "题库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "批量删除" ,notes="权限字符："+PerPrefix.EXAM_REPO+ PerFun.REMOVE)
    public AjaxResult remove(@PathVariable Long[] ids) {
        //根据ID删除
        return toAjax(elRepoService.deleteByIds(Arrays.asList(ids)));
    }

}
