feat: #10000 订单下载同步库存修改

This commit is contained in:
赵世界 2022-08-11 02:13:19 +08:00
parent a47fbb2622
commit 4819f823e4
9 changed files with 117 additions and 107 deletions

View File

@ -48,7 +48,7 @@ class Swoole extends Command
$endTime = DateTimeUtils::getMicroTime();
$beginTime = $endTime - 1000;
foreach ($shops as $shop) {
BusinessFactory::init()->make($shop->plat_id)->setShop($shop)->downloadOrdersAndSave($beginTime, $endTime, 1, '', 'increment');
BusinessFactory::init()->make($shop->plat_id)->setShop($shop)->downloadOrdersAndSave($beginTime, $endTime, 'increment', 1);
}
});
Event::wait();

View File

@ -53,9 +53,9 @@ class Test extends Command
// $business->incrQuantity(1);
// 订单下载
$beginTime = DateTimeUtils::getMicroTime('2022-08-08');
$endTime = DateTimeUtils::getMicroTime('2022-08-09');
$business->downloadOrdersAndSave($beginTime, $endTime);
// $beginTime = DateTimeUtils::getMicroTime('2022-08-08');
// $endTime = DateTimeUtils::getMicroTime('2022-08-09');
// $business->downloadOrdersAndSave($beginTime, $endTime);
$this->info('执行测试成功');
}

View File

@ -94,7 +94,7 @@ class ShopsController extends Controller
$business->bindGoods($request->get('data'));
}
if ('orders' === $request->get('type')) {
$business->saveOrders();
$business->saveOrders($request->get('data'));
}
}
}

View File

@ -2,25 +2,29 @@
namespace App\Listeners;
use App\Models\GoodsSku;
use App\Models\Log;
use App\Models\Shop;
use App\Services\Business\BusinessFactory;
use Illuminate\Auth\Events\Registered;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Models\BusinessOrderItem;
class UpdateBusinessGoodsStock implements ShouldQueue
{
protected $shopId;
protected $updateOrders;
protected $num;
protected $businessOrderItem;
/**
* Create the event listener.
*
* @return void
*/
public function __construct($shopId, $updateOrders)
public function __construct(BusinessOrderItem $item, $num)
{
$this->shopId = $shopId;
$this->updateOrders = $updateOrders;
$this->businessOrderItem = $item;
$this->num = $num;
}
/**
@ -31,8 +35,29 @@ class UpdateBusinessGoodsStock implements ShouldQueue
*/
public function handle(Registered $event)
{
if (empty($this->updateOrders)) {
return;
$shops = Shop::query()->where('id', '<>', $this->businessOrderItem->shop_id)->where('status', 1)->get(['id', 'plat_id']);
[$goodsCode, $skuCode] = explode('_', $this->businessOrderItem->external_sku_id);
$goodsSku = GoodsSku::query()->where('sku_code', $skuCode)
->with(['goods' => function ($query) use ($goodsCode) {
$query->where('goods_code', $goodsCode);
}])
->first();
if ($goodsSku) {
$goodsSku->stock += $this->num;
$goodsSku->save();
foreach ($shops as $shop) {
BusinessFactory::init()->make($shop['plat_id'])->setShopId($shop['id'])->incrQuantity($this->businessOrderItem, $this->num, true, $goodsSku);
}
} else {
$log = new Log();
$log->module = 'goods';
$log->action = 'PATCH';
$log->target_type = 'goods_sku';
$log->target_id = $goodsSku->id;
$log->target_field = 'stock';
$log->user_id = $this->businessOrderItem->shop_id;
$log->message = ($this->businessOrderItem->external_sku_id ?: '商品') . '未找到';
$log->save();
}
}
}

View File

@ -2,6 +2,10 @@
namespace App\Services\Business;
use App\Listeners\UpdateBusinessGoodsStock;
use App\Models\BusinessGoodsSku;
use App\Models\BusinessOrder;
use App\Models\GoodsSku;
use App\Models\Log;
use App\Models\Shop;
use GuzzleHttp\Client;
@ -21,11 +25,60 @@ abstract class BusinessClient
abstract public function bindGoods($goods);
abstract public function incrQuantity($skuId);
abstract public function incrQuantity(BusinessGoodsSku $businessGoodsSku, $num, $incremental, GoodsSku $goodsSku);
abstract public function downloadOrdersAndSave($beginTime, $endTime, $page = 1, $activityNo = '', $downloadType = 'default');
abstract public function downloadOrdersAndSave($beginTime, $endTime, $downloadType = 'default', $page = 1);
abstract public function saveOrders($orders);
public function saveOrders($orders)
{
$shopId = $this->getShop()->id;
foreach ($orders as $order) {
unset($order['custom_item_list'], $order['logistics_list'], $order['gift_order_list']);
$order['shop_id'] = $shopId;
$orderRecord = BusinessOrder::query()->where('shop_id', $shopId)->where('order_sn', $order['order_sn'])->first();
if (empty($orderRecord)) {
$orderRecord->create($order);
} else {
$orderRecord->update($order);
}
foreach ($order['sub_order_list'] as $item) {
$item['shop_id'] = $shopId;
$orderItem = BusinessOrder::query()->where('shop_id', $shopId)
->where('business_order_id', $orderRecord->id)
->where('goods_id', $item['goods_id'])
->where('sku_id', $item['sku_id'])
->first();
$num = 0;
if (empty($orderItem)) {
if ($item['cancel_status']) {
if ($num = $item['goods_number'] - $item['already_cancel_number']) {
// 扣库存 $reduceNum
$num = 0 - $num;
}
} else {
// 扣库存
$num = 0 - $item['goods_number'];
}
$orderItem->create($item);
} else {
if ($item['cancel_status'] !== $orderItem->cancel_status) {
if ($item['cancel_status']) {
// 加库存
$num = $item['already_cancel_number'];
} else {
// 扣库存
$num = 0 - $item['goods_number'];
}
}
$orderItem->update($item);
}
// 增量更新库存
if ($num) {
event(new UpdateBusinessGoodsStock($orderItem, $num));
}
}
}
}
public function authCallback($code, $shopId)
{
@ -81,31 +134,29 @@ abstract class BusinessClient
public function formDataPostRequest($url, $params)
{
$method = 'POST';
$headers = [
'headers' => ['Content-type' => 'application/x-www-form-urlencoded;charset=UTF-8'],
'form_params' => $params
];
$res = (new Client())->request('POST', $url, $headers);
$res = (new Client())->request($method, $url, $headers);
$size = $res->getBody()->getSize();
$res = json_decode($res->getBody()->getContents(), true);
if ('pdd.ktt.increment.order.query' === $params['type']) {
$log = Log::query()->where('user_id', $this->shop->id)->where('target_field', $params['type'])->first();
$log = Log::query()->where('user_id', $this->getShop()->id)->where('target_field', $params['type'])->first();
} else {
$log = new Log();
}
$log->module = 'plat';
$log->action = 'POST';
$log->target_type = $this->shop->plat_id;
$log->action = $method;
$log->target_type = $this->getShop()->plat_id;
$log->target_id = $this->getSkuId();
$log->target_field = $params['type'];
$log->user_id = $this->shop->id;
$log->user_id = $this->getShop()->id;
if ($size < 64000) {
$log->message = json_encode($res, 256);
}
$log->save();
if (isset($res['error_response'])) {
throw new \Exception($res['error_response']['error_msg'], $res['error_response']['error_code']);
}
return $res;
}

View File

@ -55,14 +55,9 @@ class KuaiTuanTuan extends BusinessClient
Goods::bindGoods($goods, $this->shop->id);
}
public function incrQuantity($skuId)
public function incrQuantity($businessGoodsSku, $num, $incremental, GoodsSku $goodsSku)
{
$goodsSku = GoodsSku::query()
->with(['goods:id,goods_code'])
->find($skuId);
$code = $goodsSku->goods->goods_code . '_' . $goodsSku->sku_code;
$business = BusinessGoodsSku::query()->where('shop_id', $this->shop->id)->where('external_sku_id', $code)->first(['goods_id', 'sku_id']);
[$type, $appendParams] = Goods::incrQuantity($business->goods_id, $goodsSku->stock, $business->sku_id);
[$type, $appendParams] = Goods::incrQuantity($businessGoodsSku->goods_id, $num, $businessGoodsSku->sku_id, $incremental ? 1 : 2);
$this->doRequest($type, $appendParams);
}
@ -72,11 +67,10 @@ class KuaiTuanTuan extends BusinessClient
* @param $beginTime
* @param $endTime
* @param int $page
* @param string $activityNo
* @param string $downloadType
* @return mixed
* @return void
*/
public function downloadOrdersAndSave($beginTime, $endTime, $page = 1, $activityNo = '', $downloadType = 'default')
public function downloadOrdersAndSave($beginTime, $endTime, $downloadType = 'default', $page = 1)
{
if ('increment' === $downloadType) {
[$type, $appendParams] = Order::downloadIncrementOrders($beginTime, $endTime, $page);
@ -93,11 +87,6 @@ class KuaiTuanTuan extends BusinessClient
}
}
public function saveOrders($orders)
{
Order::saveOrders($orders, $this->shop->id);
}
protected function getAccessTokenWithCode()
{
$type = 'pdd.pop.auth.token.create';
@ -134,14 +123,6 @@ class KuaiTuanTuan extends BusinessClient
return $this->formDataPostRequest($url, $publicParams);
}
public function getAuthUrl($shopId, $platId)
{
$state = $shopId . '_' . $platId;
return "https://oauth.pinduoduo.com/authorize/ktt?client_id={$this->clientId}&redirect_uri={$this->redirectUri}&state={$state}";
}
public function downloadGoods($skuId)
{
$goodsSku = GoodsSku::query()
@ -154,4 +135,11 @@ class KuaiTuanTuan extends BusinessClient
$goods = $res['response']['result'];
$this->bindGoods([$goods]);
}
public function getAuthUrl($shopId, $platId)
{
$state = $shopId . '_' . $platId;
return "https://oauth.pinduoduo.com/authorize/ktt?client_id={$this->clientId}&redirect_uri={$this->redirectUri}&state={$state}";
}
}

View File

@ -2,10 +2,6 @@
namespace App\Services\Business\KuaiTuanTuan;
use App\Listeners\UpdateBusinessGoodsStock;
use App\Models\BusinessOrder;
use App\Models\BusinessOrderItem;
class Order
{
/**
@ -21,8 +17,8 @@ class Order
'page_size' => 100, // 数量, 必填, 1100
// 非必填
// 'activity_no' => $activityNo, // 团号
'after_sales_status' => 0, // 售后状态, 可选 0-未发起售后 1-退款中 2-退款成功 3-待处理 4-拒绝退款 6-待(顾客)退货 7-待(团长)确认退货 8-(顾客)撤销 9-(系统)关闭
'cancel_status' => 0, // 取消状态, 可选 0-未取消 1-已取消
// 'after_sales_status' => 0, // 售后状态, 可选 0-未发起售后 1-退款中 2-退款成功 3-待处理 4-拒绝退款 6-待(顾客)退货 7-待(团长)确认退货 8-(顾客)撤销 9-(系统)关闭
// 'cancel_status' => 0, // 取消状态, 可选 0-未取消 1-已取消
// 'shipping_status' => '', // 发货状态 0-未发货 1-已发货 2-部分发货 3-已收货
// 'verification_status' => '', // 核销状态, 可选 0-未核销 1-已核销 2-部分核销
];
@ -43,34 +39,13 @@ class Order
'page_size' => 100, // 数量
// 非必填
// 'activity_no' => $activityNo, // 团号
'after_sales_status' => 0, // 售后状态, 可选 0-未发起售后 1-退款中 2-退款成功 3-待处理 4-拒绝退款 6-待(顾客)退货 7-待(团长)确认退货 8-(顾客)撤销 9-(系统)关闭
'cancel_status' => 0, // 取消状态, 可选 0-未取消 1-已取消
// 'after_sales_status' => 0, // 售后状态, 可选 0-未发起售后 1-退款中 2-退款成功 3-待处理 4-拒绝退款 6-待(顾客)退货 7-待(团长)确认退货 8-(顾客)撤销 9-(系统)关闭
// 'cancel_status' => 0, // 取消状态, 可选 0-未取消 1-已取消
// 'shipping_status' => '', // 发货状态 0-未发货 1-已发货 2-部分发货 3-已收货
// 'verification_status' => '', // 核销状态, 可选 0-未核销 1-已核销 2-部分核销
];
return [$type, $appendParams];
}
// 下载订单事件之后去统计 然后更新库存
public static function saveOrders(array $orders, $shopId)
{
$updateOrders = [];
foreach ($orders as $order) {
unset($order['custom_item_list'], $order['logistics_list'], $order['gift_order_list']);
$orderRecord = BusinessOrder::updateOrCreate(
['shop_id' => $shopId, 'order_sn' => $order['order_sn']],
$order
);
$updateOrders[] = $order['order_sn'];
foreach ($order['sub_order_list'] as $item) {
BusinessOrderItem::updateOrCreate(
['shop_id' => $shopId, 'business_order_id' => $orderRecord->id, 'goods_id' => $item['goods_id'], 'sku_id' => $item['sku_id']],
$item
);
}
}
event(new UpdateBusinessGoodsStock($shopId, $updateOrders));
}
}

View File

@ -24,24 +24,17 @@ class MiaoXuan extends BusinessClient
Goods::bindGoods($goods, $this->shop->id);
}
public function incrQuantity($skuId)
public function incrQuantity($businessGoodsSku, $num, $incremental, GoodsSku $goodsSku)
{
$goodsSku = GoodsSku::query()->find($skuId);
$business = BusinessGoodsSku::query()->where('shop_id', $this->shop->id)->where('self_sku_id', $skuId)->first(['goods_id', 'sku_id', 'external_sku_id'])->toArray();
$appendParams = Goods::incrQuantity($this->shop->id, $goodsSku->stock, $business);
$appendParams = Goods::incrQuantity($this->shop->id, $goodsSku->stock, $businessGoodsSku);
$url = 'http://shop.chutang66.com/miaoxuan/stock';
$this->formDataPostRequest($url, $appendParams);
}
public function downloadOrdersAndSave($beginTime, $endTime, $page = 1, $activityNo = '', $downloadType = 'default')
public function downloadOrdersAndSave($beginTime, $endTime, $downloadType = 'default', $page = 1)
{
}
public function saveOrders($orders)
{
Order::saveOrders($orders, $this->shop->id);
}
public function downloadGoods($skuId)
{
// TODO: Implement downloadGoods() method.

View File

@ -2,29 +2,7 @@
namespace App\Services\Business\MiaoXuan;
use App\Listeners\UpdateBusinessGoodsStock;
use App\Models\BusinessOrder;
class Order
{
// 下载订单事件之后去统计 然后更新库存
public static function saveOrders(array $orders, $shopId)
{
$updateOrders = [];
foreach ($orders as $order) {
$orderRecord = BusinessOrder::updateOrCreate(
['shop_id' => $shopId, 'order_sn' => $order['order_sn']],
$order
);
$updateOrders[] = $order['order_sn'];
foreach ($order['sub_order_list'] as $item) {
$orderRecord = BusinessOrder::updateOrCreate(
['shop_id' => $shopId, 'business_order_id' => $orderRecord->id, 'goods_id' => $item['goods_id'], 'sku_id' => $item['sku_id']],
$item
);
}
}
event(new UpdateBusinessGoodsStock($shopId, $updateOrders));
}
}