package com.srs.web.controller.dormitory;

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

import com.srs.dormitory.domain.Dto.DmsSearch;
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.DmsNewReg;
import com.srs.dormitory.service.IDmsNewRegService;
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-30
 */
@RestController
@RequestMapping("/dormitory/reg")
@Api(value = "新生宿舍管理", tags = "新生宿舍管理")
public class DmsNewRegController extends BaseController {
    @Autowired
    private IDmsNewRegService dmsNewRegService;

    @Autowired
    ISysPostService _postService;


    @ApiOperation("安排所有已报到新生入住")
    @PostMapping("/checkAllDorm")
    public AjaxResult checkAllDorm(){
        boolean hasRole = RoleBool.isHigh(getUserId(),_postService);
        if(hasRole){
            return dmsNewRegService.checkAllDorm();
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }


    @ApiOperation("安排新生入住")
    @PostMapping("/checkDorm/{ksh}")
    public AjaxResult checkDorm(@PathVariable String ksh){
        boolean hasRole = RoleBool.isHigh(getUserId(),_postService);
        if(hasRole){
            return dmsNewRegService.checkDorm(ksh);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }


    @GetMapping("/getOwnDorm")
    @ApiOperation("获取新生自己的宿舍")
    public AjaxResult getOwnDorm(){
        String ksh = getUsername();
        DmsSearch param = new DmsSearch();
        param.ksh = ksh;
        return dmsNewRegService.getStuDorm(param);
    }



    /**
     * 查询新生宿舍列表
     */
    @PreAuthorize("@ss.hasPermi('dormitory:reg:list')")
    @GetMapping("/list")
    @ApiOperation("查询新生宿舍列表")
    public TableDataInfo list(DmsNewReg dmsNewReg) {
        startPage();
        List<DmsNewReg> list = dmsNewRegService.selectDmsNewRegList(dmsNewReg);
        return getDataTable(list);
    }

    /**
     * 导出新生宿舍列表
     */
    @PreAuthorize("@ss.hasPermi('dormitory:reg:export')")
    @Log(title = "新生宿舍", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出新生宿舍列表")
    public void export(HttpServletResponse response, DmsNewReg dmsNewReg) {
        List<DmsNewReg> list = dmsNewRegService.selectDmsNewRegList(dmsNewReg);
        ExcelUtil<DmsNewReg> util = new ExcelUtil<DmsNewReg>(DmsNewReg.class);
        util.exportExcel(response, list, "新生宿舍数据");
    }

    /**
     * 获取新生宿舍详细信息
     */
    @PreAuthorize("@ss.hasPermi('dormitory:reg:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取新生宿舍详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dmsNewRegService.selectDmsNewRegById(id));
    }

    /**
     * 新增新生宿舍
     */
    @PreAuthorize("@ss.hasPermi('dormitory:reg:add')")
    @Log(title = "新生宿舍", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增新生宿舍")
    public AjaxResult add(@RequestBody DmsNewReg dmsNewReg) {
        return toAjax(dmsNewRegService.insertDmsNewReg(dmsNewReg));
    }

    /**
     * 修改新生宿舍
     */
    @PreAuthorize("@ss.hasPermi('dormitory:reg:edit')")
    @Log(title = "新生宿舍", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改新生宿舍")
    public AjaxResult edit(@RequestBody DmsNewReg dmsNewReg) {
        return toAjax(dmsNewRegService.updateDmsNewReg(dmsNewReg));
    }

    /**
     * 删除新生宿舍
     */
    @PreAuthorize("@ss.hasPermi('dormitory:reg:remove')")
    @Log(title = "新生宿舍", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除新生宿舍")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dmsNewRegService.deleteDmsNewRegByIds(ids));
    }
}
