package com.srs.web.controller.dormitory;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
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.DmsAccommodations;
import com.srs.dormitory.service.IDmsAccommodationsService;
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 2025-11-11
 */
@RestController
@RequestMapping("/dormitory/accommodations")
@Api(value = "外宿申请管理", tags = "外宿申请管理")
public class DmsAccommodationsController extends BaseController {
    @Autowired
    private IDmsAccommodationsService dmsAccommodationsService;

    /**
     * 查询外宿申请列表
     */
//@PreAuthorize("@ss.hasPermi('dormitory:accommodations:list')")
@GetMapping("/list")
@ApiOperation("查询外宿申请列表")
    public TableDataInfo list(DmsAccommodations dmsAccommodations)
    {
        startPage();
        String userName = getUsername();
        if(dmsAccommodations==null) {
            if (userName.equals("admin")) {
                List<DmsAccommodations> list = dmsAccommodationsService.selectDmsAccommodationsList(dmsAccommodations);
                return getDataTable(list);
            } else{
                dmsAccommodations.setStuNo(userName);
                List<DmsAccommodations> list = dmsAccommodationsService.selectDmsAccommodationsList(dmsAccommodations);
                return getDataTable(list);
            }
        }
        else{
            List<DmsAccommodations> list = dmsAccommodationsService.selectDmsAccommodationsList(dmsAccommodations);
            return getDataTable(list);
        }
    }

    /**
     * 导出外宿申请列表
     */
    @PreAuthorize("@ss.hasPermi('dormitory:accommodations:export')")
    @Log(title = "外宿申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出外宿申请列表")
    public void export(HttpServletResponse response, DmsAccommodations dmsAccommodations)
    {
        List<DmsAccommodations> list = dmsAccommodationsService.selectDmsAccommodationsList(dmsAccommodations);
        ExcelUtil<DmsAccommodations> util = new ExcelUtil<DmsAccommodations>(DmsAccommodations.class);
        util.exportExcel(response, list, "外宿申请数据");
    }

    /**
     * 获取外宿申请详细信息
     */
    //@PreAuthorize("@ss.hasPermi('dormitory:accommodations:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取外宿申请详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dmsAccommodationsService.selectDmsAccommodationsById(id));
    }

    /**
     * 新增外宿申请
     */
//    @PreAuthorize("@ss.hasPermi('dormitory:accommodations:add')")
    @Log(title = "外宿申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增外宿申请")
    public AjaxResult add(@RequestBody DmsAccommodations dmsAccommodations)
    {
        return toAjax(dmsAccommodationsService.insertDmsAccommodations(dmsAccommodations));
    }

    /**
     * 修改外宿申请
     */
   // @PreAuthorize("@ss.hasPermi('dormitory:accommodations:edit')")
    @Log(title = "外宿申请", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改外宿申请")
    public AjaxResult edit(@RequestBody DmsAccommodations dmsAccommodations)
    {
        return toAjax(dmsAccommodationsService.updateDmsAccommodations(dmsAccommodations));
    }

    /**
     * 删除外宿申请
     */
   // @PreAuthorize("@ss.hasPermi('dormitory:accommodations:remove')")
    @Log(title = "外宿申请", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除外宿申请")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dmsAccommodationsService.deleteDmsAccommodationsByIds(ids));
    }
    @GetMapping("/getStuDormitory")
    public  AjaxResult getStuDormitory(){
        String stuNo = getUsername();
        return success(dmsAccommodationsService.getStuDormitory(stuNo));
    }
}
