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.RtStuDisciplinaryRelieve;
import com.srs.routine.service.IRtStuDisciplinaryApplicationService;
import com.srs.routine.service.IRtStuDisciplinaryRelieveService;
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-04-29
 */
@RestController
@RequestMapping("/routine/relieve")
@Api(value = "学生解除处分申请管理", tags = "学生解除处分申请管理")
public class RtStuDisciplinaryRelieveController extends BaseController {
    @Autowired
    private IRtStuDisciplinaryRelieveService rtStuDisciplinaryRelieveService;

    @Autowired
    IRtStuDisciplinaryApplicationService rtStuDisciplinaryApplicationService;

    @Autowired
    ISysPostService _postService;

    @Autowired
    private ISysRoleService _roleService;


    @Autowired
    public ISrsStudentService srsStudentService;

    /**
     * 查询学生解除处分申请列表
     */
    @PreAuthorize("@ss.hasPermi('routine:relieve:list')")
    @GetMapping("/list")
    @ApiOperation("查询学生解除处分申请列表")
    public TableDataInfo list(RtStuDisciplinaryRelieve rtStuDisciplinaryRelieve) {
        // 学工
        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();
            rtStuDisciplinaryRelieve.setSubmissionStatus(1L);
            List<RtStuDisciplinaryRelieve> list = rtStuDisciplinaryRelieveService.selectRtStuDisciplinaryRelieveList(rtStuDisciplinaryRelieve);
            return getDataTable(list);
        }else if(xw){
            startPage();
            rtStuDisciplinaryRelieve.setSubmissionStatus(1L);
            rtStuDisciplinaryRelieve.setDeptId(SecurityUtils.getDeptId());
            List<RtStuDisciplinaryRelieve> list = rtStuDisciplinaryRelieveService.selectRtStuDisciplinaryRelieveListByXW(rtStuDisciplinaryRelieve);
            return getDataTable(list);
        }else if (isFdy){
            startPage();
            rtStuDisciplinaryRelieve.setSubmissionStatus(1L);
            List<RtStuDisciplinaryRelieve> list = rtStuDisciplinaryRelieveService.getRtStuDisciplinaryRelieveListByTeacherNo(rtStuDisciplinaryRelieve);
            return getDataTable(list);
        }else if (Stu){
            startPage();
            List<RtStuDisciplinaryRelieve> list = rtStuDisciplinaryRelieveService.getRtStuDisciplinaryRelieveListByStuNo(rtStuDisciplinaryRelieve);
            return getDataTable(list);
        }else{
            return getDataTable(new ArrayList<>());
        }
    }

    /**
     * 查询学生解除处分申请列表
     */
    @PreAuthorize("@ss.hasPermi('routine:relieve:list')")
    @GetMapping("/getStuList")
    @ApiOperation("查询学生解除处分申请列表")
    public TableDataInfo getStuList(RtStuDisciplinaryRelieve rtStuDisciplinaryRelieve) {
        startPage();
        List<RtStuDisciplinaryRelieve> list = rtStuDisciplinaryRelieveService.getRtStuDisciplinaryRelieveListByStuNo(rtStuDisciplinaryRelieve);
        return getDataTable(list);
    }

    /**
     * 导出学生解除处分申请列表
     */
    @PreAuthorize("@ss.hasPermi('routine:relieve:export')")
    @Log(title = "学生解除处分申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出学生解除处分申请列表")
    public void export(HttpServletResponse response, RtStuDisciplinaryRelieve rtStuDisciplinaryRelieve) {
        List<RtStuDisciplinaryRelieve> list = rtStuDisciplinaryRelieveService.selectRtStuDisciplinaryRelieveList(rtStuDisciplinaryRelieve);
        ExcelUtil<RtStuDisciplinaryRelieve> util = new ExcelUtil<RtStuDisciplinaryRelieve>(RtStuDisciplinaryRelieve.class);
        util.exportExcel(response, list, "学生解除处分申请数据");
    }

    @PostMapping("/importTemplate")
    @ApiOperation("下载学生解除处分导入模板")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<RtStuDisciplinaryRelieve> util = new ExcelUtil<RtStuDisciplinaryRelieve>(RtStuDisciplinaryRelieve.class);
        util.importTemplateExcel(response, "学生解除处分申请数据导入");
    }

    @Log(title = "学生解除处分申请", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('routine:relieve:import')")
    @PostMapping("/importData")
    @ApiOperation("导入学生解除处分申请")
    public AjaxResult importData(org.springframework.web.multipart.MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<RtStuDisciplinaryRelieve> util = new ExcelUtil<RtStuDisciplinaryRelieve>(RtStuDisciplinaryRelieve.class);
        List<RtStuDisciplinaryRelieve> list = util.importExcel(file.getInputStream());
        String message = rtStuDisciplinaryRelieveService.importDisciplinaryRelieve(list, updateSupport);
        return success(message);
    }

    /**
     * 获取学生解除处分申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('routine:relieve:query')")
    @GetMapping(value = "/{relieveId}")
    @ApiOperation("获取学生解除处分申请详细信息")
    public AjaxResult getInfo(@PathVariable("relieveId") Long relieveId) {
        return success(rtStuDisciplinaryRelieveService.selectRtStuDisciplinaryRelieveByRelieveId(relieveId));
    }

    /**
     * 新增学生解除处分申请
     */
    @PreAuthorize("@ss.hasPermi('routine:relieve:add')")
    @Log(title = "学生解除处分申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增学生解除处分申请")
    public AjaxResult add(@RequestBody RtStuDisciplinaryRelieve rtStuDisciplinaryRelieve) {
        SysUser teacherByStuNo = srsStudentService.getTeacherByStuNo(SecurityUtils.getUsername());
        rtStuDisciplinaryRelieve.setApplicantName(teacherByStuNo.getNickName());
        rtStuDisciplinaryRelieve.setSubmissionStatus(1L);

        if (rtStuDisciplinaryRelieve.getRelieveId() != null) {
            return toAjax(rtStuDisciplinaryRelieveService.editRtStuDisciplinaryRelieve(rtStuDisciplinaryRelieve));
        } else {
            return toAjax(rtStuDisciplinaryRelieveService.insertRtStuDisciplinaryRelieve(rtStuDisciplinaryRelieve));
        }
    }

    /**
     * 保存学生解除处分申请
     */
    @PreAuthorize("@ss.hasPermi('routine:relieve:add')")
    @Log(title = "学生解除处分申请", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增学生解除处分申请")
    public AjaxResult save(@RequestBody RtStuDisciplinaryRelieve rtStuDisciplinaryRelieve) {
        SysUser teacherByStuNo = srsStudentService.getTeacherByStuNo(SecurityUtils.getUsername());
        rtStuDisciplinaryRelieve.setApplicantName(teacherByStuNo.getNickName());
        rtStuDisciplinaryRelieve.setSubmissionStatus(0L);

        if (rtStuDisciplinaryRelieve.getRelieveId() != null) {
            return toAjax(rtStuDisciplinaryRelieveService.editRtStuDisciplinaryRelieve(rtStuDisciplinaryRelieve));
        } else {
            return toAjax(rtStuDisciplinaryRelieveService.insertRtStuDisciplinaryRelieve(rtStuDisciplinaryRelieve));
        }
    }

    /**
     * 修改学生解除处分申请
     */
    @PreAuthorize("@ss.hasPermi('routine:relieve:edit')")
    @Log(title = "学生解除处分申请", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改学生解除处分申请")
    public AjaxResult edit(@RequestBody RtStuDisciplinaryRelieve rtStuDisciplinaryRelieve) {
        return toAjax(rtStuDisciplinaryRelieveService.updateRtStuDisciplinaryRelieve(rtStuDisciplinaryRelieve));
    }

    /**
     * 删除学生解除处分申请
     */
    @PreAuthorize("@ss.hasPermi('routine:relieve:remove')")
    @Log(title = "学生解除处分申请", businessType = BusinessType.DELETE)
    @PostMapping("/{relieveIds}")
    @ApiOperation("删除学生解除处分申请")
    public AjaxResult remove(@PathVariable Long[] relieveIds) {
        return toAjax(rtStuDisciplinaryRelieveService.deleteRtStuDisciplinaryRelieveByRelieveIds(relieveIds));
    }

    @GetMapping("/getPenaltyNumber/{penaltyNumber}")
    @ApiOperation("根据处分文号获取处分相关信息")
    public AjaxResult getPenaltyNumber(@PathVariable String penaltyNumber) {
        return success(rtStuDisciplinaryApplicationService.getStuDisciplinaryApplicationByPenaltyNumber(penaltyNumber));
    }

    @GetMapping("/getStuDisciplinaryRelieveByProcInsId/{procInsId}")
    @ApiOperation("根据流程id获取解除处分单")
    public AjaxResult getStuDisciplinaryRelieveByProcInsId(@PathVariable("procInsId") String procInsId) {
        return success(rtStuDisciplinaryRelieveService.getDisciplinaryRelieveByProcInsId(procInsId));
    }
}
