package com.youchain;

import com.youchain.basicdata.domain.BigItem;
import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.vo.BarCodeVo;
import com.youchain.businessdata.domain.PickDetail;
import com.youchain.exception.BadRequestException;
import com.youchain.utils.BaseStatus;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EladminSystemApplicationTests {
    @Autowired
    private static TestService testService;

    @Test
    public void contextLoads() {
    }

    public static void main(String[] args) {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        // 设置服务接口
        factory.setServiceClass(TestService.class);
        // 设置服务地址
        factory.setAddress("http://example.com/yourService");

        String result = testService.pickAndPost("123");


    }

    public void  cc(){
        // 创建一个 HashSet 实例
        Set<String> hashSet = new HashSet<>();
        // 向集合中添加元素
        hashSet.add("QXJL002");
        hashSet.add("QXJL002");

        // 打印原始集合
        System.out.println("原始集合: " + hashSet);

        // 尝试移除 "apple" 元素
        boolean isRemoved = hashSet.remove("QXJL001");

        if (isRemoved) {
            System.out.println("集合中包含元素 apple，已成功移除。");
        } else {
            System.out.println("集合中不包含元素 apple。");
        }

        // 打印移除元素后的集合
        String result = hashSet.stream()
                .collect(Collectors.joining(", "));
        if(result.isEmpty()){
            System.out.println("kong");
        }else{
            System.out.println(result);
        }
    }

    public static List<String> getValuesFromMap(List<String> list, Map<String, String> map) {
        List<String> values = new ArrayList<>();
        int count = list.size();
        int index = 0;

        // 遍历 map 的键值对
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (index >= count) {
                break;
            }
            values.add(entry.getValue());
            index++;
        }
        return values;
    }

    static Set bb(Set set1, Set set2) {
        Set<String> difference = new HashSet<>(set1);
        difference.addAll(set2);
        Set<String> tmp = new HashSet<>(set1);
        tmp.retainAll(set2);
        difference.removeAll(tmp);
        return difference;
    }

    private static final int MAX_TASK_COUNT = 4;
    private static final long TIMEOUT_MS = 600000; // 10分钟，以毫秒为单位
    private long lastTaskTime = System.currentTimeMillis();
    private List<Integer> taskQueue = new ArrayList<>(MAX_TASK_COUNT);

    void aa() {

        Set<String> itemCodes = new HashSet<>();
        itemCodes.add("A");
        itemCodes.add("B");
        itemCodes.add("C");
        itemCodes.add("D");
        Set<String> newItemCodes = new HashSet<>();
        newItemCodes.add("A");
        newItemCodes.add("B");

        Set<String> difference = bb(itemCodes, newItemCodes);
        StringBuffer sbf = new StringBuffer();
        sbf.append("WMS不存在的物料集合,请维护：").append(difference);
        System.out.println(sbf.toString());

        List<String> list = new ArrayList<>();
        list.add("物料库存不足，请先补充库存！");
        list.add("数量已分配！");
        list.add("托盘有任务，请先完成任务！");
        boolean containsSuccess = list.stream().anyMatch(s -> s.contains("分配成功"));

        if (containsSuccess) {
            System.out.println("集合中存在分配成功的信息！");
        } else {
            System.out.println("集合中不存在分配成功的信息！");
        }


        Set<BarCodeVo> barCodeVos = new HashSet<>();

        BarCodeVo barCodeVo = new BarCodeVo();
        barCodeVo.setItemCode("123");
        barCodeVo.setBoxNumber("1");
        barCodeVos.add(barCodeVo);

        BarCodeVo barCodeVo2 = new BarCodeVo();
        barCodeVo2.setItemCode("123");
        barCodeVo2.setBoxNumber("2");
        barCodeVos.add(barCodeVo2);

        BarCodeVo barCodeVo3 = new BarCodeVo();
        barCodeVo3.setItemCode("456");
        barCodeVo3.setBoxNumber("3");
        barCodeVos.add(barCodeVo3);

        for (BarCodeVo vo : barCodeVos) {
            System.out.println(vo.getItemCode());
        }

        String firstItemCode = barCodeVos.iterator().next().getItemCode();
        boolean allSame = barCodeVos.stream()
                .allMatch(vo -> vo.getItemCode().equals(firstItemCode));
        if (allSame) {
            System.out.println("全部匹配");
        } else {
            System.out.println("不匹配");
        }

        PickDetail pickDetail1 = new PickDetail();
        pickDetail1.setId(1L);
        pickDetail1.setOrderQty(2d);
        pickDetail1.setAllocatedQty(2d);

        PickDetail pickDetail2 = new PickDetail();
        pickDetail2.setId(2L);
        pickDetail2.setOrderQty(2d);
        pickDetail2.setAllocatedQty(2d);

        PickDetail pickDetail3 = new PickDetail();
        pickDetail3.setId(2L);
        pickDetail3.setOrderQty(2d);
        pickDetail3.setAllocatedQty(0d);

        List<PickDetail> pickDetails = new ArrayList<>();
        pickDetails.add(pickDetail1);
        pickDetails.add(pickDetail2);
        pickDetails.add(pickDetail3);

        boolean allAllocated = pickDetails.stream()
                .allMatch(detail -> detail.getOrderQty().equals(detail.getAllocatedQty()));
        boolean anyAllocated = pickDetails.stream()
                .anyMatch(detail -> detail.getOrderQty().equals(detail.getAllocatedQty()));
        if (allAllocated) {
            System.out.println("全部已分配");
        } else if (anyAllocated) {
            System.out.println("部分已分配");
        }

        List<Integer> list1 = Arrays.asList();
        List<Integer> list2 = Arrays.asList();

        List<Integer> list3 = new ArrayList<>(list1);
        list3.addAll(list2);

        System.out.println(list3);  // 输出: [1, 2, 3, 4, 5]

            /*double taoQty=2;//基础套数
        double xTaoQty=3;//需求套数
        double ckdQty=Math.ceil(xTaoQty/taoQty);//生成出库单数量
        System.out.println(ckdQty);*/


    }

    @Test
    public void test() {
        List<Integer> taskLists = new ArrayList<>();
        taskLists.add(1);
        taskLists.add(2);
        taskLists.add(3);
        taskLists.add(4);
        taskLists.add(5);
        taskLists.add(6);

        for (Integer id : taskLists) {
            try {
                taskQueue.add(id);
                if (taskQueue.size() >= MAX_TASK_COUNT) {
                    System.out.println("任务已满足，当前任务数：" + taskQueue.size());
                    taskQueue.clear();
                    lastTaskTime = System.currentTimeMillis();
                } else if (System.currentTimeMillis() - lastTaskTime >= TIMEOUT_MS) {
                    System.out.println("超过10分钟未满足4个任务，当前任务数：" + taskQueue.size());
                    taskQueue.clear();
                    lastTaskTime = System.currentTimeMillis();
                }
            } catch (Exception e) {
                throw new BadRequestException(e.getMessage());
            }
            System.out.println(id);
        }
    }
}

