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

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.qmth.boot.core.exception.StatusException;
import com.qmth.exam.reserve.bean.login.LoginUser;
import com.qmth.exam.reserve.bean.stdapply.CategoryVO;
import com.qmth.exam.reserve.bean.teaching.TeachingSaveReq;
import com.qmth.exam.reserve.bean.teaching.TeachingSingleVO;
import com.qmth.exam.reserve.enums.Role;
import com.qmth.exam.reserve.util.ResourceUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.qmth.boot.api.annotation.Aac;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.boot.core.collection.PageResult;
import com.qmth.exam.reserve.bean.teaching.TeachingReq;
import com.qmth.exam.reserve.bean.teaching.TeachingVO;
import com.qmth.exam.reserve.controller.BaseController;
import com.qmth.exam.reserve.service.CategoryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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/teaching")
@Aac(strict = false, auth = true)
public class TeachingController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "教学点管理分页")
    @PostMapping(value = "/page")
    public PageResult<TeachingVO> page(@RequestBody TeachingReq req) {
        return categoryService.page(req);
    }

    @ApiOperation(value = "教学点所属城市选择")
    @PostMapping(value = "/city/list")
    public List<CategoryVO> listCity() {
        LoginUser user = curLoginUser();
        return categoryService.listCity(user);
    }

    @ApiOperation(value = "教学点查询")
    @PostMapping(value = "/find")
    public TeachingSingleVO find(@ApiParam("教学点ID") @RequestParam Long id) {
        return categoryService.getTeaching(id);
    }


    @ApiOperation(value = "教学点新增/编辑")
    @PostMapping(value = "/save")
    public void save(@RequestBody TeachingSaveReq req) {
        LoginUser user = curLoginUser();
        categoryService.saveTeaching(user, req);
    }

    @ApiOperation(value = "教学点启用/禁用")
    @PostMapping(value = "/enable")
    public void enable(@ApiParam("教学点ID") @RequestParam Long id, @ApiParam("启用、禁用") @RequestParam Boolean enable) {
        categoryService.enable(id, enable);
    }

    @ApiOperation(value = "教学点是否开启考生自主预约")
    @PostMapping(value = "/selfApplyEnable")
    public void selfApplyEnable(@ApiParam("教学点ID") @RequestParam Long id, @ApiParam("启用、禁用") @RequestParam Boolean enable) {
        LoginUser loginUser = curLoginUser();
        // 学校管理员无权限
        if(loginUser.getRole() == Role.ADMIN){
            throw new StatusException("无权限");
        }
        categoryService.selfApplyEnable(id, enable);
    }

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


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

}
