feat: 修改SystemStoreLists类以优化购物车商品库存查询逻辑

This commit is contained in:
mkm 2024-08-28 15:47:22 +08:00
parent dc10bce200
commit 4cef86fb87

View File

@ -48,62 +48,76 @@ class SystemStoreLists extends BaseAdminDataLists implements ListsSearchInterfac
*/ */
public function lists(): array public function lists(): array
{ {
$latitude = $this->request->get('latitude',''); $latitude = $this->request->get('latitude', '');
$longitude = $this->request->get('longitude',''); $longitude = $this->request->get('longitude', '');
$cart_id = $this->request->get('cart_id',''); $cart_id = $this->request->get('cart_id', '');
// if(empty($longitude) || empty($latitude)){ // if(empty($longitude) || empty($latitude)){
// throw new Exception('缺失经纬度'); // throw new Exception('缺失经纬度');
// } // }
$where[]=['is_show','=',YesNoEnum::YES]; $where[] = ['is_show', '=', YesNoEnum::YES];
$data = SystemStore::where($this->searchWhere)->where($where) $data = SystemStore::where($this->searchWhere)->where($where)
->field(['id', 'name', 'phone', 'detailed_address', 'image', 'is_show', ->field([
'day_time','is_store','latitude','longitude','day_start','day_end','is_store' 'id',
,'is_send','abbreviation' 'name',
'phone',
'detailed_address',
'image',
'is_show',
'day_time',
'is_store',
'latitude',
'longitude',
'day_start',
'day_end',
'is_store',
'is_send',
'abbreviation'
]) ])
->limit($this->limitOffset, $this->limitLength) ->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc']) ->order(['id' => 'desc'])
->select() ->select()
->toArray(); ->toArray();
if($cart_id){ if ($cart_id) {
if($latitude && $longitude){ if ($latitude && $longitude) {
$cart_id = explode(',',$cart_id); foreach ($data as &$values) {
$cart_select = Cart::whereIn('id', $cart_id) $values['distance'] = haversineDistance($values['latitude'], $values['longitude'], $latitude, $longitude);
->field('id,product_id,cart_num,store_id')->select()->toArray();
foreach ($cart_select as $v) {
foreach ($data as &$values){
$store = StoreBranchProduct::where([
'store_id'=>$values['id'],
'product_id'=>$v['product_id'],
])->field('id,store_name,stock')->withTrashed()->find();
if(empty($store)){
$store['stock'] =0;
}
$values['reservation'] = 0;
if($store['stock'] < $v['cart_num']){
$values['reservation'] = 1; $values['reservation'] = 1;
}
$values['distance'] = haversineDistance($values['latitude'],$values['longitude'],$latitude,$longitude);
}
} }
}else{
foreach ($data as &$values){ // $cart_id = explode(',',$cart_id);
// $cart_select = Cart::whereIn('id', $cart_id)
// ->field('id,product_id,cart_num,store_id')->select()->toArray();
// foreach ($cart_select as $v) {
// foreach ($data as &$values){
// $store = StoreBranchProduct::where([
// 'store_id'=>$values['id'],
// 'product_id'=>$v['product_id'],
// ])->field('id,store_name,stock')->withTrashed()->find();
// if(empty($store)){
// $store['stock'] =0;
// }
// $values['reservation'] = 0;
// if($store['stock'] < $v['cart_num']){
// }
// }
// }
} else {
foreach ($data as &$values) {
$values['distance'] = 0; $values['distance'] = 0;
} }
} }
} else {
}else{ if ($latitude && $longitude) {
if($latitude && $longitude){ foreach ($data as &$value) {
foreach ($data as &$value){ $value['distance'] = haversineDistance($value['latitude'], $value['longitude'], $latitude, $longitude);
$value['distance'] = haversineDistance($value['latitude'],$value['longitude'],$latitude,$longitude);
} }
}else{ } else {
foreach ($data as &$values){ foreach ($data as &$values) {
$values['distance'] = 0; $values['distance'] = 0;
} }
} }
} }
usort($data, function ($a, $b) { usort($data, function ($a, $b) {
@ -121,8 +135,7 @@ class SystemStoreLists extends BaseAdminDataLists implements ListsSearchInterfac
*/ */
public function count(): int public function count(): int
{ {
$where[]=['is_show','=',YesNoEnum::YES]; $where[] = ['is_show', '=', YesNoEnum::YES];
return SystemStore::where($this->searchWhere)->where($where)->count(); return SystemStore::where($this->searchWhere)->where($where)->count();
} }
} }