package com.ningdatech.pmapi.todocenter;

import com.ningdatech.basic.model.GenericResult;
import com.ningdatech.pmapi.AppTests;
import com.ningdatech.pmapi.staging.enums.MsgTypeEnum;
import com.ningdatech.pmapi.staging.service.INdWorkNoticeStagingService;
import com.ningdatech.pmapi.todocenter.bean.entity.WorkNoticeInfo;
import com.ningdatech.pmapi.todocenter.manage.TodoCenterManage;
import com.ningdatech.pmapi.user.entity.UserInfo;
import com.ningdatech.pmapi.user.service.IUserInfoService;
import com.ningdatech.zwdd.client.ZwddClient;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.*;

import static com.ningdatech.pmapi.todocenter.constant.WorkNotice.PASS_MSG_TEMPLATE;

/**
 * 待办中心测试
 *
 * @author CMM
 * @since 2023/02/15 22:03
 */
@Slf4j
public class TodoCenterTest extends AppTests {

    //@Autowired
    //private TaskExecutor executor;

    @Autowired
    private TodoCenterManage todoCenterManage;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ZwddClient zwddClient;
    @Autowired
    private INdWorkNoticeStagingService workNoticeStagingService;
    @Test
    public void sendWorkNoticeTest() throws ExecutionException, InterruptedException {
        //String msg = String.format(PASS_MSG_TEMPLATE, "发改委", "0223-00-测试项目");
        //log.info("开始发送工作通知");
        //zwddClient.sendWorkNotice("846085", "0223-00", msg);
        //// zwddClient.sendWorkNotice("829728","0216-5",msg);
        //log.info("发送工作通知结束");

        //ApplicationContext ac = new AnnotationConfigApplicationContext(BeanConfig.class);
        //
        ////若没有指定属性名，则默认为方法名
        //TaskExecutor taskExecutor = (TaskExecutor) ac.getBean("executor");

        Long userId = 4L;
        UserInfo auditUserInfo = userInfoService.getById(userId);
        // 获取发送浙政钉工作通知必要信息
        WorkNoticeInfo workNoticeInfo = todoCenterManage.getSendWorkNoticeInfo(auditUserInfo);
        // workNoticeInfo.setBizMsgId("1");
        String msg = String.format(PASS_MSG_TEMPLATE, "发改委", "0223-02-测试项目");

        //// 先创建1个活动线程的线程池
        //ExecutorService executor = Executors.newFixedThreadPool(1);

        // 初始化线程池
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 20,
                60, TimeUnit.SECONDS, new ArrayBlockingQueue(20), new ThreadPoolExecutor.AbortPolicy());

        // 将发送工作通知交给异步任务Future
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            // 调用浙政钉的接口发送工作通知
            long startTime = System.currentTimeMillis();
            GenericResult<Void> result =
                zwddClient.sendWorkNotice(workNoticeInfo.getReceiverUserId(), workNoticeInfo.getBizMsgId(), msg);
            String resultMsg = result.getMsg();
            if (resultMsg.equals("success")) {
                log.info("异步任务执行完成, " + workNoticeInfo.getBizMsgId() + " 当前线程：" + Thread.currentThread().getName());
                long endTime = System.currentTimeMillis();
                log.info("方法执行完成返回，耗时：" + (endTime - startTime));
            }else {
                return "发送工作通知失败!";
            }
            return "发送工作通知成功!";
        }, threadPool);
        String s = future.get();
        threadPool.shutdown();
        while (threadPool.isTerminated()) {
            log.info(s);
            break;
        }
    }

    @Test
    public void sendWorkNoticeTest2(){
        UserInfo userInfo = userInfoService.getById(4L);
        // 获取发送浙政钉工作通知必要信息
        WorkNoticeInfo passWorkNoticeInfo = todoCenterManage.getSendWorkNoticeInfo(userInfo);
        String passMsg = String.format(PASS_MSG_TEMPLATE, passWorkNoticeInfo.getOrganizationName(), "测试项目0301-1");
        passWorkNoticeInfo.setMsg(passMsg);
        // 放入工作通知暂存表中，通过扫表异步发送
        workNoticeStagingService.addByWorkNotice(passWorkNoticeInfo, MsgTypeEnum.AUDIT);
    }
}
