package com.pangu.school.controller;

import com.pangu.common.core.controller.BaseController;
import com.pangu.common.core.domain.AjaxResult;
import com.pangu.common.core.page.TableDataInfo;
import com.pangu.school.domain.dto.BindClassesDTO;
import com.pangu.school.domain.dto.BindGradesDTO;
import com.pangu.school.domain.dto.SchoolCreateDTO;
import com.pangu.school.domain.dto.SchoolQueryDTO;
import com.pangu.school.domain.entity.School;
import com.pangu.school.domain.vo.SchoolTreeVO;
import com.pangu.school.domain.vo.SchoolVO;
import com.pangu.school.service.ISchoolService;
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/school")
public class SchoolController extends BaseController {

    @Autowired
    private ISchoolService schoolService;

    /**
     * 获取学校树形结构
     */
    @GetMapping("/tree")
    public AjaxResult tree(@RequestParam(required = false) Long regionId) {
        List<SchoolTreeVO> tree = schoolService.selectSchoolTree(regionId);
        return success(tree);
    }

    /**
     * 获取学校列表（分页）
     */
    @GetMapping("/list")
    public TableDataInfo list(SchoolQueryDTO query) {
        startPage();
        List<SchoolVO> list = schoolService.selectSchoolList(query);
        return getDataTable(list);
    }

    /**
     * 获取学校详情
     */
    @GetMapping("/{schoolId}")
    public AjaxResult getInfo(@PathVariable Long schoolId) {
        SchoolVO school = schoolService.selectSchoolById(schoolId);
        return success(school);
    }

    /**
     * 新增学校
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SchoolCreateDTO dto) {
        int result = schoolService.insertSchool(dto);
        return toAjax(result);
    }

    /**
     * 修改学校
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody School school) {
        int result = schoolService.updateSchool(school);
        return toAjax(result);
    }

    /**
     * 删除学校
     */
    @DeleteMapping("/{schoolId}")
    public AjaxResult remove(@PathVariable Long schoolId) {
        int result = schoolService.deleteSchool(schoolId);
        return toAjax(result);
    }

    /**
     * 为学校挂载年级
     */
    @PostMapping("/bindGrades")
    public AjaxResult bindGrades(@Validated @RequestBody BindGradesDTO dto) {
        int result = schoolService.bindGrades(dto.getSchoolId(), dto.getGradeIds());
        return toAjax(result);
    }

    /**
     * 为年级挂载班级
     */
    @PostMapping("/bindClasses")
    public AjaxResult bindClasses(@Validated @RequestBody BindClassesDTO dto) {
        int result = schoolService.bindClasses(dto.getSchoolGradeId(), dto.getClassIds());
        return toAjax(result);
    }

    /**
     * 删除学校年级
     */
    @DeleteMapping("/grade/{schoolGradeId}")
    public AjaxResult removeGrade(@PathVariable Long schoolGradeId) {
        int result = schoolService.deleteSchoolGrade(schoolGradeId);
        return toAjax(result);
    }

    /**
     * 删除学校班级
     */
    @DeleteMapping("/class/{schoolClassId}")
    public AjaxResult removeClass(@PathVariable Long schoolClassId) {
        int result = schoolService.deleteSchoolClass(schoolClassId);
        return toAjax(result);
    }
}
