| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\models\District;
- use app\models\Md;
- use app\models\MdProfit;
- use app\models\MdSetting;
- use app\modules\client\models\ApiModel;
- use yii\data\Pagination;
- class MdListForm extends ApiModel
- {
- public $store_id;
- public $user;
- public $longitude;
- public $latitude;
- public $page;
- public $limit;
- public $keyword;
- public $is_single;
- public $mdId = 0;
- public $product_type;
- public $category_id = -1;
- public $dis = 0;
- public $orderByAi = 0;
- public $orderByDis = 0;
- public $district_id;
- public function rules()
- {
- return [
- [['longitude', 'latitude','keyword'], 'trim'],
- [['page','orderByAi','orderByDis'], 'integer'],
- [['page'], 'default', 'value' => 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;
- }
- }
|