package com.srs.web.controller.routine;

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

import com.srs.common.utils.SecurityUtils;
import com.srs.routine.service.IRtStuMultiLevelReviewService;
import com.srs.routine.domain.RtStuMultiLevelReview;
import com.srs.stureg.domain.SrsStuReg;
import com.srs.stureg.service.ISrsStuRegService;
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.RtStuIdReissue;
import com.srs.routine.service.IRtStuIdReissueService;
import com.srs.routine.domain.RtStuMultiLevelReview;
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 srs
 * @date 2024-05-16
 */
@RestController
@RequestMapping("/routine/stuIdReissue")
@Api(value = "学生证补办管理", tags = "学生证补办管理")
public class RtStuIdReissueController extends BaseController {
    @Autowired
    private IRtStuIdReissueService rtStuIdReissueService;

    @Autowired
    private ISrsStuRegService srsStuRegService;

    @Autowired
    private IRtStuMultiLevelReviewService rtStuMultiLevelReviewService;

    /**
     * 查询学生证补办列表
     */
    @PreAuthorize("@ss.hasPermi('routine:stuIdReissue:list')")
    @GetMapping("/list")
    @ApiOperation("查询学生证补办列表")
    public TableDataInfo list(RtStuIdReissue rtStuIdReissue) {
        startPage();
        List<RtStuIdReissue> list = rtStuIdReissueService.selectRtStuIdReissueList(rtStuIdReissue);
        return getDataTable(list);
    }

    /**
     * 查询学生证补办列表
     */
    @PreAuthorize("@ss.hasPermi('routine:stuIdReissue:list')")
    @GetMapping("/listName")
    @ApiOperation("查询学生证补办列表")
    public TableDataInfo listName(RtStuIdReissue rtStuIdReissue) {
        startPage();
        // 获取当前登录用户名称
        rtStuIdReissue.setStuName(SecurityUtils.getLoginUser().getUser().getNickName());
        List<RtStuIdReissue> list = rtStuIdReissueService.selectRtStuIdReissueList(rtStuIdReissue);
        return getDataTable(list);
    }

    /**
     * 根据辅导员名字查询学生证补办列表
     */
    @PreAuthorize("@ss.hasPermi('routine:stuIdReissue:list')")
    @GetMapping("/getListByCounsellorName")
    @ApiOperation("查询学生证补办列表")
    public TableDataInfo getListByCounsellorName(RtStuIdReissue rtStuIdReissue) {
        startPage();
//        RtStuIdReissue rtStuIdReissue = new RtStuIdReissue();
        rtStuIdReissue.setCounsellorName(SecurityUtils.getLoginUser().getUser().getNickName());
        List<RtStuIdReissue> list = rtStuIdReissueService.selectRtStuIdReissueList(rtStuIdReissue);
        return getDataTable(list);
    }

    /**
     * 通过学号查询注册信息
     */
    @PreAuthorize("@ss.hasPermi('routine:stuIdReissue:getStuByStuId')")
    @GetMapping("/getStuByStuId")
    @ApiOperation("通过学号查询注册信息")
    public AjaxResult getStuByStuId(String stuNo) {
        SrsStuReg srsStuReg = srsStuRegService.selectSrsStuRegByStuNo(stuNo);
        return success(srsStuReg);
    }

    @GetMapping("/getStudentInfoByStuId/{stuNo}")
    @ApiOperation("根据学生学号获取学生基础信息")
    public AjaxResult getStudentInfoByStuId(@PathVariable("stuNo") String stuNo) {
        return success(rtStuIdReissueService.getStuInfo(stuNo));
    }

    /**
     * 导出学生证补办列表
     */
    @PreAuthorize("@ss.hasPermi('routine:stuIdReissue:export')")
    @Log(title = "学生证补办", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出学生证补办列表")
    public void export(HttpServletResponse response, RtStuIdReissue rtStuIdReissue) {
        List<RtStuIdReissue> list = rtStuIdReissueService.selectRtStuIdReissueList(rtStuIdReissue);
        ExcelUtil<RtStuIdReissue> util = new ExcelUtil<RtStuIdReissue>(RtStuIdReissue.class);
        util.exportExcel(response, list, "学生证补办数据");
    }

    /**
     * 获取学生证补办详细信息
     */
    @PreAuthorize("@ss.hasPermi('routine:stuIdReissue:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取学生证补办详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(rtStuIdReissueService.selectRtStuIdReissueById(id));
    }

    /**
     * 获取学生证补办详细信息
     */
    @PreAuthorize("@ss.hasPermi('routine:stuIdReissue:query')")
    @GetMapping(value = "getInfoByStuNo/{stuNo}")
    @ApiOperation("获取学生证补办详细信息")
    public AjaxResult getInfoByStuNo(@PathVariable("stuNo") String stuNo) {
        return success(rtStuIdReissueService.selectRtStuIdReissueByStuNo(stuNo));
    }

    /**
     * 新增学生证补办
     */
    @PreAuthorize("@ss.hasPermi('routine:stuIdReissue:add')")
    @Log(title = "学生证补办", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增学生证补办")
    public AjaxResult add(@RequestBody RtStuIdReissue rtStuIdReissue) {
        // 检查该学号是否已有申请记录
        RtStuIdReissue existingApplication = rtStuIdReissueService.selectRtStuIdReissueByStuNo(rtStuIdReissue.getStuNo());
        if (existingApplication != null) {
            // 如果已有申请记录且未完成制作，则不允许重复提交
            if (existingApplication.getInspectionProgress() < 3) {
                return AjaxResult.error("您已有正在处理的申请，请勿重复提交");
            }
            // 如果已完成制作，允许提交新申请，但需要先删除旧记录
            rtStuIdReissueService.deleteRtStuIdReissueById(existingApplication.getId());
        }
        return toAjax(rtStuIdReissueService.insertRtStuIdReissue(rtStuIdReissue));
    }

    /**
     * 新增审核信息
     */
    @PreAuthorize("@ss.hasPermi('routine:stuMultiLevelReview:add')")
    @Log(title = "审核信息", businessType = BusinessType.INSERT)
    @PostMapping("/StuMultiLevelReviewAdd")
    @ApiOperation("新增审核信息")
    public AjaxResult StuMultiLevelReviewAdd(@RequestBody RtStuMultiLevelReview rtStuMultiLevelReview) {
        return toAjax(rtStuMultiLevelReviewService.insertRtStuMultiLevelReview(rtStuMultiLevelReview));
    }

    /**
     * 修改学生证补办
     */
    @PreAuthorize("@ss.hasPermi('routine:stuIdReissue:edit')")
    @Log(title = "学生证补办", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改学生证补办")
    public AjaxResult edit(@RequestBody RtStuIdReissue rtStuIdReissue) {
        // 检查当前申请状态，如果已完成制作则不允许修改
        RtStuIdReissue currentApplication = rtStuIdReissueService.selectRtStuIdReissueById(rtStuIdReissue.getId());
        if (currentApplication != null && currentApplication.getInspectionProgress() >= 3) {
            return AjaxResult.error("申请已完成制作，不可修改");
        }
        return toAjax(rtStuIdReissueService.updateRtStuIdReissue(rtStuIdReissue));
    }

    /**
     * 删除单个学生证补办申请（学生取消申请）
     */
    @PreAuthorize("@ss.hasPermi('routine:stuIdReissue:remove')")
    @Log(title = "取消学生证补办申请", businessType = BusinessType.DELETE)
    @PostMapping("/cancel/{id}")
    @ApiOperation("取消学生证补办申请")
    public AjaxResult cancelApplication(@PathVariable Long id) {
        // 先检查审核状态，如果已完成制作则不允许取消
        RtStuIdReissue rtStuIdReissue = rtStuIdReissueService.selectRtStuIdReissueById(id);
        if (rtStuIdReissue != null && rtStuIdReissue.getInspectionProgress() >= 3) {
            return AjaxResult.error("当前审核已完成，不可取消");
        }
        return toAjax(rtStuIdReissueService.deleteRtStuIdReissueById(id));
    }

    /**
     * 删除学生证补办
     */
    @PreAuthorize("@ss.hasPermi('routine:stuIdReissue:remove')")
    @Log(title = "学生证补办", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除学生证补办")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(rtStuIdReissueService.deleteRtStuIdReissueByIds(ids));
    }


}
