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

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.apply.EduProductNoticeStudent;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.dto.transaction.ListBuyStudentDTO;
import com.yida.data.school.transaction.service.EduProductNoticeService;
import com.yida.data.school.transaction.service.EduProductNoticeStudentService;
import com.yida.data.school.vo.transcation.DownStudentBuyDataVO;
import com.yida.data.user.vo.DownLoadStaffAttendanceRecordVO;
import com.yida.data.user.vo.ListParentByStudentIdsVO;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 催缴通知对应接收学生表 Controller
 *
 * @author ccl
 * @date 2021-09-03 15:33:47
 */

@Api(tags = "交易-商品催缴学生")
@Slf4j
@Validated
@RestController
@RequestMapping("/transaction/productNoticeStudent")
@RequiredArgsConstructor
public class EduProductNoticeStudentController {

    private final EduProductNoticeStudentService eduProductNoticeStudentService;
    private final EduProductNoticeService eduProductNoticeService;
    private final RedisService redisService;

    @PostMapping("/batchPutNotice")
    @ApiOperation("批量催缴")
    public ResultBean<String> batchPutNotice(@RequestBody List<EduProductNoticeStudent> eduProductNoticeStudents) {
        eduProductNoticeStudentService.batchPutNotice(eduProductNoticeStudents);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("全部催缴")
    @PostMapping("/allPutNotice")
    public ResultBean allPutNotice(@RequestBody EduProductNoticeStudent eduProductNoticeStudent) {
        eduProductNoticeStudentService.allPutNotice(eduProductNoticeStudent);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("通知记录")
    @GetMapping("/findRecordList")
    public ResultBean<IPage<EduProductNoticeStudent>> findRecordList(@RequestParam Long productId,
        @RequestParam Integer size,
        @RequestParam Integer pageNum) {
        Page page = new Page();
        page.setSize(size);
        page.setCurrent(pageNum);
        return ResultBean.buildSuccess(eduProductNoticeStudentService.findRecordList(page, productId));
    }

    @ApiOperation("查询通知记录对应家长")
    @GetMapping("/findRecordParent")
    public ResultBean<IPage<ListParentByStudentIdsVO>> findRecordParent(@RequestParam Long noticeId,
        @RequestParam Integer size,
        @RequestParam Integer pageNum) {
        Page page = new Page();
        page.setSize(size);
        page.setCurrent(pageNum);

        return ResultBean.buildSuccess(eduProductNoticeStudentService.findRecordParent(page, noticeId));
    }

    /**
     * 导出excel
     */
    @ApiOperation("导出未购买学生")
    @PostMapping("/StudentNotBuyExport")
    public void studentNotBuyExport(ListBuyStudentDTO listBuyStudentDTO, HttpServletResponse response) {

        Long deptId = FebsUtil.getCurrentUser().getDeptId();
        listBuyStudentDTO.setSchoolId(deptId);
        eduProductNoticeStudentService.studentNotBuyExport(listBuyStudentDTO, response);
    }

    /**
     * 导出excel
     */
    @ApiOperation("导出需要续费的学生")
    @ResponseBody
    @PostMapping("/studentBuyExport")
    public ResultBean<DownStudentBuyDataVO> studentBuyExport(@RequestBody ListBuyStudentDTO listBuyStudentDTO,
        HttpServletResponse response) {
        String uuid = IdUtil.simpleUUID();
        DownStudentBuyDataVO vo = DownStudentBuyDataVO.builder()
            .key(uuid)
            .finish(0)
            .totalNum(100)
            .currentNum(0).build();
        redisService.hset(CachePrefixConstant.PRODUCT_BUY_DATA_DOWNLOAD, uuid, vo);
        eduProductNoticeStudentService.studentBuyExport(listBuyStudentDTO, response, uuid);
        return ResultBean.buildSuccess(vo);
    }


    @ApiOperation("获取导出需要续费的学生进度")
    @GetMapping("/getStudentBuyExportProgress")
    public ResultBean<DownStudentBuyDataVO> getStudentBuyExportProgress(String key) {
        return ResultBean.buildSuccess(
            (DownStudentBuyDataVO) redisService.hget(CachePrefixConstant.PRODUCT_BUY_DATA_DOWNLOAD, key));
    }

}
