package com.aps.demo;

import com.aps.entity.Algorithm.OrderSortRule;
import com.aps.entity.basic.Order;
import com.aps.service.Algorithm.OrderSortService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 订单排序服务测试类
 */
@ExtendWith(MockitoExtension.class)
class OrderSortServiceTest {

    @InjectMocks
    private OrderSortService orderSortService;

    private List<Order> testOrders;

    @BeforeEach
    void setUp() {
        // 初始化服务（触发@PostConstruct）
        orderSortService.initializeFieldExtractors();
        
        // 创建测试数据
        testOrders = createTestOrders();
    }


    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd");
    @Test
    void testAssignPriority_WithMultipleConditions() {
        // 创建多条件排序规则：先按dueDate，再按priority
        OrderSortRule rule = createMultiConditionRule();

        List<Order> orders=new ArrayList<>();
        Order order=new Order();
        order.setId(1);
        order.setOrderId("001");
        order.setRoutingId(1);
        order.setMaterialCode("M001");
        order.setSerie("S001");
        orders.add(order);


        Order order2=new Order();
        order2.setId(2);
        order2.setOrderId("002");
        order2.setRoutingId(1);
        order2.setMaterialCode("M002");
        order2.setSerie("S001");
        orders.add(order2);

        Order order3=new Order();
        order3.setId(3);
        order3.setOrderId("003");
        order3.setRoutingId(1);
        order3.setMaterialCode("M002");
        order3.setSerie("S001");
        orders.add(order3);

        Order order4=new Order();
        order4.setId(4);
        order4.setOrderId("004");
        order4.setRoutingId(2);
        order4.setMaterialCode("M001");
        order4.setSerie("S002");
        orders.add(order4);

        Order order5=new Order();
        order5.setId(5);
        order5.setOrderId("005");
        order5.setRoutingId(2);
        order5.setMaterialCode("M002");
        order5.setSerie("S003");
        orders.add(order5);

        Order order6=new Order();
        order6.setId(6);
        order6.setOrderId("006");
        order6.setRoutingId(2);
        order6.setMaterialCode("M002");
        order6.setSerie("S003");
        orders.add(order6);
        // 执行测试



        orderSortService.assignPriorityValues(orders, rule);

        // 按照优先级排序
        orders.sort(
                Comparator.comparing(Order::getActualPriority).reversed()
        );
        
        // 验证多级优先级分配
        printOrderPriorities(orders); // 调试输出

    }



    /**
     * 创建测试订单数据
     */
    private List<Order> createTestOrders() {
        List<Order> orders = new ArrayList<>();
        
        // 创建5个测试订单，具有不同的属性
        for (int i = 1; i <=15; i++) {
            Order order = new Order();
            order.setId(i);
            order.setProductId(100 + i);
            order.setQuantity(50.0 * i);
            order.setPriority(15 - i); // 优先级：5,4,3,2,1（倒序）
            order.setDueDate(LocalDateTime.now().plusDays(i)); // 到期日递增
            order.setTardiness(i * 0.5);
            order.setCanSplit(i % 2 == 0);
            order.setCanInterrupt(i % 3 == 0);
            
            orders.add(order);
        }
        
        return orders;
    }



    /**
     * 创建多条件排序规则
     */
    private OrderSortRule createMultiConditionRule() {
        OrderSortRule rule = new OrderSortRule();
        rule.setEnabled(true);
        
        List<OrderSortRule.SortCondition> conditions = new ArrayList<>();
        

        
        // 条件2：按到期日
        OrderSortRule.SortCondition condition2 = new OrderSortRule.SortCondition();
        condition2.setSequence(2);
        condition2.setFieldName("minimize_changeover");
        condition2.setReverse(false); // 早到期在前
        conditions.add(condition2);
        
        // 条件3：按优先级
        OrderSortRule.SortCondition condition3 = new OrderSortRule.SortCondition();
        condition3.setSequence(3);
        condition3.setFieldName("materialCode");
        condition3.setReverse(true); // 高优先级在前
      //  conditions.add(condition3);


        rule.setConditions(conditions);
        return rule;
    }

    /**
     * 打印订单优先级（用于调试）
     */
    private void printOrderPriorities(List<Order> orders) {
        System.out.println("=== 订单优先级分配结果 ===");
        for (Order order : orders) {
            System.out.printf("订单ID: %d, 优先级: %.4f, 到期日: %s, 原优先级: %d, 可拆分: %s%n",
                    order.getId(), order.getActualPriority(), 
                    order.getDueDate(), order.getPriority(), order.isCanSplit());
        }
        System.out.println("========================");
    }
}