package com.srs.web.controller.dormitory;

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

import com.srs.comprehensive.domain.Vo.CphSearch;
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.dormitory.domain.DmsNewTask;
import com.srs.dormitory.service.IDmsNewTaskService;
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-03-19
 */
@RestController
@RequestMapping("/dormitory/task")
@Api(value = "宿舍调整任务管理", tags = "宿舍调整任务管理")
public class DmsNewTaskController extends BaseController {
    @Autowired
    private IDmsNewTaskService dmsNewTaskService;
    @Autowired
    ISysPostService _postService;

    @PostMapping("/nowDoTask/{id}")
    @ApiOperation("立即执行任务")
    public AjaxResult nowDoTask(@PathVariable("id") Long taskId){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsNewTaskService.nowDoTask(taskId);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @GetMapping("/listStuTask/{stuNo}")
    @ApiOperation("查看学生调宿任务")
    public AjaxResult listStuTask(@PathVariable("stuNo") String stuNo) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            CphSearch param = new CphSearch();
            param.setStuNo(stuNo);
            return dmsNewTaskService.listView(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @GetMapping("/listOwnTask")
    @ApiOperation("查看个人调宿任务")
    public AjaxResult listOwnTask() {
        String stuNo = getUsername();
        CphSearch param = new CphSearch();
        param.setStuNo(stuNo);
        return dmsNewTaskService.listView(param);
    }


    /**
     * 查询宿舍调整任务列表
     */
    @GetMapping("/list")
    @ApiOperation("查询宿舍调整任务列表")
    public TableDataInfo list(DmsNewTask dmsNewTask) {
        startPage();
        List<DmsNewTask> list = dmsNewTaskService.selectDmsNewTaskList(dmsNewTask);
        return getDataTable(list);
    }

    /**
     * 导出宿舍调整任务列表
     */
    @Log(title = "宿舍调整任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出宿舍调整任务列表")
    public void export(HttpServletResponse response, DmsNewTask dmsNewTask) {
        List<DmsNewTask> list = dmsNewTaskService.selectDmsNewTaskList(dmsNewTask);
        ExcelUtil<DmsNewTask> util = new ExcelUtil<DmsNewTask>(DmsNewTask.class);
        util.exportExcel(response, list, "宿舍调整任务数据");
    }

    /**
     * 获取宿舍调整任务详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取宿舍调整任务详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dmsNewTaskService.selectDmsNewTaskById(id));
    }

    /**
     * 新增宿舍调整任务
     */
    @Log(title = "宿舍调整任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增宿舍调整任务")
    public AjaxResult add(@RequestBody DmsNewTask dmsNewTask) {
        return toAjax(dmsNewTaskService.insertDmsNewTask(dmsNewTask));
    }

    /**
     * 修改宿舍调整任务
     */
    @Log(title = "宿舍调整任务", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改宿舍调整任务")
    public AjaxResult edit(@RequestBody DmsNewTask dmsNewTask) {
        return toAjax(dmsNewTaskService.updateDmsNewTask(dmsNewTask));
    }

    /**
     * 删除宿舍调整任务
     */
    @Log(title = "宿舍调整任务", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除宿舍调整任务")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dmsNewTaskService.deleteDmsNewTaskByIds(ids));
    }
}
