2022-08-06 15:25:13 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Business;
|
2022-08-08 18:53:38 +08:00
|
|
|
|
2022-08-11 11:18:21 +08:00
|
|
|
use App\Events\BusinessOrdersUpdate;
|
2022-08-11 02:13:19 +08:00
|
|
|
use App\Models\BusinessGoodsSku;
|
|
|
|
|
use App\Models\BusinessOrder;
|
2022-08-11 05:11:14 +08:00
|
|
|
use App\Models\BusinessOrderItem;
|
2022-08-11 02:13:19 +08:00
|
|
|
use App\Models\GoodsSku;
|
2022-08-06 15:25:13 +08:00
|
|
|
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;
|
2022-08-06 15:25:13 +08:00
|
|
|
|
|
|
|
|
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;
|
2022-08-06 15:25:13 +08:00
|
|
|
|
|
|
|
|
abstract public function auth();
|
|
|
|
|
|
2022-08-09 16:56:52 +08:00
|
|
|
abstract public function downloadGoodsListAndBind();
|
2022-08-08 18:53:38 +08:00
|
|
|
|
|
|
|
|
abstract public function downloadGoods($skuId);
|
2022-08-06 15:25:13 +08:00
|
|
|
|
|
|
|
|
abstract public function bindGoods($goods);
|
|
|
|
|
|
2022-08-16 21:02:31 +08:00
|
|
|
abstract public function incrQuantity($businessGoodsSku, $num, $incremental);
|
2022-08-06 15:25:13 +08:00
|
|
|
|
2022-08-11 02:13:19 +08:00
|
|
|
abstract public function downloadOrdersAndSave($beginTime, $endTime, $downloadType = 'default', $page = 1);
|
2022-08-06 15:25:13 +08:00
|
|
|
|
2022-08-11 02:13:19 +08:00
|
|
|
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();
|
2022-08-11 02:13:19 +08:00
|
|
|
} 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);
|
2022-08-11 02:13:19 +08:00
|
|
|
$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) {
|
2022-08-11 02:13:19 +08:00
|
|
|
// 扣库存 $reduceNum
|
|
|
|
|
$num = 0 - $num;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 扣库存
|
|
|
|
|
$num = 0 - $item['goods_number'];
|
|
|
|
|
}
|
2022-08-11 05:11:14 +08:00
|
|
|
$orderItem->save();
|
2022-08-11 02:13:19 +08:00
|
|
|
} else {
|
2022-08-23 20:51:13 +08:00
|
|
|
if ($item['cancel_status'] != $orderItem->cancel_status) {
|
|
|
|
|
if ((int)$item['cancel_status']) {
|
2022-08-11 02:13:19 +08:00
|
|
|
// 加库存
|
2022-08-20 16:33:57 +08:00
|
|
|
$num = $cancelNum;
|
2022-08-11 02:13:19 +08:00
|
|
|
} else {
|
|
|
|
|
// 扣库存
|
|
|
|
|
$num = 0 - $item['goods_number'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$orderItem->update($item);
|
|
|
|
|
}
|
|
|
|
|
// 增量更新库存
|
|
|
|
|
if ($num) {
|
2022-08-11 11:18:21 +08:00
|
|
|
event(new BusinessOrdersUpdate($orderItem, $num));
|
2022-08-11 02:13:19 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-06 16:38:04 +08:00
|
|
|
|
2022-08-06 15:25:13 +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)
|
2022-08-06 15:25:13 +08:00
|
|
|
{
|
|
|
|
|
$this->code = $code;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 15:05:17 +08:00
|
|
|
public function getCode()
|
2022-08-06 15:25:13 +08:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 15:25:13 +08:00
|
|
|
public function formDataPostRequest($url, $params)
|
|
|
|
|
{
|
2022-08-11 02:13:19 +08:00
|
|
|
$method = 'POST';
|
2022-08-06 15:25:13 +08:00
|
|
|
$headers = [
|
2022-08-08 15:05:17 +08:00
|
|
|
'headers' => ['Content-type' => 'application/x-www-form-urlencoded;charset=UTF-8'],
|
2022-08-06 15:25:13 +08:00
|
|
|
'form_params' => $params
|
|
|
|
|
];
|
2022-08-11 02:13:19 +08:00
|
|
|
$res = (new Client())->request($method, $url, $headers);
|
2022-08-09 16:56:52 +08:00
|
|
|
$size = $res->getBody()->getSize();
|
2022-08-06 15:25:13 +08:00
|
|
|
$res = json_decode($res->getBody()->getContents(), true);
|
2022-08-18 13:20:55 +08:00
|
|
|
if (!in_array($params['type'], ['pdd.ktt.increment.order.query', 'pdd.ktt.order.list'], true)) {
|
2022-08-10 16:39:25 +08:00
|
|
|
$log = new Log();
|
2022-08-18 13:20:55 +08:00
|
|
|
$log->module = 'plat';
|
|
|
|
|
$log->action = $method;
|
2022-08-20 17:44:14 +08:00
|
|
|
$log->target_type = $this->getShop()->plat_id . '--' . $this->getShop()->name;
|
|
|
|
|
$log->target_id = $this->getShop()->id;
|
2022-08-18 13:20:55 +08:00
|
|
|
$log->target_field = $params['type'];
|
2022-08-24 10:35:44 +08:00
|
|
|
$log->user_id = Auth::id() ?? 999;
|
2022-08-18 13:20:55 +08:00
|
|
|
if ($size < 64000) {
|
|
|
|
|
$log->message = json_encode($res, 256);
|
|
|
|
|
}
|
|
|
|
|
$log->save();
|
2022-08-10 16:39:25 +08:00
|
|
|
}
|
2022-08-06 15:25:13 +08:00
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
}
|