package com.srs.web.controller.routine;

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

import com.srs.common.core.domain.entity.SysUser;
import com.srs.common.utils.SecurityUtils;
import com.srs.comprehensive.service.ISrsStudentService;
import com.srs.routine.service.IRtStuLeaveApplicationService;
import com.srs.system.service.ISysPostService;
import com.srs.system.service.ISysRoleService;
import com.srs.web.controller.common.RoleBool;
import liquibase.pro.packaged.S;
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.RtStuDisciplinaryApplication;
import com.srs.routine.service.IRtStuDisciplinaryApplicationService;
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 LSD
 * @date 2024-04-17
 */
@RestController
@RequestMapping("/routine/disciplinaryApplication")
@Api(value = "学生处分申请管理", tags = "学生处分申请管理")
public class RtStuDisciplinaryApplicationController extends BaseController {
    @Autowired
    private IRtStuDisciplinaryApplicationService rtStuDisciplinaryApplicationService;

    @Autowired
    private IRtStuLeaveApplicationService rtStuLeaveApplicationService;


    @Autowired
    ISysPostService _postService;

    @Autowired
    private ISysRoleService _roleService;

    @Autowired
    public ISrsStudentService srsStudentService;

    /**
     * 查询学生处分申请列表
     */
    @PreAuthorize("@ss.hasPermi('routine:disciplinaryApplication:list')")
    @GetMapping("/list")
    @ApiOperation("查询学生处分申请列表")
    public TableDataInfo list(RtStuDisciplinaryApplication rtStuDisciplinaryApplication) {
        // 学工
        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();
            // 教务只看提交的
            rtStuDisciplinaryApplication.setSubmissionStatus(1L);
            List<RtStuDisciplinaryApplication> list = rtStuDisciplinaryApplicationService.selectRtStuDisciplinaryApplicationList(rtStuDisciplinaryApplication);
            return getDataTable(list);
        } else if (xw) {
            startPage();
            rtStuDisciplinaryApplication.setSubmissionStatus(1L);
            rtStuDisciplinaryApplication.setDeptId(getDeptId());
            List<RtStuDisciplinaryApplication> list = rtStuDisciplinaryApplicationService.selectRtStuDisciplinaryApplicationListByXW(rtStuDisciplinaryApplication);
            return getDataTable(list);
        } else if (isFdy) {
            startPage();
            rtStuDisciplinaryApplication.setApplicantId(SecurityUtils.getUserId());
            List<RtStuDisciplinaryApplication> list = rtStuDisciplinaryApplicationService.selectRtStuDisciplinaryApplicationList(rtStuDisciplinaryApplication);
            return getDataTable(list);
        }else if (Stu){
            startPage();
            rtStuDisciplinaryApplication.setSubmissionStatus(1L);
            rtStuDisciplinaryApplication.setStuId(SecurityUtils.getUserId());
            List<RtStuDisciplinaryApplication> list = rtStuDisciplinaryApplicationService.selectRtStuDisciplinaryApplicationList(rtStuDisciplinaryApplication);
            return getDataTable(list);
        }else{
            return getDataTable(new ArrayList<>());
        }
    }

    /**
     * 查询学生处分申请列表
     */
    @PreAuthorize("@ss.hasPermi('routine:disciplinaryApplication:list')")
    @GetMapping("/getStuList")
    @ApiOperation("查询学生处分申请列表")
    public TableDataInfo getStuList(RtStuDisciplinaryApplication rtStuDisciplinaryApplication) {
        startPage();
        // 学生只能看已提交的
        rtStuDisciplinaryApplication.setSubmissionStatus(1L);
        List<RtStuDisciplinaryApplication> list = rtStuDisciplinaryApplicationService.getRtStuDisciplinaryApplicationListByStuNo(rtStuDisciplinaryApplication);
        return getDataTable(list);
    }


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

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

    @GetMapping("/getDisciplinaryApplicationByProcInsId/{procInsId}")
    @ApiModelProperty("根据流程id获取请假单")
    public AjaxResult getDisciplinaryApplicationByProcInsId(@PathVariable("procInsId") String procInsId) {
        return success(rtStuDisciplinaryApplicationService.getDisciplinaryApplicationByProcInsId(procInsId));
    }

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

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

    /**
     * 新增学生处分申请
     */
    @PreAuthorize("@ss.hasPermi('routine:disciplinaryApplication:add')")
    @Log(title = "学生处分申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增学生处分申请提交")
    public AjaxResult add(@RequestBody RtStuDisciplinaryApplication rtStuDisciplinaryApplication) {

        rtStuDisciplinaryApplication.setApplicantName(SecurityUtils.getLoginUser().getUser().getNickName());
        rtStuDisciplinaryApplication.setApplicantId(SecurityUtils.getUserId());
        rtStuDisciplinaryApplication.setCreateBy(SecurityUtils.getUsername());
        rtStuDisciplinaryApplication.setSubmissionStatus(1L);
        if (rtStuDisciplinaryApplication.getApplicationId() != null) {
            return toAjax(rtStuDisciplinaryApplicationService.updateRtStuDisciplinaryApplication(rtStuDisciplinaryApplication));
        } else {
            return toAjax(rtStuDisciplinaryApplicationService.insertRtStuDisciplinaryApplication(rtStuDisciplinaryApplication));
        }
    }


    /**
     * 新增学生处分申请
     */
    @PreAuthorize("@ss.hasPermi('routine:disciplinaryApplication:edit')")
    @Log(title = "学生处分申请", businessType = BusinessType.UPDATE)
    @PostMapping("/save")
    @ApiOperation("新增学生处分申请保存")
    public AjaxResult save(@RequestBody RtStuDisciplinaryApplication rtStuDisciplinaryApplication) {

        rtStuDisciplinaryApplication.setApplicantName(SecurityUtils.getLoginUser().getUser().getNickName());
        rtStuDisciplinaryApplication.setApplicantId(SecurityUtils.getUserId());
        rtStuDisciplinaryApplication.setSubmissionStatus(0L);
        if (rtStuDisciplinaryApplication.getApplicationId() != null) {
            return toAjax(rtStuDisciplinaryApplicationService.updateRtStuDisciplinaryApplication(rtStuDisciplinaryApplication));
        } else {
            return toAjax(rtStuDisciplinaryApplicationService.insertRtStuDisciplinaryApplication(rtStuDisciplinaryApplication));
        }
    }

    /**
     * 修改学生处分申请
     */
    @PreAuthorize("@ss.hasPermi('routine:disciplinaryApplication:edit')")
    @Log(title = "学生处分申请", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改学生处分申请")
    public AjaxResult edit(@RequestBody RtStuDisciplinaryApplication rtStuDisciplinaryApplication) {
        return toAjax(rtStuDisciplinaryApplicationService.editRtStuDisciplinaryApplication(rtStuDisciplinaryApplication));
    }

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

    @GetMapping("/getStuDisciplinary/{stuNo}")
    @ApiOperation("根据学号查询该生是否属于处分状态：true-是，false-否")
    public AjaxResult getStuDisciplinary(@PathVariable("stuNo") String stuNo){
        return success(rtStuDisciplinaryApplicationService.selectRtStuDisciplinaryApplicationByStuNo(stuNo));
    }
    @GetMapping("/getviolationDate/{stuNo}")
    @ApiOperation("获取违纪时间")
    public AjaxResult getviolationDate(@PathVariable("stuNo") String stuNo){
        return success(rtStuDisciplinaryApplicationService.getviolationDate(stuNo));
    }
}
