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
{
$latitude = $this->request->get('latitude','');
$longitude = $this->request->get('longitude','');
$cart_id = $this->request->get('cart_id','');
// if(empty($longitude) || empty($latitude)){
// throw new Exception('缺失经纬度');
// }
$where[]=['is_show','=',YesNoEnum::YES];
$latitude = $this->request->get('latitude', '');
$longitude = $this->request->get('longitude', '');
$cart_id = $this->request->get('cart_id', '');
// if(empty($longitude) || empty($latitude)){
// throw new Exception('缺失经纬度');
// }
$where[] = ['is_show', '=', YesNoEnum::YES];
$data = SystemStore::where($this->searchWhere)->where($where)
->field(['id', 'name', 'phone', 'detailed_address', 'image', 'is_show',
'day_time','is_store','latitude','longitude','day_start','day_end','is_store'
,'is_send','abbreviation'
->field([
'id',
'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)
->order(['id' => 'desc'])
->select()
->toArray();
if($cart_id){
if($latitude && $longitude){
$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']){
$values['reservation'] = 1;
}
$values['distance'] = haversineDistance($values['latitude'],$values['longitude'],$latitude,$longitude);
}
if ($cart_id) {
if ($latitude && $longitude) {
foreach ($data as &$values) {
$values['distance'] = haversineDistance($values['latitude'], $values['longitude'], $latitude, $longitude);
$values['reservation'] = 1;
}
}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;
}
}
}else{
if($latitude && $longitude){
foreach ($data as &$value){
$value['distance'] = haversineDistance($value['latitude'],$value['longitude'],$latitude,$longitude);
} else {
if ($latitude && $longitude) {
foreach ($data as &$value) {
$value['distance'] = haversineDistance($value['latitude'], $value['longitude'], $latitude, $longitude);
}
}else{
foreach ($data as &$values){
} else {
foreach ($data as &$values) {
$values['distance'] = 0;
}
}
}
usort($data, function ($a, $b) {
@ -121,8 +135,7 @@ class SystemStoreLists extends BaseAdminDataLists implements ListsSearchInterfac
*/
public function count(): int
{
$where[]=['is_show','=',YesNoEnum::YES];
$where[] = ['is_show', '=', YesNoEnum::YES];
return SystemStore::where($this->searchWhere)->where($where)->count();
}
}
}