package com.aps.demo;

import com.aps.ApsApplication;
import com.aps.common.util.FileHelper;
import com.aps.common.util.RangeSubtractUtil;
import com.aps.entity.Algorithm.Chromosome;
import com.aps.entity.Algorithm.ObjectiveWeights;
import com.aps.entity.Gantt.ProductGanttVO;
import com.aps.entity.basic.Machine;
import com.aps.entity.basic.MaintenanceWindow;
import com.aps.service.Algorithm.NSGAIIUtils;
import com.aps.service.Algorithm.TestSortService;
import com.aps.service.plan.PlanResultService;
import com.aps.service.plan.SceneService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@SpringBootTest(classes = ApsApplication.class)
public class PlanResultServiceTest {

    @Autowired
    private PlanResultService planResultService;

    @Autowired
    private SceneService sceneService;

    @Test
    public void testExecute() {
      //  planResultService.getMaterials();
//        RangeSubtractUtil.test();
  //      planResultService.testSceneChromsome("qwerty");
//        Chromosome chromosome=  planResultService.moveChromosome("qwerty",3);
       // planResultService.execute2("B7881EF032044B9BA4F6007875510B70");
       // NSGAIIUtils nsgaiiUtils=new NSGAIIUtils();
      //  TestSortService  sortService=new TestSortService();
     //  sortService.test1();
       // nsgaiiUtils.Test();

     //     planResultService.execute2("64E64F6B68094AF38CEDC418630C3CC2");//2000
           //  planResultService.execute2("E1448B3C9C8743DEAB39708F2CFE348A");//倒排bomces
    //  planResultService.execute2("85DA28EC5F4643449E65A51253D5F127"); //bom 多半成品
        planResultService.execute2("B288477F1A594DB584C87EEA77880AA3");

      //  planResultService.execute2("FB14AF1623CB461D9BEDAFC35DBF0AFC"); //重叠

        //  planResultService.execute2("F8F147BD627C47B1A190399DD7A697F6");

      //  planResultService.execute2("9FEDFD92BB6A4675BF9B1CC64505D1AB");

        //  planResultService.execute2("15210B13B88A453F8B84AAC7F16C7541");//2000

        //  planResultService.execute2("E29F2B3ADA8149F6B916B5119296A92B");//2000
      // planResultService.execute2("E2CD1FC6FF9B4B19A59FEC7F846D4952");//600
      //  planResultService.execute2("EAF3C94B8F3345278F226C94FB0FED86");//bom
     //   planResultService.execute2("6D63146BE5C84A78B5AB044327BA55BD");//2000
    //  planResultService.execute2("6D63146BE5C84A78B5AB044327BA55BD");//5000
       // planResultService.execute2("C8B533BD8944405B9A2F8823C575C204");//500
     //   planResultService.execute2("EFDD34E4B5BC434BAEAE6A84DFCD4E7B");//20
      //  planResultService.execute2("00E0C5D3E4AD4F36B56C39395906618D");

      //   planResultService.execute2("92BB773E1E2447C99D8176C991D5C9D2");
    //   planResultService.execute2("265F24B6DF3C40E4B17D193B0CC8AAF2");
//        LocalDateTime t=   LocalDateTime.of(2026, 02, 14, 1, 25, 52);
 //      List<Integer> opids=new ArrayList<>();//BCA6FA43FFA444D3952CF8F6E1EA291B
//       opids.add(7);
     //   planResultService.Move("27065EA0ECD14A81B7FAAFEF52273F93",opids,t,1265l,0);
// planResultService.Redecode("27065EA0ECD14A81B7FAAFEF52273F93");
     //  planResultService.Redecode("921BC266191E4812A105E406BF91E812");
 //       planResultService.Drag("27065EA0ECD14A81B7FAAFEF52273F93",opids,6,false,0l);
  //      MaintenanceWindow maintenanceWindow=new MaintenanceWindow();
 //       maintenanceWindow.setStartTime(LocalDateTime.of(2026, 9, 13, 0, 0, 0));
  //      maintenanceWindow.setEndTime(LocalDateTime.of(2025, 9, 19, 0, 0, 0));
  //        planResultService.AddMaintenanceWindow("5475E00B844847ACB6DC20227967BA2F",2488l,maintenanceWindow);
//      //   planResultService.DelOperation("B6AE363FF5044DDF8DECE32D5FE0F7EA",7);
       // planResultService.SpiltOperation("92BB773E1E2447C99D8176C991D5C9D2",1,new Double[]{50d, 50d});
       // planResultService.redecode("0785676B4E5E40CDAA62AA320CFC958C");
    }

    @Test
    public void testExecute2() {
        // 这里需要一个有效的SceneId来测试
        // 在实际测试中，您需要提供一个数据库中存在的SceneId
        String sceneId = "6D63146BE5C84A78B5AB044327BA55BD";
        
        try {
//            Chromosome result = planResultService.execute2(sceneId);
//            System.out.println("执行成功，结果：" + (result != null ? "获得染色体对象" : "空结果"));
//
//            if (result != null) {
//                System.out.println("适应度: " + result.getFitness());
//                System.out.println("工序字符串: " + result.getOperationStr());
//            }
//            List<Double> splitCounts = new ArrayList<>();
//            splitCounts.add(10000.0);
//            splitCounts.add(10000.0);
//            splitCounts.add(10000.0);
//            splitCounts.add(10000.0);
//            splitCounts.add(10000.0);
////
////            planResultService.SpiltOperation(sceneId,1,splitCounts.toArray(new Double[0]));
//
//planResultService.SpiltOrder(sceneId,"2f24e563-1337-422b-a0ba-92da7e8c6584",splitCounts.toArray(new Double[0]));


//            Double[] splitCountsArray = new Double[2]; // 创建一个空的Double数组，可以根据实际需求调整大小或初始化值
//            splitCountsArray[0] = 0.0;
//            splitCountsArray[1] = 5000.0;
//
//
//                planResultService.SpiltOrder(sceneId,"2f24e563-1337-422b-a0ba-92da7e8c6584",splitCountsArray);

           // planResultService.schedule(sceneId);
            long Start = System.nanoTime();

            Chromosome schedule = sceneService.loadChromosomeFromFile(sceneId);

            FileHelper.writeLogFile("[PERF] loadChromosomeFromFile 总耗时=" + fmtMs(System.nanoTime() - Start) );

//            // 转换为 ProductGanttVO 格式
//            List<ProductGanttVO> productGanttVOList= new ArrayList<>();
//            List<ProductGanttVO> resourceGanttVOs = planResultService.convertToProductGanttVO1(schedule);
//            productGanttVOList.addAll(resourceGanttVOs);
//
//            System.out.println("执行成功，结果：" + (productGanttVOList != null ? "获得染色体对象" : "空结果"));



        } catch (Exception e) {
            System.err.println("执行过程中发生异常: " + e.getMessage());
            e.printStackTrace();
        }
    }
    private static String fmtMs(long nano) {
        if (nano < 1000000L) return (nano / 1000L) + "μs";
        if (nano < 1000000000L) return (nano / 1000000L) + "ms";
        return String.format("%.2fs", nano / 1_000_000_000.0);
    }
}