You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

184 lines
6.4 KiB

<?php
namespace App\Service\v3\Implementations;
use App\Model\v3\Goods;
use App\Model\v3\GoodsActivity;
use App\Model\v3\ShoppingCart;
use App\Model\v3\Store;
use App\Service\v3\Interfaces\GoodsActivityServiceInterface;
use App\Service\v3\Interfaces\GoodsServiceInterface;
use App\Service\v3\Interfaces\ShopCartServiceInterface;
use App\Service\v3\Interfaces\StoreServiceInterface;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
class ShopCartService implements ShopCartServiceInterface
{
/**
* @Inject
* @var StoreServiceInterface
*/
protected $storeService;
/**
* @Inject
* @var GoodsServiceInterface
*/
protected $goodsService;
/**
* @Inject
* @var GoodsActivityServiceInterface
*/
protected $goodsActivityService;
public function do($userId,$marketId)
{
$storeIds = Db::table('lanzu_shopping_cart')->where([
['user_id','=',$userId],
['market_id','=',$marketId],
])->pluck('store_id')->toArray();
$stores = Store::query()->with(['ShoppingCart' => function($query) use ($userId,$marketId){
$query->where([
['user_id','=',$userId],
['market_id','=',$marketId],
]);
}])->whereIn('id',$storeIds)
->get()
->toArray();
$storeArr = [];
foreach ($stores as $key => &$store){
$sotreType = $this->storeService->check($store['id']);
if(!$sotreType){
continue;
}
$subtotal = 0;
foreach ($store['shopping_cart'] as $k => &$shopcart){
if($shopcart['activity_type'] == 1){
$goodsType = $this->goodsService->check($shopcart['goods_id']);
}else{
$goodsType = $this->goodsActivityService->check($shopcart['goods_id'],$shopcart['num'],$shopcart['user_id']);
}
if($goodsType !== true){
unset($store['shopping_cart'][$k]);
continue;
}
if($shopcart['activity_type'] == 1){
$builder = Goods::query();
}else{
$builder = GoodsActivity::query();
}
$shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray();
$subtotal+= $shopcart['num'] * $shopcart['goods']['price'];
}
$store['subtotal'] = $subtotal;
$storeArr[] = $store;
}
return $storeArr;
}
public function check($goodsId)
{
return mt_rand(0,6);
}
public function undo($userId,$marketId)
{
$storeIds = Db::table('lanzu_shopping_cart')->where([
['user_id','=',$userId],
['market_id','=',$marketId],
])->pluck('store_id')->toArray();
$stores = Store::query()->with(['ShoppingCart' => function($query) use ($userId,$marketId){
$query->where([
['user_id','=',$userId],
['market_id','=',$marketId],
]);
}])->whereIn('id',$storeIds)
->get()
->toArray();
$storeArr = [];
foreach ($stores as $key => &$store){
$addStore = false;
$sotreType = $this->storeService->check($store['id']);
if(!$sotreType){
$addStore = true;
}
foreach ($store['shopping_cart'] as $k => &$shopcart){
if($shopcart['activity_type'] == 1){
$goodsType = $this->goodsService->check($shopcart['goods_id']);
}else{
$goodsType = $this->goodsActivityService->check($shopcart['goods_id'],$shopcart['num'],$shopcart['user_id']);
}
if($goodsType === true){
unset($store['shopping_cart'][$k]);
continue;
}
$addStore = true;
if($shopcart['activity_type'] == 1){
$builder = Goods::query();
}else{
$builder = GoodsActivity::query();
}
$shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray();
$shopcart['goods']['invalid_cause'] = $goodsType;
}
if($addStore){
$storeArr[] = $store;
}
}
return $storeArr;
}
public function countGoods($userId,$marketId)
{
return mt_rand(1,100);
}
public function getTotal($userId,$marketId)
{
$randomFloat = rand(100,999)/100;
return $randomFloat;
}
public function getGoodsByShopcartId($shopcartIds)
{
$shopcartIds = explode(',',$shopcartIds);
$storeIds = Db::table('lanzu_shopping_cart')
->whereIn('id',$shopcartIds)
->pluck('store_id')
->toArray();
$stores = Store::query()->with(['ShoppingCart' => function($query) use ($shopcartIds){
$query->whereIn('id',$shopcartIds);
}])->whereIn('id',$storeIds)
->get()
->toArray();
$storeArr = [];
foreach ($stores as $key => &$store){
$sotreType = $this->storeService->check($store['id']);
if(!$sotreType){
continue;
}
$subtotal = 0;
foreach ($store['shopping_cart'] as $k => &$shopcart){
if($shopcart['activity_type'] == 1){
$goodsType = $this->goodsService->check($shopcart['goods_id']);
}else{
$goodsType = $this->goodsActivityService->check($shopcart['goods_id'],$shopcart['num'],$shopcart['user_id']);
}
if($goodsType !== true){
unset($store['shopping_cart'][$k]);
continue;
}
if($shopcart['activity_type'] == 1){
$builder = Goods::query();
}else{
$builder = GoodsActivity::query();
}
$shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray();
$subtotal+= $shopcart['num'] * $shopcart['goods']['price'];
}
$store['subtotal'] = $subtotal;
$storeArr[] = $store;
}
return $storeArr;
}
}