package com.yida.data.user.controller;

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.annotation.ControllerLog;
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.entity.user.EduStudentApply;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.user.dto.ListStudentApplyDTO;
import com.yida.data.user.service.EduStudentApplyService;
import com.yida.data.user.vo.StudentApplyImportProgressVO;
import com.yida.data.user.vo.StudentApplyStatusVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Api(tags = "交易-学生应用关系")
@RequiredArgsConstructor
@RequestMapping("/transaction/studentApply")
@RestController
public class EduStudentApplyController {

    private final EduStudentApplyService eduStudentApplyService;

    private final RedisService redisService;

    @ApiOperation("分页查询学生开通应用")
    @GetMapping("/listStudentApply")
    public ResultBean<IPage<EduStudentApply>> listApply(ListStudentApplyDTO dto) {
        Page<EduStudentApply> page = new Page<>();
        page.setCurrent(dto.getPageNum());
        page.setSize(dto.getPageSize());
        return ResultBean.buildSuccess(eduStudentApplyService.listStudentApply(dto, page));
    }

    @ApiOperation("获取开通应用详情")
    @GetMapping("/getStudentApply")
    public ResultBean<EduStudentApply> getStudentApply(@ApiParam("主键ID") Long id) {
        return ResultBean.buildSuccess(eduStudentApplyService.getById(id));
    }

    @ApiOperation("获取开通应用详情")
    @GetMapping("/getStudentApplyByIdAndCode")
    public ResultBean<EduStudentApply> getStudentApplyByIdAndCode(
        @RequestParam("studentId") Long studentId,
        @RequestParam("applyCode") String applyCode) {
        List<EduStudentApply> list = eduStudentApplyService.list(Wrappers.lambdaQuery(new EduStudentApply())
            .eq(EduStudentApply::getStudentId, studentId)
            .eq(EduStudentApply::getApplyCode, applyCode)
            .ge(EduStudentApply::getEndTime, LocalDateTime.now())
            .le(EduStudentApply::getStartTime, LocalDateTime.now())
            .orderByDesc(EduStudentApply::getEndTime)
        );
        EduStudentApply eduStudentApply = null;
        if (CollUtil.isNotEmpty(list)) {
            eduStudentApply = list.get(0);
        }
        return ResultBean.buildSuccess(eduStudentApply);
    }

    @ApiOperation("删除开通应用")
    @GetMapping("/deleteStudentApply")
    @OperationLog(module = ModuleName.TRANSATION, methods = "删除学生开通应用", type = OperationLogTypeEnum.DELETE)
    public ResultBean deleteStudentApply(@ApiParam("主键ID") Long id) {
        eduStudentApplyService.update(Wrappers.lambdaUpdate(new EduStudentApply())
            .eq(EduStudentApply::getId, id)
            .set(EduStudentApply::getDelFlag, 1));
        return ResultBean.buildSuccess();
    }

    @ApiOperation("保存学生开通应用")
    @PostMapping("/saveStudentApply")
    @OperationLog(module = ModuleName.TRANSATION, methods = "保存学生开通应用", type = OperationLogTypeEnum.INSERT)
    public ResultBean insertApply(@ApiParam("开通应用") @RequestBody EduStudentApply eduStudentApply) {
        eduStudentApplyService.saveStudentApply(eduStudentApply);
        return ResultBean.buildSuccess();
    }


    @ApiOperation("查询学生是否拥有该应用权限")
    @GetMapping("/hasApply")
    public ResultBean<Boolean> listApplyCode(@ApiParam("学生id") Long studentId,
        @ApiParam("应用编码") String applyCode) {
        return ResultBean.buildSuccess(eduStudentApplyService.count(Wrappers.lambdaQuery(new EduStudentApply())
            .eq(EduStudentApply::getStudentId, studentId)
            .eq(EduStudentApply::getApplyCode, applyCode)
            .ge(EduStudentApply::getEndTime, LocalDateTime.now())
            .le(EduStudentApply::getStartTime, LocalDateTime.now())) > 0);
    }

    @ApiOperation("查询学生的应用状态")
    @GetMapping("/getApplyStatus")
    public ResultBean<StudentApplyStatusVO> getApplyStatus(@ApiParam("学生id") Long studentId,
        @ApiParam("应用编码") String applyCode) {
        return ResultBean.buildSuccess(eduStudentApplyService.getStudentApplyStatus(studentId, applyCode));
    }


    @ApiOperation("下载学生开通应用导入模板")
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) throws Exception {
        eduStudentApplyService.downloadTemplate(response);
    }


    @ApiOperation("导入学生开通应用信息")
    @PostMapping("/importStudentApplyInfo")
    public ResultBean<StudentApplyImportProgressVO> importStudentApplyInfo(@RequestParam(value = "file") MultipartFile file)
        throws Exception {
        String redisKey = UUID.randomUUID().toString();
        StudentApplyImportProgressVO progressVO = new StudentApplyImportProgressVO();
        progressVO.setRedisKey(redisKey);
        progressVO.setFinish(0);
        progressVO.setTotalNum(0);
        progressVO.setCurrentNum(0);
        redisService.hset(CachePrefixConstant.IMPORT_STUDENT_APPLY_PROGRESS, redisKey, progressVO);
        eduStudentApplyService.importStudentApplyInfo(file, redisKey);
        return ResultBean.buildSuccess(progressVO);
    }

    @ApiOperation("获取导入学生开通应用信息进度")
    @GetMapping("/getImportStudentApplyInfoProgress")
    public ResultBean<StudentApplyImportProgressVO> getImportStudentApplyInfoProgress(@RequestParam("redisKey") String redisKey) {
        return ResultBean.buildSuccess(eduStudentApplyService.getImportStudentApplyInfoProgress(redisKey));
    }

    @ApiOperation("删除缓存导入进度")
    @GetMapping("/deleteRedisStudentApplyImportProgress")
    public ResultBean deleteRedisStudentApplyImportProgress(@RequestParam("redisKey") String redisKey) {
        redisService.hdel(CachePrefixConstant.IMPORT_STUDENT_APPLY_PROGRESS, redisKey);
        return ResultBean.buildSuccess();
    }

}
