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.DateUtils;
import com.srs.common.utils.SecurityUtils;
import com.srs.comprehensive.domain.SrsStudent;
import com.srs.comprehensive.domain.Vo.CphGoodAuditFdyVo;
import com.srs.comprehensive.service.ISrsStudentService;
import com.srs.routine.domain.RtStuLeaveApplication;
import com.srs.routine.domain.dto.LeaveApplicationApprovalDto;
import com.srs.routine.domain.vo.StuLeaveApplicationVo;
import com.srs.routine.domain.dto.StuLeaveApplicationDto;
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 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.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-03-30
 */
@RestController
@RequestMapping("/routine/leaveApplication")
@Api(value = "学生请假申请管理", tags = "日常事务-考勤管理")
public class RtStuLeaveApplicationController extends BaseController {
    @Autowired
    private IRtStuLeaveApplicationService rtStuLeaveApplicationService;


    @Autowired
    ISrsStudentService srsStudentService;

    @Autowired
    ISysPostService _postService;

    @Autowired
    private ISysRoleService _roleService;

    /**
     * 查询学生请假申请列表
     */
    @PreAuthorize("@ss.hasPermi('routine:leaveApplication:list')")
    @GetMapping("/list")
    @ApiOperation("查询学生本人请假申请列表")
    public TableDataInfo list(StuLeaveApplicationDto rtStuLeaveApplication) {
        // 学工
        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();
            rtStuLeaveApplication.setLeaveStatus("1");
            List<StuLeaveApplicationVo> list = rtStuLeaveApplicationService.getRtStuLeaveApplicationListDto(rtStuLeaveApplication);
            return getDataTable(list);
        }else if(xw){
            startPage();
            rtStuLeaveApplication.setLeaveStatus("1");
            rtStuLeaveApplication.setDeptId(getDeptId());
            List<StuLeaveApplicationVo> list = rtStuLeaveApplicationService.getRtStuLeaveApplicationListByXW(rtStuLeaveApplication);
            return getDataTable(list);
        }else if (isFdy){
            startPage();
            rtStuLeaveApplication.setLeaveStatus("1");
            rtStuLeaveApplication.setUserName(SecurityUtils.getUsername());
            List<StuLeaveApplicationVo> list = rtStuLeaveApplicationService.getRtStuLeaveApplicationListDto(rtStuLeaveApplication);
            return getDataTable(list);
        }else if (Stu){
            startPage();
            List<StuLeaveApplicationVo> list = rtStuLeaveApplicationService.getRtStuLeaveApplicationListVo(rtStuLeaveApplication);
            return getDataTable(list);
        }else{
            return getDataTable(new ArrayList<>());
        }

    }

    @PreAuthorize("@ss.hasPermi('routine:leaveApplication:list')")
    @GetMapping("/getList")
    @ApiOperation("查询学生请假申请列表")
    public TableDataInfo getList(StuLeaveApplicationDto rtStuLeaveApplication) {
        boolean hasRole = RoleBool.isXw(getUserId(), _postService);
        // 学务查询所有，否则辅导员查询 自己所带 班级
        if (hasRole) {
            startPage();
            List<StuLeaveApplicationVo> list = rtStuLeaveApplicationService.getRtStuLeaveApplicationListDto(rtStuLeaveApplication);
            return getDataTable(list);
        } else {
            startPage();
            rtStuLeaveApplication.setUserName(SecurityUtils.getUsername());
            List<StuLeaveApplicationVo> list = rtStuLeaveApplicationService.getRtStuLeaveApplicationListDto(rtStuLeaveApplication);
            return getDataTable(list);
        }
    }

    /**
     * 导出学生请假申请列表
     */
    @PreAuthorize("@ss.hasPermi('routine:leaveApplication:export')")
    @Log(title = "学生请假申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出学生请假申请列表")
    public void export(HttpServletResponse response, RtStuLeaveApplication rtStuLeaveApplication) {
        List<RtStuLeaveApplication> list = rtStuLeaveApplicationService.selectRtStuLeaveApplicationList(rtStuLeaveApplication);
        ExcelUtil<RtStuLeaveApplication> util = new ExcelUtil<RtStuLeaveApplication>(RtStuLeaveApplication.class);
        util.exportExcel(response, list, "学生请假申请数据");
    }

    /**
     * 获取学生请假申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('routine:leaveApplication:query')")
    @GetMapping(value = "/{leaveApplicationId}")
    @ApiOperation("获取学生请假申请详细信息")
    public AjaxResult getInfo(@PathVariable("leaveApplicationId") Long leaveApplicationId) {
        return success(rtStuLeaveApplicationService.selectRtStuLeaveApplicationDetailsByLeaveApplicationId(leaveApplicationId));
    }

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

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

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


    /**
     * 新增学生请假申请
     */
    @PreAuthorize("@ss.hasPermi('routine:leaveApplication:add')")
    @Log(title = "学生请假申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("提交")
    public AjaxResult add(@RequestBody RtStuLeaveApplication rtStuLeaveApplication) {

        rtStuLeaveApplication.setLeaveStatus("1");
        SrsStudent srsStudent = srsStudentService.selectSrsStudentByStuNo(getUsername());
        if (srsStudent != null) {
            rtStuLeaveApplication.setStuId(srsStudent.getStuId());
        }
        rtStuLeaveApplication.setStuNo(getUsername());
        if (rtStuLeaveApplication.getLeaveApplicationId() != null) {
            return toAjax(rtStuLeaveApplicationService.updateRtStuLeaveApplication(rtStuLeaveApplication));
        }
        return toAjax(rtStuLeaveApplicationService.insertRtStuLeaveApplication(rtStuLeaveApplication));
    }

    @Log(title = "学生请假申请", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation(("保存"))
    public AjaxResult save(@RequestBody RtStuLeaveApplication rtStuLeaveApplication) {
        // 保存
        rtStuLeaveApplication.setLeaveStatus("0");
        rtStuLeaveApplication.setStuNo(getUsername());

        SrsStudent srsStudent = srsStudentService.selectSrsStudentByStuNo(getUsername());
        if (srsStudent != null) {
            rtStuLeaveApplication.setStuId(srsStudent.getStuId());
        }
        if (rtStuLeaveApplication.getLeaveApplicationId() == null) {
            return toAjax(rtStuLeaveApplicationService.insertRtStuLeaveApplication(rtStuLeaveApplication));
        } else {
            return toAjax(rtStuLeaveApplicationService.updateRtStuLeaveApplication(rtStuLeaveApplication));
        }
    }


    /**
     * 修改学生请假申请
     */
    @PreAuthorize("@ss.hasPermi('routine:leaveApplication:edit')")
    @Log(title = "学生请假申请", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改学生请假申请")
    public AjaxResult edit(@RequestBody RtStuLeaveApplication rtStuLeaveApplication) {
        return toAjax(rtStuLeaveApplicationService.editRtStuLeaveApplication(rtStuLeaveApplication));
    }

    /**
     * 删除学生请假申请
     */
    @PreAuthorize("@ss.hasPermi('routine:leaveApplication:remove')")
    @Log(title = "学生请假申请", businessType = BusinessType.DELETE)
    @PostMapping("/{leaveApplicationIds}")
    @ApiOperation("删除学生请假申请")
    public AjaxResult remove(@PathVariable Long[] leaveApplicationIds) {
        return toAjax(rtStuLeaveApplicationService.deleteRtStuLeaveApplicationByLeaveApplicationIds(leaveApplicationIds));
    }


}
