/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.platform.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.DvyType;
import com.yami.shop.bean.enums.OrderStatus;
import com.yami.shop.bean.model.*;
import com.yami.shop.bean.param.*;
import com.yami.shop.bean.vo.ExportTaskVo;
import com.yami.shop.bean.vo.OrderCountVo;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.R;
import com.yami.shop.security.comment.dao.AppConnectMapper;
import com.yami.shop.security.comment.model.AppConnect;
import com.yami.shop.security.platform.util.SecurityUtils;
import com.yami.shop.service.*;
import com.yami.shop.sys.service.SysUserService;
import com.yami.shop.utils.CullenUtils;
import com.yami.shop.wx.service.impl.WxProviderServiceImpl;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/**
 * 导出任务controller
 */

@RestController
@RequestMapping("/platform/exportTask")
@AllArgsConstructor
public class ExportTaskController {

    @Autowired
    private ExportTaskService exportTaskService;

    /**
     * 直接下载文件
     */
    @GetMapping("/download")
    @ApiOperation("下载文件")
    public void downloadFile(
            @RequestParam("fileId")  String fileId,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        if (StringUtils.isEmpty(fileId)){
            throw new GlobalException("文件id不能为空");
        }
        Long userId = getUserId();
        // 记录开始时间
        try {
            exportTaskService.downloadFile(request, response, fileId,userId);
        } catch (Exception e) {
            throw new GlobalException(e.getMessage());
        }
    }

    /**
     * 中断导出
     */
    @GetMapping("/cancel/{fileId}")
    public R<String> cancelExport(@PathVariable String fileId) {
        if (StringUtils.isEmpty(fileId)){
            throw new GlobalException("文件id不能为空");
        }
        Long userId = getUserId();
        return exportTaskService.cancelExport(fileId,userId);
    }

    /**
     * 查询导出任务列表
     */
    @GetMapping("/page")
    public R<IPage<ExportTaskVo>> page(PageParam<ExportTaskVo> page,@RequestParam Integer exportType) {
        if (ObjectUtil.isEmpty(exportType)){
            throw new GlobalException("导出文件类型不能为空");
        }
        Long userId = getUserId();
        IPage<ExportTaskVo> exportTaskIPage = exportTaskService.findPage(page,userId,exportType);
        return R.SUCCESS(exportTaskIPage);
    }

    /**
     * 获取当前用户信息
     * @return
     */
    private Long getUserId(){
        Long userId = null;
        try {
            userId = SecurityUtils.getSysUser().getUserId();
        } catch (Exception e) {
            throw new GlobalException("导出获取当前登录用户失败");
        }
        if (ObjectUtil.isEmpty(userId)){
            throw new GlobalException("导出获取当前登录用户信息失败,请登录");
        }
        if (userId == Constant.SUPER_ADMIN_ID){
            userId =null;
        }
        return userId;
    }



}
