diff --git a/app/common/dao/store/order/StoreCartDao.php b/app/common/dao/store/order/StoreCartDao.php index 581cd869..d4a1dc5c 100644 --- a/app/common/dao/store/order/StoreCartDao.php +++ b/app/common/dao/store/order/StoreCartDao.php @@ -29,7 +29,8 @@ class StoreCartDao extends BaseDao const SOURCE_STORE_CLOUD = 101; //店铺内云商品 const SOURCE_CLOUD = 102; //云仓内店铺商品 - const SOURCE_COMMUNITY_RESALE = 201; //转售商品 + const SOURCE_COMMUNITY_RESALE = 201; //转售商品 + const SOURCE_COMMUNITY_ENTRUST = 202; //委托商品 protected function getModel(): string { diff --git a/app/common/model/community/Community.php b/app/common/model/community/Community.php index fa77d9b2..066b0672 100644 --- a/app/common/model/community/Community.php +++ b/app/common/model/community/Community.php @@ -16,6 +16,7 @@ namespace app\common\model\community; use app\common\model\BaseModel; use app\common\model\store\product\Spu; use app\common\model\store\Resale; +use app\common\model\store\Entrust; use app\common\model\system\Relevance; use app\common\model\user\User; use app\common\repositories\system\RelevanceRepository; @@ -177,4 +178,9 @@ class Community extends BaseModel return $this->hasMany(Resale::class, 'community_id','community_id'); } + public function entrust() + { + return $this->hasMany(Entrust::class, 'community_id','community_id'); + } + } diff --git a/app/common/repositories/community/CommunityRepository.php b/app/common/repositories/community/CommunityRepository.php index 634c17c5..13a1f64d 100644 --- a/app/common/repositories/community/CommunityRepository.php +++ b/app/common/repositories/community/CommunityRepository.php @@ -768,6 +768,55 @@ class CommunityRepository extends BaseRepository } } + /** + * 委托商品加入购物车 + * @param $uid + * @param $id + * @return array + */ + public function addEntrustCart($uid, $id) + { + $where = [ + $this->dao->getPk() => $id, + 'is_del' => 0 + ]; + $community = $this->dao->getSearch($where)->with('entrust')->find()->toArray(); + if (empty($community) || $community['is_sale'] != 0) { + throw new ValidateException('委托数据不存在或已售出'); + } + if (empty($community['entrust'])) { + throw new ValidateException('委托数据不存在'); + } + StoreCart::startTrans(); + try { + /** @var StoreCartRepository $cartRepo */ + $cartRepo = app()->make(StoreCartRepository::class); + $cartIds = []; + foreach ($community['entrust'] as $item) { + $data = [ + 'uid' => $uid, + 'mer_id' => $item['mer_id'], + 'product_type' => 99, + 'product_id' => $item['product_id'], + 'product_attr_unique' => $item['product_attr_unique'], + 'cart_num' => $item['number'], + 'source' => StoreCartDao::SOURCE_COMMUNITY_ENTRUST, + 'source_id' => $community['community_id'], + ]; + $cart = $cartRepo->create($data); + $cartIds[] = $cart['cart_id']; + } + if (empty($cartIds)) { + throw new ValidateException('加入购物车出错'); + } + StoreCart::commit(); + return $cartIds; + } catch (\Exception $exception) { + StoreCart::rollback(); + throw new ValidateException('下单出错'); + } + } + public function saleOrCancel($id, $status = 1) { $where = [ diff --git a/app/controller/api/community/Community.php b/app/controller/api/community/Community.php index 8e91b7e8..9c339f94 100644 --- a/app/controller/api/community/Community.php +++ b/app/controller/api/community/Community.php @@ -548,7 +548,10 @@ class Community extends BaseController return app('json')->fail('请设置审核状态'); } if ($status == 1) { - Db::name('community')->where('uid', $this->request->uid())->where('community_id', $id)->where('is_del', 0)->update(['status' => $status, 'mer_status' => 1]); + $res = Db::name('community')->where('uid', $this->request->uid())->where('community_id', $id)->where('is_del', 0)->update(['status' => $status, 'mer_status' => 1]); + if (!$res) { + return app('json')->fail('审核失败'); + } } if ($status == 2) { Db::startTrans(); @@ -654,7 +657,10 @@ class Community extends BaseController } // 同意 if ($status == 1) { - Db::name('community')->where('community_id', $id)->where('is_del', 0)->update(['status' => $status, 'mer_status' => 1]); + $res = Db::name('community')->where('community_id', $id)->where('is_del', 0)->update(['status' => $status, 'mer_status' => 1]); + if (!$res) { + return app('json')->fail('审核失败'); + } } // 拒绝 if ($status == 2) {