where("user_id",$post['user_id']) ->find(); if($hostory == null){ TeaStoreHistory::create([ "user_id"=>$post['user_id'], "content"=>$b, "dtime"=>time() ]); } } } } $op = ''; if(isset($post['open'])){ if($post['open'] == 1){ $op = "operation_type = 1"; } } if(isset($post['sales'])){ if($post['sales'] == 1){ $or = "sales desc"; } } $c = ""; if(isset($post['city'])){ if($post['city'] != ""&&$post['city'] != null){ $ct = $post['city']; $c = "city like '%".$ct."%'";; } } $use = ""; if(isset($post['use'])&&isset($post['user_id'])){ if($post['use'] == 1){ $store_order = OrderStore::where("user_id",$post['user_id'])->group('store_id')->select(); $ids = []; foreach ($store_order as $item){ $ids[] = $item['store_id']; } if(!empty($ids)){ $ids_string = implode(',', $ids); $use = "id in ($ids_string)"; } } } $ca = ""; if(isset($post['city_area_id'])){ if($post['city_area_id'] != ""&&$post['city_area_id'] != null&&$post['city_area_id'] != 0){ $ca = "city_area_id = ".$post['city_area_id'].""; } } $distance = "'--'"; $distance = "ROUND( 6378.138 * 2 * ASIN( SQRT( POW( SIN( ( {$post['latitude']} * PI() / 180 - latitude * PI() / 180 ) / 2 ), 2 ) + COS({$post['latitude']} * PI() / 180) * COS(latitude * PI() / 180) * POW( SIN( ( {$post['longitude']} * PI() / 180 - longitude * PI() / 180 ) / 2 ), 2 ) ) ) * 1000 )"; // $distanceField = "ROUND(6378.138 * 2 * ASIN(SQRT( // POW(SIN(({$post['latitude']} * PI() / 180 - latitude * PI() / 180) / 2), 2) + // COS({$post['latitude']} * PI() / 180) * COS(latitude * PI() / 180) * // POW(SIN(({$post['longitude']} * PI() / 180 - longitude * PI() / 180) / 2), 2) // )) * 1000, 2) AS distance"; $count = TeaStore::where('del',0) ->where("status",1) ->where($s) ->where($c) ->where($ca) ->where($op) ->where($use) ->count(); $lists = TeaStore::where('del',0) ->field("*,{$distance} as distance") ->where("status",1) ->where($s) ->where($c) ->where($op) ->where($ca) ->where($use) ->page($post['page'], $post['size']) ->order($or) ->select(); $time = date('Y-m-d'); $startDate = date('Y-m-d', strtotime($time . ' -6 months')); $startTime = $startDate . ' 00:00:00'; $endTime = $time . ' 23:59:59'; $todaystartTime =$time . ' 00:00:00'; $todayendTime = $time . ' 23:59:59'; foreach($lists as $key=>$value){ if ($lists[$key]['distance'] >= 1000) { $n = $lists[$key]['distance'] > 10000 ? 0 : 1; $lists[$key]['distance'] = round( $lists[$key]['distance'] / 1000, $n) . 'km'; } elseif ($lists[$key]['distance']!= '--') { $lists[$key]['distance'] .= 'm'; } // 距离 // $lists[$key]['distance'] = calculateDistanceKm($value['latitude'],$value['longitude'],$post['latitude'],$post['longitude']); $lists[$key]['half_year_nums'] = $value['sales']; if($lists[$key]['half_year_nums']<50){ $lists[$key]['half_year_nums'] = 50; } $lists[$key]['today'] = Db::name('order_store') ->where('store_id',$value['store_id']) ->whereBetween('dtime', [$todaystartTime, $todayendTime]) ->count(); $lists[$key]['image']=FileService::getImgUrl($value['image']); } $data = [ 'list' => $lists, 'page' => $post['page'], 'size' => $post['size'], 'count' => $count, 'more' => is_more($count, $post['page'], $post['size']) ]; return $data; } public static function getTeaStoreDetails($data){ $result = TeaStore::where('id',$data['id']) ->find(); if(isset($data['room_id'])){ $room = TeaStoreRoom::where('id',$data['room_id']) ->find(); $arr = explode(",", $room['img_arr'] ?? ''); // 使用 ?? 防止 $lists['image'] 为 null $labelIds = explode(',',$room['label_id']??''); $labelList = TeaStoreRoomLabel::where('status', 1) ->where('type', 1) ->whereIn('id', $labelIds) ->select() ->toArray(); $room['label'] = $labelList; $room_arr = []; foreach ($arr as $key => $v) { if (!empty(trim($v))) { $room_arr[$key] = FileService::getImgUrl($v); } else { $room_arr[$key] = ''; } } $room['room_arr'] = $room_arr; $result['room'] = $room; } $result['collect'] = 0; if(isset($data['user_id'])){ if($data['user_id'] != "" &&$data['user_id']!=0){ $result['collect'] = TeaStore::teaStoreCollect($result['id'],$data['user_id']); $result['collect'] = $result['collect']?1:0; } } $result['distance'] = calculateDistanceKm($result['latitude'],$result['longitude'],$data['latitude'],$data['longitude']); $result['rechange_times'] = 5; $d['details'] = $result; $arr = explode(",",$result['image_arr']); $image_arr = []; foreach($arr as $key=>$value){ $image_arr[$key] =FileService::getImgUrl($value); } $result['image_arr'] = $image_arr; return $d; } public static function getTeaStoreSearchHistory($userId){ $lists = TeaStoreHistory::where("user_id",$userId) ->where("status",1) ->where("del",0) ->order("id","desc") ->select(); $data = [ 'list' => $lists ]; return $data; } public static function delTeaStoreSearchHistory($userId){ return TeaStoreHistory::where("user_id",$userId) ->where("status",1) ->where("del",0) ->update(['del'=>1]); } public static function getTeaStoreCity(){ $lists = TeaStoreCity::where("status",1) ->where("del",0) ->select(); $data = [ 'list' => $lists ]; return $data; } public static function getTeaStoreRoomLists($post) { $storeId = $post['id']; $page = $post['page'] ?? 1; $size = $post['size'] ?? 10; // 1. 基础查询 $query = TeaStoreRoom::where('del', 0) ->where("store_id", $storeId); $count = $query->count(); // 2. 获取房间列表 $lists = $query->page($page, $size) ->order('weight asc') ->select() ->toArray(); if (empty($lists)) { return [ 'list' => [], 'page' => $page, 'size' => $size, 'count' => 0, 'more' => false ]; } // 3. 批量获取标签数据 $roomIds = array_column($lists, 'id'); $labelIds = []; // 4. 批量获取订单时间数据 $orderTimes = OrderStore::whereIn('room_id', $roomIds) ->where('is_transfer', 0) ->where('end_time', '>', time() - 60 * 40) ->whereIn('order_status', [0, 1, 2]) ->where('is_transfer',0) ->field('room_id, timeslot') ->select() ->toArray(); // 按房间ID分组订单时间 $roomOrderTimes = []; foreach ($orderTimes as $order) { $roomId = $order['room_id']; if (!isset($roomOrderTimes[$roomId])) { $roomOrderTimes[$roomId] = []; } $roomOrderTimes[$roomId][] = $order['timeslot']; } // 5. 准备时间数据(每个房间都一样,可以复用) $todayStart = strtotime(date("Y-m-d 00:00:00")); $todayEnd = $todayStart + 86400; // 24小时 $now = time(); // 6. 处理房间数据 foreach ($lists as &$room) { $roomId = $room['id']; $labelIds = explode(',',$room['label_id']); $labelList = TeaStoreRoomLabel::where('status', 1) ->where('type', 1) ->whereIn('id', $labelIds) ->select() ->toArray(); $room['label'] = $labelList; // 处理时间表 $roomTimes = []; $currentOrderTimes = $roomOrderTimes[$roomId] ?? []; // 如果$currentOrderTimes是二维数组,需要扁平化成一维数组 $flattenedOrderTimes = []; if (!empty($currentOrderTimes)) { // 扁平化处理,同时处理逗号分隔的字符串 array_walk_recursive($currentOrderTimes, function($value) use (&$flattenedOrderTimes) { if (is_string($value) && strpos($value, ',') !== false) { // 如果是逗号分隔的字符串,分割成数组 $timeArray = explode(',', $value); foreach ($timeArray as $time) { $flattenedOrderTimes[] = (int)trim($time); } } else { $flattenedOrderTimes[] = (int)$value; } }); } for ($time = $todayStart; $time < $todayEnd; $time += 1800) { // 30分钟 $item = [ 'key' => $time, 'time' => date('H:i', $time), 'local_time' => date('Y/m/d H:i', $time), 'type' => '1', 'disabled' => 0 ]; // 已过时间 if ($time < $now) { $item['disabled'] = 1; $item['type'] = '2'; } // 已被预订 if (in_array($time, $flattenedOrderTimes)) { $item['disabled'] = 2; $item['type'] = '3'; } $roomTimes[] = $item; } $room['room_time'] = $roomTimes; $room['img'] = FileService::getImgUrl($room['img']); unset($room['label_id']); // 清理不需要的字段 } // 7. 返回结果 return [ 'list' => $lists, 'page' => (int)$page, 'size' => (int)$size, 'count' => $count, 'more' => $count > $page * $size ]; } public static function rechargeLists($post){ try { $store = TeaStore::where([ 'id'=>$post['store_id'], 'recharge_state'=>0, 'del'=>0 ])->find(); if($store){ throw new \Exception('当前门店暂无充值活动'); } $store_recharge = TeaStoreRecharge::where([ 'store_id'=>$post['store_id'], 'del'=>0 ])->select()->toArray(); return $store_recharge; } catch (\Exception $e) { self::setError($e->getMessage()); return false; } } public static function teaStoreCollect($post){ if($post['status'] == 1){ $result = TeaStoreCollect::where([ "tea_store_id"=>$post['id'], "user_id"=>$post['user_id'], "status"=>1 ])->find(); if($result){ return false; } return TeaStoreCollect::create([ "tea_store_id"=>$post['id'], "user_id"=>$post['user_id'], "dtime"=>date("Y-m-d H:i:s") ]); }else{ $d['status'] = 0; $d['update_dtime'] = date("Y-m-d H:i:s"); return TeaStoreCollect::where('tea_store_id',$post['id']) ->where("user_id",$post['user_id']) ->update($d); } } public static function teaStoreCollectList($post,$userId){ $count = TeaStoreCollect::where('status',1)->where('user_id',$userId) ->count(); $lists = TeaStoreCollect::with(['teaStore']) ->where('status',1) ->where('user_id',$userId) ->page($post['page'], $post['size']) ->order('id asc') ->select(); foreach($lists as $key=>$value){ $lists[$key]['count'] = TeaStoreCollect::where('status',1)->where('tea_store_id',$value['tea_store_id'])->count(); } $data = [ 'list' => $lists, 'page' => $post['page'], 'size' => $post['size'], 'count' => $count, 'more' => is_more($count, $post['page'], $post['size']) ]; return $data; } public static function getTeaStoreGroupLists($post){ $count = TeaStoreGroup::where('del',0) ->where('status',1) ->where("store_id",$post['id']) ->count(); $lists = TeaStoreGroup::where('del',0) ->where('status',1) ->where("store_id",$post['id']) ->page($post['page'], $post['size']) ->order('id desc') ->select(); foreach($lists as $key=>$value){ $img_arr = explode(",",$value['img']); $lists[$key]['img'] =FileService::getImgUrl($img_arr[0]); $lists[$key]['discount'] = round($value['discount'],2); if($value['room_id'] == 0){ $lists[$key]['room_name'] = "通用"; }else{ $room = TeaStoreRoom::where("id","in",$value['room_id'])->select(); foreach($room as $j=>$item){ if($j == 0){ $lists[$key]['room_name'].= $item['title']; }else{ $lists[$key]['room_name'].=",".$item['title']; } } } } $data = [ 'list' => $lists, 'page' => $post['page'], 'size' => $post['size'], 'count' => $count, 'more' => is_more($count, $post['page'], $post['size']) ]; return $data; } public static function getTeaStoreGroupDetails($data){ $result = TeaStoreGroup::where('id',$data['id']) ->find(); $result['room_title'] = ""; if($result['room_id'] == 0){ $result['room_name'] = "通用"; }else{ $room = TeaStoreRoom::where("id","in",$result['room_id'])->select(); foreach($room as $key=>$value){ if($key == 0){ $result['room_name'].= $value['title']; }else{ $result['room_name'].=",".$value['title']; } } } $img_arr = explode(",",$result['img']); foreach($img_arr as $key=>$value){ $img_arr[$key] = FileService::getImgUrl($value); } $result['img'] = $img_arr; $d['details'] = $result; return $d; } public static function qualDetails($data){ $result = TeaStoreQual::where('store_id',$data['store_id']) ->find(); if($result != null){ $result['license_img'] = FileService::getImgUrl($result['license_img']); if($result['effective']){ $result['start_time'] = date("Y-m-d",$result['start_time']); $result['end_time'] = date("Y-m-d",$result['end_time']); } } $d['details'] = $result; return $d; } }