package com.srs.web.controller.staff;

import com.srs.common.annotation.Anonymous;
import com.srs.common.annotation.Log;
import com.srs.common.annotation.RepeatSubmit;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.core.page.TableDataInfo;
import com.srs.common.enums.BusinessType;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.staff.domain.SrsStaffOneStopRegistrationRecord;
import com.srs.staff.domain.vo.DepartmentReservationCountExport;
import com.srs.staff.domain.vo.RoomReservationCountExport;
import com.srs.staff.domain.vo.SrsStaffOneStopRegistrationRecordExportVo;
import com.srs.staff.service.ISrsStaffOneStopRegistrationRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 一站式社区模块-社区活动-报名记录Controller
 *
 * @author chc
 * @date 2024-07-16
 */
@Anonymous
@RestController
@RequestMapping("/staff/OneStopRegistrationRecord")
@Api(value = "一站式社区模块-社区活动-报名记录管理", tags = "一站式社区模块-社区活动-报名记录管理")
public class SrsStaffOneStopRegistrationRecordController extends BaseController {
    @Autowired
    private ISrsStaffOneStopRegistrationRecordService srsStaffOneStopRegistrationRecordService;

    /**
     * 查询一站式社区模块-社区活动-报名记录列表
     */
    @GetMapping("/list")
    @ApiOperation("查询一站式社区模块-社区活动-报名记录列表")
    public TableDataInfo list(SrsStaffOneStopRegistrationRecord srsStaffOneStopRegistrationRecord) {
        startPage();
        List<SrsStaffOneStopRegistrationRecord> list = srsStaffOneStopRegistrationRecordService.selectSrsStaffOneStopRegistrationRecordList(srsStaffOneStopRegistrationRecord);
        return getDataTable(list);
    }

    /**
     * 导出一站式社区模块-社区活动-报名记录列表
     */
    @PreAuthorize("@ss.hasPermi('staff:OneStopRegistrationRecord:export')")
    @Log(title = "一站式社区模块-社区活动-报名记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出一站式社区模块-社区活动-报名记录列表")
    public void export(HttpServletResponse response, SrsStaffOneStopRegistrationRecord srsStaffOneStopRegistrationRecord) {
        List<SrsStaffOneStopRegistrationRecordExportVo> list = srsStaffOneStopRegistrationRecordService.selectSrsStaffOneStopRegistrationRecordListexport(srsStaffOneStopRegistrationRecord);
        ExcelUtil<SrsStaffOneStopRegistrationRecordExportVo> util = new ExcelUtil<SrsStaffOneStopRegistrationRecordExportVo>(SrsStaffOneStopRegistrationRecordExportVo.class);
        util.exportExcel(response, list, "一站式社区模块-社区活动-报名记录数据");
    }

    /**
     * 获取一站式社区模块-社区活动-报名记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('staff:OneStopRegistrationRecord:query')")
    @GetMapping(value = "/{recordId}")
    @ApiOperation("获取一站式社区模块-社区活动-报名记录详细信息")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId) {
        return success(srsStaffOneStopRegistrationRecordService.selectSrsStaffOneStopRegistrationRecordByRecordId(recordId));
    }

    /**
     * 新增一站式社区模块-社区活动-报名记录
     */
    @PreAuthorize("@ss.hasPermi('staff:OneStopRegistrationRecord:add')")
    @Log(title = "一站式社区模块-社区活动-报名记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增一站式社区模块-社区活动-报名记录")
    @RepeatSubmit(interval = 1000, message = "请求过于频繁")
    public AjaxResult add(@RequestBody SrsStaffOneStopRegistrationRecord srsStaffOneStopRegistrationRecord) {
        return toAjax(srsStaffOneStopRegistrationRecordService.insertSrsStaffOneStopRegistrationRecord(srsStaffOneStopRegistrationRecord));
    }


    /**
     * 修改一站式社区模块-社区活动-报名记录
     */
    @PreAuthorize("@ss.hasPermi('staff:OneStopRegistrationRecord:edit')")
    @Log(title = "一站式社区模块-社区活动-报名记录", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改一站式社区模块-社区活动-报名记录")
    @RepeatSubmit(interval = 1000, message = "请求过于频繁")
    public AjaxResult edit(@RequestBody SrsStaffOneStopRegistrationRecord srsStaffOneStopRegistrationRecord) {
        return toAjax(srsStaffOneStopRegistrationRecordService.updateSrsStaffOneStopRegistrationRecord(srsStaffOneStopRegistrationRecord));
    }

    /**
     * 批量修改一站式社区模块-社区活动-报名记录
     */
    @PreAuthorize("@ss.hasPermi('staff:OneStopRegistrationRecord:edit')")
    @Log(title = "一站式社区模块-社区活动-报名记录", businessType = BusinessType.UPDATE)
    @PostMapping("/batchUpdateSysRegistrationRecord")
    @ApiOperation("批量修改一站式社区模块-社区活动-报名记录")
    public AjaxResult batchUpdateSysRegistrationRecord(@RequestBody List<SrsStaffOneStopRegistrationRecord> registrationRecordList) {
        return toAjax(srsStaffOneStopRegistrationRecordService.batchUpdateSysRegistrationRecord(registrationRecordList));
    }

    /**
     * 删除一站式社区模块-社区活动-报名记录
     */
    @PreAuthorize("@ss.hasPermi('staff:OneStopRegistrationRecord:remove')")
    @Log(title = "一站式社区模块-社区活动-报名记录", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{recordIds}")
    @ApiOperation("删除一站式社区模块-社区活动-报名记录")
    public AjaxResult remove(@PathVariable Long[] recordIds) {
        return toAjax(srsStaffOneStopRegistrationRecordService.deleteSrsStaffOneStopRegistrationRecordByRecordIds(recordIds));
    }

    /**
     * 取消报名 - 学生取消自己的社区活动报名
     */
    @Log(title = "一站式社区模块-社区活动-取消报名", businessType = BusinessType.DELETE)
    @DeleteMapping("/cancelRegistration/{recordId}")
    @ApiOperation("取消社区活动报名")
    @RepeatSubmit(interval = 1000, message = "请求过于频繁")
    public AjaxResult cancelRegistration(@PathVariable("recordId") Long recordId) {
        System.out.println("DEBUG: Controller cancelRegistration method called with recordId: " + recordId);
        try {
            AjaxResult result = srsStaffOneStopRegistrationRecordService.cancelRegistration(recordId);
            System.out.println("DEBUG: Service method completed successfully");
            return result;
        } catch (Exception e) {
            System.out.println("DEBUG: Exception in Controller: " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }

    @GetMapping("/getDepartmentReservationCount")
    @ApiOperation("统计各部门预约功能房数量")
    public AjaxResult getDepartmentReservationCount(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "role", required = false) String role,
            @RequestParam(value = "borrower", required = false) String borrower,
            @RequestParam(value = "auditStatus", required = false) Integer auditStatus,
            @RequestParam(value = "rtDepar", required = false) List<String> rtDepar) {

        // Call Service method with the Map
            return success(srsStaffOneStopRegistrationRecordService.getDepartmentReservationCount(startTime,endTime,role,borrower,auditStatus,rtDepar));
    }
    //修改为post请求 知无涯
    @PreAuthorize("@ss.hasPermi('staff:export:department')")
    @PostMapping("/exportDepartmentReservationCount")
    @ApiOperation("导出各部门预约功能房数量")
    public void exportDepartmentReservationCount(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "role", required = false) String role,
            @RequestParam(value = "borrower", required = false) String borrower,
            @RequestParam(value = "auditStatus", required = false) Integer auditStatus,
            @RequestParam(value = "rtDepar", required = false) List<String> rtDepar,HttpServletResponse response) {
        List<Map<String, Object>> departmentReservationCount = srsStaffOneStopRegistrationRecordService.getDepartmentReservationCount(startTime, endTime, role, borrower, auditStatus, rtDepar);
        // 把departmentReservationCount的值转给DepartmentReservationCountExport对象
        List<DepartmentReservationCountExport> list = new ArrayList<>();
        for (Map<String, Object> map : departmentReservationCount) {
            DepartmentReservationCountExport departmentReservationCountExport = new DepartmentReservationCountExport();
            departmentReservationCountExport.setDepartment(map.get("department").toString());
            departmentReservationCountExport.setReservation_count(Integer.parseInt(map.get("reservation_count").toString()));
            departmentReservationCountExport.setTotal_visitors(Integer.parseInt(map.get("total_visitors").toString()));
            list.add(departmentReservationCountExport);
        }
        ExcelUtil<DepartmentReservationCountExport> util = new ExcelUtil<DepartmentReservationCountExport>(DepartmentReservationCountExport.class);
        util.exportExcel(response, list, "一站式社区模块-社区活动-报名记录数据");
    }

    @GetMapping("/getRoomReservationCount")
    @ApiOperation("统计各个功能房的预约数量")
    public AjaxResult getRoomReservationCount(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "role", required = false) String role,
            @RequestParam(value = "borrower", required = false) String borrower,
            @RequestParam(value = "auditStatus", required = false) Integer auditStatus,
            @RequestParam(value = "rtDepar", required = false) List<String> rtDepar) {
        // Call Service method with the Map
        return success(srsStaffOneStopRegistrationRecordService.getRoomReservationCount(startTime,endTime,role,borrower,auditStatus,rtDepar));
    }

    //统计改为导出  get改post  知无涯
    @PostMapping("/exportRoomReservationCount")
    @ApiOperation("导出各个功能房的预约数量")
    public void exportRoomReservationCount(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "role", required = false) String role,
            @RequestParam(value = "borrower", required = false) String borrower,
            @RequestParam(value = "auditStatus", required = false) Integer auditStatus,
            @RequestParam(value = "rtDepar", required = false) List<String> rtDepar,HttpServletResponse response) {
        // Call Service method with the Map
        List<Map<String, Object>> roomReservationCount = srsStaffOneStopRegistrationRecordService.getRoomReservationCount(startTime, endTime, role, borrower, auditStatus, rtDepar);
        List<RoomReservationCountExport> list = new ArrayList<>();
        for (Map<String, Object> map : roomReservationCount) {
            RoomReservationCountExport departmentReservationCountExport = new RoomReservationCountExport();
            departmentReservationCountExport.setRoomName(map.get("roomName").toString());
            departmentReservationCountExport.setReservation_count(Integer.parseInt(map.get("reservation_count").toString()));
            departmentReservationCountExport.setTotal_visitors(Integer.parseInt(map.get("total_visitors").toString()));
            list.add(departmentReservationCountExport);
        }
        ExcelUtil<RoomReservationCountExport> util = new ExcelUtil<RoomReservationCountExport>(RoomReservationCountExport.class);
        util.exportExcel(response, list, "一站式社区模块-社区活动-报名记录数据");
    }

    @GetMapping("/getDepartmentUsageStatistics")
    @ApiOperation("统计各部门功能房使用情况")
    public AjaxResult getDepartmentUsageStatistics(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "role", required = false) String role,
            @RequestParam(value = "borrower", required = false) String borrower,
            @RequestParam(value = "auditStatus", required = false) Integer auditStatus,
            @RequestParam(value = "rtDepar", required = false) List<String> rtDepar) {
        // Call Service method with the Map
        return success(srsStaffOneStopRegistrationRecordService.getDepartmentUsageStatistics(startTime,endTime,role,borrower,auditStatus,rtDepar));
    }

    @GetMapping("/getRoomUsageStatistics")
    @ApiOperation("统计各个功能房使用情况")
    public AjaxResult getRoomUsageStatistics(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "role", required = false) String role,
            @RequestParam(value = "borrower", required = false) String borrower,
            @RequestParam(value = "auditStatus", required = false) Integer auditStatus,
            @RequestParam(value = "rtDepar", required = false) List<String> rtDepar) {

        // Call Service method with the Map
        return success(srsStaffOneStopRegistrationRecordService.getRoomUsageStatistics(startTime,endTime,role,borrower,auditStatus,rtDepar));
    }


    @GetMapping("/resetAuditStatus/{id}")
    @ApiOperation("将指定预约记录的审核状态重置为待审核")
    public AjaxResult resetAuditStatus(@PathVariable("id") Integer id) {
        int result = srsStaffOneStopRegistrationRecordService.resetAuditStatus(id);
        if (result > 0) {
            return AjaxResult.success("审核状态已重置为待审核");
        } else {
            return AjaxResult.error("重置失败，请检查记录是否存在");
        }
    }

}
