package com.pangu.base.controller;

import com.pangu.base.domain.PgClass;
import com.pangu.base.service.IPgClassService;
import com.pangu.common.core.controller.BaseController;
import com.pangu.common.core.domain.AjaxResult;
import com.pangu.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班级管理Controller
 * @author pangu
 */
@RestController
@RequestMapping("/api/class")
public class PgClassController extends BaseController {

    @Autowired
    private IPgClassService pgClassService;

    /**
     * 获取班级列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PgClass pgClass) {
        startPage();
        List<PgClass> list = pgClassService.selectClassList(pgClass);
        return getDataTable(list);
    }

    /**
     * 获取班级选项列表
     */
    @GetMapping("/options")
    public AjaxResult options() {
        List<PgClass> list = pgClassService.selectClassOptions();
        return success(list);
    }

    /**
     * 获取班级详情
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long classId) {
        return success(pgClassService.selectClassById(classId));
    }

    /**
     * 新增班级
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PgClass pgClass) {
        if (!pgClassService.checkClassNameUnique(pgClass)) {
            return error("班级名称已存在");
        }
        pgClass.setCreateBy("admin");
        return toAjax(pgClassService.insertClass(pgClass));
    }

    /**
     * 修改班级
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PgClass pgClass) {
        if (!pgClassService.checkClassNameUnique(pgClass)) {
            return error("班级名称已存在");
        }
        pgClass.setUpdateBy("admin");
        return toAjax(pgClassService.updateClass(pgClass));
    }

    /**
     * 删除班级
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long classId) {
        return toAjax(pgClassService.deleteClassById(classId));
    }
}
