package com.qmth.exam.reserve.controller.admin;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.qmth.boot.api.annotation.Aac;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.boot.core.collection.PageResult;
import com.qmth.boot.core.exception.StatusException;
import com.qmth.exam.reserve.bean.login.LoginUser;
import com.qmth.exam.reserve.bean.room.ExamRoomReq;
import com.qmth.exam.reserve.bean.room.ExamRoomSaveReq;
import com.qmth.exam.reserve.bean.room.ExamRoomVO;
import com.qmth.exam.reserve.controller.BaseController;
import com.qmth.exam.reserve.entity.ExamRoomEntity;
import com.qmth.exam.reserve.enums.Role;
import com.qmth.exam.reserve.service.ExamRoomService;
import com.qmth.exam.reserve.util.ResourceUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "【管理端】考场管理相关接口")
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/admin/room")
@Aac(strict = false, auth = true)
public class ExamRoomController extends BaseController {

    @Autowired
    private ExamRoomService examRoomService;

    @ApiOperation(value = "考场管理分页")
    @PostMapping(value = "/page")
    public PageResult<ExamRoomVO> page(@RequestBody ExamRoomReq req) {
        LoginUser user = curLoginUser();
        if (user.getRole().equals(Role.TEACHING)) {
            req.setTeachingId(user.getCategoryId());
        }
        return examRoomService.pageExamRoom(req);
    }

    @ApiOperation(value = "考场单个查询")
    @PostMapping(value = "/find")
    public ExamRoomEntity find(@RequestParam Long id) {
        return examRoomService.getById(id);
    }

    @ApiOperation(value = "考场新增/编辑")
    @PostMapping(value = "/save")
    public void save(@RequestBody ExamRoomSaveReq req) {
        LoginUser user = curLoginUser();
        examRoomService.saveExamRoom(user, req);
    }

    @ApiOperation(value = "考场启用/禁用")
    @PostMapping(value = "/enable")
    public void enable(@ApiParam("考场ID") @RequestParam Long id, @ApiParam("启用/禁用") @RequestParam Boolean enable) {
        examRoomService.enable(id, enable);
    }

    @ApiOperation(value = "考场模版下载")
    @PostMapping(value = "/import/template")
    public void download() {
        exportFile("考场模板.xlsx", ResourceUtil.getStream("templates/examRoom.xlsx"));
    }

    @ApiOperation(value = "考场导入")
    @PostMapping(value = "/import")
    public Map<String, Object> importExamRoom(@RequestParam MultipartFile file) {
        LoginUser user = this.curLoginUser();
        List<Map<String, Object>> failRecords;
        try {
            failRecords = examRoomService.importExamRoom(user, file.getInputStream());
        } catch (IOException e) {
            throw new StatusException("文件读取出错", e);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("hasError", CollectionUtils.isNotEmpty(failRecords));
        map.put("failRecords", failRecords);
        return map;
    }

}
