package com.ruoyi.web.controller.test.gl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.test.domain.Task;
import com.ruoyi.test.domain.Vo.TaskVo;
import com.ruoyi.test.seriver.ITaskService;
import com.ruoyi.test.seriver.IUserService;
import com.ruoyi.web.controller.tool.testTool.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "管理端任务管理")
@RestController
@RequestMapping("/gl/tesk")
public class TaskController {

    @Autowired
    private ITaskService iTaskService;

    @Autowired
    private IUserService iUserService;

    @ApiOperation("任务列表")
    @GetMapping("selectList")
    public ResultVo<IPage<Task>> selectList(int pagenum,int pagesize){
//        return R.ok(iTaskService.selectList(pagenum,pagesize));
        return ResultVo.ok(iTaskService.selectList(pagenum,pagesize));
    }

//    @ApiOperation("根据年级获取任务列表")
//    @GetMapping("selectListByGrade")
//    public R<IPage<Task>> selectListByGrade(String grade,int pagenum){
//        return R.ok(iTaskService.selectListByGrade(grade,pagenum));
//    }

    @ApiOperation("根据年级或学科查询任务列表")
    @GetMapping("selectListByGradeAndSubject")
    public R<IPage<Task>> selectListByGradeAndSubject(String grade,String subject,int pagenum,int pagesize){
        return R.ok(iTaskService.selectListByGradeAndSubject(grade,subject,pagenum,pagesize));
    }

    @ApiOperation("任务创编")
    @PostMapping("addTask")
    public R<String> addTask(@RequestBody TaskVo taskVo){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long userId = sysUser.getUserId();
        String tname = iUserService.user(iUserService.selectByzhid(userId)).getTname();
        if(iTaskService.addTask(taskVo,tname)!=0){
            return R.ok("任务添加成功！");
        }
        return R.fail("任务添加失败");
    }

    @ApiOperation("删除任务")
    @DeleteMapping("deleteTask")
    public R<String> deleteTask(Long id){
        if(iTaskService.deleteTask(id)!=0){
            return R.ok("删除成功！");
        }
        return R.fail("删除失败！");
    }

//    @ApiOperation("根据学科获取任务列表")
//    @GetMapping("selectListBySubject")
//    public R<IPage<Task>> selectListBySubject(String subject,int pagenum){
//        return R.ok(iTaskService.selectListBySubject(subject,pagenum));
//    }
}
