package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.test;

import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker;
import cn.estsh.i3plus.pojo.mes.bean.MesEquipmentLogDetail;
import cn.estsh.i3plus.pojo.mes.repository.MesEquipmentLogDetailRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Service
public class TestServiceImpl implements TestService{

    @Autowired
   private MesEquipmentLogDetailRepository equipmentLogDetailRepository;
    @Autowired
    private SnowflakeIdMaker snowflakeIdMaker;

    private static ExecutorService executorService = new ThreadPoolExecutor(1, 10,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(200), r -> {
        Thread thread = new Thread(r);
        thread.setName("executorService--"+r.hashCode());
        return thread;
    },new ThreadPoolExecutor.DiscardPolicy());

    private static ExecutorService executorService2 = new ThreadPoolExecutor(1, 10,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(200), r -> {
        Thread thread = new Thread(r);
        thread.setName("executorService--"+r.hashCode());
        return thread;
    },new ThreadPoolExecutor.DiscardPolicy());
   public void insertList() {
       executorService = new ThreadPoolExecutor(1, 10,
               0L, TimeUnit.MILLISECONDS,
               new LinkedBlockingQueue<>(200), r -> {
           Thread thread = new Thread(r);
           thread.setName("executorService--"+r.hashCode());
           return thread;
       },new ThreadPoolExecutor.DiscardPolicy());

       executorService2 = new ThreadPoolExecutor(1, 10,
               0L, TimeUnit.MILLISECONDS,
               new LinkedBlockingQueue<>(200), r -> {
           Thread thread = new Thread(r);
           thread.setName("executorService--"+r.hashCode());
           return thread;
       },new ThreadPoolExecutor.DiscardPolicy());
       List<MesEquipmentLogDetail> list = new ArrayList<>();

       for (int i= 1; i< 10; i ++) {
           executorService.execute(new Runnable() {
               @Override
               public void run() {
                   for (int j = 1; j < 500; j++) {

                       executorService2.execute(new Runnable() {
                           @Override
                           public void run() {
                               MesEquipmentLogDetail equipmentLogDetail = new MesEquipmentLogDetail();
                               ConvertBean.serviceModelInitialize(equipmentLogDetail, "test");
                               equipmentLogDetail.setId(snowflakeIdMaker.nextId());
                               equipmentLogDetail.setEquipVariableId(123123l);
                               equipmentLogDetail.setEquipVariableName("aaa");
                               equipmentLogDetail.setReadWriteFlag(1);
                               //equipmentLogDetail.setDataType(40);
                               equipmentLogDetail.setEquipId(7);
                               equipmentLogDetail.setEquipmentCode("Foaming5#");
                               list.add(equipmentLogDetail);
                              /* try {
                                   Thread.sleep(50);
                               } catch (InterruptedException e) {
                                   e.printStackTrace();
                               }*/
                           }
                       });
                   }
               }
           });
           try {
               Thread.sleep(10000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           equipmentLogDetailRepository.saveAll(list);

       }
   }
}
