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.examsite.ExamSiteGuideReq;
import com.qmth.exam.reserve.bean.examsite.ExamSiteReq;
import com.qmth.exam.reserve.bean.examsite.ExamSiteSaveReq;
import com.qmth.exam.reserve.bean.examsite.ExamSiteVO;
import com.qmth.exam.reserve.bean.login.LoginUser;
import com.qmth.exam.reserve.controller.BaseController;
import com.qmth.exam.reserve.entity.ExamSiteEntity;
import com.qmth.exam.reserve.enums.Role;
import com.qmth.exam.reserve.service.ExamSiteService;
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/site")
@Aac(strict = false, auth = true)
public class ExamSiteManageController extends BaseController {

    @Autowired
    private ExamSiteService examSiteService;

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

    @ApiOperation(value = "考点管理查询")
    @PostMapping(value = "/find")
    public ExamSiteEntity find(@ApiParam("考点ID") @RequestParam Long id) {
        return examSiteService.getById(id);
    }

    @ApiOperation(value = "考点新增/编辑")
    @PostMapping(value = "/save")
    public void save(@RequestBody ExamSiteSaveReq req) {
        LoginUser user = curLoginUser();
        if(!user.getRole().equals(Role.ADMIN)) {
            throw new StatusException("没有权限");
        }
        examSiteService.saveExamSite(user, req);
    }

    @ApiOperation(value = "考点启用/禁用")
    @PostMapping(value = "/enable")
    public void enable(@ApiParam("考点ID") @RequestParam Long id, @ApiParam("启用/禁用") @RequestParam Boolean enable) {
        LoginUser user = curLoginUser();
        if(!user.getRole().equals(Role.ADMIN)) {
            throw new StatusException("没有权限");
        }
        examSiteService.enable(id, enable);
    }

    @ApiOperation(value = "查询考点指引")
    @PostMapping(value = "/guide")
    public String guide(@ApiParam("考点ID") @RequestParam Long id) {
        ExamSiteEntity examSite = examSiteService.getById(id);
        return examSite.getGuide();
    }

    @ApiOperation(value = "保存考点指引")
    @PostMapping(value = "/guide/save")
    public void saveGuide(@ApiParam("考点指引") @RequestBody ExamSiteGuideReq req) {
        examSiteService.saveGuide(req);
    }

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

    @ApiOperation(value = "考点导入")
    @PostMapping(value = "/import")
    public Map<String, Object> importExamSite(@RequestParam MultipartFile file) {
        LoginUser user = this.curLoginUser();
        if (!Role.ADMIN.equals(user.getRole())) {
            throw new StatusException("没有权限");
        }
        List<Map<String, Object>> failRecords;
        try {
            failRecords = examSiteService.importExamSite(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;
    }
}
