feat: 购物车列表增加用户特权价格计算功能

This commit is contained in:
mkm 2024-06-26 13:56:20 +08:00
parent 27b199994a
commit d8d4f7f9de

View File

@ -6,10 +6,12 @@ namespace app\store\lists\cart;
use app\admin\lists\BaseAdminDataLists;
use app\common\lists\ListsSearchInterface;
use app\common\model\order\Cart;
use app\common\lists\ListsExtendInterface;
use app\common\model\Config;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_product_attr_value\StoreProductAttrValue;
use app\common\model\store_product_unit\StoreProductUnit;
use app\common\model\user\User;
use app\common\lists\ListsExtendInterface;
/**
* 购物车列表
@ -18,10 +20,9 @@ use app\common\model\store_product_unit\StoreProductUnit;
*/
class CartList extends BaseAdminDataLists implements ListsSearchInterface, ListsExtendInterface
{
protected $total_price = 0;
protected $activity_price = 0;
protected $off_activity = 0;
/**
* @notes 设置搜索条件
* @return \string[][]
@ -31,8 +32,6 @@ class CartList extends BaseAdminDataLists implements ListsSearchInterface, Lists
{
return [];
}
/**
* @notes 购物车列表
* @return array
@ -43,27 +42,54 @@ class CartList extends BaseAdminDataLists implements ListsSearchInterface, Lists
*/
public function lists($where = []): array
{
$this->searchWhere[]=['staff_id','=',$this->adminId];
$this->searchWhere[]=['is_pay','=',0];
$list = Cart::where($this->searchWhere)
->field('id,product_id,cart_num,store_id')
$userId = $this->request->get('uid');
$where = [
'staff_id' => $this->adminId,
'is_pay' => 0
];
$list = Cart::where($this->searchWhere)->where($where)
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()->each(function ($item) {
$find = StoreBranchProduct::where(['product_id' => $item['product_id'],'store_id'=>$item['store_id']])
->field('product_id,image,price,store_name,unit')
->find();
if ($find) {
$item['total_price'] = bcmul($item['cart_num'], $find['price'], 2);
$item['image'] = $find['image'];
$item['price'] = $find['price'];
$item['store_name'] = $find['store_name'];
$item['unit_name'] = StoreProductUnit::where('id', $find['unit'])->value('name');
}
return $item;
})
->toArray();
$off_activity = Config::where('name', 'off_activity')->value('value');
$this->off_activity = $off_activity;
$user = User::where('id', $userId)->find();
foreach ($list as $key => &$item) {
$find = StoreBranchProduct::where(['product_id' => $item['product_id'], 'store_id' => $item['store_id']])
->field('product_id,image,price,cost,store_name,unit,delete_time,vip_price')
->withTrashed()
->find();
if ($find) {
if ($off_activity == 1) {
$this->activity_price = bcadd(bcmul($find['cost'], $item['cart_num'], 2), $this->activity_price, 2);
} else {
if ($user && $user['user_ship'] == 1) {
//更新 会员为1的时候原价减去会员价
$deduction_price_count = bcmul(bcsub($find['price'], $find['vip_price'], 2), $item['cart_num'], 2);
$this->activity_price = bcadd($this->activity_price, $deduction_price_count, 2);
} elseif ($user && $user['user_ship'] == 4) {
//更新 为4商户的时候减去商户价格
$deduction_price_count = bcmul(bcsub($find['price'], $find['cost'], 2), $item['cart_num'], 2);
$this->activity_price = bcadd($this->activity_price, $deduction_price_count, 2);
} else {
$this->activity_price = 0;
}
}
$item['goods_total_price'] = bcmul($item['cart_num'], $find['price'], 2);
$this->total_price = bcadd($this->total_price, $item['goods_total_price'], 2);
$item['image'] = $find['image'];
$item['sell'] = $find['price'];
$item['store_name'] = $find['store_name'];
$item['unit_name'] = StoreProductUnit::where('id', $find['unit'])->value('name');
}
}
return $list;
}
@ -75,11 +101,30 @@ class CartList extends BaseAdminDataLists implements ListsSearchInterface, Lists
*/
public function count(): int
{
return Cart::where($this->searchWhere)->count();
$userId = $this->request->userId;
if (!$userId) return 0;
$where = [
'uid' => $userId,
'is_pay' => 0
];
return Cart::where($this->searchWhere)->where($where)->count();
}
public function extend()
{
// return ['total_price' => $this->total_price];
$data = [
'off_activity' => $this->off_activity,
'total_price' => $this->total_price,
'msg' => '',
'pay_price' => bcsub($this->total_price, $this->activity_price, 2)
];
if ($this->off_activity == 1) {
$data['pay_price'] = $this->activity_price;
if ($this->total_price < 500) {
$data['msg'] = '还差' . bcsub(500, $this->total_price, 2) . '元可获得10%品牌礼品券';
$data['pay_price'] = $this->activity_price;
}
}
return $data;
}
}