package com.srs.web.controller.routine;

import com.srs.common.annotation.Log;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.core.domain.entity.SysUser;
import com.srs.common.core.page.TableDataInfo;
import com.srs.common.enums.BusinessType;
import com.srs.common.utils.SecurityUtils;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.comprehensive.service.ISrsStudentService;
import com.srs.routine.domain.RtStuReentrySchool;
import com.srs.routine.service.IRtStuReentrySchoolService;
import com.srs.system.service.ISysPostService;
import com.srs.system.service.ISysRoleService;
import com.srs.web.controller.common.RoleBool;
import io.swagger.annotations.Api;
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.*;

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

/**
 * 复学申请记录Controller
 *
 * @author srs
 * @date 2024-07-05
 */
@RestController
@RequestMapping("/routine/rtStuReentrySchool")
@Api(value = "复学申请记录管理", tags = "复学申请记录管理")
public class RtStuReentrySchoolController extends BaseController {
    @Autowired
    private IRtStuReentrySchoolService rtStuReentrySchoolService;

    @Autowired
    public ISrsStudentService srsStudentService;

    @Autowired
    ISysPostService _postService;

    @Autowired
    private ISysRoleService _roleService;

    /**
     * 查询复学申请记录列表
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuReentrySchool:list')")
    @GetMapping("/list")
    @ApiOperation("查询复学申请记录列表")
    public TableDataInfo list(RtStuReentrySchool rtStuReentrySchool) {
        // 学工
        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();
            rtStuReentrySchool.setSubmissionStatus(1L);
            List<RtStuReentrySchool> list = rtStuReentrySchoolService.selectRtStuReentrySchoolList(rtStuReentrySchool);
            return getDataTable(list);
        } else if (xw) {
            startPage();
            rtStuReentrySchool.setSubmissionStatus(1L);
            rtStuReentrySchool.setDeptId(SecurityUtils.getDeptId());
            List<RtStuReentrySchool> list = rtStuReentrySchoolService.selectRtStuReentrySchoolListByXW(rtStuReentrySchool);
            return getDataTable(list);
        } else if (isFdy) {
            startPage();
            rtStuReentrySchool.setSubmissionStatus(1L);
            rtStuReentrySchool.setUserName(SecurityUtils.getUsername());
            List<RtStuReentrySchool> list = rtStuReentrySchoolService.selectRtStuReentrySchoolListByFdy(rtStuReentrySchool);
            return getDataTable(list);
        } else if (Stu) {
            startPage();
            rtStuReentrySchool.setStuNo(SecurityUtils.getUsername());
            List<RtStuReentrySchool> list = rtStuReentrySchoolService.selectRtStuReentrySchoolList(rtStuReentrySchool);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }

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

    /**
     * 获取复学申请记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuReentrySchool:query')")
    @GetMapping(value = "/{reentryId}")
    @ApiOperation("获取复学申请记录详细信息")
    public AjaxResult getInfo(@PathVariable("reentryId") Long reentryId) {
        return success(rtStuReentrySchoolService.selectRtStuReentrySchoolByReentryId(reentryId));
    }

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

    /**
     * 新增复学申请记录
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuReentrySchool:add')")
    @Log(title = "复学申请记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增复学申请记录")
    public AjaxResult add(@RequestBody RtStuReentrySchool rtStuReentrySchool) {
        SysUser teacherByStuNo = srsStudentService.getTeacherByStuNo(SecurityUtils.getUsername());
        rtStuReentrySchool.setApplicantName(teacherByStuNo.getNickName());
        rtStuReentrySchool.setSubmissionStatus(1L);
        if (rtStuReentrySchool.getReentryId() != null) {
            return toAjax(rtStuReentrySchoolService.editRtStuReentrySchool(rtStuReentrySchool));
        } else {
            return toAjax(rtStuReentrySchoolService.insertRtStuReentrySchool(rtStuReentrySchool));
        }
    }

    /**
     * 新增复学申请记录
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuReentrySchool:add')")
    @Log(title = "复学申请记录", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("保存复学申请记录")
    public AjaxResult save(@RequestBody RtStuReentrySchool rtStuReentrySchool) {
        SysUser teacherByStuNo = srsStudentService.getTeacherByStuNo(SecurityUtils.getUsername());
        rtStuReentrySchool.setApplicantName(teacherByStuNo.getNickName());
        rtStuReentrySchool.setSubmissionStatus(0L);
        if (rtStuReentrySchool.getReentryId() != null) {
            return toAjax(rtStuReentrySchoolService.editRtStuReentrySchool(rtStuReentrySchool));
        } else {
            return toAjax(rtStuReentrySchoolService.insertRtStuReentrySchool(rtStuReentrySchool));
        }
    }

    /**
     * 修改复学申请记录
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuReentrySchool:edit')")
    @Log(title = "复学申请记录", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改复学申请记录")
    public AjaxResult edit(@RequestBody RtStuReentrySchool rtStuReentrySchool) {
        return toAjax(rtStuReentrySchoolService.updateRtStuReentrySchool(rtStuReentrySchool));
    }

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