package com.srs.web.controller.dormitory;

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

import com.srs.dormitory.domain.Dto.DmsSearch;
import com.srs.dormitory.domain.Vo.DomInfo;
import com.srs.system.domain.SysDeptMap;
import com.srs.system.service.ISysDeptMapService;
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.*;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;

import com.srs.dormitory.domain.DmsManageApplication;
import com.srs.dormitory.service.IDmsManageApplicationService;
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-02-29
 */
@RestController
@RequestMapping("/dormitory/application")
@Api(value = "宿舍管理申请管理", tags = "宿舍管理申请管理")
public class DmsManageApplicationController extends BaseController {
    @Autowired
    private IDmsManageApplicationService dmsManageApplicationService;


    @Autowired
    ISysPostService _postService;
    @Autowired
    ISysDeptMapService _deptMap;


    @GetMapping("/listView")
    @ApiOperation("审核记录")
    public TableDataInfo listView(DmsSearch param){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            startPage();
            List<DomInfo> list = dmsManageApplicationService.listView(param);
            return getDataTable(list);
        }else {
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }




    @PostMapping("/stuChange")
    public AjaxResult stuChange(@RequestBody DmsManageApplication param) {
        return dmsManageApplicationService.stuChange(param);
    }


    @GetMapping("/listOwnApply")
    @ApiOperation("查询学生自己的宿舍管理申请列表")
    public TableDataInfo listOwnApply(DmsManageApplication dmsManageApplication) {
        dmsManageApplication.setGetBy("student");
        startPage();
        if (dmsManageApplication.getGetBy().equals("student")) {
            dmsManageApplication.setStuNo(getUsername());
        }
        List<DmsManageApplication> list = dmsManageApplicationService.selectDmsManageApplicationList(dmsManageApplication);
        return getDataTable(list);
    }



    /**
     * 查询宿舍管理申请列表
     */
//@PreAuthorize("@ss.hasPermi('dormitory:application:list')")
    @GetMapping("/list")
    @ApiOperation("查询宿舍管理申请列表")
    public TableDataInfo list(DmsManageApplication dmsManageApplication) {
        if (dmsManageApplication.getGetBy().equals("student")) {
            dmsManageApplication.setStuNo(getUsername());
        }
        if (dmsManageApplication.getGetBy().equals("teacher")) {
            dmsManageApplication.setFdyNo(getUsername());
        }
        if (dmsManageApplication.getGetBy().equals("work")) {
            Long oldId = getDeptId();
            SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
            Long deptId = null;
            if(dept != null){
                deptId = dept.getOldDeptId();
            }
            dmsManageApplication.deptId = deptId;
        }
        startPage();
        List<DmsManageApplication> list = dmsManageApplicationService.selectDmsManageApplicationList(dmsManageApplication);
        return getDataTable(list);
    }

    /**
     * 导出宿舍管理申请列表
     */
//    @PreAuthorize("@ss.hasPermi('dormitory:application:export')")
    @Log(title = "宿舍管理申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出宿舍管理申请列表")
    public void export(HttpServletResponse response, DmsManageApplication dmsManageApplication) {
        List<DmsManageApplication> list = dmsManageApplicationService.selectDmsManageApplicationList(dmsManageApplication);
        ExcelUtil<DmsManageApplication> util = new ExcelUtil<DmsManageApplication>(DmsManageApplication.class);
        util.exportExcel(response, list, "宿舍管理申请数据");
    }

    /**
     * 获取宿舍管理申请详细信息
     */
//    @PreAuthorize("@ss.hasPermi('dormitory:application:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取宿舍管理申请详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dmsManageApplicationService.selectDmsManageApplicationById(id));
    }

    /**
     * 新增宿舍管理申请
     */
//    @PreAuthorize("@ss.hasPermi('dormitory:application:add')")
    @Log(title = "宿舍管理申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增宿舍管理申请")
    public AjaxResult add(@RequestBody DmsManageApplication dmsManageApplication) {
        return dmsManageApplicationService.insertDmsManageApplication(dmsManageApplication);
    }

    /**
     * 修改宿舍管理申请
     */
//    @PreAuthorize("@ss.hasPermi('dormitory:application:edit')")
    @Log(title = "宿舍管理申请", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改宿舍管理申请")
    public AjaxResult edit(@RequestBody DmsManageApplication dmsManageApplication) {
        return dmsManageApplicationService.updateDmsManageApplication(dmsManageApplication);
    }

    /**
     * 删除宿舍管理申请
     */
//    @PreAuthorize("@ss.hasPermi('dormitory:application:remove')")
    @Log(title = "宿舍管理申请", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除宿舍管理申请")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dmsManageApplicationService.deleteDmsManageApplicationByIds(ids));
    }

    @PostMapping("/stuDel/{ids}")
    @ApiOperation("删除宿舍管理申请")
    public AjaxResult stuDel(@PathVariable Long[] ids) {
        return dmsManageApplicationService.stuDel(ids);
    }

    /**
     * 根据学号获取学生当前宿舍信息
     */
    @GetMapping("/studentDormitoryInfo")
    @ApiOperation("根据学号获取学生当前宿舍信息")
    public AjaxResult getStudentDormitoryInfo(@RequestParam String studentNo) {
        // 调用service层方法获取学生宿舍信息
        return dmsManageApplicationService.getStudentDormitoryInfo(studentNo);
    }


}
