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

import cn.exploring.engine.server.core.util.ResponseUtil;
import cn.exploring.engine.server.db.domain.ClassInfo;
import cn.exploring.engine.server.db.service.ClassService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

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

    @Resource
    ClassService classService;

    @GetMapping("selectClass")
    public Object selectClass(@RequestParam String className, @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(classService.webSelectClass(className,  page, limit, sort, order));
    }

    @GetMapping("addClass")
    public Object addClass(@RequestParam String className) {
        String errMsg = classService.addClassInfo(className);
        return StringUtils.isNotBlank(errMsg) ? ResponseUtil.fail(402, errMsg) : ResponseUtil.ok("成功");
    }

    @PostMapping("updateClass")
    public Object updateClass(@RequestBody ClassInfo classInfo) {
        classService.updateClassInfo(classInfo);
        return ResponseUtil.ok("OK");
    }

    @PostMapping("batchDeleted")
    public Object batchDeleted(@RequestBody List<Integer> idList) {
        String errMsg = classService.batchDeletedData(idList);
        return StringUtils.isNotBlank(errMsg) ? ResponseUtil.fail(402, errMsg) : ResponseUtil.ok("成功");
    }

    @GetMapping("/listAll")
    public Object listAll() {
        return ResponseUtil.ok(classService.listAll());
    }






}
