package com.ywt.mg.web.controllers;

import com.google.protobuf.ByteString;
import com.ywt.gapi.base.file.FileServiceGrpc;
import com.ywt.gapi.base.file.UploadFileRequest;
import com.ywt.gapi.base.file.UploadFileResponse;
import com.ywt.mg.core.MGRight;
import com.ywt.mg.core.utils.Checker;
import com.ywt.mg.core.utils.FormatUtil;
import com.ywt.mg.domain.models.ConstantDef;
import com.ywt.mg.domain.models.OssSettring;
import com.ywt.mg.domain.models.enums.DownloadRecordStatusEnum;
import com.ywt.mg.params.registeredOrder.RegisteredOrderListRequest;
import com.ywt.mg.web.BaseResponse;
import com.ywt.mg.web.DataResponse;
import com.ywt.mg.web.common.FileSrv;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.Date;

/**
 * @author daiyihua
 * @create 2022-11-21 14:37
 * @program download_service
 * @description test
 **/
@MGRight(need = false)
@RestController("/test")
@RequestMapping({"/test"})
public class TestController {

    @Autowired
    private FileServiceGrpc.FileServiceBlockingStub fileServiceBlockingStub;

    @Autowired
    private OssSettring ossSettring;

    @RequestMapping({"/test"})
    public String test() {
        // 本地文件上传到oss，文件名带后缀
        String fileName = "订餐订单列表";
        // 前缀（路径）
        String preLocalPath = ConstantDef.LOCAL_DOWNLOAD_FILE_PATH;
        // 后缀（下载时间和格式）
        String suffixFormat = FormatUtil.formatDate(new Date(), FormatUtil.FORMAT_DATE_SECOND_NUMBER) + ConstantDef.EXCEL_SUFFIX_FORMAT;
        // 写到本地，拼凑本地路径
        String localPath = preLocalPath + fileName + ".xls";
        String ossPath = String.format(ConstantDef.BIG_DATA_EXCEL_FILE_PATH, FormatUtil.formatDate(new Date(), "yyyy-MM"));
        String ossFileName = fileName + suffixFormat + ".xls";
        try {
            byte[] bytes = FileToByte(localPath);
            if (Checker.isNone(bytes)) {
                return "";
            }
            UploadFileRequest rest = UploadFileRequest.newBuilder()
                    .setFilename(ossFileName)
                    .setVpath(ossPath)
                    .setDatas(ByteString.copyFrom(bytes))
                    .build();
            UploadFileResponse resp = fileServiceBlockingStub.uploadFile(rest);
            return resp.getFileUrl();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static byte[] FileToByte(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

    @RequestMapping({"/test1"})
    public String test1() {
        // 本地文件上传到oss，文件名带后缀
        String fileName = "订餐订单列表";
        // 前缀（路径）
        String preLocalPath = ConstantDef.LOCAL_DOWNLOAD_FILE_PATH;
        // 后缀（下载时间和格式）
        String suffixFormat = FormatUtil.formatDate(new Date(), FormatUtil.FORMAT_DATE_SECOND_NUMBER) + ConstantDef.EXCEL_SUFFIX_FORMAT;
        // 写到本地，拼凑本地路径
        String localPath = preLocalPath + fileName + ".xls";
        String ossPath = String.format(ConstantDef.BIG_DATA_EXCEL_FILE_PATH, FormatUtil.formatDate(new Date(), "yyyy-MM"));
        String ossFileName = ossPath+"/"+ fileName + suffixFormat + ".xls";
        String url = ossSettring.uploadFile(localPath, ossFileName);
        System.out.println("------------");
        System.out.println(url);
        return url;
    }

}
