package com.srs.web.controller.workstudy;

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.page.TableDataInfo;
import com.srs.common.enums.BusinessType;
import com.srs.common.enums.QgzxApplyStatus;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.comprehensive.domain.CphTeacher;
import com.srs.system.service.ISysRoleService;
import com.srs.web.controller.common.RoleBool;
import com.srs.workstudy.domain.QgzxPost;
import com.srs.workstudy.domain.QgzxStuPost;
import com.srs.workstudy.domain.dto.QgzxStuPostDto;
import com.srs.workstudy.domain.dto.StuPostSelectDto;
import com.srs.workstudy.domain.vo.StuPostListFindVo;
import com.srs.workstudy.service.IQgzxStuPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 学生岗位Controller
 *
 * @author srs
 * @date 2024-06-03
 */
@RestController
@RequestMapping("/workstudy/stuPost")
@Api(value = "学生岗位管理", tags = "学生岗位管理")
public class QgzxStuPostController extends BaseController {
    @Autowired
    private IQgzxStuPostService qgzxStuPostService;

    @Autowired
    private ISysRoleService _roleService;

    @GetMapping("/listTeacher")
    @ApiOperation("查询教职工列表")
    public TableDataInfo listTeacher(CphTeacher param){
        startPage();
        List<CphTeacher> list = qgzxStuPostService.listTeacher(param);
        return getDataTable(list);
    }


    @GetMapping("/getStuFdyName/{stuNo}")
    @ApiOperation("获取学生的辅导员姓名")
    public AjaxResult getStuFdyName(@PathVariable("stuNo") String stuNo){
        if(RoleBool.notStu(getUserId(), _roleService)){
            return qgzxStuPostService.getStuFdyName(stuNo);
        }else{
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @GetMapping("/getOwnFdyName")
    @ApiOperation("获取学生自己的辅导员姓名")
    public AjaxResult getOwnFdyName(){
        return qgzxStuPostService.getStuFdyName(getUsername());
    }

    @PostMapping("/doReply")
    @ApiOperation("重新提交勤工助学岗位")
    public AjaxResult doReply(@RequestBody QgzxStuPost param) {

        return qgzxStuPostService.doReply(param);
    }

    @GetMapping("/countStuPostInByPostId/{id}")
    @ApiOperation("通过岗位id查询学生在岗数")
    public AjaxResult countStuPostInByPostId(@PathVariable("id") Long postId) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            Map<String, Object> data = qgzxStuPostService.countStuPostInByPostId(postId);
            return AjaxResult.success(data);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @PostMapping("/doQuit")
    @ApiOperation("老师退岗")
    public AjaxResult doQuit(@RequestBody QgzxStuPost param) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            return qgzxStuPostService.doQuit(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @GetMapping("/listAllCanSelectPost")
    @ApiOperation("列出指导老师通过审核并且在岗的记录")
    public TableDataInfo listAllCanSelectPost(QgzxStuPost param) {
        param.applyStatus = "4";
        param.workStatus = "是";
        startPage();
        List<QgzxStuPost> list = qgzxStuPostService.listView(param);
        return getDataTable(list);
    }


    @GetMapping("/listTeacherCanSelectPost")
    @ApiOperation("列出指导老师通过审核并且在岗的记录")
    public TableDataInfo listTeacherCanSelectPost(QgzxStuPost param) {
        param.setZdlsNo(getUsername());
        param.applyStatus = "4";
        param.workStatus = "是";
        startPage();
        List<QgzxStuPost> list = qgzxStuPostService.listView(param);
        return getDataTable(list);
    }


    @GetMapping("/listOwnCanSelectPost")
    @ApiOperation("列出学生通过审核并且在岗的记录")
    public TableDataInfo listOwnCanSelectPost(QgzxStuPost param) {
        param.stuNo = getUsername();
        param.applyStatus = "4";
        param.workStatus = "是";
        startPage();
        List<QgzxStuPost> list = qgzxStuPostService.listView(param);
        return getDataTable(list);
    }

    @PostMapping("/doAudit")
    @ApiOperation("老师审核")
    public AjaxResult doAudit(@RequestBody QgzxStuPost param) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            return qgzxStuPostService.doAudit(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @ApiOperation("批量审核")
    @PostMapping("/manyAudit")
    public AjaxResult manyAudit(@RequestBody List<QgzxStuPost> param) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            return qgzxStuPostService.manyAudit(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @ApiOperation("辅导员查看申请")
    @GetMapping("/listFdy")
    public TableDataInfo listFdyApply(QgzxStuPost param) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            param.setFdyNo(getUsername());
            param.applyStatus = QgzxApplyStatus.SUBMITTED.getCode();
            startPage();
            List<QgzxStuPost> list = qgzxStuPostService.listView(param);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }

    @ApiOperation("辅导员查看所有申请")
    @GetMapping("/listFdyAll")
    public TableDataInfo listFdyAll(QgzxStuPost param) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            param.setFdyNo(getUsername());
            startPage();
            List<QgzxStuPost> list = qgzxStuPostService.listView(param);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }

    @GetMapping("/listZdlsAll")
    @ApiOperation("指导老师查看所有申请")
    public TableDataInfo listZdlsAll(QgzxStuPost param) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            param.setZdlsNo(getUsername());
            startPage();
            List<QgzxStuPost> list = qgzxStuPostService.listView(param);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }

    @GetMapping("/listZdls")
    @ApiOperation("指导老师查看申请")
    public TableDataInfo listZdls(QgzxStuPost param) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            param.setZdlsNo(getUsername());
            param.applyStatus = QgzxApplyStatus.COUNSELOR_APPROVED.getCode();
            startPage();
            List<QgzxStuPost> list = qgzxStuPostService.listView(param);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }

    @ApiOperation("资助办领导查看申请")
    @GetMapping("/listZzbld")
    public TableDataInfo listZzbldApply(QgzxStuPost param) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
//            param.setZzbNo(getUsername());
            param.applyStatus = QgzxApplyStatus.INSTRUCTOR_APPROVED.getCode();
            startPage();
            List<QgzxStuPost> list = qgzxStuPostService.listView(param);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }

    @ApiOperation("资助办领导查看所有申请")
    @GetMapping("/listZzbldAll")
    public TableDataInfo listZzbldAll(QgzxStuPost param) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            param.setZzbNo(getUsername());
            startPage();
            List<QgzxStuPost> list = qgzxStuPostService.listView(param);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }


    @PostMapping("/cancelApply/{id}")
    @ApiOperation("学生取消申请")
    public AjaxResult cancelApply(@PathVariable Long id) {
        return qgzxStuPostService.cancelApply(id);
    }


    @GetMapping("/listOwnApply")
    @ApiOperation("学生查看申请")
    public TableDataInfo listOwnApply(QgzxStuPost param) {
        param.stuNo = getUsername();
        startPage();
        List<QgzxStuPost> list = qgzxStuPostService.listView(param);
        return getDataTable(list);
    }


    @PostMapping("/doApply")
    @ApiOperation("学生提交申请")
    public AjaxResult doApply(@RequestBody QgzxStuPost param) {
        param.stuNo = getUsername();
        return qgzxStuPostService.doApply(param);
    }


    @PostMapping("/doSave")
    @ApiOperation("学生保存申请")
    public AjaxResult doSave(@RequestBody QgzxStuPost param) {
        param.stuNo = getUsername();
        return qgzxStuPostService.doSave(param);
    }


    /**
     * 查询学生岗位列表
     */
    @PreAuthorize("@ss.hasPermi('workstudy:stuPost:list')")
    @GetMapping("/list")
    @ApiOperation("查询学生岗位列表")
    public TableDataInfo list(QgzxStuPost qgzxStuPost) {
        startPage();
        List<QgzxStuPost> list = qgzxStuPostService.selectQgzxStuPostList(qgzxStuPost);
        return getDataTable(list);
    }

    @GetMapping("/getList")
    @ApiOperation("列出学生岗位列表")
    public TableDataInfo getList(StuPostSelectDto param) {
        startPage();
        param.setWorkStatus("是");
        List<StuPostListFindVo> list = qgzxStuPostService.getQgzxStuPostList(param);
        return getDataTable(list);
    }

    /**
     * 导出学生岗位列表
     */
    @PreAuthorize("@ss.hasPermi('workstudy:stuPost:export')")
    @Log(title = "学生岗位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出学生岗位列表")
    public void export(HttpServletResponse response, QgzxStuPost qgzxStuPost) {
        List<QgzxStuPost> list = qgzxStuPostService.selectQgzxStuPostList(qgzxStuPost);
        ExcelUtil<QgzxStuPost> util = new ExcelUtil<QgzxStuPost>(QgzxStuPost.class);
        util.exportExcel(response, list, "学生岗位数据");
    }

    /**
     * 获取学生岗位详细信息
     */
    @PreAuthorize("@ss.hasPermi('workstudy:stuPost:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取学生岗位详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(qgzxStuPostService.selectQgzxStuPostById(id));
    }

    /**
     * 新增学生岗位
     */
    @PreAuthorize("@ss.hasPermi('workstudy:stuPost:add')")
    @Log(title = "学生岗位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增学生岗位")
    public AjaxResult add(@RequestBody QgzxStuPost qgzxStuPost) {
        return toAjax(qgzxStuPostService.insertQgzxStuPost(qgzxStuPost));
    }

    /**
     * 修改学生岗位
     */
    @PreAuthorize("@ss.hasPermi('workstudy:stuPost:edit')")
    @Log(title = "学生岗位", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改学生岗位")
    public AjaxResult edit(@RequestBody QgzxStuPost qgzxStuPost) {
        return toAjax(qgzxStuPostService.updateQgzxStuPost(qgzxStuPost));
    }
    /**
     * 修改指导老师
     */
    @PreAuthorize("@ss.hasPermi('workstudy:stuPost:edit')")
    @Log(title = "学生岗位", businessType = BusinessType.UPDATE)
    @PostMapping("/updateInstructor")
    @ApiOperation("修改学生岗位")
    public AjaxResult updateInstructor(@RequestBody QgzxStuPostDto qgzxStuPostDto) {
        return toAjax(qgzxStuPostService.updateInstructor(qgzxStuPostDto));
    }

    /**
     * 删除学生岗位
     */
    @PreAuthorize("@ss.hasPermi('workstudy:stuPost:remove')")
    @Log(title = "学生岗位", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除学生岗位")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(qgzxStuPostService.deleteQgzxStuPostByIds(ids));
    }
}
