package com.srs.web.controller.workstudy;

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

import com.baomidou.mybatisplus.annotation.TableField;
import com.srs.common.core.domain.entity.SysDept;
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 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.workstudy.domain.QgzxPostChange;
import com.srs.workstudy.service.IQgzxPostChangeService;
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 srs
 * @date 2024-09-11
 */
@RestController
@RequestMapping("/workstudy/change")
@Api(value = "勤工助学岗位修改人数管理", tags = "勤工助学岗位修改人数管理")
public class QgzxPostChangeController extends BaseController {
    @Autowired
    private IQgzxPostChangeService qgzxPostChangeService;

    @Autowired
    private ISysRoleService _roleService;

    @Autowired
    public ISysDeptService _deptService;

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

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


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

    @GetMapping("/listDept")
    @ApiOperation("查询部门的勤工助学岗位修改设岗人数需要审核的申请列表")
    public TableDataInfo listDept(QgzxPostChange param) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            SysDept dept = _deptService.selectDeptById(getDeptId());
            if (dept == null) {
                return getDataTable(new ArrayList<>());
            }
            param.deptName = dept.getDeptName();
            param.applyStatus = "1";
            startPage();
            List<QgzxPostChange> list = qgzxPostChangeService.listView(param);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }

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


    @GetMapping("/listOwnChange")
    @ApiOperation("学务列出自己的修改设岗人数申请列表")
    public TableDataInfo listOwnChange(QgzxPostChange param) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            param.applyNo = getUsername();
            startPage();
            List<QgzxPostChange> list = qgzxPostChangeService.listView(param);
            return getDataTable(list);
        } else {
            return getDataTable(null);
        }
    }

    @PostMapping("/doChange")
    @ApiOperation("申请 修改勤工助学岗位设岗人数")
    public AjaxResult doChange(@RequestBody QgzxPostChange param) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            return qgzxPostChangeService.doChange(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    /**
     * 查询勤工助学岗位修改人数列表
     */
    @PreAuthorize("@ss.hasPermi('workstudy:change:list')")
    @GetMapping("/list")
    @ApiOperation("查询勤工助学岗位修改人数列表")
    public TableDataInfo list(QgzxPostChange qgzxPostChange) {
        startPage();
        List<QgzxPostChange> list = qgzxPostChangeService.selectQgzxPostChangeList(qgzxPostChange);
        return getDataTable(list);
    }

    /**
     * 导出勤工助学岗位修改人数列表
     */
    @PreAuthorize("@ss.hasPermi('workstudy:change:export')")
    @Log(title = "勤工助学岗位修改人数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出勤工助学岗位修改人数列表")
    public void export(HttpServletResponse response, QgzxPostChange qgzxPostChange) {
        List<QgzxPostChange> list = qgzxPostChangeService.selectQgzxPostChangeList(qgzxPostChange);
        ExcelUtil<QgzxPostChange> util = new ExcelUtil<QgzxPostChange>(QgzxPostChange.class);
        util.exportExcel(response, list, "勤工助学岗位修改人数数据");
    }

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

    /**
     * 新增勤工助学岗位修改人数
     */
    @PreAuthorize("@ss.hasPermi('workstudy:change:add')")
    @Log(title = "勤工助学岗位修改人数", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增勤工助学岗位修改人数")
    public AjaxResult add(@RequestBody QgzxPostChange qgzxPostChange) {
        return toAjax(qgzxPostChangeService.insertQgzxPostChange(qgzxPostChange));
    }

    /**
     * 修改勤工助学岗位修改人数
     */
    @PreAuthorize("@ss.hasPermi('workstudy:change:edit')")
    @Log(title = "勤工助学岗位修改人数", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改勤工助学岗位修改人数")
    public AjaxResult edit(@RequestBody QgzxPostChange qgzxPostChange) {
        return toAjax(qgzxPostChangeService.updateQgzxPostChange(qgzxPostChange));
    }

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