package com.boman.file.controller;

import com.alibaba.fastjson.JSONObject;
import com.boman.common.core.utils.SecurityUtils;
import com.boman.common.redis.RedisKey;
import com.boman.common.redis.service.RedisService;
import com.boman.domain.VaccineInfoOperation;
import com.boman.domain.dto.AjaxResult;
import com.boman.domain.dto.ExportExcelDto;
import com.boman.file.service.ISysFileService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

import static com.boman.common.core.utils.obj.ObjectUtils.isEmpty;

/**
 * @author shiqian
 * @date 2021年06月07日 16:24
 **/
@RestController
@RequestMapping("/common/file")
public class ExcelController {

    @Resource
    private ISysFileService fileService;
    @Resource
    private RedisService redisService;

    /**
     * 功能描述: 通用的导入接口
     *
     * @param multipartFile MultipartFile
     * @param tableName     tableName
     * @return void
     */
    @PostMapping("/import")
    public AjaxResult importExcelCommon(MultipartFile multipartFile, String tableName) {
        try {
            return fileService.importExcelCommon(multipartFile, tableName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return AjaxResult.error("失败");
    }


    /**
     * 功能描述: 通用的导出接口
     *
     * @param response response
     * @param dto      tableName
     *                 empty(true=>只带表头的excel，不含数据, false=>带数据的excel)
     *                 condition 查询条件
     * @return com.boman.domain.dto.AjaxResult
     */
    @PostMapping("/export")
    public AjaxResult exportExcelCommon(HttpServletResponse response, @RequestBody ExportExcelDto dto) {
        try {
            return fileService.exportExcelCommon(response, dto);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return AjaxResult.error("失败");
    }

    /**
     * 功能描述: 通用的导出接口   异步
     *
     * @param response response
     * @param dto      tableName
     *                 empty(true=>只带表头的excel，不含数据, false=>带数据的excel)
     *                 condition 查询条件
     * @return com.boman.domain.dto.AjaxResult
     */
    @PostMapping("async/export")
    public AjaxResult asyncExportExcelCommon(HttpServletRequest request, HttpServletResponse response, @RequestBody ExportExcelDto dto) {
        try {
            return fileService.asyncExportExcelCommon(request, response, dto);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return AjaxResult.error("失败");
    }

    @PostMapping("async/export/ymjz")
    public AjaxResult asyncExportYmjzExcel(HttpServletResponse response, @RequestBody VaccineInfoOperation info) {
        try {
            return fileService.asyncExportYmjzExcel(response, info);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return AjaxResult.error("失败");
    }

    /**
     * 功能描述: 是否可以下载
     *
     * @return com.boman.domain.dto.AjaxResult
     */
    @GetMapping("/before/download")
    public AjaxResult canDownloadIfOrNot() {
        String redisKey = RedisKey.ASYNC_DOWNLOAD_YMJZ + SecurityUtils.getUsername();
        JSONObject jsonObject = redisService.getCacheObject(redisKey);
        if (isEmpty(jsonObject)) {
            return AjaxResult.success(false);
        }

        String fileAbsPath = jsonObject.getString("fileAbsPath");
        String fileStaticPath = jsonObject.getString("fileStaticPath");
        if (isEmpty(fileAbsPath)) {
            return AjaxResult.success(false);
        }

        File file = new File(fileAbsPath);
        if (!file.exists()) {
            // 还没有生成文件，可能还在查询的过程中，无法下载
            return AjaxResult.success(false);
        }

        long length = file.length();
        if (length == 0) {
            // 生成文件中，内容还未写进excel，此时文件的大小为0
            return AjaxResult.success(false);
        } else {
            // todo 需要优化，删除了用户只可以下载一次
            redisService.deleteObject(redisKey);
            return AjaxResult.success(fileStaticPath, true);
        }
    }

    /**
     * 功能描述: 导出数据，只是sql不一样，其余的都是通用接口的内容
     *
     * @param response response
     * @param dto      tableName
     *                 empty(true=>只带表头的excel，不含数据, false=>带数据的excel)
     *                 condition 查询条件
     * @return com.boman.domain.dto.AjaxResult
     */
    @PostMapping("/export/statisticsByMonth")
    public AjaxResult statisticsByMonth(HttpServletResponse response, @RequestBody ExportExcelDto dto) {
        try {
            return fileService.statisticsByMonth(response, dto);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return AjaxResult.error("失败");
    }



}
