package io.renren.modules.demo.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.demo.dto.SubjectSelectionDTO;
import io.renren.modules.demo.excel.SubjectSelectionExcel;
import io.renren.modules.demo.service.SubjectSelectionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 选题表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2025-03-06
 */
@RestController
@RequestMapping("demo/subjectselection")
@Tag(name="选题表")
public class SubjectSelectionController {
    @Autowired
    private SubjectSelectionService subjectSelectionService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
        @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref="int") ,
        @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY,required = true, ref="int") ,
        @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref="String") ,
        @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref="String")
    })
    @RequiresPermissions("demo:subjectselection:page")
    public Result<PageData<SubjectSelectionDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params){
        PageData<SubjectSelectionDTO> page = subjectSelectionService.page(params);

        return new Result<PageData<SubjectSelectionDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("demo:subjectselection:info")
    public Result<SubjectSelectionDTO> get(@PathVariable("id") Long id){
        SubjectSelectionDTO data = subjectSelectionService.get(id);

        return new Result<SubjectSelectionDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    @RequiresPermissions("demo:subjectselection:save")
    public Result save(@RequestBody SubjectSelectionDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        subjectSelectionService.save(dto);

        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    @RequiresPermissions("demo:subjectselection:update")
    public Result update(@RequestBody SubjectSelectionDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        subjectSelectionService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("demo:subjectselection:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        subjectSelectionService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @Operation(summary = "导出")
    @LogOperation("导出")
    @RequiresPermissions("demo:subjectselection:export")
    public void export(@Parameter(hidden = true) @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<SubjectSelectionDTO> list = subjectSelectionService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "选题表", list, SubjectSelectionExcel.class);
    }

}
