erp/app/Services/Business/BusinessClient.php

164 lines
4.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Services\Business;
use App\Events\BusinessOrdersUpdate;
use App\Models\BusinessGoodsSku;
use App\Models\BusinessOrder;
2022-08-11 05:11:14 +08:00
use App\Models\BusinessOrderItem;
use App\Models\GoodsSku;
use App\Models\Log;
use App\Models\Shop;
2022-08-08 13:59:44 +08:00
use GuzzleHttp\Client;
2022-08-20 17:44:14 +08:00
use Illuminate\Support\Facades\Auth;
abstract class BusinessClient
{
protected $redirectUri = 'http://erp.chutang66.com/callback';
protected $code;
protected $shop;
2022-08-08 15:05:17 +08:00
protected $skuId = 0;
abstract public function auth();
2022-08-09 16:56:52 +08:00
abstract public function downloadGoodsListAndBind();
abstract public function downloadGoods($skuId);
abstract public function bindGoods($goods);
2022-08-16 21:02:31 +08:00
abstract public function incrQuantity($businessGoodsSku, $num, $incremental);
abstract public function downloadOrdersAndSave($beginTime, $endTime, $downloadType = 'default', $page = 1);
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;
2022-08-11 05:11:14 +08:00
$orderRecord = BusinessOrder::firstOrNew(['shop_id' => $shopId, 'order_sn' => $order['order_sn']], $order);
if (empty($orderRecord->id)) {
$orderRecord->save();
} else {
$orderRecord->update($order);
}
foreach ($order['sub_order_list'] as $item) {
$item['shop_id'] = $shopId;
2022-08-11 05:11:14 +08:00
$orderItem = BusinessOrderItem::firstOrNew(['shop_id' => $shopId, 'business_order_id' => $orderRecord->id, 'goods_id' => $item['goods_id'], 'sku_id' => $item['sku_id']], $item);
$num = 0;
2022-08-20 16:33:57 +08:00
$cancelNum = $item['already_cancel_number'] ?? 0;
2022-08-11 05:11:14 +08:00
if (empty($orderItem->id)) {
2022-08-23 20:51:13 +08:00
if ((int)$item['cancel_status']) {
2022-08-20 16:33:57 +08:00
if ($num = $item['goods_number'] - $cancelNum) {
// 扣库存 $reduceNum
$num = 0 - $num;
}
} else {
// 扣库存
$num = 0 - $item['goods_number'];
}
2022-08-11 05:11:14 +08:00
$orderItem->save();
} else {
2022-08-23 20:51:13 +08:00
if ($item['cancel_status'] != $orderItem->cancel_status) {
if ((int)$item['cancel_status']) {
// 加库存
2022-08-20 16:33:57 +08:00
$num = $cancelNum;
} else {
// 扣库存
$num = 0 - $item['goods_number'];
}
}
$orderItem->update($item);
}
// 增量更新库存
2022-08-31 10:39:12 +08:00
if ($num && $item['external_sku_id']) {
event(new BusinessOrdersUpdate($orderItem, $num));
}
}
}
}
2022-08-06 16:38:04 +08:00
public function authCallback($code, $shopId)
{
$this->setCode($code);
$this->setShop($shopId);
$this->auth();
return $this;
}
public function setShopWithId($shopId)
{
$this->shop = Shop::query()->find($shopId);
return $this;
}
public function setShop(Shop $shop)
{
$this->shop = $shop;
return $this;
}
public function getShop()
{
return $this->shop;
}
2022-08-08 15:05:17 +08:00
public function setCode($code)
{
$this->code = $code;
return $this;
}
2022-08-08 15:05:17 +08:00
public function getCode()
{
return $this->code;
}
2022-08-08 15:05:17 +08:00
public function setSkuId($skuId)
{
$this->skuId = $skuId;
return $this;
}
public function getSkuId()
{
return $this->skuId;
}
public function formDataPostRequest($url, $params)
{
$method = 'POST';
$headers = [
2022-08-08 15:05:17 +08:00
'headers' => ['Content-type' => 'application/x-www-form-urlencoded;charset=UTF-8'],
'form_params' => $params
];
$res = (new Client())->request($method, $url, $headers);
2022-08-09 16:56:52 +08:00
$size = $res->getBody()->getSize();
$res = json_decode($res->getBody()->getContents(), true);
2022-08-31 10:39:12 +08:00
if (!in_array($params['type'], ['pdd.ktt.increment.order.query', 'pdd.ktt.order.list'], true)) {
$log = new Log();
$log->module = 'plat';
$log->action = $method;
$log->target_type = $this->getShop()->plat_id . '--' . $this->getShop()->name;
$log->target_id = $this->getShop()->id;
$log->target_field = $params['type'];
$log->user_id = Auth::id() ?? 999;
if ($size < 48000) {
$log->message = json_encode($res, 256) . '=====' . json_encode($params, 256);
}
$log->save();
2022-08-30 23:11:48 +08:00
}
if (isset($res['error_response'])) {
2022-08-31 10:39:12 +08:00
exit();
2022-08-10 16:39:25 +08:00
}
return $res;
}
}