订单按sku和数量拆分逻辑

06-26 11:51 字数 4382 阅读 988 已编辑

废了一番功夫,所以记录一下

$orderItemListByStockId = [
    "1231298956" => [
        [
            "id" => 683457815,
            "orderId" => 983250480,
            "stockId" => 1231298956,
            "stockSku" => "再来三次加工",
            "quantity" => 3
        ],
        [
            "id" => 683457938,
            "orderId" => 983250480,
            "stockId" => 1231298956,
            "stockSku" => "再来三次加工",
            "quantity" => 10
        ]
    ],
    "1231298957" => [
        [
            "id" => 683457816,
            "orderId" => 983250480,
            "stockId" => 1231298957,
            "stockSku" => "再来四次加工",
            "quantity" => 8
        ],
        [
            "id" => 683457939,
            "orderId" => 983250480,
            "stockId" => 1231298957,
            "stockSku" => "再来四次加工",
            "quantity" => 10
        ]
    ]
];

$splitDataByStockId = [
    "1231298956" => [
        "stockId" => 1231298956,
        "stockSku" => "再来三次加工",
        "quantity" => 2
    ],
    "1231298957" => [
        "stockId" => 1231298957,
        "stockSku" => "再来四次加工",
        "quantity" => 2
    ]
];

var_dump($orderItemListByStockId);

// sku对应的总数量 和 sku拆出去的数量
/*$totalSkuCountByStockId = $totalSplitSkuCountByStockId = [];
foreach ($orderItemListByStockId as $stockId => $itemList) {
    $totalSkuCountByStockId[$stockId] = array_sum(array_column($itemList, "quantity"));
    $totalSplitSkuCountByStockId[$stockId] = 0;
}*/

// 订单下的商品总数
$totalSkuCount = $totalSplitSkuCount = 0;
foreach ($orderItemListByStockId as $stockId => $itemList) {
    $totalSkuCount += array_sum(array_column($itemList, "quantity"));
}

// 剔除订单下不需要处理的sku
foreach ($orderItemListByStockId as $orderItemStockId => $orderItemList) {
    if (!isset($splitDataByStockId[$orderItemStockId])) {
        unset($orderItemListByStockId[$orderItemStockId]);
    }
}

$toBeSplitData = [];
$platformOrderId = "ii20240528100701_2";

/**
 * @var integer 拆分方式,1:全部商品拆分到一单;2:单个商品拆分到一单
 */
$splitMethod = 1;

/**
 * @var integer 拆分类型,1:循环拆分;2:单次拆分
 */
$splitType = 1;


while (true) {
    if ($splitMethod == 1) {
        $orderItemSplitData = [];
    }
    foreach ($splitDataByStockId as $splitStockId => $splitStockData) {
        if ($splitMethod == 2) {
            $orderItemSplitData = [];
        }
        if (!empty($orderItemListByStockId[$splitStockId])) {
            foreach ($orderItemListByStockId[$splitStockId] as $oiIndex => $orderItem) {
                if ($orderItemListByStockId[$splitStockId][$oiIndex]['quantity'] >= $splitStockData['quantity']) {
                    $orderItemListByStockId[$splitStockId][$oiIndex]['quantity'] -= $splitStockData['quantity'];
                    /*$totalSplitSkuCountByStockId[$splitStockId] += $splitStockData['quantity'];
                    if ($totalSplitSkuCountByStockId[$splitStockId] < $totalSkuCountByStockId[$splitStockId]) { // 不可全部拆完
                        $orderItemSplitData[] = $orderItem['id'] . "-||-" . $splitStockData['quantity'];
                    }*/
                    $totalSplitSkuCount += $splitStockData['quantity'];
                    if ($totalSplitSkuCount < $totalSkuCount) { // 不可全部拆完
                        $orderItemSplitData[] = $orderItem['id'] . "-||-" . $splitStockData['quantity'];
                    }
                    break;
                }
                // 不满足拆分数量要求的unset掉
                unset($orderItemListByStockId[$splitStockId][$oiIndex]);
                if (empty($orderItemListByStockId[$splitStockId])) {
                    unset($orderItemListByStockId[$splitStockId]);
                }
            }
        } else {
            unset($orderItemListByStockId[$splitStockId]);
        }
        if ($splitMethod == 2 && !empty($orderItemSplitData)) {
            $toBeSplitData[] = ['platformOrderId' => $platformOrderId, 'orderSplitData' => implode(";", $orderItemSplitData)];
        }
    }
    if ($splitMethod == 1 && count($orderItemSplitData) === count($splitDataByStockId)) {
        $toBeSplitData[] = ['platformOrderId' => $platformOrderId, 'orderSplitData' => implode(";", $orderItemSplitData)];
    }
    if (empty($orderItemListByStockId) || $splitType == 2) {
        break;
    }
}


var_dump($toBeSplitData);
exit;
1人点赞>
关注 收藏 改进 举报
0 条评论
排序方式 时间 投票
快来抢占一楼吧
请登录后发表评论
站长 @ 十七度
文章
384
粉丝
23
喜欢
195
收藏
31
排名 : 1
访问 : 144.96万
私信