package com.srs.web.controller.workstudy;

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

import com.srs.system.service.ISysRoleService;
import com.srs.web.controller.common.RoleBool;
import com.srs.workstudy.domain.QgzxMoney;
import com.srs.workstudy.domain.QgzxPost;
import com.srs.workstudy.domain.QgzxStuPost;
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.QgzxWorkLog;
import com.srs.workstudy.service.IQgzxWorkLogService;
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-06-11
 */
@RestController
@RequestMapping("/workstudy/worklog")
@Api(value = "勤工助学工作记录管理", tags = "勤工助学工作记录管理")
public class QgzxWorkLogController extends BaseController {
    @Autowired
    private IQgzxWorkLogService qgzxWorkLogService;

    @Autowired
    private ISysRoleService _roleService;

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


    @GetMapping("/listView")
    @ApiOperation("listView")
    public TableDataInfo listView(QgzxWorkLog param) {
        if(RoleBool.notStu(getUserId(), _roleService)){
            startPage();
            List<QgzxWorkLog> list = qgzxWorkLogService.listView( param);
            return getDataTable(list);
        }else{
            return getDataTable(null);
        }
    }

    @GetMapping("/listWorkLogMonthTotal")
    @ApiOperation("列出学生月总工作时间")
    public TableDataInfo listWorkLogMonthTotal(QgzxWorkLog param) {
        if(RoleBool.notStu(getUserId(), _roleService)){
            startPage();
            List< HashMap <String, Object> > list = qgzxWorkLogService.listWorkLogMonthTotal( param);
            return getDataTable(list);
        }else{
            return getDataTable(null);
        }
    }


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

    @GetMapping("/computeTimeByStuNo")
    @ApiOperation("学生结算月总工作时间")
    public AjaxResult computeTimeByStuNo() {
        return qgzxWorkLogService.computeTimeByStuNo(getUsername());
    }

    @PostMapping("/computeTime")
    @ApiOperation("结算总工作时间")
    public AjaxResult computeTime(@RequestBody QgzxMoney param){
        return qgzxWorkLogService.computeTime(param);
    }

    @GetMapping("/computeTimeById/{id}")
    @ApiOperation("通过学生岗位id结算总工作时间")
    public AjaxResult computeTimeById(@PathVariable Long id) {
        return qgzxWorkLogService.computeTimeById(id);
    }


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


    @GetMapping("/listZdlsAll")
    @ApiOperation("指导老师查看所有申请")
    public TableDataInfo listZdlsAll(QgzxWorkLog param) {
        param.zdlsNo = getUsername();
        startPage();
        List<QgzxWorkLog> list = qgzxWorkLogService.listView(param);
        return getDataTable(list);
    }

    @GetMapping("/listZdls")
    @ApiOperation("指导老师查看申请")
    public TableDataInfo listZdls(QgzxWorkLog param) {
        param.zdlsNo = getUsername();
        param.applyStatus = "1";
        startPage();
        List<QgzxWorkLog> list = qgzxWorkLogService.listView(param);
        return getDataTable(list);
    }

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

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


    @PostMapping("/doApply")
    @ApiOperation("学生提交申请")
    public AjaxResult doApply(@RequestBody QgzxWorkLog param) {
        return qgzxWorkLogService.doApply(param);
    }


    @PostMapping("/doSave")
    @ApiOperation("学生保存申请")
    public AjaxResult doSave(@RequestBody QgzxWorkLog param) {
        return qgzxWorkLogService.doSave(param);
    }


    /**
     * 查询勤工助学工作记录列表
     */
    @PreAuthorize("@ss.hasPermi('workstudy:worklog:list')")
    @GetMapping("/list")
    @ApiOperation("查询勤工助学工作记录列表")
    public TableDataInfo list(QgzxWorkLog qgzxWorkLog) {
        startPage();
        List<QgzxWorkLog> list = qgzxWorkLogService.selectQgzxWorkLogList(qgzxWorkLog);
        return getDataTable(list);
    }

    /**
     * 导出勤工助学工作记录列表
     */
    @PreAuthorize("@ss.hasPermi('workstudy:worklog:export')")
    @Log(title = "勤工助学工作记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出勤工助学工作记录列表")
    public void export(HttpServletResponse response, QgzxWorkLog qgzxWorkLog) {
        List<QgzxWorkLog> list = qgzxWorkLogService.selectQgzxWorkLogList(qgzxWorkLog);
        ExcelUtil<QgzxWorkLog> util = new ExcelUtil<QgzxWorkLog>(QgzxWorkLog.class);
        util.exportExcel(response, list, "勤工助学工作记录数据");
    }

    /**
     * 获取勤工助学工作记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('workstudy:worklog:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取勤工助学工作记录详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(qgzxWorkLogService.selectQgzxWorkLogById(id));
    }

    /**
     * 新增勤工助学工作记录
     */
    @PreAuthorize("@ss.hasPermi('workstudy:worklog:add')")
    @Log(title = "勤工助学工作记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增勤工助学工作记录")
    public AjaxResult add(@RequestBody QgzxWorkLog qgzxWorkLog) {
        return toAjax(qgzxWorkLogService.insertQgzxWorkLog(qgzxWorkLog));
    }

    /**
     * 修改勤工助学工作记录
     */
    @PreAuthorize("@ss.hasPermi('workstudy:worklog:edit')")
    @Log(title = "勤工助学工作记录", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改勤工助学工作记录")
    public AjaxResult edit(@RequestBody QgzxWorkLog qgzxWorkLog) {
        return toAjax(qgzxWorkLogService.updateQgzxWorkLog(qgzxWorkLog));
    }

    /**
     * 删除勤工助学工作记录
     */
    @PreAuthorize("@ss.hasPermi('workstudy:worklog:remove')")
    @Log(title = "勤工助学工作记录", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除勤工助学工作记录")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(qgzxWorkLogService.deleteQgzxWorkLogByIds(ids));
    }
}
