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.RtStuDropOutSchool;
import com.srs.routine.service.IRtStuDropOutSchoolService;
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 LSD
 * @date 2024-06-20
 */
@RestController
@RequestMapping("/routine/rtStuDropOutSchool")
@Api(value = "学生退学申请记录管理", tags = "学生退学申请记录管理")
public class RtStuDropOutSchoolController extends BaseController {
    @Autowired
    private IRtStuDropOutSchoolService rtStuDropOutSchoolService;

    @Autowired
    public ISrsStudentService srsStudentService;


    @Autowired
    ISysPostService _postService;

    @Autowired
    private ISysRoleService _roleService;

    /**
     * 查询学生退学申请记录列表
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuDropOutSchool:list')")
    @GetMapping("/list")
    @ApiOperation("查询学生退学申请记录列表")
    public TableDataInfo list(RtStuDropOutSchool rtStuDropOutSchool) {
        // 学工
        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();
            rtStuDropOutSchool.setSubmissionStatus("1");
            List<RtStuDropOutSchool> list = rtStuDropOutSchoolService.selectRtStuDropOutSchoolList(rtStuDropOutSchool);
            return getDataTable(list);
        } else if (Stu) {
            startPage();
            rtStuDropOutSchool.setStuId(SecurityUtils.getUserId());
            List<RtStuDropOutSchool> list = rtStuDropOutSchoolService.selectRtStuDropOutSchoolList(rtStuDropOutSchool);
            return getDataTable(list);
        } else if (xw) {
            startPage();
            rtStuDropOutSchool.setSubmissionStatus("1");
            rtStuDropOutSchool.setDeptId(SecurityUtils.getDeptId());
            List<RtStuDropOutSchool> list = rtStuDropOutSchoolService.selectRtStuDropOutSchoolListByXW(rtStuDropOutSchool);
            return getDataTable(list);
        } else if (isFdy) {
            startPage();
            rtStuDropOutSchool.setSubmissionStatus("1");
            rtStuDropOutSchool.setApplicantId(SecurityUtils.getUserId());
            List<RtStuDropOutSchool> list = rtStuDropOutSchoolService.selectRtStuDropOutSchoolList(rtStuDropOutSchool);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }

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

    /**
     * 获取学生退学申请记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuDropOutSchool:query')")
    @GetMapping(value = "/{dropOutSchoolId}")
    @ApiOperation("获取学生退学申请记录详细信息")
    public AjaxResult getInfo(@PathVariable("dropOutSchoolId") Long dropOutSchoolId) {
        return success(rtStuDropOutSchoolService.selectRtStuDropOutSchoolByDropOutSchoolId(dropOutSchoolId));
    }


    /**
     * 新增学生退学申请记录
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuDropOutSchool:add')")
    @Log(title = "学生退学申请记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增学生退学申请记录")
    public AjaxResult add(@RequestBody RtStuDropOutSchool rtStuDropOutSchool) {
        SysUser teacherByStuNo = srsStudentService.getTeacherByStuNo(SecurityUtils.getUsername());
        rtStuDropOutSchool.setApplicantName(teacherByStuNo.getNickName());
        rtStuDropOutSchool.setApplicantId(teacherByStuNo.getUserId());
        rtStuDropOutSchool.setApplicantId(teacherByStuNo.getUserId());
        rtStuDropOutSchool.setStuId(SecurityUtils.getUserId());
        rtStuDropOutSchool.setSubmissionStatus("1");
        if (rtStuDropOutSchool.getDropOutSchoolId() != null) {
            return toAjax(rtStuDropOutSchoolService.editRtStuDropOutSchool(rtStuDropOutSchool));
        } else {
            return toAjax(rtStuDropOutSchoolService.insertRtStuDropOutSchool(rtStuDropOutSchool));
        }
    }

    /**
     * 新增学生退学申请记录
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuDropOutSchool:edit')")
    @Log(title = "学生退学申请记录", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增学生退学申请记录")
    public AjaxResult save(@RequestBody RtStuDropOutSchool rtStuDropOutSchool) {
        SysUser teacherByStuNo = srsStudentService.getTeacherByStuNo(SecurityUtils.getUsername());
        rtStuDropOutSchool.setApplicantName(teacherByStuNo.getNickName());
        rtStuDropOutSchool.setApplicantId(teacherByStuNo.getUserId());
        rtStuDropOutSchool.setStuId(SecurityUtils.getUserId());
        rtStuDropOutSchool.setSubmissionStatus("0");
        if (rtStuDropOutSchool.getDropOutSchoolId() != null) {
            return toAjax(rtStuDropOutSchoolService.editRtStuDropOutSchool(rtStuDropOutSchool));
        } else {
            return toAjax(rtStuDropOutSchoolService.insertRtStuDropOutSchool(rtStuDropOutSchool));
        }
    }

    @GetMapping("/getRtStuDropOutSchoolByprocInsId/{procInsId}")
    @ApiOperation("根据流程id获取退学数据")
    public AjaxResult getRtStuDropOutSchoolByprocInsId(@PathVariable("procInsId") String procInsId) {
        return success(rtStuDropOutSchoolService.selectRtStuDropOutSchoolByprocInsId(procInsId));
    }

    /**
     * 修改学生退学申请记录
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuDropOutSchool:edit')")
    @Log(title = "学生退学申请记录", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改学生退学申请记录")
    public AjaxResult edit(@RequestBody RtStuDropOutSchool rtStuDropOutSchool) {
        return toAjax(rtStuDropOutSchoolService.updateRtStuDropOutSchool(rtStuDropOutSchool));
    }

    /**
     * 删除学生退学申请记录
     */
    @PreAuthorize("@ss.hasPermi('routine:rtStuDropOutSchool:remove')")
    @Log(title = "学生退学申请记录", businessType = BusinessType.DELETE)
    @PostMapping("/{dropOutSchoolIds}")
    @ApiOperation("删除学生退学申请记录")
    public AjaxResult remove(@PathVariable Long[] dropOutSchoolIds) {
        return toAjax(rtStuDropOutSchoolService.deleteRtStuDropOutSchoolByDropOutSchoolIds(dropOutSchoolIds));
    }
}
