import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.JSONResponse;
import com.gic.haoban.manage.api.dto.WxEnterpriseQwDTO;
import com.gic.haoban.manage.api.qdto.combined.CombinedQDTO;
import com.gic.haoban.manage.service.context.combined.ImageCombined;
import com.gic.haoban.manage.service.service.MaterialService;
import com.gic.haoban.manage.service.service.WxEnterpriseService;
import com.gic.wechat.api.enums.QywxMediaTypeEnum;
import com.gic.wechat.api.service.qywx.QywxSuiteApiService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-conf.xml"})
public class QWmediaTest {

    private static Logger logger = LoggerFactory.getLogger(QWmediaTest.class);
    @Autowired
    private QywxSuiteApiService qywxSuiteApiService ;
    @Autowired
    private WxEnterpriseService wxEnterpriseService;
    @Autowired
    private MaterialService materialService;

    private static final String LARGE_IMAGE_URL = "https://jhdm-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-4582434f589b494aa4f27df6e3aa3770.png?imageView2/2/w/1080/h/10800/format/jpg";
    private static final String QR_CODE_URL = "https://gicinner-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-4ffc77073ca1476fb264bf1be9f11383.png";
    private static final String STORE_NAME = "门店CCA";
    private static final String GUIDE_NAME = "宇智222";

    @Test
    public void test() throws Exception{
        CombinedQDTO combinedQDTO = new CombinedQDTO();
        combinedQDTO.setImageUrl(LARGE_IMAGE_URL);
        combinedQDTO.setQrCodeUrl(QR_CODE_URL);
        combinedQDTO.setShowStyle(1);
        combinedQDTO.setLineOne(STORE_NAME);
        combinedQDTO.setLineTwo(GUIDE_NAME);
        JSONResponse json = materialService.getMaterialIdByCombined("b18ffdc9d0644912865a248859914d80", combinedQDTO);
        System.out.println(JSON.toJSONString(json));

    }

    public static void main(String[] args) {
        int TOTAL_REQUESTS = 100; // 总请求数
        List<String> list = FileUtil.readLines("/Users/wang/Downloads/image.txt", "UTF-8");
        JSONObject jo = new JSONObject();
        jo.put("wxEnterpriseId", "b18ffdc9d0644912865a248859914d80");
        jo.put("qrCodeUrl", "https://gicinner-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-4ffc77073ca1476fb264bf1be9f11383.png");
        jo.put("showStyle", "0");
        jo.put("lineOne", "门店CCA");
        jo.put("lineTwo", "宇智222为您推荐");

        ExecutorService executorService = Executors.newFixedThreadPool(50); // 创建线程池
        CountDownLatch latch = new CountDownLatch(TOTAL_REQUESTS); // 创建CountDownLatch，初始值为请求总数

        // 用于统计请求耗时
        AtomicLong totalTime = new AtomicLong(0);
        AtomicLong maxTime = new AtomicLong(0);
        AtomicLong minTime = new AtomicLong(Long.MAX_VALUE);

        for (int i = 0; i < list.size(); i++) {
            final int requestId = i;
            executorService.submit(() -> {
                long startTime = System.currentTimeMillis();
                try {
                    jo.put("imageUrl", list.get(requestId));
                    String post = HttpUtil.post("https://www.gicdev.com/haoban-manage3-wx/combined-qw-materialid.json", jo.toJSONString());
                    System.out.println(post);
                } catch (Exception e) {
                    System.out.println("异常");
                } finally {
                    long duration = System.currentTimeMillis() - startTime;
                    totalTime.addAndGet(duration);
                    maxTime.updateAndGet(x -> Math.max(x, duration));
                    minTime.updateAndGet(x -> Math.min(x, duration));
                    latch.countDown();
                }
            });
        }

        try {
            latch.await();
            System.out.println("完成所有请求");
            long averageTime = totalTime.get() / TOTAL_REQUESTS;
            System.out.println("平均耗时: " + averageTime + " ms");
            System.out.println("最大耗时: " + maxTime.get() + " ms");
            System.out.println("最小耗时: " + (minTime.get() == Long.MAX_VALUE ? 0 : minTime.get()) + " ms");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            executorService.shutdown();
        }
    }

}
