package com.ly.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.controller.util.Result;
import com.ly.domain.Course;
import com.ly.domain.Role;
import com.ly.domain.User;
import com.ly.service.CourseService;
import com.ly.service.RoleService;
import com.ly.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/course")
@RestController
public class CourseController {
    @Resource
    private CourseService courseService;

    @Resource
    private UserService userService;
    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Course course) {
        courseService.saveOrUpdate(course);
        return Result.success();
    }
    @PostMapping("/studentCourse/{courseId}/{studentId}")
    public Result studentCourse(@PathVariable Integer courseId, @PathVariable Integer studentId) {
        courseService.setStudentCourse(courseId, studentId);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        courseService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        courseService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result findAll() {
        return Result.success(courseService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(courseService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
       // QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
      //  queryWrapper.like("name", name);
      // queryWrapper.orderByDesc("id");
      //  Page<Course> page = courseService.page(new Page<>(pageNum, pageSize), queryWrapper);
      //  List<Course> records = page.getRecords();
     //  for (Course record : records) {
       //    User user = userService.getById(record.getTeacherId());
      //     if(user != null) {
        //       record.setTeacher(user.getNickname());
      //     }


    //   }
    Page<Course> page = courseService.findPage(new Page<>(pageNum, pageSize), name);
        return Result.success(page);

    }



}
