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.RtStuDisqualification;
import com.srs.routine.service.IRtStuDisqualificationService;
import com.srs.routine.service.IRtStuLeaveApplicationService;
import com.srs.system.service.ISysPostService;
import com.srs.system.service.ISysRoleService;
import com.srs.system.service.ISysUserService;
import com.srs.web.controller.common.RoleBool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
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 LSD
 * @date 2024-07-31
 */
@RestController
@RequestMapping("/routine/disqualification")
@Api(value = "给予学生退学申请管理", tags = "给予学生退学申请管理")
public class RtStuDisqualificationController extends BaseController {
    @Autowired
    private IRtStuDisqualificationService rtStuDisqualificationService;

    @Autowired
    public ISrsStudentService srsStudentService;

    @Autowired
    private IRtStuLeaveApplicationService rtStuLeaveApplicationService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    ISysPostService _postService;

    @Autowired
    private ISysRoleService _roleService;

    /**
     * 查询给予学生退学申请列表
     */
    @PreAuthorize("@ss.hasPermi('routine:disqualification:list')")
    @GetMapping("/list")
    @ApiOperation("查询给予学生退学申请列表")
    public TableDataInfo list(RtStuDisqualification rtStuDisqualification) {
        // 学工
        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();
            rtStuDisqualification.setSubmissionStatus(1L);
            List<RtStuDisqualification> list = rtStuDisqualificationService.selectRtStuDisqualificationList(rtStuDisqualification);
            return getDataTable(list);
        }else if(xw){
            startPage();
            rtStuDisqualification.setSubmissionStatus(1L);
            rtStuDisqualification.setDeptId(SecurityUtils.getDeptId());
            List<RtStuDisqualification> list = rtStuDisqualificationService.selectRtStuDisqualificationListByXW(rtStuDisqualification);
            return getDataTable(list);
        }else if (isFdy){
            startPage();
            rtStuDisqualification.setApplicantId(SecurityUtils.getUserId());
            List<RtStuDisqualification> list = rtStuDisqualificationService.selectRtStuDisqualificationList(rtStuDisqualification);
            return getDataTable(list);
        }else if (Stu){
            startPage();
            rtStuDisqualification.setSubmissionStatus(1L);
            rtStuDisqualification.setStuId(SecurityUtils.getUserId());
            List<RtStuDisqualification> list = rtStuDisqualificationService.selectRtStuDisqualificationList(rtStuDisqualification);
            return getDataTable(list);
        }else{
            return getDataTable(new ArrayList<>());
        }
    }

    /**
     * 导出给予学生退学申请列表
     */
    @PreAuthorize("@ss.hasPermi('routine:disqualification:export')")
    @Log(title = "给予学生退学申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出给予学生退学申请列表")
    public void export(HttpServletResponse response, RtStuDisqualification rtStuDisqualification) {
        List<RtStuDisqualification> list = rtStuDisqualificationService.selectRtStuDisqualificationList(rtStuDisqualification);
        ExcelUtil<RtStuDisqualification> util = new ExcelUtil<RtStuDisqualification>(RtStuDisqualification.class);
        util.exportExcel(response, list, "给予学生退学申请数据");
    }

    /**
     * 获取给予学生退学申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('routine:disqualification:query')")
    @GetMapping(value = "/{disqualificationId}")
    @ApiOperation("获取给予学生退学申请详细信息")
    public AjaxResult getInfo(@PathVariable("disqualificationId") Long disqualificationId) {
        return success(rtStuDisqualificationService.selectRtStuDisqualificationByDisqualificationId(disqualificationId));
    }

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

    /**
     * 获取给予学生退学申请详细信息
     */
    @GetMapping("/getRtStuDisqualificationByProcInsId/{procInsId}")
    @ApiModelProperty("根据流程id获取请假单")
    public AjaxResult getRtStuDisqualificationByProcInsId(@PathVariable("procInsId") String procInsId) {
        return success(rtStuDisqualificationService.getRtStuDisqualificationByProcInsId(procInsId));
    }

    /**
     * 新增给予学生退学申请
     */
    @PreAuthorize("@ss.hasPermi('routine:disqualification:add')")
    @Log(title = "给予学生退学申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增给予学生退学申请")
    public AjaxResult add(@RequestBody RtStuDisqualification rtStuDisqualification) {
        SysUser sysUser = sysUserService.selectUserByUserName(SecurityUtils.getUsername());
        rtStuDisqualification.setApplicantName(sysUser.getNickName());
        rtStuDisqualification.setApplicantId(sysUser.getUserId());
        rtStuDisqualification.setSubmissionStatus(1L);

        if (rtStuDisqualification.getDisqualificationId() != null) {
            return toAjax(rtStuDisqualificationService.editRtStuDisqualification(rtStuDisqualification));
        } else {
            return toAjax(rtStuDisqualificationService.insertRtStuDisqualification(rtStuDisqualification));
        }
    }

    /**
     * 新增给予学生退学申请
     */
    @PreAuthorize("@ss.hasPermi('routine:disqualification:add')")
    @Log(title = "给予学生退学申请", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("保存给予学生退学申请")
    public AjaxResult save(@RequestBody RtStuDisqualification rtStuDisqualification) {
        SysUser sysUser = sysUserService.selectUserByUserName(SecurityUtils.getUsername());
        rtStuDisqualification.setApplicantName(sysUser.getNickName());
        rtStuDisqualification.setSubmissionStatus(0L);
        rtStuDisqualification.setApplicantId(sysUser.getUserId());
        rtStuDisqualification.setCreateBy(SecurityUtils.getUsername());

        if (rtStuDisqualification.getDisqualificationId() != null) {

            return toAjax(rtStuDisqualificationService.updateRtStuDisqualification(rtStuDisqualification));
        } else {

            return toAjax(rtStuDisqualificationService.insertRtStuDisqualification(rtStuDisqualification));
        }
    }

    /**
     * 修改给予学生退学申请
     */
    @PreAuthorize("@ss.hasPermi('routine:disqualification:edit')")
    @Log(title = "给予学生退学申请", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改给予学生退学申请")
    public AjaxResult edit(@RequestBody RtStuDisqualification rtStuDisqualification) {
        return toAjax(rtStuDisqualificationService.updateRtStuDisqualification(rtStuDisqualification));
    }

    /**
     * 删除给予学生退学申请
     */
    @PreAuthorize("@ss.hasPermi('routine:disqualification:remove')")
    @Log(title = "给予学生退学申请", businessType = BusinessType.DELETE)
    @PostMapping("/{disqualificationIds}")
    @ApiOperation("删除给予学生退学申请")
    public AjaxResult remove(@PathVariable Long[] disqualificationIds) {
        return toAjax(rtStuDisqualificationService.deleteRtStuDisqualificationByDisqualificationIds(disqualificationIds));
    }
}
