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

import cc.mrbird.febs.common.redis.service.RedisService;
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.ResultBean;
import com.yida.data.common.core.entity.apply.EduApply;
import com.yida.data.common.core.entity.apply.EduStudentApply;
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.school.transaction.service.EduStudentApplyService;
import com.yida.data.school.vo.transcation.EduApplyVO;
import com.yida.data.school.vo.transcation.StudentApplyImportProgressVO;
import com.yida.data.school.vo.transcation.StudentApplyStatusVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import org.springframework.web.multipart.MultipartFile;

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

    private final EduStudentApplyService eduStudentApplyService;

    private final RedisService redisService;


    @ApiOperation("分页查询学生开通应用")
    @GetMapping("/listStudentApply")
    @ControllerLog(operation = "分页查询学生开通应用")
    public ResultBean<IPage<EduStudentApply>> listApply(
        @ApiParam("学生名字") @RequestParam(required = false) String studentName,
        @ApiParam("学号") @RequestParam(required = false) String studentNumber,
        @ApiParam("学校ID") @RequestParam(required = false) Long schoolId,
        @ApiParam("校区ID") @RequestParam(required = false) Long campusId,
        @ApiParam("学段ID") @RequestParam(required = false) Long sectionId,
        @ApiParam("年级ID") @RequestParam(required = false) Long gradeId,
        @ApiParam("班级ID") @RequestParam(required = false) Long classId,
        @ApiParam("当前页码") @RequestParam(defaultValue = "1") Integer pageNum,
        @ApiParam("页面大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<EduStudentApply> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        return ResultBean.buildSuccess(
            eduStudentApplyService
                .listStudentApply(studentName, studentNumber, schoolId, sectionId, campusId, gradeId, classId, page));
    }

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

    @ApiOperation("删除开通应用")
    @GetMapping("/deleteStudentApply")
    @ControllerLog(operation = "删除学生开通应用")
    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")
    @ControllerLog(operation = "保存学生开通应用")
    public ResultBean insertApply(@ApiParam("开通应用") @RequestBody EduStudentApply eduStudentApply) {
        eduStudentApplyService.save(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();
    }

}
