MdListForm.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\District;
  9. use app\models\Md;
  10. use app\models\MdProfit;
  11. use app\models\MdSetting;
  12. use app\modules\client\models\ApiModel;
  13. use yii\data\Pagination;
  14. class MdListForm extends ApiModel
  15. {
  16. public $store_id;
  17. public $user;
  18. public $longitude;
  19. public $latitude;
  20. public $page;
  21. public $limit;
  22. public $keyword;
  23. public $is_single;
  24. public $mdId = 0;
  25. public $product_type;
  26. public $category_id = -1;
  27. public $dis = 0;
  28. public $orderByAi = 0;
  29. public $orderByDis = 0;
  30. public $district_id;
  31. public function rules()
  32. {
  33. return [
  34. [['longitude', 'latitude','keyword'], 'trim'],
  35. [['page','orderByAi','orderByDis'], 'integer'],
  36. [['page'], 'default', 'value' => 1],
  37. [['limit', 'mdId', 'product_type','category_id','dis', 'district_id'], 'integer'],
  38. [['limit'], 'default', 'value' => 20],
  39. [['is_single'], 'safe'],
  40. ];
  41. }
  42. public function search()
  43. {
  44. $this->product_type = intval($this->product_type);
  45. if (!$this->validate()) {
  46. return [
  47. 'code' => 1,
  48. 'msg' => $this->getErrorSummary(false)[0]
  49. ];
  50. }
  51. $shop_table_name = Md::tableName();
  52. $store_id = get_store_id();
  53. $latitude = $this->latitude ? $this->latitude : 0;
  54. $longitude = $this->longitude ? $this->longitude : 0;
  55. $time_ = date("Y-m-d ");
  56. $time = time();
  57. $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
  58. 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)) ";
  59. if ($this->keyword) {
  60. $sql .= " AND (`name` like '%{$this->keyword}%') ";
  61. }
  62. if($this->category_id > 0){
  63. $sql .= " AND `cat_id` = {$this->category_id}";
  64. }
  65. if($this->district_id > 0){
  66. $sql .= " AND `district` = {$this->district_id}";
  67. }
  68. if (!in_array($this->product_type, [1, 2])) {
  69. $open = true;
  70. if (!empty($this->mdId)) {
  71. $mdInfo = Md::find()->where(['id' => $this->mdId, 'is_delete' => 0, 'store_id' => get_store_id()])
  72. ->select('id, is_single')->asArray()->one();
  73. if ((int)$mdInfo['is_single'] === 1) {
  74. $open = false;
  75. $sql .= ' AND id ="'. intval($this->mdId).'"';
  76. }
  77. }
  78. if ($open) {
  79. if (isset($this->is_single)) {
  80. $sql .= ' AND is_single ="'. intval($this->is_single).'"';
  81. }
  82. }
  83. }
  84. if($this->dis > 0){
  85. $sql .= " HAVING (`distance` <= '{$this->dis}')";
  86. }
  87. if (get_md_id()) {
  88. $sql .= ' ORDER BY id <> ' . get_md_id() . ' , distance ASC';
  89. } else {
  90. if($this->orderByAi){
  91. $sql .= ' ORDER BY total_profit DESC, id ASC';
  92. }
  93. if($this->orderByDis){
  94. $sql .= ' ORDER BY distance ASC, id ASC';
  95. }
  96. if(!$this->orderByAi && !$this->orderByDis){
  97. $sql .= ' ORDER BY id ASC';
  98. }
  99. }
  100. $count = \Yii::$app->db->createCommand($sql, [
  101. ':latitude' => $latitude,
  102. ':longitude' => $longitude,
  103. ':store_id' => $store_id,
  104. ])->query()->count();
  105. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  106. $sql .= ' LIMIT ' . $pagination->offset . ',' . $pagination->limit;
  107. $sql = \Yii::$app->db->quoteSql($sql);
  108. $list = \Yii::$app->db->createCommand($sql, [
  109. ':latitude' => $latitude,
  110. ':longitude' => $longitude,
  111. ':store_id' => $store_id,
  112. ])->queryAll();
  113. foreach ($list as $index => $item) {
  114. $list[$index]['coordinate'] = $item['latitude'] . ',' . $item['longitude'];
  115. $list[$index]['score'] = isset($item['score']) ? (int)$item['score'] : 0;
  116. $list[$index]['distance'] = self::distance($item['distance']);
  117. $province_name = District::findOne($item['province'])->name;
  118. $city_name = District::findOne($item['city'])->name;
  119. $district_name = District::findOne($item['district'])->name;
  120. if (!$province_name) {
  121. $province_name = District::findOne(['adcode' => $item['province']])->name;
  122. }
  123. if (!$city_name) {
  124. $city_name = District::findOne(['adcode' => $item['city']])->name;
  125. }
  126. if (!$district_name) {
  127. $district_name = District::findOne(['adcode' => $item['district']])->name;
  128. }
  129. $list[$index]['province_name'] = $province_name;
  130. $list[$index]['city_name'] = $city_name;
  131. $list[$index]['district_name'] = $district_name;
  132. $list[$index]['open_status'] = intval($item['open_status']);
  133. }
  134. $mdSetting = MdSetting::findOne(['store_id' => $store_id]);
  135. $change_shop = $mdSetting->change_shop ?? 1;
  136. if (intval($change_shop) === 0) {
  137. $list = [
  138. $list[0]
  139. ];
  140. }
  141. return [
  142. 'code' => 0,
  143. 'msg' => 'success',
  144. 'data' => [
  145. 'list' => $list,
  146. 'page_count' => $pagination->pageCount,
  147. 'row_count' => $count
  148. ]
  149. ];
  150. }
  151. public static function distance($distance)
  152. {
  153. if ($distance == -1) {
  154. return -1;
  155. }
  156. if ($distance > 1000) {
  157. $distance = round($distance / 1000, 2) . 'km';
  158. } else {
  159. $distance = round($distance, 2);
  160. $distance .= 'm';
  161. }
  162. return $distance;
  163. }
  164. }