package com.srs.web.controller.comprehensive;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.srs.comprehensive.domain.SrsClass;
import com.srs.comprehensive.domain.Vo.SrsClassInfo;
import com.srs.comprehensive.domain.Vo.CphSearch;
import com.srs.comprehensive.service.ICascaderDataStudentService;
import com.srs.comprehensive.service.ISrsClassService;
import com.srs.system.domain.SysDeptMap;
import com.srs.system.service.ISysDeptMapService;
import com.srs.system.service.ISysPostService;
import com.srs.web.controller.common.RoleBool;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.srs.common.annotation.Log;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.enums.BusinessType;

import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.core.page.TableDataInfo;

/**
 * 班级信息
 * Controller
 *
 * @author srs
 * @date 2023-06-27
 */
@RestController
@RequestMapping("/system/class")
public class SrsClassController extends BaseController {
    @Autowired
    private ISrsClassService srsClassService;
    @Resource
    private ICascaderDataStudentService cascaderDataStudentService;

    @Autowired
    ISysPostService _postService;

    @Autowired
    ISysDeptMapService _deptMap;

    @PreAuthorize("@ss.hasPermi('system:class:xw')")
    @ApiOperation("列出学院班级")
    @GetMapping("/listDeptClass")
    public TableDataInfo listDeptClass(SrsClass param){
        Long oldId = getDeptId();
        SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
        Long deptId = null;
        if (dept != null) {
            deptId = dept.getOldDeptId();
        }
        param.setDeptId(deptId);
        startPage();
        List<SrsClass> list = srsClassService.selectSrsClassList(param);
        return getDataTable(list);
    }


    @ApiOperation("列出在校班级")
    @GetMapping("/listInSchoolClass")
    public AjaxResult listInSchoolClass(CphSearch param) {
        List<SrsClass> list = srsClassService.listInSchoolClass(param);
        return AjaxResult.success(list);
    }


    @ApiOperation("列出辅导员自己的班级")
    @GetMapping("/listOwnClass")
    public TableDataInfo listOwnClass(CphSearch param) {
        boolean hasRole = RoleBool.isFdy(getUserId(), _postService);
        if (hasRole) {
            param.setTNo(getUsername());
            startPage();
            List<SrsClassInfo> list = srsClassService.listClassView(param);
            return getDataTable(list);
        } else {
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    /**
     * 查询班级信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:class:list')")
    @GetMapping("/list")
    public TableDataInfo list(SrsClass srsClass) {
        startPage();
        List<SrsClass> list = srsClassService.selectSrsClassList(srsClass);
        return getDataTable(list);
    }

    /**
     * 查询班级名字列表 以及所有上级
     */

    @GetMapping("/name")
    public AjaxResult listNameAll() {
        return AjaxResult.success(cascaderDataStudentService.getCascadeData(3));

    }

    /**
     * 导出班级信息
     * 列表
     */
//    @PreAuthorize("@ss.hasPermi('system:class:export')")
    @Log(title = "班级信息 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SrsClass srsClass) {
        List<SrsClass> list = srsClassService.selectSrsClassList(srsClass);
        ExcelUtil<SrsClass> util = new ExcelUtil<SrsClass>(SrsClass.class);
        util.exportExcel(response, list, "班级信息 数据");
    }

    /**
     * 获取班级信息
     * 详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:class:query')")
    @GetMapping(value = "/{classId}")
    public AjaxResult getInfo(@PathVariable("classId") Long classId) {
        return success(srsClassService.selectSrsClassByClassId(classId));
    }

    /**
     * 新增班级信息
     */
//    @PreAuthorize("@ss.hasPermi('system:class:add')")
    @Log(title = "班级信息 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SrsClass srsClass) {
        return toAjax(srsClassService.insertSrsClass(srsClass));
    }

    /**
     * 修改班级信息
     */
//    @PreAuthorize("@ss.hasPermi('system:class:edit')")
    @Log(title = "班级信息 ", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody SrsClass srsClass) {
        return toAjax(srsClassService.updateSrsClass(srsClass));
    }

    /**
     * 删除班级信息
     */
//    @PreAuthorize("@ss.hasPermi('system:class:remove')")
    @Log(title = "班级信息 ", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{classIds}")
    public AjaxResult remove(@PathVariable Long[] classIds) {
        return toAjax(srsClassService.deleteSrsClassByClassIds(classIds));
    }

    /**
     * 同步数据
     *
     * @return 结果
     */
    @ApiOperation("班级信息同步")
    @PostMapping("/dataSync")
    public AjaxResult dataSync() {
        return AjaxResult.success(srsClassService.datasync());
    }

}
