From 16909c252d867049fac5de8db3a4ed4425e39dca Mon Sep 17 00:00:00 2001 From: yaooo <272523191@qq.com> Date: Tue, 22 Aug 2023 16:58:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=A7=94=E6=89=98=E5=95=86?= =?UTF-8?q?=E5=93=81=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../community/CommunityRepository.php | 62 +++++++++++++++++++ app/controller/api/community/Community.php | 13 ++++ route/api.php | 2 + 3 files changed, 77 insertions(+) diff --git a/app/common/repositories/community/CommunityRepository.php b/app/common/repositories/community/CommunityRepository.php index 2c074e33..6aea1eda 100644 --- a/app/common/repositories/community/CommunityRepository.php +++ b/app/common/repositories/community/CommunityRepository.php @@ -19,6 +19,7 @@ use app\common\model\store\order\StoreCart; use app\common\model\store\product\ProductAttrValue; use app\common\model\store\product\PurchaseRecord; use app\common\model\store\Resale; +use app\common\model\store\Entrust; use app\common\repositories\BaseRepository; use app\common\repositories\store\order\StoreCartRepository; use app\common\repositories\store\order\StoreOrderProductRepository; @@ -375,6 +376,9 @@ class CommunityRepository extends BaseRepository if ($data['product_info'] && $data['is_type'] == self::COMMUNITY_TYPE_RESALE) { $this->resale($community->community_id, $data['product_info'], $data['resale_type'] ?? 0); } + if ($data['product_info'] && $data['is_type'] == self::COMMUNITY_TYPE_ENTRUST) { + $this->entrust($community->community_id, $data['product_info'], $data['entrust_mer_id'] ?? 0, $data['entrust_day'] ?? 0); + } event('community.create',compact('community')); return $community->community_id; }); @@ -406,6 +410,9 @@ class CommunityRepository extends BaseRepository if ($productInfo && $data['is_type'] == self::COMMUNITY_TYPE_RESALE) { $this->resale($id, $productInfo, $data['resale_type'] ?? 0); } + if ($productInfo && $data['is_type'] == self::COMMUNITY_TYPE_ENTRUST) { + $this->entrust($community->community_id, $data['product_info'], $data['entrust_mer_id'] ?? 0, $data['entrust_day'] ?? 0); + } event('community.update.before',compact('id','community')); }); } @@ -644,6 +651,61 @@ class CommunityRepository extends BaseRepository } } + /** + * 委托贴 + * @param $id + * @param array $data + * @return void + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function entrust($id, array $data, $entrustMerId, $entrustDay) + { + + $insert = []; + foreach ($data as $value) { + if (isset($value['id'])) { + $entrustInfo = Entrust::find($value['id']); + unset($value['product_attr_unique']); + $entrustInfo->update($value); + continue; + } + $purchaseRecord = PurchaseRecord::where('unique', $value['product_attr_unique'])->find(); + $totalNumber = PurchaseRecord::where('unique', $value['product_attr_unique'])->sum('number'); + $totalSalesVolume = PurchaseRecord::where('unique', $value['product_attr_unique'])->sum('sales_volume'); + if (empty($purchaseRecord) || ($totalNumber - $totalSalesVolume) <= 0) { + throw new ValidateException('进货记录不存在或已售罄'); + } + if (($totalNumber - $totalSalesVolume) < $value['number']) { + throw new ValidateException('库存不足'); + } + + if ($value) { + $insert[] = [ + 'community_id' => $id, + 'purchase_record_id' => $purchaseRecord['id'], + 'product_id' => $purchaseRecord['product_id'], + 'product_attr_unique' => $purchaseRecord['unique'], + 'mer_id' => $purchaseRecord['mer_id'], + 'entrust_mer_id' => $entrustMerId, + 'entrust_day' => $entrustDay, + 'number' => $value['number'], + 'price' => $value['price'], + 'update_time' => date('Y-m-d H:i:s'), + ]; + } + $purchaseRecord->product->stock -= $value['number']; + $purchaseRecord->product->save(); + $attrValue = ProductAttrValue::where('product_id', $purchaseRecord['product_id'])->where('unique', $purchaseRecord['unique'])->find(); + $attrValue->stock -= $value['number']; + $attrValue->save(); + } + if ($insert) { + Entrust::getInstance()->insertAll($insert); + } + } + /** * 转售加入购物车 * @param $uid diff --git a/app/controller/api/community/Community.php b/app/controller/api/community/Community.php index b1a24f07..721089d3 100644 --- a/app/controller/api/community/Community.php +++ b/app/controller/api/community/Community.php @@ -613,4 +613,17 @@ class Community extends BaseController $res = $this->repository->create($data); return app('json')->success(['community_id' => $res]); } + + /** + * 发布委托商品 + * @return \think\response\Json + */ + public function entrust() + { + $data = $this->checkParams(); + $this->checkUserAuth(); + $data['uid'] = $this->request->uid(); + $res = $this->repository->create($data); + return app('json')->success(['community_id' => $res]); + } } diff --git a/route/api.php b/route/api.php index af608aa3..17fe0b26 100644 --- a/route/api.php +++ b/route/api.php @@ -372,6 +372,8 @@ Route::group('api/', function () { Route::post('resale/delete/:id', 'Community/deleteResale'); Route::post('resale/check/:id', 'Community/checkResale'); + Route::post('/entrust', 'Community/entrust'); + })->prefix('api.community.'); Route::group('svip', function () {