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.domain.entity.SysDept;
import com.srs.common.core.page.TableDataInfo;
import com.srs.common.enums.BusinessType;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.system.service.ISysDeptService;
import com.srs.system.service.ISysRoleService;
import com.srs.web.controller.common.RoleBool;
import com.srs.workstudy.domain.QgzxPost;
import com.srs.workstudy.service.IQgzxPostService;
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 srs
 * @date 2024-05-30
 */
@RestController
@RequestMapping("/workstudy/post")
@Api(value = "勤工助学岗位管理", tags = "勤工助学岗位管理")
public class QgzxPostController extends BaseController {
    @Autowired
    private IQgzxPostService qgzxPostService;

    @Autowired
    private ISysRoleService _roleService;

    @Autowired
    public ISysDeptService _deptService;

    @PreAuthorize("@ss.hasPermi('workstudy:post:zzbExport')")
    @Log(title = "勤工助学岗位", businessType = BusinessType.EXPORT)
    @PostMapping("/zzbExport")
    @ApiOperation("资助办导出勤工助学岗位列表")
    public void zzbExport(HttpServletResponse response, QgzxPost qgzxPost) {
        List<QgzxPost> list = qgzxPostService.listView(qgzxPost);
        ExcelUtil<QgzxPost> util = new ExcelUtil<QgzxPost>(QgzxPost.class);
        util.exportExcel(response, list, "勤工助学岗位数据");
    }


    @PreAuthorize("@ss.hasPermi('home:zzb:qgzx')")
    @GetMapping("/countZzbUnTodo")
    @ApiOperation("统计资助办公室勤工助学模块下的待审核")
    public AjaxResult countZzbUnTodo() {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            return qgzxPostService.countZzbUnTodo();
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @PreAuthorize("@ss.hasPermi('home:zzbld:qgzx')")
    @GetMapping("/countZzbldUnTodo")
    @ApiOperation("统计资助办领导勤工助学模块下的待审核")
    public AjaxResult countZzbldUnTodo() {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            return qgzxPostService.countZzbldUnTodo();
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @PreAuthorize("@ss.hasPermi('home:zdls:qgzx')")
    @GetMapping("/countZdlsUnTodo")
    @ApiOperation("统计指导老师勤工助学模块下的待审核")
    public AjaxResult countZdlsUnTodo() {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            String zdlsNo = getUsername();
            return qgzxPostService.countZdlsUnTodo(zdlsNo);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @PreAuthorize("@ss.hasPermi('home:fdy:qgzx')")
    @GetMapping("/countFdyUnTodo")
    @ApiOperation("统计辅导员勤工助学模块下的待审核")
    public AjaxResult countFdyUnTodo() {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            String fdyNo = getUsername();
            return qgzxPostService.countFdyUnTodo(fdyNo);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @PreAuthorize("@ss.hasPermi('home:dept:qgzx')")
    @GetMapping("/countDeptUnTodo")
    @ApiOperation("统计部门勤工助学模块下的待审核")
    public AjaxResult countDeptUnTodo(){
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            SysDept dept = _deptService.selectDeptById(getDeptId());
            if (dept == null) {
                return AjaxResult.success();
            }
            String deptName = dept.getDeptName();
            return qgzxPostService.countDeptUnTodo(deptName);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @PostMapping("/doExport")
    @ApiOperation("导出勤工助学岗位列表")
    public void doExport(HttpServletResponse response, QgzxPost qgzxPost) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            List<QgzxPost> list = qgzxPostService.listView(qgzxPost);
            ExcelUtil<QgzxPost> util = new ExcelUtil<QgzxPost>(QgzxPost.class);
            util.exportExcel(response, list, "勤工助学岗位数据");
        }
    }

    @GetMapping("/postDataList")
    @ApiOperation("岗位数据")
    public AjaxResult postDataList(QgzxPost qgzxPost) {
        return success(qgzxPostService.listView(qgzxPost));
    }


    @GetMapping(value = "/stuGetInfo/{id}")
    @ApiOperation("学生获取勤工助学岗位详细信息")
    public AjaxResult stuGetInfo(@PathVariable("id") Long id) {
        return success(qgzxPostService.selectQgzxPostById(id));
    }


    @GetMapping("/listHall")
    @ApiOperation("岗位大厅")
    public TableDataInfo listHall(QgzxPost qgzxPost) {
        qgzxPost.applyStatus = "3";
        startPage();
        List<QgzxPost> list = qgzxPostService.listView(qgzxPost);
        return getDataTable(list);
    }


    @GetMapping("/listHighAll")
    @ApiOperation("查询资助办的勤工助学岗位申请列表")
    public TableDataInfo listHighAll(QgzxPost qgzxPost) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            startPage();
            List<QgzxPost> list = qgzxPostService.listView(qgzxPost);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }


    @GetMapping("/listHigh")
    @ApiOperation("查询资助办待审核的勤工助学岗位申请列表")
    public TableDataInfo listHigh(QgzxPost qgzxPost) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            qgzxPost.applyStatus = "2";
            startPage();
            List<QgzxPost> list = qgzxPostService.listView(qgzxPost);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }

    @PostMapping("/doReply")
    @ApiOperation("重新提交勤工助学岗位")
    public AjaxResult doReply(@RequestBody QgzxPost param) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            return qgzxPostService.doReply(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @PostMapping("/doAudit")
    @ApiOperation("审核勤工助学岗位")
    public AjaxResult doAudit(@RequestBody QgzxPost qgzxPost) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            return qgzxPostService.doAudit(qgzxPost);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }



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


    @GetMapping("/getOwnSign")
    @ApiOperation("获取自己的签名")
    public AjaxResult getOwnSign() {
        return qgzxPostService.getUserSign(getUserId());
    }


    @GetMapping("/listDeptAll")
    @ApiOperation("查询部门的勤工助学岗位记录")
    public TableDataInfo listDeptAll(QgzxPost qgzxPost) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            SysDept dept = _deptService.selectDeptById(getDeptId());
            if (dept == null) {
                return getDataTable(new ArrayList<>());
            }
            qgzxPost.deptName = dept.getDeptName();
            startPage();
            List<QgzxPost> list = qgzxPostService.listView(qgzxPost);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }

    @GetMapping("/listDept")
    @ApiOperation("查询部门的勤工助学岗位申请列表")
    public TableDataInfo listDept(QgzxPost qgzxPost) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            SysDept dept = _deptService.selectDeptById(getDeptId());
            if (dept == null) {
                return getDataTable(new ArrayList<>());
            }
            qgzxPost.deptName = dept.getDeptName();
            qgzxPost.applyStatus = "1";
            startPage();
            List<QgzxPost> list = qgzxPostService.listView(qgzxPost);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }



    @PostMapping("/cancelApply/{id}")
    @ApiOperation("取消申请")
    public AjaxResult cancelApply(@PathVariable Long id) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            return qgzxPostService.cancelApply(id);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @GetMapping("/listOwnApply")
    @ApiOperation("查询自己的勤工助学岗位申请列表")
    public TableDataInfo listOwnApply(QgzxPost qgzxPost) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            startPage();
            qgzxPost.applyNo = getUsername();
            List<QgzxPost> list = qgzxPostService.listView(qgzxPost);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }


    @PostMapping("/doSave")
    @ApiOperation("保存勤工助学岗位")
    public AjaxResult doSave(@RequestBody QgzxPost qgzxPost) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            return qgzxPostService.doSave(qgzxPost);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @PostMapping("/doApply")
    @ApiOperation("申请勤工助学岗位")
    public AjaxResult doApply(@RequestBody QgzxPost qgzxPost) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            return qgzxPostService.doApply(qgzxPost);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    /**
     * 查询勤工助学岗位列表
     */
    @PreAuthorize("@ss.hasPermi('workstudy:post:list')")
    @GetMapping("/list")
    @ApiOperation("查询勤工助学岗位列表")
    public TableDataInfo list(QgzxPost qgzxPost) {
        startPage();
        List<QgzxPost> list = qgzxPostService.selectQgzxPostList(qgzxPost);
        return getDataTable(list);
    }

    /**
     * 导出勤工助学岗位列表
     */
    @PreAuthorize("@ss.hasPermi('workstudy:post:export')")
    @Log(title = "勤工助学岗位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出勤工助学岗位列表")
    public void export(HttpServletResponse response, QgzxPost qgzxPost) {
        List<QgzxPost> list = qgzxPostService.selectQgzxPostList(qgzxPost);
        ExcelUtil<QgzxPost> util = new ExcelUtil<QgzxPost>(QgzxPost.class);
        util.exportExcel(response, list, "勤工助学岗位数据");
    }

    /**
     * 获取勤工助学岗位详细信息
     */
    @PreAuthorize("@ss.hasPermi('workstudy:post:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取勤工助学岗位详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(qgzxPostService.selectQgzxPostById(id));
    }

    /**
     * 新增勤工助学岗位
     */
    @PreAuthorize("@ss.hasPermi('workstudy:post:add')")
    @Log(title = "勤工助学岗位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增勤工助学岗位")
    public AjaxResult add(@RequestBody QgzxPost qgzxPost) {
        return toAjax(qgzxPostService.insertQgzxPost(qgzxPost));
    }

    /**
     * 修改勤工助学岗位
     */
    @PreAuthorize("@ss.hasPermi('workstudy:post:edit')")
    @Log(title = "勤工助学岗位", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改勤工助学岗位")
    public AjaxResult edit(@RequestBody QgzxPost qgzxPost) {
        return toAjax(qgzxPostService.updateQgzxPost(qgzxPost));
    }

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