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.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.DmsNewRecord;
import com.srs.dormitory.service.IDmsNewRecordService;
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-15
 */
@RestController
@RequestMapping("/dormitory/newRecord")
@Api(value = "住宿记录管理", tags = "住宿记录管理")
public class DmsNewRecordController extends BaseController {
    @Autowired
    private IDmsNewRecordService dmsNewRecordService;


    @Autowired
    ISysPostService _postService;

    @ApiOperation("辅导员一件确认")
    @PostMapping("/fdyManyConfirm")
    public AjaxResult fdyManyConfirm(@RequestBody List<Long> ids) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsNewRecordService.fdyManyConfirm(ids);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @ApiOperation("学工一键确认未进行住宿费用确认的学生")
    @PostMapping("/confirmUnconfirmedStudents")
    public AjaxResult confirmUnconfirmedStudents() {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsNewRecordService.confirmUnconfirmedStudents();
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @ApiOperation("学生填充信息")
    @PostMapping("/fullInfo/{id}")
    public AjaxResult fullInfo(@PathVariable("id") Long id) {
        return dmsNewRecordService.fullInfo(id);
    }


    @ApiOperation("初始化费用记录")
    @PostMapping("/initRecord")
    public AjaxResult initRecord(@RequestBody DmsSearch param) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsNewRecordService.initRecord(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @ApiOperation("学工一键确认所有辅导员已确认的记录")
    @PostMapping("/fastConfirm")
    public AjaxResult fastConfirm() {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsNewRecordService.fastConfirm();
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @ApiOperation("辅导员发起一个人的费用确认")
    @PostMapping("/oneConfirm/{id}")
    public AjaxResult oneConfirm(@PathVariable("id") Long id) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsNewRecordService.oneConfirm(id);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @ApiOperation("辅导员确认费用记录")
    @PostMapping("/fdyConfirm/{id}")
    public AjaxResult fdyConfirm(@PathVariable("id") Long id) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsNewRecordService.fdyConfirm(id);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @ApiOperation("学工确认费用记录")
    @PostMapping("/jwcConfirm/{id}")
    public AjaxResult jwcConfirm(@PathVariable("id") Long id) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsNewRecordService.jwcConfirm(id);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @ApiOperation("学生确认费用记录")
    @PostMapping("/stuConfirm/{id}")
    public AjaxResult stuConfirm(@PathVariable("id") Long id) {
        return dmsNewRecordService.stuConfirm(id);
    }


    @ApiOperation("发起确认")
    @PostMapping("/sendConfirm")
    public AjaxResult sendConfirm(@RequestBody DmsSearch param) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            param.tNo = getUsername();
            return dmsNewRecordService.sendConfirm(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @ApiOperation("学工导出费用记录")
    @GetMapping("/exportJwc")
    public AjaxResult exportJwc(DmsSearch param) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsNewRecordService.exportJwc(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @GetMapping("/listFdy")
    @ApiOperation("查询辅导员住宿记录列表")
    public TableDataInfo listFdy(DmsNewRecord dmsNewRecord) {
        dmsNewRecord.tNo = getUsername();
        dmsNewRecord.searchRole = "fdy";
        startPage();
        List<DmsNewRecord> list = dmsNewRecordService.selectDmsNewRecordList(dmsNewRecord);
        return getDataTable(list);
    }


    /**
     * 查询住宿记录列表
     */
    @GetMapping("/list")
    @ApiOperation("查询住宿记录列表")
    public TableDataInfo list(DmsNewRecord dmsNewRecord) {
        dmsNewRecord.searchRole = "jwc";
        startPage();
        List<DmsNewRecord> list = dmsNewRecordService.selectDmsNewRecordList(dmsNewRecord);
        return getDataTable(list);
    }

    /**
     * 导出住宿记录列表
     */
    @Log(title = "住宿记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出住宿记录列表")
    public void export(HttpServletResponse response, DmsNewRecord dmsNewRecord) {
        List<DmsNewRecord> list = dmsNewRecordService.selectDmsNewRecordList(dmsNewRecord);
        ExcelUtil<DmsNewRecord> util = new ExcelUtil<DmsNewRecord>(DmsNewRecord.class);
        util.exportExcel(response, list, "住宿记录数据");
    }

    /**
     * 获取住宿记录详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取住宿记录详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dmsNewRecordService.selectDmsNewRecordById(id));
    }

    /**
     * 新增住宿记录
     */
    @Log(title = "住宿记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增住宿记录")
    public AjaxResult add(@RequestBody DmsNewRecord dmsNewRecord) {
        return toAjax(dmsNewRecordService.insertDmsNewRecord(dmsNewRecord));
    }

    /**
     * 修改住宿记录
     */
    @Log(title = "住宿记录", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改住宿记录")
    public AjaxResult edit(@RequestBody DmsNewRecord dmsNewRecord) {
        return toAjax(dmsNewRecordService.updateDmsNewRecord(dmsNewRecord));
    }

    /**
     * 删除住宿记录
     */
    @Log(title = "住宿记录", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除住宿记录")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dmsNewRecordService.deleteDmsNewRecordByIds(ids));
    }
}
