package com.prolog.eis;

import com.alibaba.fastjson.JSONObject;
import com.prolog.eis.dto.buildwave.*;
import com.prolog.eis.dto.eis4.StationOBDispatchDto;
import com.prolog.eis.service.buildwave.BuildWaveService;
import com.prolog.framework.utils.FileUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;

/**
 * @Version: 1.0
 * @Author: wuhan
 * @Created: 2024/7/31 16:32
 */
@SpringBootTest(classes = DispatchApplication.class)
@RunWith(SpringRunner.class)
public class DispatchTest {
    @Resource
    private BuildWaveService buildWaveService;

    private static int ORDER_ID_SEED = 1;
    private static int ORDER_DETAIL_ID_SEED = 10000;
    private static int CONTAINER_NO_SEED = 1000000;
    private final static int MAX_ORDER_COUNT = 5;
    private final static String OWNER_Id = "owner";

    @Test
    public void waveDispatchTest() {
        for (int i = 0; i < 20; i++) {
            try {
//                BuildWaveDispatchDto dispatchDto = this.createDispatchDto();
                File file = new File("D:\\222.txt");
                String str = FileUtils.readAllText(file);
                BuildWaveDispatchDto dispatchDto = JSONObject.parseObject(str, BuildWaveDispatchDto.class);
                PObResultDto result = buildWaveService.buildWave(dispatchDto);
                int temp = 0;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private BuildWaveDispatchDto createDispatchDto() {
        BuildWaveDispatchDto dispatchDto = new BuildWaveDispatchDto();

        dispatchDto.setMaxWaveOrderCount(MAX_ORDER_COUNT);
        this.createOrder1(dispatchDto);
        this.createOrder2(dispatchDto);
        this.createOrder12(dispatchDto);
        this.createOrder12(dispatchDto);
        this.createOrder123(dispatchDto);
        this.createStore(dispatchDto, "item1", "lot1", "pack1", 1);
        this.createStore(dispatchDto, "item1", "lot1", "pack1", 1);
        this.createStore(dispatchDto, "item2", "lot2", "pack2", 10);

        dispatchDto.init();

        return dispatchDto;
    }

    private void createOrder1(BuildWaveDispatchDto dispatchDto) {
        PObOrderDto order = new PObOrderDto();
        order.setOrderId(ORDER_ID_SEED++);
        dispatchDto.getOrderList().add(order);

        this.createOrderDetail(order, "item1", "lot1", "pack1", 1);
    }

    private void createOrder2(BuildWaveDispatchDto dispatchDto) {
        PObOrderDto order = new PObOrderDto();
        order.setOrderId(ORDER_ID_SEED++);
        dispatchDto.getOrderList().add(order);

        this.createOrderDetail(order, "item2", "lot2", "pack2", 2);
    }

    private void createOrder12(BuildWaveDispatchDto dispatchDto) {
        PObOrderDto order = new PObOrderDto();
        order.setOrderId(ORDER_ID_SEED++);
        dispatchDto.getOrderList().add(order);

        this.createOrderDetail(order, "item1", "lot1", "pack1", 1);
        this.createOrderDetail(order, "item2", "lot2", "pack2", 2);
    }

    private void createOrder123(BuildWaveDispatchDto dispatchDto) {
        PObOrderDto order = new PObOrderDto();
        order.setOrderId(ORDER_ID_SEED++);
        dispatchDto.getOrderList().add(order);

        this.createOrderDetail(order, "item1", "lot1", "pack1", 1);
        this.createOrderDetail(order, "item2", "lot2", "pack2", 2);
        this.createOrderDetail(order, "item2", "lot3", "pack2", 3);
    }

    private void createOrderDetail(PObOrderDto order, String itemId, String lotId, String packId, int qty) {
        PObOrderDetailDto detail1 = new PObOrderDetailDto();
        detail1.setOrderDetailId(ORDER_DETAIL_ID_SEED++);
        detail1.setOrderId(order.getOrderId());
        String itemNo = String.format("%s@@%s@@%s@@%s", OWNER_Id, itemId, lotId, packId);
        detail1.setItemNo(itemNo);
        detail1.setPlanNum(new BigDecimal(qty));
        order.getDetailList().add(detail1);
    }

    private void createStore(BuildWaveDispatchDto dispatchDto, String itemId, String lotId, String packId, int qty) {
        PObContainerStoreDto store = new PObContainerStoreDto();
        store.setContainerNo("P" + String.valueOf(CONTAINER_NO_SEED++));
        store.setItemId(itemId);
        store.setLotId(lotId);
        store.setOwnerId(OWNER_Id);
        store.setPackId(packId);
        store.setUseQty(new BigDecimal(qty));
        dispatchDto.getContainerStoreList().add(store);
    }
}
