From 3e8a8751facaefd49e4373eaf4dbf344e0d8d88b Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Mon, 26 Jun 2023 14:48:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E4=B8=B4=E6=9C=9F?= =?UTF-8?q?=E6=89=93=E6=8A=98=E3=80=81=E5=A7=94=E6=89=98=E9=94=80=E5=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/store/activity/BusinessActivity.php | 38 +++++++++ .../store/activity/ActivityRepository.php | 34 ++++++++ .../repositories/store/activity/Discount.php | 34 ++++++++ .../repositories/store/activity/Entrust.php | 68 ++++++++++++++++ app/controller/api/server/Activity.php | 78 +++++++++++++++++++ route/api.php | 8 +- 6 files changed, 258 insertions(+), 2 deletions(-) create mode 100644 app/common/model/store/activity/BusinessActivity.php create mode 100644 app/common/repositories/store/activity/ActivityRepository.php create mode 100644 app/common/repositories/store/activity/Discount.php create mode 100644 app/common/repositories/store/activity/Entrust.php create mode 100644 app/controller/api/server/Activity.php diff --git a/app/common/model/store/activity/BusinessActivity.php b/app/common/model/store/activity/BusinessActivity.php new file mode 100644 index 00000000..7ce0d1b5 --- /dev/null +++ b/app/common/model/store/activity/BusinessActivity.php @@ -0,0 +1,38 @@ +type == self::TYPE_ENTRUST; + } + + public function isDiscount() { + return $this->type == self::TYPE_DISCOUNT; + } + +} diff --git a/app/common/repositories/store/activity/ActivityRepository.php b/app/common/repositories/store/activity/ActivityRepository.php new file mode 100644 index 00000000..a021dd1a --- /dev/null +++ b/app/common/repositories/store/activity/ActivityRepository.php @@ -0,0 +1,34 @@ + 'entrust', + BusinessActivity::TYPE_DISCOUNT => 'discount', + ]; + + /** + * @param $type + * @return ActivityRepository + * @throws \Exception + */ + public static function instance($type) + { + $type = self::TYPE_MAP[$type]; + $class = 'app\common\repositories\store\activity\\' . ucfirst($type); + if (!class_exists($class)) { + throw new \Exception('请求类型错误'); + } + return new $class(); + } + + abstract public function save($param); + + abstract public function status($id, $status); + +} \ No newline at end of file diff --git a/app/common/repositories/store/activity/Discount.php b/app/common/repositories/store/activity/Discount.php new file mode 100644 index 00000000..d54b103b --- /dev/null +++ b/app/common/repositories/store/activity/Discount.php @@ -0,0 +1,34 @@ +make(ProductRepository::class); + $product = $productRepo->get($param['product_id']); + if (empty($product)) { + throw new \Exception('商品不存在'); + } + $data = [ + 'type' => BusinessActivity::TYPE_DISCOUNT, + 'product_id' => $param['product_id'], + 'to_mer_id' => 0, + 'mer_id' => $param['mer_id'], + 'price' => $param['price'], + 'number' => $param['number'], + 'other' => json_encode($param['other'] ?? []), + 'update_time' => date('Y-m-d H:i:s'), + ]; + if (!BusinessActivity::create($data)) { + throw new \Exception('保存失败'); + } + } + +} \ No newline at end of file diff --git a/app/common/repositories/store/activity/Entrust.php b/app/common/repositories/store/activity/Entrust.php new file mode 100644 index 00000000..63af019a --- /dev/null +++ b/app/common/repositories/store/activity/Entrust.php @@ -0,0 +1,68 @@ +make(ProductRepository::class); + $product = $productRepo->get($param['product_id']); + if (empty($product)) { + throw new \Exception('商品不存在'); + } + /** @var MerchantDao $merchantDao */ + $merchantDao = app()->make(MerchantDao::class); + $supplier = $merchantDao->apiGetOne($param['to_mer_id']); + if (empty($supplier) || $supplier['type_id'] != Merchant::TypeSupplyChain) { + throw new \Exception('供应商不存在'); + } + if ($product->mer_id == $param['to_mer_id']) { + throw new \Exception('不能委托给自己'); + } + $data = [ + 'type' => BusinessActivity::TYPE_ENTRUST, + 'product_id' => $param['product_id'], + 'to_mer_id' => $param['to_mer_id'], + 'mer_id' => $product['mer_id'], + 'price' => $param['price'], + 'number' => $param['number'], + 'other' => json_encode($param['other'] ?? []), + 'update_time' => date('Y-m-d H:i:s'), + ]; + if (!BusinessActivity::create($data)) { + throw new \Exception('保存失败'); + } + } + + public function status($id, $status) + { + Db::startTrans(); + try { + $data = BusinessActivity::getInstance()->find($id); + if (empty($data)) { + throw new \Exception('数据不存在'); + } + if ($data['status'] != BusinessActivity::STATUS_WAIT) { + throw new \Exception('当前状态不支持操作'); + } + $data->status = $status; + if (!$data->save()) { + throw new \Exception('操作失败'); + } + Db::commit(); + } catch (\Exception $e) { + Db::rollback(); + throw new \Exception($e->getMessage()); + } + } + +} \ No newline at end of file diff --git a/app/controller/api/server/Activity.php b/app/controller/api/server/Activity.php new file mode 100644 index 00000000..05d6ac3f --- /dev/null +++ b/app/controller/api/server/Activity.php @@ -0,0 +1,78 @@ +request->param(); + $param['mer_id'] = $param['merId']; + $instance = ActivityRepository::instance($param['type']); + $instance->save($param); + return app('json')->success('提交成功'); + } + + /** + * 接受活动申请 + * @return mixed + * @throws \Exception + */ + public function accept() + { + $param = $this->request->param(); + $instance = ActivityRepository::instance($param['type']); + $instance->status($param['id'], BusinessActivity::STATUS_ACCEPT); + return app('json')->success('操作成功'); + } + + /** + * 拒绝活动申请 + * @return mixed + * @throws \Exception + */ + public function refuse() + { + $param = $this->request->param(); + $instance = ActivityRepository::instance($param['type']); + $instance->status($param['id'], BusinessActivity::STATUS_REFUSE); + return app('json')->success('操作成功'); + } + + /** + * 确认活动申请 + * @return mixed + * @throws \Exception + */ + public function confirm() + { + $param = $this->request->param(); + $instance = ActivityRepository::instance($param['type']); + $instance->status($param['id'], BusinessActivity::STATUS_CONFIRM); + return app('json')->success('操作成功'); + } + + /** + * 拒绝确认活动申请 + * @return mixed + * @throws \Exception + */ + public function refuseConfirm() + { + $param = $this->request->param(); + $instance = ActivityRepository::instance($param['type']); + $instance->status($param['id'], BusinessActivity::STATUS_REFUSE_CONFIRM); + return app('json')->success('操作成功'); + } + +} \ No newline at end of file diff --git a/route/api.php b/route/api.php index 6dabf1d3..101083c3 100644 --- a/route/api.php +++ b/route/api.php @@ -287,8 +287,12 @@ Route::group('api/', function () { Route::get('attr/detail/:id', 'StoreProductAttrTemplate/detail'); Route::get('attr/list', 'StoreProductAttrTemplate/getlist'); - - + //活动 + Route::post('activity/create', 'Activity/create'); + Route::post('activity/accept', 'Activity/accept'); + Route::post('activity/refuse', 'Activity/refuse'); + Route::post('activity/confirm', 'Activity/confirm'); + Route::post('activity/refuseConfirm', 'Activity/refuseConfirm'); })->prefix('api.server.')->middleware(\app\common\middleware\MerchantServerMiddleware::class,1); //管理员订单