package com.srs.web.controller.comprehensive;

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

import com.baomidou.mybatisplus.annotation.TableField;
import com.srs.comprehensive.domain.CphLakeApply;
import com.srs.comprehensive.domain.Dto.CphAuditMany;
import com.srs.comprehensive.domain.Dto.CphCancel;
import com.srs.comprehensive.domain.Vo.CphBiyeApplyVo;
import com.srs.comprehensive.domain.Vo.CphLakeApplyVo;
import com.srs.comprehensive.domain.Vo.CphSearch;
import com.srs.system.domain.SysDeptMap;
import com.srs.system.service.ISysDeptMapService;
import com.srs.system.service.ISysPostService;
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.comprehensive.domain.CphBiyeApply;
import com.srs.comprehensive.service.ICphBiyeApplyService;
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;

import static com.srs.common.utils.DateUtils.getNowDate;

/**
 * 优秀毕业生Controller
 *
 * @author srs
 * @date 2024-01-05
 */
@RestController
@RequestMapping("/comprehensive/biyeapply")
@Api(value = "优秀毕业生管理", tags = "优秀毕业生管理")
public class CphBiyeApplyController extends BaseController {
    @Autowired
    private ICphBiyeApplyService cphBiyeApplyService;

    @Autowired
    ISysPostService _postService;
    @Autowired
    ISysDeptMapService _deptMap;

    @PostMapping("/manyAudit")
    @ApiOperation("一键审核通过")
    public AjaxResult manyAudit(@RequestBody CphAuditMany param){
        boolean hasRole = RoleBool.isJwc(getUserId(),_postService);
        if(hasRole){
            return cphBiyeApplyService.manyAudit(param);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }


    @GetMapping("/jwcExport")
    @ApiOperation("学工导出")
    public AjaxResult jwcExport(CphSearch param){
        boolean hasRole = RoleBool.isJwc(getUserId(),_postService);
        if(hasRole){
            List<CphBiyeApplyVo> list = cphBiyeApplyService.exportView(param);
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }

    @PostMapping("/jwcAudit")
    @ApiOperation("学工审核")
    public AjaxResult jwcAudit(@RequestBody CphBiyeApply param){
        boolean hasRole = RoleBool.isJwc(getUserId(),_postService);
        if(hasRole){
            CphBiyeApply temp = new CphBiyeApply();
            temp.setId(param.getId());
            temp.setApplyStatus(param.getApplyStatus());
            temp.setJwcCmt(param.getJwcCmt());
            temp.setXgTime(getNowDate());
            temp.setXgNo(getUsername());
            return cphBiyeApplyService.doAudit(temp);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }

    @GetMapping("/listJwcAll")
    @ApiOperation("学工所有审核记录")
    public TableDataInfo listJwcAll(CphSearch param){
        boolean hasRole = RoleBool.isJwc(getUserId(),_postService);
        if(hasRole){
            startPage();
            List<CphBiyeApplyVo> list = cphBiyeApplyService.listView(param);
            return getDataTable(list);
        }else{
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    @GetMapping("/listJwc")
    @ApiOperation("查询学工待审核")
    public TableDataInfo listJwc(CphSearch param){
        boolean hasRole = RoleBool.isJwc(getUserId(),_postService);
        if(hasRole){
            param.setStatus("3");
            startPage();
            List<CphBiyeApplyVo> list = cphBiyeApplyService.listView(param);
            return getDataTable(list);
        }else{
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    @PostMapping("/xwManyAudit")
    @ApiOperation("学务一键审核通过")
    public AjaxResult xwManyAudit(@RequestBody CphAuditMany param){
        boolean hasRole = RoleBool.isXw(getUserId(),_postService);
        if(hasRole){
            return cphBiyeApplyService.xwManyAudit(param);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }



    @GetMapping("/xwExport")
    @ApiOperation("学务导出")
    public AjaxResult xwExport(CphSearch param){
        boolean hasRole = RoleBool.isXw(getUserId(),_postService);
        if(hasRole){
            Long oldId = getDeptId();
            SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
            Long deptId = 0L;
            if(dept != null){
                deptId = dept.getOldDeptId();
            }
            param.setDeptId(deptId);
            List<CphBiyeApplyVo> list = cphBiyeApplyService.exportView(param);
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }

    @PostMapping("/xwAudit")
    @ApiOperation("学务审核")
    public AjaxResult xwAudit(@RequestBody CphBiyeApply param){
        boolean hasRole = RoleBool.isXw(getUserId(),_postService);
        if(hasRole){
            CphBiyeApply temp = new CphBiyeApply();
            temp.setId(param.getId());
            temp.setApplyStatus(param.getApplyStatus());
            temp.setXwCmt(param.getXwCmt());
            temp.setXwNo(getUsername());
            temp.setXwTime(getNowDate());
            return cphBiyeApplyService.doAudit(temp);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }

    @GetMapping("/listXwAll")
    @ApiOperation("查询学务所有审核记录")
    public TableDataInfo listXwAll(CphSearch param){
        boolean hasRole = RoleBool.isXw(getUserId(),_postService);
        if(hasRole){
            Long oldId = getDeptId();
            SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
            Long deptId = 0L;
            if(dept != null){
                deptId = dept.getOldDeptId();
            }
            param.setDeptId(deptId);
            startPage();
            List<CphBiyeApplyVo> list = cphBiyeApplyService.listView(param);
            return getDataTable(list);
        }else{
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    @GetMapping("/listXw")
    @ApiOperation("查询学务待审核")
    public TableDataInfo listXw(CphSearch param){
        boolean hasRole = RoleBool.isXw(getUserId(),_postService);
        if(hasRole){
            Long oldId = getDeptId();
            SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
            Long deptId = 0L;
            if(dept != null){
                deptId = dept.getOldDeptId();
            }
            param.setDeptId(deptId);
            param.setStatus("2");
            startPage();
            List<CphBiyeApplyVo> list = cphBiyeApplyService.listView(param);
            return getDataTable(list);
        }else{
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }



    @GetMapping("/fdyExport")
    @ApiOperation("辅导员导出")
    public AjaxResult fdyExport(CphSearch param){
        boolean hasRole = RoleBool.isFdy(getUserId(),_postService);
        if(hasRole){
            param.setTNo(getUsername());
            List<CphBiyeApplyVo> list = cphBiyeApplyService.exportView(param);
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }


    @PostMapping("/fdyAudit")
    @ApiOperation("辅导员审核")
    public AjaxResult fdyAudit(@RequestBody CphBiyeApply param) {
        boolean hasRole = RoleBool.isFdy(getUserId(), _postService);
        if (hasRole) {
            CphBiyeApply temp = new CphBiyeApply();
            temp.setId(param.getId());
            temp.setApplyStatus(param.getApplyStatus());
            temp.setFdyCmt(param.getFdyCmt());
            temp.setFdyNo(getUsername());
            temp.setFdyTime(getNowDate());
            return cphBiyeApplyService.doAudit(temp);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @GetMapping("/listFdyAll")
    @ApiOperation("查询辅导所有审核")
    public TableDataInfo listFdyAll(CphSearch param) {
        boolean hasRole = RoleBool.isFdy(getUserId(), _postService);
        if (hasRole) {
            param.setTNo(getUsername());
            startPage();
            List<CphBiyeApplyVo> list = cphBiyeApplyService.listView(param);
            return getDataTable(list);
        } else {
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }


    @GetMapping("/listFdy")
    @ApiOperation("辅导员审核")
    public TableDataInfo listFdy(CphSearch param) {
        boolean hasRole = RoleBool.isFdy(getUserId(), _postService);
        if (hasRole) {
            param.setTNo(getUsername());
            param.setStatus("1");
            startPage();
            List<CphBiyeApplyVo> list = cphBiyeApplyService.listView(param);
            return getDataTable(list);
        } else {
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    @GetMapping("/listStuSportScore/{stuNo}")
    @ApiOperation("获取学生体测成绩")
    public AjaxResult listStuSportScore(@PathVariable("stuNo") String stuNo) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return cphBiyeApplyService.listStuSportScore(stuNo);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    /**
     * 根据标签绑定的学年获取学生体测成绩
     * @param stuNo
     * @param tag
     * @return
     */
    @GetMapping("/listStuSportScoreByTag/{stuNo}")
    @ApiOperation("获取学生体测成绩")
    public AjaxResult listStuSportScore(@PathVariable("stuNo") String stuNo, String tag) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return cphBiyeApplyService.listStuSportScore(stuNo, tag);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @GetMapping("/getStuScoreMajorRank/{stuNo}")
    @ApiOperation("获取我的学业成绩排名")
    public AjaxResult getStuScoreMajorRank(@PathVariable("stuNo") String stuNo) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return cphBiyeApplyService.getStuScoreMajorRank(stuNo);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

/**
     * 根据标签绑定的学年获取我的体测成绩
     * @return
     */
    @GetMapping("/listOwnSportScoreByTag")
    @ApiOperation("获取我的体测成绩")
    public AjaxResult listOwnSportScore(String tag) {
        return cphBiyeApplyService.listStuSportScore(getUsername(), tag);
    }



    @GetMapping("/listOwnSportScore")
    @ApiOperation("获取我的体测成绩")
    public AjaxResult listOwnSportScore() {
        return cphBiyeApplyService.listStuSportScore(getUsername());
    }


    @GetMapping("/getOwnStuScoreMajorRank")
    @ApiOperation("获取我的学业成绩排名")
    public AjaxResult getOwnStuScoreMajorRank() {
        return cphBiyeApplyService.getStuScoreMajorRank(getUsername());
    }


    @PostMapping("/cancelApply")
    @ApiOperation("取消申请")
    public AjaxResult cancelApply(@RequestBody CphCancel param) {
        return cphBiyeApplyService.cancelApply(param);
    }

    @PostMapping("/doApply")
    @ApiOperation("申请")
    public AjaxResult doApply(@RequestBody CphBiyeApply param) {
        param.setCreateTime(getNowDate());
        param.setCreateBy(getUsername());
        return cphBiyeApplyService.doApply(param);
    }

    @PostMapping("/reApply")
    @ApiOperation("重新提交")
    public AjaxResult reApply(@RequestBody CphBiyeApply param){
        return cphBiyeApplyService.reApply(param);
    }

    @GetMapping("/listOwnApply")
    @ApiOperation("列出我的申请")
    public TableDataInfo listOwnApply() {
        startPage();
        List<CphBiyeApplyVo> list = cphBiyeApplyService.listOwnApply();
        return getDataTable(list);
    }


    /**
     * 查询优秀毕业生列表
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:biyeapply:list')")
    @GetMapping("/list")
    @ApiOperation("查询优秀毕业生列表")
    public TableDataInfo list(CphBiyeApply cphBiyeApply) {
        startPage();
        List<CphBiyeApply> list = cphBiyeApplyService.selectCphBiyeApplyList(cphBiyeApply);
        return getDataTable(list);
    }

    /**
     * 导出优秀毕业生列表
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:biyeapply:export')")
    @Log(title = "优秀毕业生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出优秀毕业生列表")
    public void export(HttpServletResponse response, CphBiyeApply cphBiyeApply) {
        List<CphBiyeApply> list = cphBiyeApplyService.selectCphBiyeApplyList(cphBiyeApply);
        ExcelUtil<CphBiyeApply> util = new ExcelUtil<CphBiyeApply>(CphBiyeApply.class);
        util.exportExcel(response, list, "优秀毕业生数据");
    }

    /**
     * 获取优秀毕业生详细信息
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:biyeapply:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取优秀毕业生详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cphBiyeApplyService.selectCphBiyeApplyById(id));
    }

    /**
     * 新增优秀毕业生
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:biyeapply:add')")
    @Log(title = "优秀毕业生", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增优秀毕业生")
    public AjaxResult add(@RequestBody CphBiyeApply cphBiyeApply) {
        return toAjax(cphBiyeApplyService.insertCphBiyeApply(cphBiyeApply));
    }

    /**
     * 修改优秀毕业生
     */
//    @PreAuthorize("@ss.hasPermi('comprehensive:biyeapply:edit')")
    @Log(title = "优秀毕业生", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改优秀毕业生")
    public AjaxResult edit(@RequestBody CphBiyeApply cphBiyeApply) {
        return toAjax(cphBiyeApplyService.updateCphBiyeApply(cphBiyeApply));
    }

    /**
     * 删除优秀毕业生
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:biyeapply:remove')")
    @Log(title = "优秀毕业生", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除优秀毕业生")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cphBiyeApplyService.deleteCphBiyeApplyByIds(ids));
    }
}
