diff --git a/app/Console/Commands/KttOrderQuery.php b/app/Console/Commands/KttOrderQuery.php index 76bbf82..9ca4f0a 100644 --- a/app/Console/Commands/KttOrderQuery.php +++ b/app/Console/Commands/KttOrderQuery.php @@ -40,7 +40,7 @@ class KttOrderQuery extends Command */ 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(); $beginTime = $endTime - 60000; foreach ($shops as $shop) { diff --git a/app/Console/Commands/Swoole.php b/app/Console/Commands/Swoole.php index 136b5f5..3d51945 100644 --- a/app/Console/Commands/Swoole.php +++ b/app/Console/Commands/Swoole.php @@ -44,7 +44,7 @@ class Swoole extends Command public function handle() { 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(); $beginTime = $endTime - 3000; foreach ($shops as $shop) { diff --git a/app/Http/Controllers/Group/GroupsController.php b/app/Http/Controllers/Group/GroupsController.php index a342d0b..ca9bf84 100644 --- a/app/Http/Controllers/Group/GroupsController.php +++ b/app/Http/Controllers/Group/GroupsController.php @@ -8,6 +8,7 @@ use App\Http\Requests\GroupsRequest; use App\Http\Resources\GoodsSkuResource; use App\Models\Goods; use App\Models\GoodsSku; +use App\Models\Shop; use App\Utils\ArrayUtils; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; @@ -52,6 +53,11 @@ class GroupsController extends Controller $changeData = $request->change_data; $changeData = ArrayUtils::index($changeData, 'id'); DB::beginTransaction(); + $shopIds = Shop::query() + ->where('plat_id', Shop::$PLAT_KTT) + ->where('expires_at', '>', time()) + ->pluck('id') + ->toArray(); try { $group = new Groups(); $group->title = $request->title; @@ -59,6 +65,18 @@ class GroupsController extends Controller $group->start_time = $request->datetimerange[0]; $group->end_time = $request->datetimerange[1]; $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 = []; foreach ($skus as $sku) { $price = $sku['cost'] * 100; @@ -121,9 +139,17 @@ class GroupsController extends Controller DB::beginTransaction(); try { $group = Groups::query()->find($id); + $group->title = $request->title; $group->start_time = $request->datetimerange[0]; $group->end_time = $request->datetimerange[1]; $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(); foreach ($skus as $sku) { $price = $sku['cost'] * 100; diff --git a/app/Http/Controllers/Shop/ShopsController.php b/app/Http/Controllers/Shop/ShopsController.php index 34678f6..527eca4 100644 --- a/app/Http/Controllers/Shop/ShopsController.php +++ b/app/Http/Controllers/Shop/ShopsController.php @@ -40,13 +40,18 @@ class ShopsController extends Controller $validator = Validator::make($request->all(), [ 'name' => 'required|string|max:191|unique:shops,name', 'plat_id' => 'required|integer', - 'ratio' => 'required|numeric', + 'ratio' => 'required', ]); if ($validator->fails()) { $this->setValidatorFailResponse($validator->getMessageBag()->getMessages()); 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->name = $request->name; $shop->plat_id = $request->plat_id; @@ -61,6 +66,11 @@ class ShopsController extends Controller 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->ratio = $request->ratio; $shop->save(); diff --git a/app/Jobs/KttQueryGroupStatus.php b/app/Jobs/KttQueryGroupStatus.php new file mode 100644 index 0000000..4e91666 --- /dev/null +++ b/app/Jobs/KttQueryGroupStatus.php @@ -0,0 +1,42 @@ +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); + } +} diff --git a/app/Listeners/GroupQueryListener.php b/app/Listeners/GroupQueryListener.php index 58a86d3..2b88629 100644 --- a/app/Listeners/GroupQueryListener.php +++ b/app/Listeners/GroupQueryListener.php @@ -3,12 +3,16 @@ namespace App\Listeners; use App\Events\GroupSetEvent; -use App\Models\Groups; use App\Models\Shop; 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. * @@ -27,11 +31,15 @@ class GroupQueryListener */ public function handle(GroupSetEvent $event) { - $shopId = Groups::query()->where('id', $event->groupId)->value('shop_id'); - $shop = Shop::query()->find($shopId); - $client = BusinessFactory::init()->make($shop['plat_id'])->setShop($shop); - $client->createGroup($event->groupId); - sleep(1); - $client->queryGroupStatus($event->groupId); + $shops = Shop::query() + ->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->createGroup($event->groupId); + KttQueryGroupStatus::dispatch($event->groupId, $shop['id'])->delay(5); + } } } diff --git a/app/Listeners/StockUpdateListener.php b/app/Listeners/StockUpdateListener.php index eeb6920..6ae9761 100644 --- a/app/Listeners/StockUpdateListener.php +++ b/app/Listeners/StockUpdateListener.php @@ -29,7 +29,7 @@ class StockUpdateListener */ 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)) { return; } diff --git a/app/Listeners/UpdateBusinessGoodsStock.php b/app/Listeners/UpdateBusinessGoodsStock.php index 6b2b8a9..3920617 100644 --- a/app/Listeners/UpdateBusinessGoodsStock.php +++ b/app/Listeners/UpdateBusinessGoodsStock.php @@ -49,7 +49,7 @@ class UpdateBusinessGoodsStock implements ShouldQueue if ('下架' === $event->goodsSku->status) { 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'])) { $builder->where('id', $event->businessGoods['shop_id']); diff --git a/app/Models/Shop.php b/app/Models/Shop.php index 4a914fa..ca4ab91 100644 --- a/app/Models/Shop.php +++ b/app/Models/Shop.php @@ -9,6 +9,14 @@ class Shop extends Model { 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 = [ 'plat_id', diff --git a/app/Services/Business/KuaiTuanTuan/Groups.php b/app/Services/Business/KuaiTuanTuan/Groups.php index 7fa265a..bf172a8 100644 --- a/app/Services/Business/KuaiTuanTuan/Groups.php +++ b/app/Services/Business/KuaiTuanTuan/Groups.php @@ -8,26 +8,44 @@ use App\Utils\DateTimeUtils; class Groups { - public static function createGroup($localGroupId) + public static function createGroup($localGroupId, $shop) { $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() ->where('group_id', $group->id) ->with(['goodsSku:id,stock']) ->orderBy('sort') ->get(); $goodsSkus = []; + $operator = substr($shop->ratio, 0, 1); + $ratio = (float)trim(substr($shop->ratio, 1)); 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[] = [ 'category_name' => $item['category_name'], - 'goods_desc' => $item['goods_desc'] ?: $group['title'], + 'goods_desc' => ' ', 'goods_name' => $item['goods_name'], 'limit_buy' => $item['limit_buy'], - 'market_price' => $item['market_price'], + 'market_price' => $priceInFen, 'sku_list' => [[ 'external_sku_id' => $item['external_sku_id'], - 'price_in_fen' => $item['price_in_fen'] * 100, + 'price_in_fen' => $priceInFen, 'quantity_type' => 0, 'spec_id_list' => [], 'total_quantity' => $item['goodsSku']['stock'], @@ -40,16 +58,15 @@ class Groups 'is_save_preview' => $group['is_save_preview'], 'start_time' => $group->getOriginal('start_time'), 'title' => $group['title'], - 'isv_no' => $group['activity_no'] ?: '', ]; return [$type, $appendParams]; } - public static function queryGroupStatus($localGroupId) + public static function queryGroupStatus($localGroupId, $shopId) { $type = 'pdd.ktt.group.query.status'; - $group = GroupsModel::query()->find($localGroupId); + $group = GroupsModel::query()->where('parent_id', $localGroupId)->where('shop_id', $shopId)->first(); $appendParams = [ 'activity_no' => $group->activity_no ]; diff --git a/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php b/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php index 4a828f4..7afd6bf 100644 --- a/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php +++ b/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php @@ -164,7 +164,7 @@ class KuaiTuanTuan extends BusinessClient public function createGroup($localGroupId) { - [$type, $appendParams] = Groups::createGroup($localGroupId); + [$type, $appendParams] = Groups::createGroup($localGroupId, $this->shop); $res = $this->doRequest($type, $appendParams); if (isset($res['response']['success'])) { $group = GroupsModel::query()->find($localGroupId); @@ -177,22 +177,22 @@ class KuaiTuanTuan extends BusinessClient public function queryGroupStatus($localGroupId) { - [$type, $appendParams] = Groups::queryGroupStatus($localGroupId); + [$type, $appendParams] = Groups::queryGroupStatus($localGroupId, $this->shop->id); $res = $this->doRequest($type, $appendParams); 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']; if (1 === $res['response']['status']) { $group->qr_code_url = $res['response']['qr_code_url']; - foreach ($res['response']['goods_list'] as $goods) { - $groupGoods = GroupGoods::query() - ->where('group_id', $localGroupId) - ->where('external_sku_id', $goods['sku_list'][0]['external_sku_id']) - ->first(); - $groupGoods->ktt_goods_id = $goods['goods_id']; - $groupGoods->ktt_sku_id = $goods['sku_list'][0]['sku_id']; - $groupGoods->save(); - } +// foreach ($res['response']['goods_list'] as $goods) { +// $groupGoods = GroupGoods::query() +// ->where('group_id', $localGroupId) +// ->where('external_sku_id', $goods['sku_list'][0]['external_sku_id']) +// ->first(); +// $groupGoods->ktt_goods_id = $goods['goods_id']; +// $groupGoods->ktt_sku_id = $goods['sku_list'][0]['sku_id']; +// $groupGoods->save(); +// } } if (2 === $res['response']['status']) { $group->error_msg = $res['response']['error_msg']; diff --git a/resources/frontend/src/views/group/addGroup.vue b/resources/frontend/src/views/group/addGroup.vue index e460924..1584df1 100644 --- a/resources/frontend/src/views/group/addGroup.vue +++ b/resources/frontend/src/views/group/addGroup.vue @@ -31,12 +31,12 @@ style="width: 100%" height="800" :row-key="getRowKeys"> - + @@ -271,7 +271,7 @@ export default { this.groupGoods = res.data; this.groupGoods.data.forEach((sku, index) => { 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].price_in_fen = this.changeData[sku.id].price_in_fen; } @@ -316,7 +316,7 @@ export default { handleCellChange(row) { this.changeData[row.id] = { id: row.id, - sort: row.sort, + // sort: row.sort, limit_buy: row.limit_buy, price_in_fen: row.price_in_fen, }; diff --git a/resources/frontend/src/views/group/editGroup.vue b/resources/frontend/src/views/group/editGroup.vue index 9857269..d8d6a5f 100644 --- a/resources/frontend/src/views/group/editGroup.vue +++ b/resources/frontend/src/views/group/editGroup.vue @@ -31,12 +31,12 @@ style="width: 100%" height="800" :row-key="getRowKeys"> - + @@ -274,7 +274,7 @@ export default { this.groupGoods = res.data; this.groupGoods.data.forEach((sku, index) => { 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].price_in_fen = this.changeData[sku.id].price_in_fen; } @@ -327,7 +327,7 @@ export default { handleCellChange(row) { this.changeData[row.id] = { id: row.id, - sort: row.sort, + // sort: row.sort, limit_buy: row.limit_buy, price_in_fen: row.price_in_fen, };