订单按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 条评论
排序方式
时间
投票
快来抢占一楼吧
请登录后发表评论
相关推荐
文章归档
2024-11
1 篇
2024-06
1 篇
2024-05
2 篇
2024-04
2 篇
2024-03
2 篇
展开剩余 68 条
2024-01
1 篇
2023-10
1 篇
2023-09
1 篇
2023-08
1 篇
2023-06
1 篇
2023-04
1 篇
2022-12
2 篇
2022-06
1 篇
2022-04
4 篇
2022-03
3 篇
2022-01
6 篇
2021-12
2 篇
2021-11
2 篇
2021-10
2 篇
2021-09
1 篇
2021-08
2 篇
2021-07
4 篇
2021-06
1 篇
2021-05
3 篇
2021-04
3 篇
2021-01
2 篇
2020-11
1 篇
2020-10
3 篇
2020-09
2 篇
2020-08
1 篇
2020-07
5 篇
2020-06
5 篇
2020-05
1 篇
2020-04
1 篇
2020-03
2 篇
2020-02
3 篇
2020-01
1 篇
2019-11
5 篇
2019-10
10 篇
2019-09
12 篇
2019-08
17 篇
2019-07
8 篇
2019-05
3 篇
2019-04
8 篇
2019-03
7 篇
2019-02
8 篇
2019-01
5 篇
2018-12
7 篇
2018-11
8 篇
2018-10
4 篇
2018-09
7 篇
2018-08
12 篇
2018-07
9 篇
2018-06
6 篇
2018-05
11 篇
2018-04
18 篇
2018-03
1 篇
2018-02
2 篇
2018-01
10 篇
2017-12
14 篇
2017-11
44 篇
2017-10
13 篇
2017-09
4 篇
2017-08
12 篇
2017-07
5 篇
2017-06
4 篇
2017-05
2 篇
2017-04
3 篇
2017-03
9 篇
2017-02
3 篇
2017-01
2 篇
2016-12
10 篇
2016-11
4 篇
最新文章
最受欢迎
11-07 19:00
06-26 11:51
05-17 17:08
05-17 10:59
04-11 17:05
13 评论
11 评论
10 评论