package com.jay.monitor.data.server.factory;

import com.jay.monitor.data.server.config.props.MonitorProperties;
import com.jay.monitor.data.server.factory.callback.DefaultIdChangeCallback;
import com.jay.monitor.data.server.factory.generate.DayIdGenerate;
import com.jay.monitor.data.server.store.SplitDataManager;
import com.jay.monitor.data.server.store.mysql.MysqlDDLProcess;
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.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CyclicBarrier;

/**
 * @Module 数据生成器
 * @Description 根据天生层编号
 * @Author liukaixiong
 * @Date 2020/11/6 15:54
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MonitorProperties.class, DataSourceAutoConfiguration.class, MysqlDDLProcess.class, SplitDataManager.class, DayIdGenerate.class, DataIdFactory.class, DefaultIdChangeCallback.class})
public class DayIdGenerateTest {
    SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");

    private Logger logger = LoggerFactory.getLogger(getClass());
    //    @Autowired
//    private DataIdFactory idFactory;
    @Autowired
    private DayIdGenerate dayIdGenerate;

    private CyclicBarrier barrier = new CyclicBarrier(10);

    @Test
    public void getConcurrentCurrentId() throws Exception {
        //
        dayIdGenerate.setM_timestamp(0L);

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    logger.info("开始等待");
                    barrier.await();
                    logger.info("抢占开始");
//                    String currentId = idFactory.getCurrentId(DataSplitType.DAY);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
        System.in.read();
    }


    @Test
    public void testDayGenerate() throws Exception {

        logger.info("程序初始化的数据:" + dayIdGenerate.getM_timestamp() + "\t" + dayIdGenerate.getDefaultId());

        dayIdGenerate.setM_timestamp(0L);
        dayIdGenerate.setDefaultId("201106");

        logger.info("修改后的数据:" + dayIdGenerate.getM_timestamp() + "\t" + dayIdGenerate.getDefaultId());

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    logger.info("开始等待");
                    barrier.await();
                    logger.info("抢占开始");
                    String currentId = dayIdGenerate.getCurrentId((id) -> {
                        logger.info("------------------------->发生改变的编号:" + id);
                    });
                    logger.info("正常获取的编号:" + currentId);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }

        Thread.sleep(1000);
        logger.info("最终的数据结果:" + dayIdGenerate.getM_timestamp() + "\t" + dayIdGenerate.getDefaultId());
    }

    @Test
    public void testBatchDayGenerate() throws Exception {
        testDayGenerate();
        testDayGenerate();
        testDayGenerate();
        testDayGenerate();
    }

    @Test
    public void testTimeStrap() {


        // 1109
//        Long startTime = 1604880000000L;
        Long startTime = 1604851200000L;
        long timestrap = getTimestrap(startTime);

        // 1110
        startTime = 1604937602000L;
        timestrap = getTimestrap(startTime);

        startTime = System.currentTimeMillis();
        timestrap = getTimestrap(startTime);

    }

    private long getTimestrap(Long startTime) {
        Long add = 8 * 3600 * 1000l;
        startTime = startTime + add;
        long DAY = 24 * 60 * 60 * 1000L;
        long value = startTime / DAY;
        Date date = new Date(startTime);
        System.out.println(sfd.format(date) + "\t" + value);
        return value;
    }

}