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

import cn.exploring.engine.server.core.util.ResponseUtil;
import cn.exploring.engine.server.db.domain.StudentInfo;
import cn.exploring.engine.server.db.service.StudentInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/wx/student")
@Validated
public class StudentController {

    @Resource
    StudentInfoService studentInfoService;


    @GetMapping("selectStudent")
    public Object selectClass(@RequestParam String studentName, @RequestParam String studentUniqueId, @RequestParam Integer classId,
                              @RequestParam(required = false, defaultValue = "1") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit,
                              @RequestParam(required = false, defaultValue = "add_time") String sort, @RequestParam(required = false, defaultValue = "desc") String order) {
        return ResponseUtil.ok(studentInfoService.selectStudentInfoByWeb(studentName, studentUniqueId, null, classId,  page, limit, sort, order));
    }

    @GetMapping("randomSelect")
    public Object randomSelect(@RequestParam Integer classId) {
        return ResponseUtil.ok(studentInfoService.selectStudentInfo(classId));
    }

    @PostMapping("add")
    public Object add(@RequestBody StudentInfo studentInfo) {
        String status = studentInfoService.addStudentInfo(studentInfo);
        return status.contains("OK") ? ResponseUtil.ok(status) : ResponseUtil.fail(503, status);
    }


    @PostMapping("update")
    public Object update(@RequestBody StudentInfo studentInfo) {
        studentInfoService.updateStudentInfo(studentInfo);
        return ResponseUtil.ok("OK");
    }

    @PostMapping("batchDeleted")
    public Object batchDeleted(@RequestBody List<Integer> idList) {
        return ResponseUtil.ok(studentInfoService.batchDeleted(idList));
    }
}
