141 lines
4.7 KiB
PHP
141 lines
4.7 KiB
PHP
<?php
|
||
|
||
namespace app\api\lists\order;
|
||
|
||
|
||
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\dict\DictType;
|
||
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;
|
||
|
||
/**
|
||
* 购物车列表
|
||
* Class RetailOrderList
|
||
* @package app\api\order
|
||
*/
|
||
class CartList extends BaseAdminDataLists implements ListsSearchInterface, ListsExtendInterface
|
||
{
|
||
|
||
protected $total_price = 0;
|
||
protected $activity_price = 0;
|
||
protected $off_activity = 0;
|
||
protected $user_ship = 0;
|
||
|
||
|
||
/**
|
||
* @notes 设置搜索条件
|
||
* @return \string[][]
|
||
* @author likeadmin
|
||
*/
|
||
public function setSearch(): array
|
||
{
|
||
return [];
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 购物车列表
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
* @date 2024/04/27 11:26
|
||
*/
|
||
public function lists($where = []): array
|
||
{
|
||
$userId = $this->request->userId;
|
||
if (!$userId) return [];
|
||
$where = [
|
||
'uid' => $userId,
|
||
'is_pay' => 0
|
||
];
|
||
$list = Cart::where($this->searchWhere)->where($where)
|
||
->limit($this->limitOffset, $this->limitLength)
|
||
->order(['id' => 'desc'])
|
||
->select()->each(function ($item) {
|
||
|
||
return $item;
|
||
})
|
||
->toArray();
|
||
$off_activity = Config::where('name', 'off_activity')->value('value');
|
||
$user_ship = User::where('id', $userId)->value('user_ship');
|
||
$field='product_id,image,price,cost,store_name,unit,delete_time,vip_price';
|
||
if (in_array($user_ship, [4, 6, 7])) {
|
||
$field='product_id,image,cost price,cost,store_name,unit,delete_time,vip_price';
|
||
}
|
||
$this->user_ship=$user_ship;
|
||
$this->off_activity=$off_activity;
|
||
foreach ($list as $key => &$item) {
|
||
$find = StoreBranchProduct::where(['product_id' => $item['product_id'], 'store_id' => $item['store_id']])
|
||
->field($field)
|
||
->withTrashed()
|
||
->find();
|
||
if ($find) {
|
||
if($off_activity==1){
|
||
$this->activity_price = bcadd(bcmul($find['cost'],$item['cart_num'], 2), $this->activity_price, 2);
|
||
}
|
||
if ($off_activity == 0 && $user_ship == 5 && $item['top_cate_id'] == 15189) {
|
||
$find['price']=$find['cost'];
|
||
}
|
||
$item['goods_total_price'] = bcmul($item['cart_num'], $find['price'], 2);
|
||
$this->total_price = bcadd($this->total_price, $item['goods_total_price'], 2);
|
||
$item['imgs'] = $find['image'];
|
||
$item['price'] = $find['price'];
|
||
$item['cost'] = $find['cost'];
|
||
$item['goods_name'] = $find['store_name'];
|
||
$item['unit_name'] = StoreProductUnit::where('id', $find['unit'])->value('name');
|
||
}
|
||
}
|
||
return $list;
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 购物车数量
|
||
* @return int
|
||
* @date 2024/04/27 11:26
|
||
*/
|
||
public function count(): int
|
||
{
|
||
$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()
|
||
{
|
||
$data= [
|
||
'off_activity' => $this->off_activity,
|
||
'total_price' => $this->total_price,
|
||
'msg' => '',
|
||
'pay_price' => $this->total_price
|
||
];
|
||
if($this->user_ship==0){
|
||
$data['msg']='您已选购满500元,支付成功后即可获得'.bcmul($this->total_price,0.1,2).'元品牌礼品兑换券,可到线下门店兑换礼品。';
|
||
}
|
||
if($this->off_activity==1){//1
|
||
$data['pay_price']=$this->activity_price;
|
||
if($this->activity_price<500){
|
||
if($this->user_ship==0){
|
||
$data['msg']='还差'.bcsub(500,$this->activity_price,2).'即可获得10%的品牌礼品兑换券,可到线下门店兑换礼品。';
|
||
}
|
||
}else{
|
||
if($this->user_ship==0){
|
||
$data['msg']= '您已选购满500元,支付成功后即可获得'.bcmul($this->activity_price,0.1,2).'元品牌礼品兑换券,可到线下门店兑换礼品。';
|
||
}
|
||
}
|
||
}
|
||
return $data;
|
||
}
|
||
}
|