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

import cc.mrbird.febs.common.redis.service.RedisService;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.common.core.utils.FileUtil;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.school.health.service.EduStudentPhysicalExaminationGradeResultsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 学生学年成绩记录表 Controller
 * @author wjm
 * @date 2021-09-14 17:16:20
 */
@Api(tags = "体质健康-学生成绩导入，模板导出")
@Slf4j
@Validated
@RestController
@RequestMapping("/health/studentGrade")
@RequiredArgsConstructor
public class EduStudentPhysicalExaminationGradeResultsController {

    private final EduStudentPhysicalExaminationGradeResultsService eduStudentPhysicalExaminationGradeResultsService;

    @Resource
    private RedisService redisService;

    @ApiOperation("批量上传学生体侧成绩")
    @PostMapping("/onLoadScore")
    @OperationLog(module = ModuleName.HEALTH, methods = "导入学生体侧成绩", type =
            OperationLogTypeEnum.INSERT)
    public ResultBean uploadExcel(@RequestParam MultipartFile file, @RequestParam Long schoolId, @RequestParam String schoolName,@RequestParam Long groupId) {

        String uuid = UUID.randomUUID().toString();


        eduStudentPhysicalExaminationGradeResultsService.batchUpScore(FileUtil.uploadMultipartFile(file)
                , uuid
                , schoolId, schoolName,groupId);
        if(redisService.hasKey(CachePrefixConstant.HEALTH_USER_IMPORT)){
            List<String> uuidIes = (List<String>)redisService.hget(CachePrefixConstant.HEALTH_USER_IMPORT,FebsUtil.getCurrentUser().getUserId().toString() );
            uuidIes.add(uuid);
        }else {
            List<String> uuidIes = new ArrayList<>();
            uuidIes.add(uuid);
            redisService.hset(CachePrefixConstant.HEALTH_USER_IMPORT,FebsUtil.getCurrentUser().getUserId().toString(),uuidIes);
        }
        return ResultBean.buildSuccess(uuid);
    }

    @ApiOperation("查询用户对应uuid")
    @GetMapping("/findUserUUID")
    public ResultBean downloadTemplate() {
        return ResultBean.buildSuccess(redisService.hget(CachePrefixConstant.HEALTH_USER_IMPORT,FebsUtil.getCurrentUser().getUserId().toString()));
    }

    @ApiOperation("下载健康体测相关导入模板")
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) throws Exception {
        this.eduStudentPhysicalExaminationGradeResultsService.downloadTemplate(response);
    }

    @ApiOperation("查询导入体测数据的进度信息")
    @GetMapping("/getImportPercent")
    @OperationLog(module = ModuleName.HEALTH, methods = "导入体测信息", type =
            OperationLogTypeEnum.INSERT)
    public ResultBean<Object> getImportPercent(String uuid) {
        return ResultBean.buildSuccess(redisService.hget(CachePrefixConstant.HEALTH_IMPORT, uuid));
    }
}
