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

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.common.ResultMsgType;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.constant.FebsConstant;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.common.core.utils.FileUtil;
import com.yida.data.school.dto.smart.ClassFixImportDTO;
import com.yida.data.school.dto.smart.ClassImportDTO;
import com.yida.data.school.dto.smart.DormImportDTO;
import com.yida.data.school.smart.service.EduSmartWelcomeGuideStepClassService;
import com.yida.data.school.smart.service.EduSmartWelcomeGuideStepDormService;
import com.yida.data.school.vo.smart.ClassImportVO;
import com.yida.data.school.vo.smart.DormImportFixVO;
import com.yida.data.school.vo.smart.DormImportVO;
import com.yida.data.school.vo.smart.ImportRosterDataVO;
import com.yida.data.school.vo.smart.WelcomeImportProgress;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

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

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

    private final EduSmartWelcomeGuideStepDormService eduSmartWelcomeGuideStepDormService;
    private final RedisService redisService;

    @PostMapping(value = "importDormData")
    @ApiOperation(value = "导入预选宿舍数据")
    public ResultBean<WelcomeImportProgress<DormImportVO>> importDormData(
        @ApiParam(value = "导入的excel文件", required = true) @RequestParam("file") MultipartFile file) {
        String uuid = UUID.randomUUID().toString();
        eduSmartWelcomeGuideStepDormService.importDormData(file, uuid, FebsUtil.getCurrentUser());
        WelcomeImportProgress<DormImportVO> progress = WelcomeImportProgress.<DormImportVO>builder().importKey(uuid)
            .importStatus(0).total(100).current(0).build();
        redisService.set(CachePrefixConstant.WELCOME_DORM_IMPOST_PROGRESS + uuid, progress);
        return ResultBean.buildSuccess(progress);
    }


    @PostMapping(value = "downLoadImportDormDataTemplate")
    @ApiOperation(value = "下载导入预选宿舍模板")
    public void downLoadImportDormDataTemplate(HttpServletResponse response) {
        eduSmartWelcomeGuideStepDormService.downloadDormTemplate(response);
    }

    @GetMapping(value = "getImportDormDataProgress")
    @ApiOperation(value = "获取导入预选宿舍数据进度")
    public ResultBean<WelcomeImportProgress<DormImportVO>> getImportDormDataProgress(String importKey) {
        WelcomeImportProgress<DormImportVO> o = (WelcomeImportProgress<DormImportVO>) redisService
            .get(CachePrefixConstant.WELCOME_DORM_IMPOST_PROGRESS + importKey);
        return ResultBean.buildSuccess(o);
    }


    @PostMapping(value = "downLoadImportDormFixDataTemplate")
    @ApiOperation(value = "下载导入固定宿舍模板")
    public void downLoadImportDormFixDataTemplate(HttpServletResponse response) {
        eduSmartWelcomeGuideStepDormService.downloadDormFixTemplate(response);
    }

    @PostMapping(value = "importDormFixData")
    @ApiOperation(value = "导入固定宿舍数据")
    public ResultBean<DormImportFixVO> importDormFixData(
        @ApiParam(value = "导入的excel文件", required = true) @RequestParam("file") MultipartFile file,
        @RequestParam Long guideId) {
        return ResultBean
            .buildSuccess(eduSmartWelcomeGuideStepDormService.importDormFixData(file, FebsUtil.getCurrentUser(), guideId));
    }

    @PostMapping(value = "importDormDataByCustom")
    @ApiOperation(value = "导入预选宿舍数据")
    public ResultBean importDormDataByCustom(
        @ApiParam(value = "导入的excel文件", required = true) @RequestParam("file") MultipartFile file, Long guideId) {
        // 验证文件类型
        if (!FileUtil.checkMultipartFile(file, FebsConstant.EXCEL_FILE_TYPE)) {
            return ResultBean.buildError(ResultMsgType.FILE_TYPE_ERROR.getValue());
        }
        // 当前登录用户信息
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        String fileUrl = FileUtil.uploadMultipartFile(file);
        // 导入花名册数据
        eduSmartWelcomeGuideStepDormService.importDormDataByCustom(fileUrl, currentUser);
        return ResultBean.buildSuccess();
    }
}
