package com.pangu.base.controller;

import com.pangu.base.domain.Subject;
import com.pangu.base.service.ISubjectService;
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/subject")
public class SubjectController extends BaseController {

    @Autowired
    private ISubjectService subjectService;

    /**
     * 获取学科列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Subject subject) {
        startPage();
        List<Subject> list = subjectService.selectSubjectList(subject);
        return getDataTable(list);
    }

    /**
     * 获取学科选项列表
     */
    @GetMapping("/options")
    public AjaxResult options() {
        List<Subject> list = subjectService.selectSubjectOptions();
        return success(list);
    }

    /**
     * 获取学科详情
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long subjectId) {
        return success(subjectService.selectSubjectById(subjectId));
    }

    /**
     * 新增学科
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Subject subject) {
        if (!subjectService.checkSubjectNameUnique(subject)) {
            return error("学科名称已存在");
        }
        subject.setCreateBy("admin");
        return toAjax(subjectService.insertSubject(subject));
    }

    /**
     * 修改学科
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Subject subject) {
        if (!subjectService.checkSubjectNameUnique(subject)) {
            return error("学科名称已存在");
        }
        subject.setUpdateBy("admin");
        return toAjax(subjectService.updateSubject(subject));
    }

    /**
     * 删除学科
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long subjectId) {
        return toAjax(subjectService.deleteSubjectById(subjectId));
    }
}
