package com.jjt.order;

import com.jjt.JjtApplication;
import com.jjt.order.domain.VmsStock;
import com.jjt.order.service.ITwinOrderService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * DataProcess$
 *
 * @author wukai
 * @date 2024/5/7 11:49
 */
@SpringBootTest(classes = JjtApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class OrderTest {
    @Resource
    private ITwinOrderService orderService;
//    @Resource
//    private ITwinFormulaInfoService formulaInfoService;

    @Test
    void test() {
        String d = "2025-02-18";
        LocalDate start = LocalDate.parse(d);
        LocalDate end = LocalDate.now().minusDays(1);
        do {
            orderService.getOrder(start);
            start = start.plusDays(1);
        } while (start.isBefore(end));
    }

//    @Test
//    void match() {
//        String d = "2024-12-02";
//        TwinOrder search = new TwinOrder();
//        search.setOrderDate(DateUtils.parseDate(d));
//        List<TwinOrder> list = orderService.selectTwinOrderList(search);
//        List<TwinFormulaInfo> infos = formulaInfoService.selectTwinFormulaInfoList(new TwinFormulaInfo());
//        List<VmsStock> stocks = MssqlUtil.vmsStock();
//        for (TwinOrder twinOrder : list) {
//
//            List<TwinOrderDetail> details = twinOrder.getTwinOrderDetailList();
//            for (TwinOrderDetail detail : details) {
//                //匹配白胚布库存
//                List<VmsStock> matchStock = matchStock(detail.getBomSpec(), stocks);
//                System.err.println();
//                for (VmsStock match : matchStock) {
//                    System.err.println(detail.getBomSpec() + "\t" + detail.getBomName() + "\t" + match.getDesc());
//                }
//                //匹配生产设备
////                List<TwinFormulaInfo> matchFormula = matchFormula(detail.getBomSpec(), infos);
////                for (TwinFormulaInfo match : matchFormula) {
////                    System.err.println(detail.getBomSpec() + "\t" + detail.getBomName() + "\t" + match.getDeviceId());
////                }
//            }
//
//        }
//
//        System.err.println(list.size());
//    }

    List<VmsStock> matchStock(String spec, List<VmsStock> stocks) {
        List<VmsStock> result = new ArrayList<>();
        //按重量分组
        Map<Integer, List<VmsStock>> stockMap = stocks.stream().collect(Collectors.groupingBy(VmsStock::getWeight, LinkedHashMap::new, Collectors.toList()));
        if (spec != null) {
            String[] tmp = spec.split("\\*");
            if (tmp.length == 3) {
                Float length = Float.parseFloat(tmp[0]);
                Float width = Float.parseFloat(tmp[1]);
                int weight = (int) (Float.parseFloat(tmp[2]) * 1000);
                List<VmsStock> tempList = stockMap.get(weight);
                for (VmsStock stock : tempList) {
                    if (length > stock.getLength() || width > stock.getLength()) {
                        result.add(stock);
                    }
                }
            }
        }

        return result;
    }

//    List<TwinFormulaInfo> matchFormula(String spec, List<TwinFormulaInfo> list) {
//        List<TwinFormulaInfo> result = new ArrayList<>();
//        //按重量分组
//        Map<Float, List<TwinFormulaInfo>> formulaMap = list.stream().collect(Collectors.groupingBy(o -> o.getWeight().floatValue(), LinkedHashMap::new, Collectors.toList()));
//        if (spec != null) {
//            String[] tmp = spec.split("\\*");
//            if (tmp.length == 3) {
//                Float length = Float.parseFloat(tmp[0]);
//                Float width = Float.parseFloat(tmp[1]);
//                Float weight = Float.parseFloat(tmp[2]);
//                List<TwinFormulaInfo> infos = formulaMap.get(weight);
//                if (infos != null) {
//                    for (TwinFormulaInfo info : infos) {
//                        if (length == info.getLength().floatValue() && width == info.getWidth().floatValue() ||
//                                length == info.getWeight().floatValue() && width == info.getLength().floatValue()) {
//                            result.add(info);
//                        }
//                    }
//                }
//            }
//        }
//        return result;
//    }
}

