1], [['limit', 'mdId', 'product_type','category_id','dis', 'district_id'], 'integer'], [['limit'], 'default', 'value' => 20], [['is_single'], 'safe'], ]; } public function search() { $this->product_type = intval($this->product_type); if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } $shop_table_name = Md::tableName(); $store_id = get_store_id(); $latitude = $this->latitude ? $this->latitude : 0; $longitude = $this->longitude ? $this->longitude : 0; $time_ = date("Y-m-d "); $time = time(); $sql = "SELECT *, acos(cos({$latitude}*pi()/180 )*cos(latitude*pi()/180)*cos({$longitude}*pi()/180 -longitude*pi()/180)+sin({$latitude}*pi()/180 )*sin(latitude*pi()/180))*6370996.81 as distance FROM {$shop_table_name} WHERE ((`store_id`={$store_id}) AND (`is_delete`=0) AND (`shop_audit`=1) AND ((open_status = 1 AND shop_time_type = 1 AND UNIX_TIMESTAMP(CONCAT('{$time_}', start_time)) < {$time} AND UNIX_TIMESTAMP(CONCAT('{$time_}', end_time)) > {$time}) OR (open_status = 1 AND shop_time_type = 0) OR is_time_forbid = 1)) "; if ($this->keyword) { $sql .= " AND (`name` like '%{$this->keyword}%') "; } if($this->category_id > 0){ $sql .= " AND `cat_id` = {$this->category_id}"; } if($this->district_id > 0){ $sql .= " AND `district` = {$this->district_id}"; } if (!in_array($this->product_type, [1, 2])) { $open = true; if (!empty($this->mdId)) { $mdInfo = Md::find()->where(['id' => $this->mdId, 'is_delete' => 0, 'store_id' => get_store_id()]) ->select('id, is_single')->asArray()->one(); if ((int)$mdInfo['is_single'] === 1) { $open = false; $sql .= ' AND id ="'. intval($this->mdId).'"'; } } if ($open) { if (isset($this->is_single)) { $sql .= ' AND is_single ="'. intval($this->is_single).'"'; } } } if($this->dis > 0){ $sql .= " HAVING (`distance` <= '{$this->dis}')"; } if (get_md_id()) { $sql .= ' ORDER BY id <> ' . get_md_id() . ' , distance ASC'; } else { if($this->orderByAi){ $sql .= ' ORDER BY total_profit DESC, id ASC'; } if($this->orderByDis){ $sql .= ' ORDER BY distance ASC, id ASC'; } if(!$this->orderByAi && !$this->orderByDis){ $sql .= ' ORDER BY id ASC'; } } $count = \Yii::$app->db->createCommand($sql, [ ':latitude' => $latitude, ':longitude' => $longitude, ':store_id' => $store_id, ])->query()->count(); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]); $sql .= ' LIMIT ' . $pagination->offset . ',' . $pagination->limit; $sql = \Yii::$app->db->quoteSql($sql); $list = \Yii::$app->db->createCommand($sql, [ ':latitude' => $latitude, ':longitude' => $longitude, ':store_id' => $store_id, ])->queryAll(); foreach ($list as $index => $item) { $list[$index]['coordinate'] = $item['latitude'] . ',' . $item['longitude']; $list[$index]['score'] = isset($item['score']) ? (int)$item['score'] : 0; $list[$index]['distance'] = self::distance($item['distance']); $province_name = District::findOne($item['province'])->name; $city_name = District::findOne($item['city'])->name; $district_name = District::findOne($item['district'])->name; if (!$province_name) { $province_name = District::findOne(['adcode' => $item['province']])->name; } if (!$city_name) { $city_name = District::findOne(['adcode' => $item['city']])->name; } if (!$district_name) { $district_name = District::findOne(['adcode' => $item['district']])->name; } $list[$index]['province_name'] = $province_name; $list[$index]['city_name'] = $city_name; $list[$index]['district_name'] = $district_name; $list[$index]['open_status'] = intval($item['open_status']); } $mdSetting = MdSetting::findOne(['store_id' => $store_id]); $change_shop = $mdSetting->change_shop ?? 1; if (intval($change_shop) === 0) { $list = [ $list[0] ]; } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'list' => $list, 'page_count' => $pagination->pageCount, 'row_count' => $count ] ]; } public static function distance($distance) { if ($distance == -1) { return -1; } if ($distance > 1000) { $distance = round($distance / 1000, 2) . 'km'; } else { $distance = round($distance, 2); $distance .= 'm'; } return $distance; } }