package com.hzu.labbooking.controller;

import java.util.List;

import com.hzu.labbooking.domain.vo.CollegeApplyVO;
import com.hzu.labbooking.domain.vo.LabRecordVO;
import com.hzu.labbooking.domain.vo.LabTableVO;
import com.hzu.labbooking.domain.vo.RecordVO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.hzu.labbooking.domain.LabRecord;
import com.hzu.labbooking.service.ILabRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 实验室预约信息Controller
 *
 * @author yezihao
 * @date 2021-06-14
 */
@RestController
@RequestMapping("/labbooking/record")
public class LabRecordController extends BaseController {

    @Autowired
    private ILabRecordService labRecordService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询实验室预约信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(LabRecord labRecord, long year, long term) {
        // startPage(); 不分页
        List<LabRecordVO> list = labRecordService.selectLabRecordList(labRecord, year, term);
        return getDataTable(list);
    }

    @PostMapping("/addRecordAndWeek")
    public R<Integer> addRecordAndWeek(@RequestBody LabTableVO vo) {
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        int sz = labRecordService.addRecordAndWeek(vo, user.getUserId());
        if (sz > 0) {
            return R.ok(sz, "预约成功");
        }
        // 这里应该加状态码和msg放到Constants里
        return R.fail("预约失败");
    }

    /**
     * 教师取消实验室预约信息列表
     */
    @Log(title = "取消实验室预约", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel")
    public AjaxResult cancelBooking(@RequestBody List<Long> recordIdList) {
        return toAjax(labRecordService.cancelBooking(recordIdList));
    }

    /**
     * 管理员查询实验室预约信息列表
     */
    @GetMapping("/managerList")
    public TableDataInfo managerList(LabRecord labRecord, long year, long term) {
        List<LabRecordVO> list = labRecordService.selectManagerRecordList(labRecord, year, term);
        return getDataTable(list);
    }

    /**
     * 管理员审核实验室预约信息列表
     */
    @Log(title = "管理员审核实验室预约", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public AjaxResult approveBooking(@RequestBody List<RecordVO> vos) {
        return toAjax(labRecordService.approveBooking(vos));
    }

    /**
     * 管理员取消实验室预约信息列表
     */
    @Log(title = "管理员取消实验室预约", businessType = BusinessType.UPDATE)
    @PutMapping("/managerCancel")
    public AjaxResult managerCancelBooking(@RequestBody List<RecordVO> vos) {
        return toAjax(labRecordService.managerCancelBooking(vos));
    }

    /**
     * 每学期/每个学院实验室申请率（扇形展示echarts）
     */
    @GetMapping("/college")
    public AjaxResult college(Long year, Long term) {
        List<CollegeApplyVO> list = labRecordService.selectCollegeApply(year, term);
        return AjaxResult.success(list);
    }

    /**
     * 导出实验室预约信息列表
     */
    @Log(title = "实验室预约信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LabRecord labRecord, long year, long term) {
        List<LabRecordVO> list = labRecordService.selectLabRecordList(labRecord, year, term);
        ExcelUtil<LabRecordVO> util = new ExcelUtil<LabRecordVO>(LabRecordVO.class);
        return util.exportExcel(list, "实验室预约信息数据");
    }

}
