SystemStore.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\models\system;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. /**
  6. * 门店自提 model
  7. * Class SystemStore
  8. * @package app\model\system
  9. */
  10. class SystemStore extends BaseModel
  11. {
  12. const EARTH_RADIUS = 6371;
  13. use ModelTrait;
  14. /**
  15. * 数据表主键
  16. * @var string
  17. */
  18. protected $pk = 'id';
  19. /**
  20. * 模型名称
  21. * @var string
  22. */
  23. protected $name = 'system_store';
  24. public static function getLatlngAttr($value, $data)
  25. {
  26. return $data['latitude'] . ',' . $data['longitude'];
  27. }
  28. public static function verificWhere()
  29. {
  30. return self::where('is_show', 1)->where('is_del', 0);
  31. }
  32. /**
  33. * 获取门店信息
  34. * @param int $id
  35. * @param string $felid
  36. * @return array|mixed|null|string|\think\Model
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public static function getStoreDispose($id = 0, $felid = '')
  42. {
  43. if ($id)
  44. $storeInfo = self::verificWhere()->where('id', $id)->find();
  45. else
  46. $storeInfo = self::verificWhere()->find();
  47. if ($storeInfo) {
  48. $storeInfo['latlng'] = self::getLatlngAttr(null, $storeInfo);
  49. $storeInfo['valid_time'] = $storeInfo['valid_time'] ? explode(' - ', $storeInfo['valid_time']) : [];
  50. $storeInfo['_valid_time'] = str_replace('-', '/', ($storeInfo['valid_time'][0] ?? '') . ' ~ ' . ($storeInfo['valid_time'][1] ?? ""));
  51. $storeInfo['day_time'] = $storeInfo['day_time'] ? str_replace(' - ', ' ~ ', $storeInfo['day_time']) : [];
  52. $storeInfo['_detailed_address'] = $storeInfo['address'] . ' ' . $storeInfo['detailed_address'];
  53. $storeInfo['address'] = $storeInfo['address'] ? explode(',', $storeInfo['address']) : [];
  54. if ($felid) return $storeInfo[$felid] ?? '';
  55. }
  56. return $storeInfo;
  57. }
  58. /**
  59. * 获取排序sql
  60. * @param $latitude
  61. * @param $longitude
  62. * @return mixed
  63. */
  64. public static function distanceSql($latitude, $longitude)
  65. {
  66. $field = "(round(6367000 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2) + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
  67. return $field;
  68. }
  69. /**
  70. * 门店列表
  71. * @return mixed
  72. */
  73. public static function lst($latitude, $longitude, $page, $limit)
  74. {
  75. $model = new self();
  76. $model = $model->where('is_del', 0);
  77. $model = $model->where('is_show',1);
  78. if ($latitude && $longitude) {
  79. $model = $model->field(['*', self::distanceSql($latitude, $longitude)])->order('distance asc');
  80. }
  81. $list = $model->page((int)$page, (int)$limit)
  82. ->select()
  83. ->hidden(['is_show', 'is_del'])
  84. ->toArray();
  85. if ($latitude && $longitude) {
  86. foreach ($list as &$value) {
  87. //计算距离
  88. $value['distance'] = sqrt((pow((($latitude - $value['latitude']) * 111000), 2)) + (pow((($longitude - $value['longitude']) * 111000), 2)));
  89. //转换单位
  90. $value['range'] = bcdiv($value['distance'], 1000, 1);
  91. }
  92. }
  93. return $list;
  94. }
  95. }