package com.hr.repository.service;

import com.hr.fegin.demo2.Demo2FeginServer;
import com.hr.repository.domain.TestEntity;
import com.hr.repository.jpa.Demo1Jpa;
import com.hr.repository.jpa.TestJpa;
import com.yy.basedevelop.common.data.BasePageResult;
import com.yy.basedevelop.common.data.BaseReturnDto;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author AAA
 * @createTime 2025/9/25 08:55
 **/
@Component
@Slf4j
public class TransServer {
    @Autowired
    private TestJpa testJpa;
    @Autowired
    private Demo1Jpa demo1Jpa;
    @Autowired
    private Demo2FeginServer demo2FeginServer;
    @Autowired
    private DemoAsyncServer demoAsyncServer;


    public void testsubThread(){
        log.info("==processDataAsync---开始异步处理虚拟线程,name:{}");


        try {

//            Thread.sleep(300);
            List<CompletableFuture<String>> futures = new ArrayList<>();

            for (int i = 1; i <= 10; i++) {
                CompletableFuture<String> future = demoAsyncServer.testAsyncThread( i);
                futures.add(future);
            }
            for(CompletableFuture<String> s1 :futures){
                String s = s1.get();
                log.info("====虚拟线程执行完成后返回值:{}", s);
            }


        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
    @GlobalTransactional
    public void method1(String name){


        List<CompletableFuture<String>> futures = new ArrayList<>();
        try {
            for (int i = 1; i <= 2; i++) {
                CompletableFuture<String> future = demoAsyncServer.inserttest1(name, i);
                futures.add(future);
            }
            for (CompletableFuture<String> s1 : futures) {
                String s = s1.get();
                log.info("====虚拟线程执行完成后返回值:{}", s);
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }




    @GlobalTransactional
    public void insertLog(){


        TestEntity tt1=new TestEntity();
        tt1.setName("demo1测试");
        testJpa.save(tt1);
        log.info("===完成了一个日志的插入===={}",tt1);

        if(1==1){
            throw new RuntimeException("测试事务");
        }
    }
}
