package cn.exploring.engine.server.admin.web;

import cn.exploring.engine.server.admin.annotation.RequiresPermissionsDesc;
import cn.exploring.engine.server.core.util.ResponseUtil;
import cn.exploring.engine.server.core.validator.Order;
import cn.exploring.engine.server.core.validator.Sort;
import cn.exploring.engine.server.db.domain.CourseInitTest;
import cn.exploring.engine.server.db.service.InitTestService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/initTest")
@Validated
public class AdminInitTestController {

    @Autowired
    private InitTestService initTestService;

    @RequiresPermissions("admin:initTest:select")
    @RequiresPermissionsDesc(menu={"创业基因测试" , "课程介绍视频"}, button="搜索")
    @GetMapping("select")
    public Object selectInitTest(@RequestParam(required = false) Integer id,
                                 @RequestParam(required = false) Integer titleRank,
                                 @RequestParam(required = false) String title,
                                 @RequestParam(required = false) String topic,
                                 @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer limit,
                                 @Sort @RequestParam(defaultValue = "add_time") String sort,
                                 @Order @RequestParam(defaultValue = "asc") String order
                                 ){
        return ResponseUtil.ok(initTestService.selectCourseInitTest(id, titleRank, title, topic, null, page, limit, sort, order));
    }

    @RequiresPermissions("admin:initTest:add")
    @RequiresPermissionsDesc(menu={"创业基因测试" , "课程介绍视频"}, button="添加")
    @PostMapping("/add")
    public Object addInitTest(@RequestBody CourseInitTest initTest){
        String addMessage = initTestService.addInitTest(initTest);
        Object backData = "insert Ok".equals(addMessage) ? ResponseUtil.ok(addMessage) : ResponseUtil.fail(403, addMessage);
        return backData;
    }

    @RequiresPermissions("admin:initTest:update")
    @RequiresPermissionsDesc(menu={"创业基因测试" , "课程介绍视频"}, button="编辑")
    @PostMapping("/update")
    public Object updateInitTest(@RequestBody CourseInitTest initTest){
        return ResponseUtil.ok(initTestService.updateInitTest(initTest));
    }

    @RequiresPermissions("admin:initTest:batchDeleted")
    @RequiresPermissionsDesc(menu={"创业基因测试" , "课程介绍视频"}, button="批量删除")
    @PostMapping("/batchDeleted")
    public Object updateInitTest(@RequestBody List<Integer> idList){
        return ResponseUtil.ok(initTestService.batchDeletedInitTest(idList));
    }


}
