package com.yida.data.school.smart.controller;

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.lang.UUID;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.dto.smart.ClassFixImportDTO;
import com.yida.data.school.smart.service.EduSmartWelcomeGuideStepClassService;
import com.yida.data.school.vo.smart.ClassFixImportVO;
import com.yida.data.school.vo.smart.ClassImportVO;
import com.yida.data.school.vo.smart.WelcomeImportProgress;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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

@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "智慧迎新指南后台api")
@RequestMapping("eduSmartWelcomeGuideStepClass")
public class EduSmartWelcomeGuideStepClassController {

    private final EduSmartWelcomeGuideStepClassService eduSmartWelcomeGuideStepClassService;
    private final RedisService redisService;

    @PostMapping(value = "importClassData")
    @ApiOperation(value = "导入预选班级数据")
    public ResultBean<WelcomeImportProgress<ClassImportVO>> importClassData(
        @ApiParam(value = "导入的excel文件", required = true) @RequestParam("file") MultipartFile file) {
        String uuid = UUID.randomUUID().toString();
        eduSmartWelcomeGuideStepClassService.importClassData(file, uuid, FebsUtil.getCurrentUser());
        WelcomeImportProgress<ClassImportVO> progress = WelcomeImportProgress.<ClassImportVO>builder().importKey(uuid)
            .importStatus(0).current(0).build();
        redisService.set(CachePrefixConstant.WELCOME_CLASS_IMPOST_PROGRESS + uuid, progress);
        return ResultBean.buildSuccess(progress);
    }

    @PostMapping(value = "downLoadImportClassDataTemplate")
    @ApiOperation(value = "下载导入预选班级模板")
    public void downLoadImportClassDataTemplate(HttpServletResponse response) {
        eduSmartWelcomeGuideStepClassService.downloadClassTemplate(response);
    }

    @GetMapping(value = "getImportClassDataProgress")
    @ApiOperation(value = "获取导入预选班级数据进度")
    public ResultBean<WelcomeImportProgress<ClassImportVO>> getImportClassDataProgress(String importKey) {
        WelcomeImportProgress<ClassImportVO> o = (WelcomeImportProgress<ClassImportVO>) redisService
            .get(CachePrefixConstant.WELCOME_CLASS_IMPOST_PROGRESS + importKey);
        return ResultBean.buildSuccess(o);
    }

    @PostMapping(value = "downLoadImportClassFixDataTemplate")
    @ApiOperation(value = "下载导入固定班级学生模板")
    public void downLoadImportClassFixDataTemplate(HttpServletResponse response) {
        eduSmartWelcomeGuideStepClassService.downloadClassFixTemplate(response);
    }

    @PostMapping(value = "importClassFixData")
    @ApiOperation(value = "导入固定班级学生数据")
    public ResultBean<ClassFixImportVO> importClassFixData(
        @ApiParam(value = "导入的excel文件", required = true) @RequestParam("file") MultipartFile file, @RequestParam Long guideId) {
        return ResultBean
            .buildSuccess(eduSmartWelcomeGuideStepClassService.importClassFixData(file, FebsUtil.getCurrentUser(), guideId));
    }

}
