2022-08-02 11:43:46 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Shop;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2022-10-31 14:14:51 +08:00
|
|
|
use App\Models\BusinessGoodsSku;
|
2022-08-18 14:57:38 +08:00
|
|
|
use App\Models\GoodsSku;
|
2022-08-02 11:43:46 +08:00
|
|
|
use App\Models\Shop;
|
|
|
|
|
use App\Http\Resources\ShopsResource;
|
|
|
|
|
use Illuminate\Http\Request;
|
2022-08-12 16:20:40 +08:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2022-08-02 11:43:46 +08:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2022-08-06 15:25:13 +08:00
|
|
|
use App\Services\Business\BusinessFactory;
|
|
|
|
|
use Illuminate\Validation\Rule;
|
2022-08-12 13:30:32 +08:00
|
|
|
use App\Models\BusinessOrderItem;
|
2022-08-02 11:43:46 +08:00
|
|
|
|
|
|
|
|
class ShopsController extends Controller
|
|
|
|
|
{
|
2022-08-19 22:52:04 +08:00
|
|
|
public function index(Request $request)
|
2022-08-02 11:43:46 +08:00
|
|
|
{
|
2022-10-21 13:09:30 +08:00
|
|
|
$shops = Shop::query()->filter()->paginate($request->get('per_page'));
|
2022-08-06 15:25:13 +08:00
|
|
|
foreach ($shops as $shop) {
|
|
|
|
|
$shop->authUrl = '';
|
2022-08-16 18:24:56 +08:00
|
|
|
if ('妙选' !== $shop->plat_id && ('未授权' === $shop->status || '重新授权' === $shop->status)) {
|
2022-08-06 15:25:13 +08:00
|
|
|
$shop->authUrl = BusinessFactory::init()->make($shop->plat_id)->getAuthUrl($shop->id, $shop->getOriginal('plat_id'));
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-02 11:43:46 +08:00
|
|
|
|
|
|
|
|
return ShopsResource::collection($shops);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getPlatList()
|
|
|
|
|
{
|
|
|
|
|
$shop = new Shop();
|
|
|
|
|
|
|
|
|
|
return new ShopsResource($shop->getPlatList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$validator = Validator::make($request->all(), [
|
2022-08-09 16:56:52 +08:00
|
|
|
'name' => 'required|string|max:191|unique:shops,name',
|
2022-08-02 11:43:46 +08:00
|
|
|
'plat_id' => 'required|integer',
|
2022-10-25 10:47:36 +08:00
|
|
|
'ratio' => 'required',
|
2022-08-02 11:43:46 +08:00
|
|
|
]);
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
|
|
|
|
|
|
|
|
|
|
return response($this->res, $this->res['httpCode']);
|
|
|
|
|
}
|
2022-10-25 10:47:36 +08:00
|
|
|
$operator = substr($request->ratio, 0, 1);
|
|
|
|
|
if (!in_array($operator, ['+', '-', '*', '/'])) {
|
|
|
|
|
$this->res->errorMessage = '运算符号仅允许+,-,*,/';
|
|
|
|
|
return response($this->res, $this->res['httpCode']);
|
|
|
|
|
}
|
2022-08-02 11:43:46 +08:00
|
|
|
$shop = new Shop();
|
|
|
|
|
$shop->name = $request->name;
|
|
|
|
|
$shop->plat_id = $request->plat_id;
|
2022-10-24 00:23:09 +08:00
|
|
|
$shop->ratio = $request->ratio;
|
2022-08-11 04:06:38 +08:00
|
|
|
if (0 == $request->plat_id) {
|
2022-08-10 23:45:03 +08:00
|
|
|
$shop->status = 2;
|
|
|
|
|
}
|
2022-08-02 11:43:46 +08:00
|
|
|
$shop->save();
|
|
|
|
|
|
|
|
|
|
return response($this->res, $this->res['httpCode']);
|
|
|
|
|
}
|
2022-08-06 15:25:13 +08:00
|
|
|
|
2022-10-24 00:23:09 +08:00
|
|
|
public function update(Request $request, $id)
|
|
|
|
|
{
|
2022-10-25 10:47:36 +08:00
|
|
|
$operator = substr($request->ratio, 0, 1);
|
|
|
|
|
if (!in_array($operator, ['+', '-', '*', '/'])) {
|
|
|
|
|
$this->res->errorMessage = '运算符号仅允许+,-,*,/';
|
|
|
|
|
return response($this->res, $this->res['httpCode']);
|
|
|
|
|
}
|
2022-10-24 00:23:09 +08:00
|
|
|
$shop = Shop::query()->find($id);
|
|
|
|
|
$shop->ratio = $request->ratio;
|
|
|
|
|
$shop->save();
|
|
|
|
|
|
|
|
|
|
return response($this->res, $this->res['httpCode']);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 15:25:13 +08:00
|
|
|
public function authBind(Request $request)
|
|
|
|
|
{
|
|
|
|
|
[$shopId, $platId] = explode('_', $request->get('state'));
|
|
|
|
|
$shop = new Shop();
|
|
|
|
|
$platList = $shop->getPlatList();
|
|
|
|
|
$shop = $shop->find($shopId);
|
|
|
|
|
if ($platList[$platId] === $shop->plat_id) {
|
2022-08-08 13:45:54 +08:00
|
|
|
BusinessFactory::init()->make($shop->plat_id)->authCallback($request->get('code'), $shop);
|
2022-08-06 15:25:13 +08:00
|
|
|
} else {
|
|
|
|
|
$this->res = [
|
|
|
|
|
'httpCode' => 403,
|
|
|
|
|
'errorCode' => 403400,
|
|
|
|
|
'errorMessage' => '信息不匹配',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response($this->res, $this->res['httpCode']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function business(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
|
'type' => ['required', 'string', Rule::in(['goods', 'orders'])],
|
|
|
|
|
'erp_shop_id' => ['required', 'integer', 'exists:shops,id'],
|
|
|
|
|
]);
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
|
|
|
|
|
|
|
|
|
|
return response($this->res, $this->res['httpCode']);
|
|
|
|
|
}
|
|
|
|
|
$shop = new Shop();
|
|
|
|
|
$shop = $shop->find($request->get('erp_shop_id'));
|
|
|
|
|
$business = BusinessFactory::init()->make($shop->plat_id);
|
|
|
|
|
$business->setShop($shop);
|
|
|
|
|
if ('goods' === $request->get('type')) {
|
2022-08-08 10:43:59 +08:00
|
|
|
$business->bindGoods($request->get('data'));
|
2022-08-06 15:25:13 +08:00
|
|
|
}
|
|
|
|
|
if ('orders' === $request->get('type')) {
|
2022-08-11 02:13:19 +08:00
|
|
|
$business->saveOrders($request->get('data'));
|
2022-08-06 15:25:13 +08:00
|
|
|
}
|
2022-08-11 04:06:38 +08:00
|
|
|
|
|
|
|
|
return response(['Code' => 10000, 'Message' => 'SUCCESS']);
|
2022-08-06 15:25:13 +08:00
|
|
|
}
|
2022-08-12 13:30:32 +08:00
|
|
|
|
|
|
|
|
public function countOrdersNumWithSkuCode(Request $request)
|
|
|
|
|
{
|
2022-08-12 16:20:40 +08:00
|
|
|
$validator = Validator::make($request->all(), [
|
2022-08-12 13:30:32 +08:00
|
|
|
'sku_code' => ['required', 'array'],
|
2022-08-12 16:20:40 +08:00
|
|
|
]);
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
|
|
|
|
|
|
|
|
|
|
return response($this->res, $this->res['httpCode']);
|
|
|
|
|
}
|
|
|
|
|
$fields = implode(',', [
|
|
|
|
|
'shop_id',
|
|
|
|
|
'external_sku_id',
|
2022-08-18 14:57:38 +08:00
|
|
|
'sum(goods_number) as num',
|
2022-08-12 16:20:40 +08:00
|
|
|
]);
|
|
|
|
|
$res = BusinessOrderItem::query()
|
|
|
|
|
->select(DB::raw($fields))
|
2022-08-12 13:30:32 +08:00
|
|
|
->whereIn('external_sku_id', $request->get('sku_code'))
|
2022-08-12 16:20:40 +08:00
|
|
|
->groupBy(['shop_id', 'external_sku_id'])
|
2022-08-12 13:30:32 +08:00
|
|
|
->with(['shop:id,name'])
|
2022-08-12 16:20:40 +08:00
|
|
|
->get();
|
|
|
|
|
$data = [];
|
|
|
|
|
foreach ($res as $item) {
|
2022-08-18 14:57:38 +08:00
|
|
|
[$goodsCode, $skuCode] = explode('_', $item['external_sku_id']);
|
|
|
|
|
$sku = GoodsSku::query()->where('sku_code', $skuCode)
|
|
|
|
|
->whereHas('goods', function ($query) use ($goodsCode) {
|
|
|
|
|
$query->where('goods_code', $goodsCode);
|
|
|
|
|
})
|
|
|
|
|
->first();
|
|
|
|
|
if (empty($sku)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$sku = $sku->toArray();
|
|
|
|
|
if (!isset($data[$sku['id']]['total'])) {
|
|
|
|
|
$data[$sku['id']]['total'] = 0;
|
|
|
|
|
}
|
|
|
|
|
$data[$sku['id']]['total'] += $item->num;
|
|
|
|
|
$data[$sku['id']]['items'][] = [
|
2022-08-12 16:20:40 +08:00
|
|
|
'shop_name' => $item->shop->name,
|
2022-08-18 14:57:38 +08:00
|
|
|
'num' => $item->num,
|
2022-08-12 16:20:40 +08:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
2022-08-12 13:30:32 +08:00
|
|
|
}
|
2022-08-16 21:15:15 +08:00
|
|
|
|
|
|
|
|
public function downloadGoods($id, Request $request)
|
|
|
|
|
{
|
|
|
|
|
$shop = Shop::query()->find($id);
|
|
|
|
|
$business = BusinessFactory::init()->make($shop->plat_id);
|
|
|
|
|
$business->setShop($shop);
|
2023-01-29 11:29:48 +08:00
|
|
|
$res = $business->queryGroup();
|
|
|
|
|
if (isset($res['ktt_group_query_list_response'])) {
|
|
|
|
|
foreach ($res['ktt_group_query_list_response']['activity_list'] as $activity) {
|
2023-01-29 14:47:59 +08:00
|
|
|
if (0 === $activity['is_help_sell']) {
|
|
|
|
|
$business->downloadGoodsListAndBind($activity['activity_no'], $activity['title']);
|
|
|
|
|
}
|
2023-01-29 11:29:48 +08:00
|
|
|
}
|
|
|
|
|
}
|
2022-08-16 21:15:15 +08:00
|
|
|
|
|
|
|
|
return response($this->res, $this->res['httpCode']);
|
|
|
|
|
}
|
2022-08-02 11:43:46 +08:00
|
|
|
}
|