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.School;
import com.schoolinout.system.service.ISchoolService;
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 = "学校管理", description = "学校管理的CRUD接口")
@RestController
@RequestMapping("/system/school")
public class SchoolController extends BaseController {

    @Autowired
    private ISchoolService schoolService;

    /**
     * 查询学校管理列表
     */
    @Operation(summary = "查询学校管理列表")
    @PreAuthorize("@ss.hasPermi('system:school:list')")
    @GetMapping("/list")
    public TableDataInfo list(School school) {
        startPage();
        List<School> list = schoolService.selectSchoolList(school);
        return getDataTable(list);
    }

    /**
     * 导出学校管理列表
     */
    @Operation(summary = "导出学校管理列表")
    @PreAuthorize("@ss.hasPermi('system:school:export')")
    @Log(title = "学校管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, School school) {
        List<School> list = schoolService.selectSchoolList(school);
        ExcelUtil<School> util = new ExcelUtil<School>(School.class);
        util.exportExcel(response, list, "学校管理数据");
    }

    /**
     * 获取学校管理详细信息
     */
    @Operation(summary = "获取学校管理详细信息")
    @PreAuthorize("@ss.hasPermi('system:school:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(schoolService.selectSchoolById(id));
    }

    /**
     * 新增学校管理
     */
    @Operation(summary = "新增学校信息")
    @PreAuthorize("@ss.hasPermi('system:school:add')")
    @Log(title = "学校管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody School school) {
        return toAjax(schoolService.insertSchool(school));
    }

    /**
     * 修改学校管理
     */
    @Operation(summary = "修改学校信息")
    @PreAuthorize("@ss.hasPermi('system:school:edit')")
    @Log(title = "学校管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody School school) {
        return toAjax(schoolService.updateSchool(school));
    }

    /**
     * 删除学校管理
     */
    @Operation(summary = "删除学校信息")
    @PreAuthorize("@ss.hasPermi('system:school:remove')")
    @Log(title = "学校管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(schoolService.deleteSchoolByIds(ids));
    }
}
