diff --git a/app/common/dao/store/order/StoreCartDao.php b/app/common/dao/store/order/StoreCartDao.php index 5aa780f8..581cd869 100644 --- a/app/common/dao/store/order/StoreCartDao.php +++ b/app/common/dao/store/order/StoreCartDao.php @@ -29,6 +29,7 @@ class StoreCartDao extends BaseDao const SOURCE_STORE_CLOUD = 101; //店铺内云商品 const SOURCE_CLOUD = 102; //云仓内店铺商品 + const SOURCE_COMMUNITY_RESALE = 201; //转售商品 protected function getModel(): string { diff --git a/app/common/dao/store/product/ProductAttrValueDao.php b/app/common/dao/store/product/ProductAttrValueDao.php index 3b0b29a0..531fe78b 100644 --- a/app/common/dao/store/product/ProductAttrValueDao.php +++ b/app/common/dao/store/product/ProductAttrValueDao.php @@ -155,6 +155,18 @@ class ProductAttrValueDao extends BaseDao ]); } + public function incSales(int $productId, string $unique, int $number) + { + return model::getDB()->where('product_id', $productId)->where('unique', $unique)->update([ + 'sales' => Db::raw('sales+' . $number) + ]); + } + + public function descSales(int $productId, string $unique, int $number) + { + model::getDB()->where('product_id', $productId)->where('unique', $unique)->where('sales', '>=', $number)->dec('sales', $number)->update(); + } + /** * @param int $productId * @param string $sku diff --git a/app/common/dao/store/product/ProductDao.php b/app/common/dao/store/product/ProductDao.php index 7c24138f..45a3a944 100644 --- a/app/common/dao/store/product/ProductDao.php +++ b/app/common/dao/store/product/ProductDao.php @@ -318,6 +318,18 @@ class ProductDao extends BaseDao ]); } + public function incSales(int $productId, int $number) + { + return model::getDB()->where('product_id', $productId)->update([ + 'sales' => Db::raw('sales+' . $number) + ]); + } + + public function descSales(int $productId, int $number) + { + model::getDB()->where('product_id', $productId)->where('sales', '>=', $number)->dec('sales', $number)->update(); + } + /** * @param int $productId * @param int $inc diff --git a/app/common/repositories/community/CommunityRepository.php b/app/common/repositories/community/CommunityRepository.php index a8f7707a..a2880fa2 100644 --- a/app/common/repositories/community/CommunityRepository.php +++ b/app/common/repositories/community/CommunityRepository.php @@ -13,10 +13,13 @@ namespace app\common\repositories\community; use app\common\dao\community\CommunityDao; +use app\common\dao\store\order\StoreCartDao; +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\repositories\BaseRepository; +use app\common\repositories\store\order\StoreCartRepository; use app\common\repositories\store\order\StoreOrderProductRepository; use app\common\repositories\store\product\SpuRepository; use app\common\repositories\system\RelevanceRepository; @@ -96,7 +99,9 @@ class CommunityRepository extends BaseRepository if (!isset($where['is_type']) && $config) $where['is_type'] = $config; $where['is_del'] = 0; - $query = $this->dao->search($where)->order('start DESC,Community.create_time DESC,community_id DESC'); + $query = $this->dao->search($where)->when(in_array(self::COMMUNITY_TYPE_RESALE, explode(',', $where['is_type'])), function ($query) { + $query->where('is_sale', 0); + })->order('start DESC,Community.create_time DESC,community_id DESC'); $query->with([ 'author' => function($query) use($userInfo){ $query->field('uid,real_name,status,avatar,nickname,count_start'); @@ -592,5 +597,72 @@ class CommunityRepository extends BaseRepository } } + /** + * 转售加入购物车 + * @param $uid + * @param $id + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function addCart($uid, $id) + { + $where = [ + $this->dao->getPk() => $id, + 'is_del' => 0 + ]; + $community = $this->dao->getSearch($where)->with('resale')->find()->toArray(); + if (empty($community) || $community['is_sale'] != 0) { + throw new ValidateException('转售数据不存在或已售出'); + } + if (empty($community['resale'])) { + throw new ValidateException('转售数据不存在'); + } + StoreCart::startTrans(); + try { + /** @var StoreCartRepository $cartRepo */ + $cartRepo = app()->make(StoreCartRepository::class); + $cartIds = []; + foreach ($community['resale'] as $item) { + $data = [ + 'uid' => $uid, + 'mer_id' => $item['mer_id'], + 'product_type' => 98, + 'product_id' => $item['product_id'], + 'product_attr_unique' => $item['product_attr_unique'], + 'cart_num' => $item['number'], + 'source' => StoreCartDao::SOURCE_COMMUNITY_RESALE, + '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 = [ + $this->dao->getPk() => $id, + 'is_del' => 0 + ]; + $community = $this->dao->getSearch($where)->with('resale')->find(); + $community->is_sale = $status; + $community->save(); + foreach ($community->resale as $item) { + $item->status = $status; + $item->save(); + } + } + } diff --git a/app/common/repositories/store/order/StoreOrderCreateRepository.php b/app/common/repositories/store/order/StoreOrderCreateRepository.php index bdba027a..ab56fdad 100644 --- a/app/common/repositories/store/order/StoreOrderCreateRepository.php +++ b/app/common/repositories/store/order/StoreOrderCreateRepository.php @@ -5,6 +5,7 @@ namespace app\common\repositories\store\order; use app\common\dao\store\order\StoreCartDao; use app\common\model\store\order\StoreGroupOrder; use app\common\model\store\order\StoreOrder; +use app\common\repositories\community\CommunityRepository; use app\common\repositories\store\coupon\StoreCouponRepository; use app\common\repositories\store\coupon\StoreCouponUserRepository; use app\common\repositories\store\product\ProductAssistSkuRepository; @@ -1139,7 +1140,9 @@ class StoreOrderCreateRepository extends StoreOrderRepository $group = Db::transaction(function () use ($ex, $user, $topUid, $spreadUid, $uid, $receipt_data, $cartIds, $allUseCoupon, $groupOrder, $orderList, $orderInfo) { $storeGroupOrderRepository = app()->make(StoreGroupOrderRepository::class); $storeCartRepository = app()->make(StoreCartRepository::class); + /** @var ProductAttrValueRepository $attrValueRepository */ $attrValueRepository = app()->make(ProductAttrValueRepository::class); + /** @var ProductRepository $productRepository */ $productRepository = app()->make(ProductRepository::class); $storeOrderProductRepository = app()->make(StoreOrderProductRepository::class); $couponUserRepository = app()->make(StoreCouponUserRepository::class); @@ -1173,8 +1176,16 @@ class StoreOrderCreateRepository extends StoreOrderRepository $productRepository->descStock($cart['product']['old_product_id'], $cart['cart_num']); $attrValueRepository->descStock($cart['product']['old_product_id'], $cart['productAttr']['unique'], $cart['cart_num']); } else { - $attrValueRepository->descStock($cart['productAttr']['product_id'], $cart['productAttr']['unique'], $cart['cart_num']); - $productRepository->descStock($cart['product']['product_id'], $cart['cart_num']); + if ($cart['source'] == StoreCartDao::SOURCE_COMMUNITY_RESALE) { + /** @var CommunityRepository $communityRepository */ + $communityRepository = app()->make(CommunityRepository::class); + $communityRepository->saleOrCancel($cart['source_id']); + $attrValueRepository->incSales($cart['productAttr']['product_id'], $cart['productAttr']['unique'], $cart['cart_num']); + $productRepository->incSales($cart['product']['product_id'], $cart['cart_num']); + } else { + $attrValueRepository->descStock($cart['productAttr']['product_id'], $cart['productAttr']['unique'], $cart['cart_num']); + $productRepository->descStock($cart['product']['product_id'], $cart['cart_num']); + } /** @var PurchaseRecordRepository $purchaseRecordRepo */ $purchaseRecordRepo = app()->make(PurchaseRecordRepository::class); $purchaseRecordRepo->incSalesVolume($cart['product']['product_id'], $cart['productAttr']['unique'], $cart['cart_num']); diff --git a/app/common/repositories/store/product/ProductRepository.php b/app/common/repositories/store/product/ProductRepository.php index fafa497f..6c2c7cca 100644 --- a/app/common/repositories/store/product/ProductRepository.php +++ b/app/common/repositories/store/product/ProductRepository.php @@ -13,12 +13,14 @@ namespace app\common\repositories\store\product; +use app\common\dao\store\order\StoreCartDao; use app\common\model\store\order\StoreOrder; use app\common\model\store\order\StoreOrderProduct; use app\common\model\store\product\ProductAttrValue; use app\common\model\store\product\ProductLabel; use app\common\model\store\product\PurchaseRecord; use app\common\model\store\product\Spu; +use app\common\model\store\Resale; use app\common\model\user\User; use app\common\repositories\community\CommunityRepository; use app\common\repositories\store\coupon\StoreCouponRepository; @@ -1760,6 +1762,7 @@ class ProductRepository extends BaseRepository { $productNum = $productNum ?? $cart['product_num']; Db::transaction(function () use ($order, $cart, $productNum) { + /** @var ProductAttrValueRepository $productAttrValueRepository */ $productAttrValueRepository = app()->make(ProductAttrValueRepository::class); if ($cart['product_type'] == '1') { $oldId = $cart['cart_info']['product']['old_product_id']; @@ -1784,11 +1787,19 @@ class ProductRepository extends BaseRepository $productAttrValueRepository->incSkuStock($oldId, $cart['cart_info']['productAttr']['sku'], $productNum); $this->dao->incStock($oldId, $productNum); } else { - $productAttrValueRepository->incStock($cart['product_id'], $cart['cart_info']['productAttr']['unique'], $productNum); + if ($cart['source'] == StoreCartDao::SOURCE_COMMUNITY_RESALE) { + /** @var CommunityRepository $communityRepository */ + $communityRepository = app()->make(CommunityRepository::class); + $communityRepository->saleOrCancel($cart['source_id'], 0); + $productAttrValueRepository->descSales($cart['productAttr']['product_id'], $cart['productAttr']['unique'], $cart['cart_num']); + $this->dao->descSales($cart['product']['product_id'], $cart['cart_num']); + } else { + $productAttrValueRepository->incStock($cart['product_id'], $cart['cart_info']['productAttr']['unique'], $productNum); + $this->dao->incStock($cart['product_id'], $productNum); + } /** @var PurchaseRecordRepository $purchaseRecordRepo */ $purchaseRecordRepo = app()->make(PurchaseRecordRepository::class); $purchaseRecordRepo->descSalesVolume($cart['product_id'], $cart['cart_info']['productAttr']['unique'], $productNum); - $this->dao->incStock($cart['product_id'], $productNum); } if ($cart->integral > 0) { $totalIntegral = bcmul($productNum, $cart->integral, 0); diff --git a/app/controller/api/community/Community.php b/app/controller/api/community/Community.php index b9af4a60..31f9afa5 100644 --- a/app/controller/api/community/Community.php +++ b/app/controller/api/community/Community.php @@ -440,4 +440,18 @@ class Community extends BaseController return app('json')->success($list); } + /** + * 转售加购 + * @return mixed + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function addCart() + { + $communityId = $this->request->param('community_id'); + $cartIds = $this->repository->addCart($this->request->uid(), $communityId); + return app('json')->success(['cart_id' => $cartIds]); + } + } diff --git a/route/api.php b/route/api.php index 96e49911..00c04742 100644 --- a/route/api.php +++ b/route/api.php @@ -401,6 +401,7 @@ Route::group('api/', function () { Route::get('/focuslst', 'Community/focuslst'); Route::get('/getOrderList', 'Community/getOrderList'); + Route::post('/addCart', 'Community/addCart'); })->prefix('api.community.'); //上传图片 Route::post('upload/image/:field', 'api.Common/uploadImage');