package com.schoolinout.web.controller.system;

import com.schoolinout.common.annotation.Log;
import com.schoolinout.common.core.controller.BaseController;
import com.schoolinout.common.core.domain.AjaxResult;
import com.schoolinout.common.core.page.TableDataInfo;
import com.schoolinout.common.enums.BusinessType;
import com.schoolinout.common.utils.poi.ExcelUtil;
import com.schoolinout.system.domain.Teacher;
import com.schoolinout.system.service.ITeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 教师管理Controller
 *
 * @author sakura
 * @date 2024-01-03
 */
@Tag(name = "教师管理接口")
@RestController
@RequestMapping("/system/teacher")
public class TeacherController extends BaseController {
    @Autowired
    private ITeacherService teacherService;

    @GetMapping("/option/{schoolId}")
    public AjaxResult searchOption(@PathVariable Long schoolId) {
        return success(teacherService.selectTeacherOptionList(schoolId));
    }

    /**
     * 查询教师管理列表
     */
    @Operation(summary = "查询教师管理列表")
    @PreAuthorize("@ss.hasPermi('system:teacher:list')")
    @GetMapping("/list")
    public TableDataInfo list(Teacher teacher) {
        startPage();
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        return getDataTable(list);
    }

    /**
     * 导出教师管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:export')")
    @Log(title = "教师管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Teacher teacher) {
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
        util.exportExcel(response, list, "教师管理数据");
    }

    /**
     * 获取教师管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(teacherService.selectTeacherById(id));
    }

    /**
     * 新增教师管理
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:add')")
    @Log(title = "教师管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Teacher teacher) {
        return toAjax(teacherService.insertTeacher(teacher));
    }

    /**
     * 修改教师管理
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:edit')")
    @Log(title = "教师管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Teacher teacher) {
        return toAjax(teacherService.updateTeacher(teacher));
    }

    /**
     * 删除教师管理
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:remove')")
    @Log(title = "教师管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(teacherService.deleteTeacherByIds(ids));
    }
}
