package com.miaxis.system.controller.test;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.miaxis.common.constant.Constants;
import com.miaxis.common.core.domain.Response;
import com.miaxis.feign.service.IWxMpService;
import com.miaxis.feign.service.IWxSendService;
import com.miaxis.wx.service.IWxGzhService;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Calendar;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/10/21 14:02
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(Constants.OPEN_PREFIX + "/wxTest")
@Api(tags={"wx测试"})
public class TestController {

    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    @Autowired
    IWxSendService wxSendService;

    @Autowired
    IWxGzhService wxGzhService;

    @Autowired
    IWxMpService wxMpService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    private COSClient cosClient;

    @Value("${cos.bucketName}")
    private String bucketName;
    @Value("${cos.path}")
    private String path;
    @Value("${cos.preffix}")
    private String preffix;

    @Value("${file.ticketPath}")
    private String ticketPath;

    @Value("${wxgzh.mediaUpload}")
    private String materialUrl;



    /**
     * cos上传二维码
     */
    @GetMapping(value = "/test01")
    @ApiOperation("cos上传二维码")
    public Response test01() throws Exception{

        ByteArrayOutputStream os = new ByteArrayOutputStream();

        ImageIO.write(QrCodeUtil.generate(
                "二维码内容", //二维码内容
                300, 300
        ), "JPG", os);
        InputStream inputStream = new ByteArrayInputStream(os.toByteArray());

        ObjectMetadata objectMetadata = new ObjectMetadata();
        // 从输入流上传必须制定content length, 否则http客户端可能会缓存所有数据，存在内存OOM的情况
        objectMetadata.setContentLength(os.size());

        // bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
        String bucketName = this.bucketName;

        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month=cal.get(Calendar.MONTH)+ 1;
        int day=cal.get(Calendar.DATE);

        //存储的文件名
        String storagefileName = System.currentTimeMillis() + ".png";
        // 指定要上传到 COS 上的路径
        String key = "/"+this.preffix+"/"+ "testTicket"+"/"+year+"/"+month+"/"+day+"/"+storagefileName;
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream,objectMetadata);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        String fileUrl=this.path + putObjectRequest.getKey();

        return Response.success(fileUrl);
    }





}
