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.LessonInfo;
import cn.exploring.engine.server.db.service.LessonInfoService;
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/lessonInfo")
@Validated
public class AdminLessonInfoController {

    @Autowired
    private LessonInfoService lessonInfoService;

    @RequiresPermissions("admin:lessonInfo:select")
    @RequiresPermissionsDesc(menu={"1系课程视频" , "1系课程视频"}, button="搜索")
    @GetMapping("/select")
    public Object selectLessonInfo(@RequestParam(required = false) Integer id,
                                 @RequestParam(required = false) Integer chapters,
                                 @RequestParam(required = false) String chaptersName,
                                   @RequestParam(required = false) Integer session,
                                   @RequestParam(required = false) String sessionName,
                                 @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(lessonInfoService.selectLessonInfo(id, chapters, chaptersName, session, sessionName, page, limit, sort, order));
    }

    @RequiresPermissions("admin:initTest:add")
    @RequiresPermissionsDesc(menu={"1系课程视频" , "1系课程视频"}, button="添加")
    @PostMapping("/add")
    public Object addLessonInfo(@RequestBody LessonInfo lessonInfo){
        String addMessage = lessonInfoService.addLessonInfo(lessonInfo);
        Object backData = "insert Ok".equals(addMessage) ? ResponseUtil.ok(addMessage) : ResponseUtil.fail(403, addMessage);
        return backData;
    }

    @RequiresPermissions("admin:initTest:update")
    @RequiresPermissionsDesc(menu={"1系课程视频" , "1系课程视频"}, button="编辑")
    @PostMapping("/update")
    public Object updateLessonInfo(@RequestBody LessonInfo lessonInfo){
        return ResponseUtil.ok(lessonInfoService.updateLessonInfo(lessonInfo));
    }

    @RequiresPermissions("admin:initTest:batchDeleted")
    @RequiresPermissionsDesc(menu={"1系课程视频" , "1系课程视频"}, button="批量删除")
    @PostMapping("/batchDeleted")
    public Object batchUpdateLessonInfo(@RequestBody List<Integer> lessonInfoIdList){
        return ResponseUtil.ok(lessonInfoService.batchDeletedLessonInfo(lessonInfoIdList));
    }

    @GetMapping("selectLessonChaptersInfo")
    public Object selectLessonChaptersInfo() {
        return ResponseUtil.ok(lessonInfoService.getAllChaptersInfo());
    }


}
