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.bean.BeanUtils;
import com.schoolinout.common.utils.poi.ExcelUtil;
import com.schoolinout.system.domain.Clazz;
import com.schoolinout.system.domain.TeacherClassRelation;
import com.schoolinout.system.domain.dto.AddClassDto;
import com.schoolinout.system.domain.vo.ClazzInfoVo;
import com.schoolinout.system.service.IClazzService;
import com.schoolinout.system.service.ITeacherClassRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 班级管理Controller
 *
 * @author sakura
 * @date 2024-01-03
 */
@RestController
@RequestMapping("/system/clazz")
public class ClazzController extends BaseController {
    @Autowired
    private IClazzService clazzService;

    @Autowired
    private ITeacherClassRelationService relationService;

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

    /**
     * 查询班级管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:clazz:list')")
    @GetMapping("/list")
    public TableDataInfo list(Clazz clazz) {
        startPage();
        List<Clazz> list = clazzService.selectClazzList(clazz);
        return getDataTable(list);
    }

    /**
     * 导出班级管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:clazz:export')")
    @Log(title = "班级管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Clazz clazz) {
        List<Clazz> list = clazzService.selectClazzList(clazz);
        ExcelUtil<Clazz> util = new ExcelUtil<Clazz>(Clazz.class);
        util.exportExcel(response, list, "班级管理数据");
    }

    /**
     * 获取班级管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:clazz:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Clazz clazz = clazzService.selectClazzById(id);

        ClazzInfoVo infoVo = new ClazzInfoVo();
        BeanUtils.copyBeanProp(infoVo, clazz);

        TeacherClassRelation relation = new TeacherClassRelation();
        relation.setClassId(clazz.getId());
        relation.setSchoolId(clazz.getSchoolId());
        List<Long> teacherIds = relationService.selectTeacherClassRelationList(relation).stream()
                .map(TeacherClassRelation::getTeacherId)
                .collect(Collectors.toList());
        infoVo.setTeacherIds(teacherIds);

        return success(infoVo);
    }

//    /**
//     * 新增班级管理
//     */
//    @PreAuthorize("@ss.hasPermi('system:clazz:add')")
//    @Log(title = "班级管理", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody Clazz clazz) {
//        return toAjax(clazzService.insertClazz(clazz));
//    }

    @PreAuthorize("@ss.hasPermi('system:clazz:add')")
    @Log(title = "班级管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addClass(@Validated @RequestBody AddClassDto form) {
        return toAjax(clazzService.saveClassAndRelation(form));
    }

    /**
     * 修改班级管理
     */
    @PreAuthorize("@ss.hasPermi('system:clazz:edit')")
    @Log(title = "班级管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody AddClassDto form) {
        return toAjax(clazzService.updateClazz(form));
    }

    /**
     * 删除班级管理
     */
    @PreAuthorize("@ss.hasPermi('system:clazz:remove')")
    @Log(title = "班级管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(clazzService.deleteClazzByIds(ids));
    }
}
