feat: #10000 团购修改

This commit is contained in:
赵世界 2022-10-25 10:47:36 +08:00
parent 203d977e71
commit f4e1b0ae22
13 changed files with 152 additions and 41 deletions

View File

@ -40,7 +40,7 @@ class KttOrderQuery extends Command
*/ */
public function handle() public function handle()
{ {
$shops = Shop::query()->where('plat_id', 1)->where('status', 1)->get(); $shops = Shop::query()->where('plat_id', Shop::$PLAT_KTT)->where('status', Shop::$STATUS_AUTHORIZED)->get();
$endTime = DateTimeUtils::getMicroTime(); $endTime = DateTimeUtils::getMicroTime();
$beginTime = $endTime - 60000; $beginTime = $endTime - 60000;
foreach ($shops as $shop) { foreach ($shops as $shop) {

View File

@ -44,7 +44,7 @@ class Swoole extends Command
public function handle() public function handle()
{ {
Timer::tick(3000, function () { Timer::tick(3000, function () {
$shops = Shop::query()->where('plat_id', 1)->where('status', 1)->get(); $shops = Shop::query()->where('plat_id', Shop::$PLAT_KTT)->where('status', Shop::$STATUS_AUTHORIZED)->get();
$endTime = DateTimeUtils::getMicroTime(); $endTime = DateTimeUtils::getMicroTime();
$beginTime = $endTime - 3000; $beginTime = $endTime - 3000;
foreach ($shops as $shop) { foreach ($shops as $shop) {

View File

@ -8,6 +8,7 @@ use App\Http\Requests\GroupsRequest;
use App\Http\Resources\GoodsSkuResource; use App\Http\Resources\GoodsSkuResource;
use App\Models\Goods; use App\Models\Goods;
use App\Models\GoodsSku; use App\Models\GoodsSku;
use App\Models\Shop;
use App\Utils\ArrayUtils; use App\Utils\ArrayUtils;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
@ -52,6 +53,11 @@ class GroupsController extends Controller
$changeData = $request->change_data; $changeData = $request->change_data;
$changeData = ArrayUtils::index($changeData, 'id'); $changeData = ArrayUtils::index($changeData, 'id');
DB::beginTransaction(); DB::beginTransaction();
$shopIds = Shop::query()
->where('plat_id', Shop::$PLAT_KTT)
->where('expires_at', '>', time())
->pluck('id')
->toArray();
try { try {
$group = new Groups(); $group = new Groups();
$group->title = $request->title; $group->title = $request->title;
@ -59,6 +65,18 @@ class GroupsController extends Controller
$group->start_time = $request->datetimerange[0]; $group->start_time = $request->datetimerange[0];
$group->end_time = $request->datetimerange[1]; $group->end_time = $request->datetimerange[1];
$group->save(); $group->save();
$shopGroups = [];
foreach ($shopIds as $shopId) {
$shopGroups[] = [
'parent_id' => $group->id,
'shop_id' => $shopId,
'title' => $group->title,
'is_save_preview' => $group->is_save_preview,
'start_time' => $group->start_time,
'end_time' => $group->end_time,
];
}
(new Groups())->batchInsert($shopGroups);
$groupGoods = []; $groupGoods = [];
foreach ($skus as $sku) { foreach ($skus as $sku) {
$price = $sku['cost'] * 100; $price = $sku['cost'] * 100;
@ -121,9 +139,17 @@ class GroupsController extends Controller
DB::beginTransaction(); DB::beginTransaction();
try { try {
$group = Groups::query()->find($id); $group = Groups::query()->find($id);
$group->title = $request->title;
$group->start_time = $request->datetimerange[0]; $group->start_time = $request->datetimerange[0];
$group->end_time = $request->datetimerange[1]; $group->end_time = $request->datetimerange[1];
$group->save(); $group->save();
DB::table('groups')
->where('parent_id', $group->id)
->update([
'title' => $group->title,
'start_time' => $group->start_time,
'end_time' => $group->end_time,
]);
GroupGoods::where('group_id', $id)->whereIn('sku_id', $deleteIds)->delete(); GroupGoods::where('group_id', $id)->whereIn('sku_id', $deleteIds)->delete();
foreach ($skus as $sku) { foreach ($skus as $sku) {
$price = $sku['cost'] * 100; $price = $sku['cost'] * 100;

View File

@ -40,13 +40,18 @@ class ShopsController extends Controller
$validator = Validator::make($request->all(), [ $validator = Validator::make($request->all(), [
'name' => 'required|string|max:191|unique:shops,name', 'name' => 'required|string|max:191|unique:shops,name',
'plat_id' => 'required|integer', 'plat_id' => 'required|integer',
'ratio' => 'required|numeric', 'ratio' => 'required',
]); ]);
if ($validator->fails()) { if ($validator->fails()) {
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages()); $this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
return response($this->res, $this->res['httpCode']); return response($this->res, $this->res['httpCode']);
} }
$operator = substr($request->ratio, 0, 1);
if (!in_array($operator, ['+', '-', '*', '/'])) {
$this->res->errorMessage = '运算符号仅允许+,-,*,/';
return response($this->res, $this->res['httpCode']);
}
$shop = new Shop(); $shop = new Shop();
$shop->name = $request->name; $shop->name = $request->name;
$shop->plat_id = $request->plat_id; $shop->plat_id = $request->plat_id;
@ -61,6 +66,11 @@ class ShopsController extends Controller
public function update(Request $request, $id) public function update(Request $request, $id)
{ {
$operator = substr($request->ratio, 0, 1);
if (!in_array($operator, ['+', '-', '*', '/'])) {
$this->res->errorMessage = '运算符号仅允许+,-,*,/';
return response($this->res, $this->res['httpCode']);
}
$shop = Shop::query()->find($id); $shop = Shop::query()->find($id);
$shop->ratio = $request->ratio; $shop->ratio = $request->ratio;
$shop->save(); $shop->save();

View File

@ -0,0 +1,42 @@
<?php
namespace App\Jobs;
use App\Models\Shop;
use App\Services\Business\BusinessFactory;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class KttQueryGroupStatus implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $groupId;
public $shopId;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($groupId, $shopId)
{
$this->groupId = $groupId;
$this->shopId = $shopId;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$shop = Shop::query()->find($this->shopId);
$client = BusinessFactory::init()->make($shop['plat_id'])->setShop($shop);
$client->queryGroupStatus($this->groupId);
}
}

View File

@ -3,12 +3,16 @@
namespace App\Listeners; namespace App\Listeners;
use App\Events\GroupSetEvent; use App\Events\GroupSetEvent;
use App\Models\Groups;
use App\Models\Shop; use App\Models\Shop;
use App\Services\Business\BusinessFactory; use App\Services\Business\BusinessFactory;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Jobs\KttQueryGroupStatus;
class GroupQueryListener class GroupQueryListener implements ShouldQueue
{ {
use InteractsWithQueue;
/** /**
* Create the event listener. * Create the event listener.
* *
@ -27,11 +31,15 @@ class GroupQueryListener
*/ */
public function handle(GroupSetEvent $event) public function handle(GroupSetEvent $event)
{ {
$shopId = Groups::query()->where('id', $event->groupId)->value('shop_id'); $shops = Shop::query()
$shop = Shop::query()->find($shopId); ->where('plat_id', Shop::$PLAT_KTT)
->where('expires_at', '>', time())
->get()
->toArray();
foreach ($shops as $shop) {
$client = BusinessFactory::init()->make($shop['plat_id'])->setShop($shop); $client = BusinessFactory::init()->make($shop['plat_id'])->setShop($shop);
$client->createGroup($event->groupId); $client->createGroup($event->groupId);
sleep(1); KttQueryGroupStatus::dispatch($event->groupId, $shop['id'])->delay(5);
$client->queryGroupStatus($event->groupId); }
} }
} }

View File

@ -29,7 +29,7 @@ class StockUpdateListener
*/ */
public function handle(StockUpdateEvent $event) public function handle(StockUpdateEvent $event)
{ {
$shops = Shop::query()->whereNotIn('status', [0, 3])->get(['id', 'plat_id']); $shops = Shop::query()->whereNotIn('status', [Shop::$STATUS_UNAUTHORIZED, Shop::$STATUS_STOP])->get(['id', 'plat_id']);
if (empty($shops)) { if (empty($shops)) {
return; return;
} }

View File

@ -49,7 +49,7 @@ class UpdateBusinessGoodsStock implements ShouldQueue
if ('下架' === $event->goodsSku->status) { if ('下架' === $event->goodsSku->status) {
return; return;
} }
$builder = Shop::query()->whereNotIn('status', [0, 3]); $builder = Shop::query()->whereNotIn('status', [Shop::$STATUS_UNAUTHORIZED, Shop::$STATUS_STOP]);
// 非订单影响库存变更,只更新本店铺下商品 // 非订单影响库存变更,只更新本店铺下商品
if (!isset($event->businessGoods['business_order_id'])) { if (!isset($event->businessGoods['business_order_id'])) {
$builder->where('id', $event->businessGoods['shop_id']); $builder->where('id', $event->businessGoods['shop_id']);

View File

@ -9,6 +9,14 @@ class Shop extends Model
{ {
use Filter; use Filter;
public static $PLAT_KTT = 1;
public static $PLAT_MX = 0;
public static $STATUS_UNAUTHORIZED = 0;
public static $STATUS_AUTHORIZED = 1;
public static $STATUS_NO_AUTHORIZED = 2;
public static $STATUS_STOP = 3;
//查询字段 //查询字段
public $fieldSearchable = [ public $fieldSearchable = [
'plat_id', 'plat_id',

View File

@ -8,26 +8,44 @@ use App\Utils\DateTimeUtils;
class Groups class Groups
{ {
public static function createGroup($localGroupId) public static function createGroup($localGroupId, $shop)
{ {
$type = 'pdd.ktt.group.create'; $type = 'pdd.ktt.group.create';
$group = GroupsModel::query()->find($localGroupId); $group = GroupsModel::query()->where('parent_id', $localGroupId)->where('shop_id', $shop->id)->first();
$groupGoods = GroupGoods::query() $groupGoods = GroupGoods::query()
->where('group_id', $group->id) ->where('group_id', $group->id)
->with(['goodsSku:id,stock']) ->with(['goodsSku:id,stock'])
->orderBy('sort') ->orderBy('sort')
->get(); ->get();
$goodsSkus = []; $goodsSkus = [];
$operator = substr($shop->ratio, 0, 1);
$ratio = (float)trim(substr($shop->ratio, 1));
foreach ($groupGoods as $item) { foreach ($groupGoods as $item) {
$priceInFen = $item['price_in_fen']; // 常规数值
switch ($operator) {
case '+':
$priceInFen += $ratio;
break;
case '-':
$priceInFen -= $ratio;
break;
case '*':
$priceInFen *= $ratio;
break;
case '/':
$priceInFen /= $ratio;
break;
}
$priceInFen *= 100;
$goodsSkus[] = [ $goodsSkus[] = [
'category_name' => $item['category_name'], 'category_name' => $item['category_name'],
'goods_desc' => $item['goods_desc'] ?: $group['title'], 'goods_desc' => ' ',
'goods_name' => $item['goods_name'], 'goods_name' => $item['goods_name'],
'limit_buy' => $item['limit_buy'], 'limit_buy' => $item['limit_buy'],
'market_price' => $item['market_price'], 'market_price' => $priceInFen,
'sku_list' => [[ 'sku_list' => [[
'external_sku_id' => $item['external_sku_id'], 'external_sku_id' => $item['external_sku_id'],
'price_in_fen' => $item['price_in_fen'] * 100, 'price_in_fen' => $priceInFen,
'quantity_type' => 0, 'quantity_type' => 0,
'spec_id_list' => [], 'spec_id_list' => [],
'total_quantity' => $item['goodsSku']['stock'], 'total_quantity' => $item['goodsSku']['stock'],
@ -40,16 +58,15 @@ class Groups
'is_save_preview' => $group['is_save_preview'], 'is_save_preview' => $group['is_save_preview'],
'start_time' => $group->getOriginal('start_time'), 'start_time' => $group->getOriginal('start_time'),
'title' => $group['title'], 'title' => $group['title'],
'isv_no' => $group['activity_no'] ?: '',
]; ];
return [$type, $appendParams]; return [$type, $appendParams];
} }
public static function queryGroupStatus($localGroupId) public static function queryGroupStatus($localGroupId, $shopId)
{ {
$type = 'pdd.ktt.group.query.status'; $type = 'pdd.ktt.group.query.status';
$group = GroupsModel::query()->find($localGroupId); $group = GroupsModel::query()->where('parent_id', $localGroupId)->where('shop_id', $shopId)->first();
$appendParams = [ $appendParams = [
'activity_no' => $group->activity_no 'activity_no' => $group->activity_no
]; ];

View File

@ -164,7 +164,7 @@ class KuaiTuanTuan extends BusinessClient
public function createGroup($localGroupId) public function createGroup($localGroupId)
{ {
[$type, $appendParams] = Groups::createGroup($localGroupId); [$type, $appendParams] = Groups::createGroup($localGroupId, $this->shop);
$res = $this->doRequest($type, $appendParams); $res = $this->doRequest($type, $appendParams);
if (isset($res['response']['success'])) { if (isset($res['response']['success'])) {
$group = GroupsModel::query()->find($localGroupId); $group = GroupsModel::query()->find($localGroupId);
@ -177,22 +177,22 @@ class KuaiTuanTuan extends BusinessClient
public function queryGroupStatus($localGroupId) public function queryGroupStatus($localGroupId)
{ {
[$type, $appendParams] = Groups::queryGroupStatus($localGroupId); [$type, $appendParams] = Groups::queryGroupStatus($localGroupId, $this->shop->id);
$res = $this->doRequest($type, $appendParams); $res = $this->doRequest($type, $appendParams);
if (isset($res['response'])) { if (isset($res['response'])) {
$group = GroupsModel::query()->find($localGroupId); $group = GroupsModel::query()->where('parent_id', $localGroupId)->where('shop_id', $this->shop->id)->first();
$group->create_status = $res['response']['status']; $group->create_status = $res['response']['status'];
if (1 === $res['response']['status']) { if (1 === $res['response']['status']) {
$group->qr_code_url = $res['response']['qr_code_url']; $group->qr_code_url = $res['response']['qr_code_url'];
foreach ($res['response']['goods_list'] as $goods) { // foreach ($res['response']['goods_list'] as $goods) {
$groupGoods = GroupGoods::query() // $groupGoods = GroupGoods::query()
->where('group_id', $localGroupId) // ->where('group_id', $localGroupId)
->where('external_sku_id', $goods['sku_list'][0]['external_sku_id']) // ->where('external_sku_id', $goods['sku_list'][0]['external_sku_id'])
->first(); // ->first();
$groupGoods->ktt_goods_id = $goods['goods_id']; // $groupGoods->ktt_goods_id = $goods['goods_id'];
$groupGoods->ktt_sku_id = $goods['sku_list'][0]['sku_id']; // $groupGoods->ktt_sku_id = $goods['sku_list'][0]['sku_id'];
$groupGoods->save(); // $groupGoods->save();
} // }
} }
if (2 === $res['response']['status']) { if (2 === $res['response']['status']) {
$group->error_msg = $res['response']['error_msg']; $group->error_msg = $res['response']['error_msg'];

View File

@ -31,12 +31,12 @@
style="width: 100%" height="800" :row-key="getRowKeys"> style="width: 100%" height="800" :row-key="getRowKeys">
<el-table-column type="selection" :reserve-selection="true" width="55"> <el-table-column type="selection" :reserve-selection="true" width="55">
</el-table-column> </el-table-column>
<el-table-column label="排序"> <!-- <el-table-column label="排序">
<template slot-scope="scope"> <template slot-scope="scope">
<el-input v-model="scope.row.sort" placeholder="排序" <el-input v-model="scope.row.sort" placeholder="排序"
@change="handleCellChange(scope.row)"></el-input> @change="handleCellChange(scope.row)"></el-input>
</template> </template>
</el-table-column> </el-table-column> -->
<el-table-column prop="goods_name" label=" 商品名称"> <el-table-column prop="goods_name" label=" 商品名称">
</el-table-column> </el-table-column>
<el-table-column prop="external_sku_id" label="编码"> <el-table-column prop="external_sku_id" label="编码">
@ -271,7 +271,7 @@ export default {
this.groupGoods = res.data; this.groupGoods = res.data;
this.groupGoods.data.forEach((sku, index) => { this.groupGoods.data.forEach((sku, index) => {
if (undefined !== this.changeData[sku.id]) { if (undefined !== this.changeData[sku.id]) {
this.groupGoods.data[index].sort = this.changeData[sku.id].sort; // this.groupGoods.data[index].sort = this.changeData[sku.id].sort;
this.groupGoods.data[index].limit_buy = this.changeData[sku.id].limit_buy; this.groupGoods.data[index].limit_buy = this.changeData[sku.id].limit_buy;
this.groupGoods.data[index].price_in_fen = this.changeData[sku.id].price_in_fen; this.groupGoods.data[index].price_in_fen = this.changeData[sku.id].price_in_fen;
} }
@ -316,7 +316,7 @@ export default {
handleCellChange(row) { handleCellChange(row) {
this.changeData[row.id] = { this.changeData[row.id] = {
id: row.id, id: row.id,
sort: row.sort, // sort: row.sort,
limit_buy: row.limit_buy, limit_buy: row.limit_buy,
price_in_fen: row.price_in_fen, price_in_fen: row.price_in_fen,
}; };

View File

@ -31,12 +31,12 @@
style="width: 100%" height="800" :row-key="getRowKeys"> style="width: 100%" height="800" :row-key="getRowKeys">
<el-table-column type="selection" :reserve-selection="true" width="55"> <el-table-column type="selection" :reserve-selection="true" width="55">
</el-table-column> </el-table-column>
<el-table-column label="排序"> <!-- <el-table-column label="排序">
<template slot-scope="scope"> <template slot-scope="scope">
<el-input v-model="scope.row.sort" placeholder="排序" <el-input v-model="scope.row.sort" placeholder="排序"
@change="handleCellChange(scope.row)"></el-input> @change="handleCellChange(scope.row)"></el-input>
</template> </template>
</el-table-column> </el-table-column> -->
<el-table-column prop="goods_name" label=" 商品名称"> <el-table-column prop="goods_name" label=" 商品名称">
</el-table-column> </el-table-column>
<el-table-column prop="external_sku_id" label="编码"> <el-table-column prop="external_sku_id" label="编码">
@ -274,7 +274,7 @@ export default {
this.groupGoods = res.data; this.groupGoods = res.data;
this.groupGoods.data.forEach((sku, index) => { this.groupGoods.data.forEach((sku, index) => {
if (undefined !== this.changeData[sku.id]) { if (undefined !== this.changeData[sku.id]) {
this.groupGoods.data[index].sort = this.changeData[sku.id].sort; // this.groupGoods.data[index].sort = this.changeData[sku.id].sort;
this.groupGoods.data[index].limit_buy = this.changeData[sku.id].limit_buy; this.groupGoods.data[index].limit_buy = this.changeData[sku.id].limit_buy;
this.groupGoods.data[index].price_in_fen = this.changeData[sku.id].price_in_fen; this.groupGoods.data[index].price_in_fen = this.changeData[sku.id].price_in_fen;
} }
@ -327,7 +327,7 @@ export default {
handleCellChange(row) { handleCellChange(row) {
this.changeData[row.id] = { this.changeData[row.id] = {
id: row.id, id: row.id,
sort: row.sort, // sort: row.sort,
limit_buy: row.limit_buy, limit_buy: row.limit_buy,
price_in_fen: row.price_in_fen, price_in_fen: row.price_in_fen,
}; };