diff --git a/app/common/model/community/Community.php b/app/common/model/community/Community.php index b5aaf962..fa77d9b2 100644 --- a/app/common/model/community/Community.php +++ b/app/common/model/community/Community.php @@ -15,6 +15,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\system\Relevance; use app\common\model\user\User; use app\common\repositories\system\RelevanceRepository; @@ -173,8 +174,7 @@ class Community extends BaseModel public function resale() { - return $this->hasMany(Relevance::class, 'left_id','community_id') - ->where('type',RelevanceRepository::TYPE_COMMUNITY_PRODUCT); + return $this->hasMany(Resale::class, 'community_id','community_id'); } } diff --git a/app/common/model/store/Resale.php b/app/common/model/store/Resale.php new file mode 100644 index 00000000..ca4882bd --- /dev/null +++ b/app/common/model/store/Resale.php @@ -0,0 +1,26 @@ +hasOne(Spu::class, 'product_id','product_id'); + } + +} diff --git a/app/common/repositories/community/CommunityRepository.php b/app/common/repositories/community/CommunityRepository.php index f7b6c549..7363d8f3 100644 --- a/app/common/repositories/community/CommunityRepository.php +++ b/app/common/repositories/community/CommunityRepository.php @@ -13,6 +13,9 @@ namespace app\common\repositories\community; use app\common\dao\community\CommunityDao; +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\StoreOrderProductRepository; use app\common\repositories\store\product\SpuRepository; @@ -112,13 +115,22 @@ class CommunityRepository extends BaseRepository ], 'is_fans' => function($query) use($userInfo){ $query->where('left_id',$userInfo->uid?? 0); - } + }, + 'resale', ]); $count = $query->count(); $list = $query->page($page, $limit)->setOption('field',[]) ->field('community_id,title,image,topic_id,Community.count_start,count_reply,start,Community.create_time,Community.uid,Community.status,is_show,content,video_link,is_type,refusal') ->select()->append(['time']); if ($list) $list = $list->toArray(); + foreach ($list as $k => $item) { + if ($item['is_type'] == self::COMMUNITY_TYPE_RESALE) { + $list[$k]['total_price'] = '0'; + foreach ($item['resale'] as $value) { + $list[$k]['total_price'] = bcadd($list[$k]['total_price'], bcmul($value['price'], $value['number'], 2), 2); + } + } + } return compact('count','list'); } @@ -238,6 +250,11 @@ class CommunityRepository extends BaseRepository 'is_start' => function ($query) use ($user) { $query->where('left_id', $user->uid ?? ''); }, + 'resale' => [ + 'spu' => function ($query) { + $query->field('spu_id,store_name,image,price,product_type,activity_id,product_id'); + } + ], ])->hidden(['is_del'])->find(); if (!$data) throw new ValidateException('内容不存在,可能已被删除了哦~'); @@ -251,6 +268,12 @@ class CommunityRepository extends BaseRepository 'type' => RelevanceRepository::TYPE_COMMUNITY_FANS, ]); $data['is_fans'] = $is_fans; + if ($data['is_type'] == self::COMMUNITY_TYPE_RESALE) { + $data['total_price'] = '0'; + foreach ($data['resale'] as $value) { + $data['total_price'] = bcadd($data['total_price'], bcmul($value['price'], $value['number'], 2), 2); + } + } return $data; } @@ -304,6 +327,9 @@ class CommunityRepository extends BaseRepository return Db::transaction(function () use($data) { $community = $this->dao->create($data); if ($data['spu_id'])$this->joinProduct($community->community_id,$data['spu_id']); + if ($data['product_info'] && $data['is_type'] == self::COMMUNITY_TYPE_RESALE) { + $this->resale($community->community_id, $data['product_info']); + } event('community.create',compact('community')); return $community->community_id; }); @@ -508,5 +534,53 @@ class CommunityRepository extends BaseRepository return $make->getWechatQrcodePath($name, $link, false, $key); } } + + /** + * 转售贴 + * @param $id + * @param array $data + * @return void + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function resale($id, array $data) + { + $insert = []; + foreach ($data as $value) { + $purchaseRecord = PurchaseRecord::find($value['purchase_record_id']); + $exist = Resale::where('purchase_record_id', $purchaseRecord['id'])->find(); + if ($exist) { + throw new ValidateException('已发起转售'); + } + if (empty($purchaseRecord) || ($purchaseRecord['number'] - $purchaseRecord['sales_volume']) <= 0) { + throw new ValidateException('进货记录不存在或已售罄'); + } + if (($purchaseRecord['number'] - $purchaseRecord['sales_volume']) < $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'], + '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) { + Resale::getInstance()->insertAll($insert); + } + } + } diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index 79b897ad..65358766 100644 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -851,7 +851,6 @@ class StoreOrderRepository extends BaseRepository $param['StoreOrder.is_del'] = 0; switch ($status) { case 1: - $param['paid'] = 0; break; // 未支付 case 2: $param['paid'] = 1; @@ -1494,6 +1493,12 @@ class StoreOrderRepository extends BaseRepository $status = $where['status']; unset($where['status']); $query = $this->dao->search($where)->where($this->getOrderType($status)) + ->when($status == 4, function ($query) { + $query->where('paid', 1)->where('pay_type', '<>', StoreGroupOrder::PAY_TYPE_CREDIT_BUY); + }) + ->when($status == 1, function ($query) { + $query->whereRaw("(StoreOrder.paid=0 and StoreOrder.status!=12) or (StoreOrder.paid=1 and StoreOrder.pay_type=8 and StoreOrder.status=2)"); + }) ->with([ 'orderProduct', 'merchant' => function ($query) { @@ -2591,7 +2596,7 @@ class StoreOrderRepository extends BaseRepository $products = $order->orderProduct; $currentOrder = $order->toArray(); foreach ($products as $k => $product) { - $purchaseRecord = PurchaseRecord::where(['order_id' => $product['order_id'], 'order_product_id' => $product['product_id'], 'order_unique' => $product['product_sku']])->field('mer_id,product_id,unique,number,sales_volume')->find(); + $purchaseRecord = PurchaseRecord::where(['order_id' => $product['order_id'], 'order_product_id' => $product['product_id'], 'order_unique' => $product['product_sku']])->field('id,number,sales_volume')->find(); if (!empty($purchaseRecord)) { $currentOrder['orderProduct'][$k]['sales_volume'] = $purchaseRecord->sales_volume; $currentOrder['orderProduct'][$k]['purchase_record'] = $purchaseRecord->toArray(); diff --git a/app/controller/api/community/Community.php b/app/controller/api/community/Community.php index a743761b..b9af4a60 100644 --- a/app/controller/api/community/Community.php +++ b/app/controller/api/community/Community.php @@ -12,8 +12,10 @@ namespace app\controller\api\community; +use app\common\model\system\merchant\Merchant; use app\common\repositories\community\CommunityRepository; use app\common\repositories\store\order\StoreOrderProductRepository; +use app\common\repositories\store\order\StoreOrderRepository; use app\common\repositories\system\RelevanceRepository; use app\common\repositories\user\UserHistoryRepository; use app\common\repositories\user\UserRelationRepository; @@ -268,7 +270,7 @@ class Community extends BaseController public function checkParams() { - $data = $this->request->params(['image','topic_id','content','spu_id','order_id',['is_type',1],'video_link']); + $data = $this->request->params(['image','topic_id','content','spu_id','order_id',['is_type',1],'video_link', 'product_info']); $config = systemConfig(["community_app_switch",'community_audit','community_video_audit']); $data['status'] = 0; $data['is_show'] = 0; @@ -280,7 +282,7 @@ class Community extends BaseController $data['is_show'] = 1; $data['status_time'] = date('Y-m-d H:i:s', time()); } - } else { + } elseif ($data['is_type'] == 2) { if (!in_array($this->repository::COMMUNIT_TYPE_VIDEO,$config['community_app_switch'])) throw new ValidateException('短视频未开启'); if ($config['community_video_audit']) { @@ -427,4 +429,15 @@ class Community extends BaseController if (!$url) return app('json')->fail('二维码生成失败'); return app('json')->success(compact('url')); } + + public function getOrderList() + { + [$page, $limit] = $this->getPage(); + $keyword = $this->request->param('keyword'); + /** @var StoreOrderRepository $repo */ + $repo = app()->make(StoreOrderRepository::class); + $list = $repo->purchaseOrder(['uid' => $this->request->uid()], $keyword, $page, $limit); + return app('json')->success($list); + } + } diff --git a/route/api.php b/route/api.php index 131eb8e8..96e49911 100644 --- a/route/api.php +++ b/route/api.php @@ -400,6 +400,7 @@ Route::group('api/', function () { Route::get('/:id/reply', 'CommunityReply/lst'); Route::get('/focuslst', 'Community/focuslst'); + Route::get('/getOrderList', 'Community/getOrderList'); })->prefix('api.community.'); //上传图片 Route::post('upload/image/:field', 'api.Common/uploadImage');