package com.srs.web.controller.routine;

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

import com.srs.common.core.domain.entity.SysUser;
import com.srs.common.utils.SecurityUtils;
import com.srs.comprehensive.service.ISrsStudentService;
import com.srs.routine.service.IRtStuLeaveApplicationService;
import com.srs.system.service.ISysPostService;
import com.srs.system.service.ISysRoleService;
import com.srs.web.controller.common.RoleBool;
import org.springframework.beans.factory.annotation.Autowired;
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 io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;

import com.srs.routine.domain.RtStuQuitSchool;
import com.srs.routine.service.IRtStuQuitSchoolService;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.enums.BusinessType;
import com.srs.common.annotation.Log;
import com.srs.common.core.page.TableDataInfo;

/**
 * 休学申请记录Controller
 *
 * @author LSD
 * @date 2024-06-12
 */
@RestController
@RequestMapping("/routine/rtStuQuitSchool")
@Api(value = "休学申请记录管理", tags = "休学申请记录管理")
public class RtStuQuitSchoolController extends BaseController {
    @Autowired
    private IRtStuQuitSchoolService rtStuQuitSchoolService;

    @Autowired
    private IRtStuLeaveApplicationService rtStuLeaveApplicationService;

    @Autowired
    ISysPostService _postService;

    @Autowired
    private ISysRoleService _roleService;

    @Autowired
    public ISrsStudentService srsStudentService;

    /**
     * 查询休学申请记录列表
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuQuitSchool:list')")
    @GetMapping("/list")
    @ApiOperation("查询休学申请记录列表")
    public TableDataInfo list(RtStuQuitSchool rtStuQuitSchool) {

        // 学工
        boolean hasRole = RoleBool.isJwc(getUserId(), _postService);

        // 学生
        boolean Stu = RoleBool.isStu(getUserId(), _roleService);

        // 学务
        boolean xw = RoleBool.isXw(getUserId(), _postService);

        // 辅导员
        boolean isFdy = RoleBool.isFdy(getUserId(), _postService);

        if (hasRole) {
            startPage();
            rtStuQuitSchool.setSubmissionStatus(1L);
            List<RtStuQuitSchool> list = rtStuQuitSchoolService.selectRtStuQuitSchoolList(rtStuQuitSchool);
            return getDataTable(list);
        } else if (xw) {
            startPage();
            rtStuQuitSchool.setSubmissionStatus(1L);
            rtStuQuitSchool.setDeptId(SecurityUtils.getDeptId());
            List<RtStuQuitSchool> list = rtStuQuitSchoolService.selectRtStuQuitSchoolListByXW(rtStuQuitSchool);
            return getDataTable(list);
        } else if (isFdy) {
            startPage();
            rtStuQuitSchool.setSubmissionStatus(1L);
            rtStuQuitSchool.setUserName(SecurityUtils.getUsername());
            List<RtStuQuitSchool> list = rtStuQuitSchoolService.selectRtStuQuitSchoolListByFdy(rtStuQuitSchool);
            return getDataTable(list);
        } else if (Stu) {
            startPage();
            rtStuQuitSchool.setStuNo(SecurityUtils.getUsername());
            List<RtStuQuitSchool> list = rtStuQuitSchoolService.selectRtStuQuitSchoolList(rtStuQuitSchool);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }

    /**
     * 导出休学申请记录列表
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuQuitSchool:export')")
    @Log(title = "休学申请记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出休学申请记录列表")
    public void export(HttpServletResponse response, RtStuQuitSchool rtStuQuitSchool) {
        List<RtStuQuitSchool> list = rtStuQuitSchoolService.selectRtStuQuitSchoolList(rtStuQuitSchool);
        ExcelUtil<RtStuQuitSchool> util = new ExcelUtil<RtStuQuitSchool>(RtStuQuitSchool.class);
        util.exportExcel(response, list, "休学申请记录数据");
    }

    /**
     * 获取休学申请记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuQuitSchool:query')")
    @GetMapping(value = "/{quitSchoolId}")
    @ApiOperation("获取休学申请记录详细信息")
    public AjaxResult getInfo(@PathVariable("quitSchoolId") Long quitSchoolId) {
        return success(rtStuQuitSchoolService.selectRtStuQuitSchoolByQuitSchoolId(quitSchoolId));
    }

    /**
     * 新增休学申请记录
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuQuitSchool:add')")
    @Log(title = "休学申请记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增休学申请记录")
    public AjaxResult add(@RequestBody RtStuQuitSchool rtStuQuitSchool) {
        SysUser teacherByStuNo = srsStudentService.getTeacherByStuNo(SecurityUtils.getUsername());
        rtStuQuitSchool.setApplicantName(teacherByStuNo.getNickName());
        rtStuQuitSchool.setApplicantId(teacherByStuNo.getUserId());
        rtStuQuitSchool.setSubmissionStatus(1L);
        if (rtStuQuitSchool.getQuitSchoolId() != null) {
            return toAjax(rtStuQuitSchoolService.editRtStuQuitSchool(rtStuQuitSchool));
        } else {
            return toAjax(rtStuQuitSchoolService.insertRtStuQuitSchool(rtStuQuitSchool));
        }
    }

    /**
     * 修改休学申请记录
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuQuitSchool:edit')")
    @Log(title = "休学申请记录", businessType = BusinessType.UPDATE)
    @PostMapping("/save")
    @ApiOperation("保存休学申请记录")
    public AjaxResult save(@RequestBody RtStuQuitSchool rtStuQuitSchool) {
        SysUser teacherByStuNo = srsStudentService.getTeacherByStuNo(SecurityUtils.getUsername());
        rtStuQuitSchool.setApplicantName(teacherByStuNo.getNickName());
        rtStuQuitSchool.setApplicantId(teacherByStuNo.getUserId());

        rtStuQuitSchool.setSubmissionStatus(0L);
        if (rtStuQuitSchool.getQuitSchoolId() != null) {
            return toAjax(rtStuQuitSchoolService.editRtStuQuitSchool(rtStuQuitSchool));
        } else {
            return toAjax(rtStuQuitSchoolService.insertRtStuQuitSchool(rtStuQuitSchool));
        }
    }

    /**
     * 修改休学申请记录
     */
    @GetMapping("/getRtStuQuitSchoolByProcInsId/{procInsId}")
    @ApiOperation("根据procInsId获取休学数据")
    public AjaxResult getRtStuQuitSchoolByProcInsId(@PathVariable("procInsId") String procInsId) {
        return success(rtStuQuitSchoolService.getRtStuQuitSchoolByProcInsId(procInsId));
    }

    /**
     * 修改休学申请记录
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuQuitSchool:edit')")
    @Log(title = "休学申请记录", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改休学申请记录")
    public AjaxResult edit(@RequestBody RtStuQuitSchool rtStuQuitSchool) {
        return toAjax(rtStuQuitSchoolService.updateRtStuQuitSchool(rtStuQuitSchool));
    }


    /**
     * 删除休学申请记录
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuQuitSchool:remove')")
    @Log(title = "休学申请记录", businessType = BusinessType.DELETE)
    @PostMapping("/{quitSchoolIds}")
    @ApiOperation("删除休学申请记录")
    public AjaxResult remove(@PathVariable Long[] quitSchoolIds) {
        return toAjax(rtStuQuitSchoolService.deleteRtStuQuitSchoolByQuitSchoolIds(quitSchoolIds));
    }

    @GetMapping(value = "/getMyStuInfo")
    @ApiOperation("获取学生基础数据")
    public AjaxResult getMyStuInfo() {
        String stuNo = SecurityUtils.getUsername();
        return success(rtStuLeaveApplicationService.getStuInfo(stuNo));
    }
}
