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.
350 lines
13 KiB
350 lines
13 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($userId,$goodsId)
|
|
{
|
|
$ShoppingCart = ShoppingCart::query()->where([
|
|
['user_id','=',$userId],
|
|
['goods_id','=',$goodsId],
|
|
])
|
|
->select('num')
|
|
->first();
|
|
return $ShoppingCart->num;
|
|
}
|
|
|
|
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)
|
|
{
|
|
$ShoppingCart = ShoppingCart::query()->where([
|
|
['user_id','=',$userId],
|
|
['market_id','=',$marketId],
|
|
])->get();
|
|
$num = 0;
|
|
foreach ($ShoppingCart as $cart){
|
|
if($cart->activity_type == 2){
|
|
$goods = GoodsActivity::query()->find($cart->goods_id);
|
|
$goodsCheck = $this->goodsActivityService->check($goods,$cart->num,$cart->user_id);
|
|
}else{
|
|
$goods = Goods::query()->find($cart->goods_id);
|
|
$goodsCheck = $this->goodsService->check($goods,$cart->num);
|
|
}
|
|
if($goodsCheck){
|
|
$num+= $cart->num;
|
|
}
|
|
}
|
|
return $num;
|
|
}
|
|
|
|
public function getTotal($userId,$marketId)
|
|
{
|
|
$ShoppingCart = ShoppingCart::query()->where([
|
|
['user_id','=',$userId],
|
|
['market_id','=',$marketId],
|
|
])->get();
|
|
$total = 0;
|
|
foreach ($ShoppingCart as $cart){
|
|
if($cart->activity_type == 2){
|
|
$goods = GoodsActivity::query()->find($cart->goods_id);
|
|
$goodsCheck = $this->goodsActivityService->check($goods,$cart->num,$cart->user_id);
|
|
}else{
|
|
$goods = Goods::query()->find($cart->goods_id);
|
|
$goodsCheck = $this->goodsService->check($goods,$cart->num);
|
|
}
|
|
if($goodsCheck){
|
|
$subTotal = bcmul($goods->price,$cart->num);
|
|
$total = bcadd($subTotal,$total,2);
|
|
}
|
|
}
|
|
return $total;
|
|
}
|
|
|
|
public function getGoodsByShopcartId($shopcartIds)
|
|
{
|
|
bcscale(6);
|
|
// 查询当前用户的市场下的购物车数据
|
|
$shopcartIdsArr = explode(',',$shopcartIds);
|
|
$carts = ShoppingCart::query()
|
|
->with(['store', 'goods'])
|
|
->whereIn('id',$shopcartIdsArr)
|
|
->get();
|
|
$cartList = [];
|
|
$totalAmount = 0;
|
|
foreach ($carts as $key => &$cart) {
|
|
|
|
if (empty($cart->store)||empty($cart->goods)) {
|
|
continue;
|
|
}
|
|
|
|
$shoppingCart = $cart->toArray();
|
|
$store = $shoppingCart['store'];
|
|
unset($shoppingCart['store']);
|
|
|
|
// 商户是否歇业
|
|
$checkStore = $this->storeService->check($store['id']);
|
|
if (!$checkStore) {
|
|
continue;
|
|
}
|
|
|
|
// 商品是否失效
|
|
if ($cart->activity_type == 2) {
|
|
$goods = GoodsActivity::query()->find($cart->goods_id);
|
|
$res = $this->goodsActivityService->check($goods, $cart->num, $cart->user_id);
|
|
} else {
|
|
$goods = Goods::query()->find($cart->goods_id);
|
|
$res = $this->goodsService->check($goods, $cart->num);
|
|
}
|
|
|
|
if ($res === true) {
|
|
|
|
$cartList['store_lists'][$store['id']]['store'] = $store;
|
|
$cartList['store_lists'][$store['id']]['shopping_cart'][] = $shoppingCart;
|
|
|
|
if (isset($cartList['store_lists'][$store['id']]['subtotal'])) {
|
|
$cartList['store_lists'][$store['id']]['subtotal'] = bcadd(
|
|
$cartList['store_lists'][$store['id']]['subtotal'],
|
|
bcmul($cart->goods->price, $cart->num)
|
|
);
|
|
} else {
|
|
$cartList['store_lists'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
|
|
}
|
|
|
|
$totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num));
|
|
}
|
|
}
|
|
|
|
$cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : [];
|
|
|
|
foreach ($cartList['store_lists'] as $key => &$value) {
|
|
$value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
|
|
}
|
|
|
|
$cartList['total'] = bcadd($totalAmount, '0', 2);
|
|
|
|
return $cartList['store_lists'];
|
|
}
|
|
|
|
/**
|
|
* 获取用户购物车数据
|
|
* @param $userId
|
|
* @param $marketId
|
|
* @return array
|
|
*/
|
|
public function allForUser($userId, $marketId)
|
|
{
|
|
|
|
bcscale(6);
|
|
// 查询当前用户的市场下的购物车数据
|
|
$carts = ShoppingCart::query()
|
|
->with(['store', 'goods'])
|
|
->where(['user_id' => $userId])
|
|
->where(['market_id' => $marketId])
|
|
->get();
|
|
$cartList = [];
|
|
$totalAmount = 0;
|
|
foreach ($carts as $key => &$cart) {
|
|
|
|
if (empty($cart->store)||empty($cart->goods)) {
|
|
continue;
|
|
}
|
|
|
|
$shoppingCart = $cart->toArray();
|
|
$store = $shoppingCart['store'];
|
|
unset($shoppingCart['store']);
|
|
|
|
// 商户是否歇业
|
|
$checkStore = $this->storeService->check($store['id']);
|
|
if (!$checkStore) {
|
|
|
|
$cartList['store_lists_invalid'][$store['id']]['store'] = $store;
|
|
$cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart;
|
|
|
|
if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) {
|
|
$cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd(
|
|
$cartList['store_lists_invalid'][$store['id']]['subtotal'],
|
|
bcmul($cart->goods->price, $cart->num)
|
|
);
|
|
} else {
|
|
$cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
// 商品是否失效
|
|
if ($cart->activity_type == 2) {
|
|
$goods = GoodsActivity::query()->find($cart->goods_id);
|
|
$res = $this->goodsActivityService->check($goods, $cart->num, $cart->user_id);
|
|
} else {
|
|
$goods = Goods::query()->find($cart->goods_id);
|
|
$res = $this->goodsService->check($goods, $cart->num);
|
|
}
|
|
|
|
if ($res === true) {
|
|
|
|
$cartList['store_lists'][$store['id']]['store'] = $store;
|
|
$cartList['store_lists'][$store['id']]['shopping_cart'][] = $shoppingCart;
|
|
|
|
if (isset($cartList['store_lists'][$store['id']]['subtotal'])) {
|
|
$cartList['store_lists'][$store['id']]['subtotal'] = bcadd(
|
|
$cartList['store_lists'][$store['id']]['subtotal'],
|
|
bcmul($cart->goods->price, $cart->num)
|
|
);
|
|
} else {
|
|
$cartList['store_lists'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
|
|
}
|
|
|
|
$totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num));
|
|
} else {
|
|
|
|
$cartList['store_lists_invalid'][$store['id']]['store'] = $store;
|
|
$cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart;
|
|
|
|
if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) {
|
|
$cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd(
|
|
$cartList['store_lists_invalid'][$store['id']]['subtotal'],
|
|
bcmul($cart->goods->price, $cart->num)
|
|
);
|
|
} else {
|
|
$cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
$cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : [];
|
|
$cartList['store_lists_invalid'] = isset($cartList['store_lists_invalid']) ? array_values($cartList['store_lists_invalid']) : [];
|
|
|
|
foreach ($cartList['store_lists'] as $key => &$value) {
|
|
$value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
|
|
}
|
|
|
|
foreach ($cartList['store_lists_invalid'] as $key => &$value) {
|
|
$value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
|
|
}
|
|
|
|
$cartList['total'] = bcadd($totalAmount, '0', 2);
|
|
|
|
return $cartList;
|
|
}
|
|
}
|