SystemStoreDao.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\dao\system\store;
  12. use app\dao\BaseDao;
  13. use app\model\system\store\SystemStore;
  14. /**
  15. * 门店dao
  16. * Class SystemStoreDao
  17. * @package app\dao\system\store
  18. */
  19. class SystemStoreDao extends BaseDao
  20. {
  21. /**
  22. * 设置模型
  23. * @return string
  24. */
  25. protected function setModel(): string
  26. {
  27. return SystemStore::class;
  28. }
  29. /**
  30. * 经纬度排序计算
  31. * @param string $latitude
  32. * @param string $longitude
  33. * @return string
  34. */
  35. public function distance(string $latitude, string $longitude)
  36. {
  37. return "(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";
  38. }
  39. /**
  40. * 获取
  41. * @param array $where
  42. * @param int $page
  43. * @param int $limit
  44. * @param string $latitude
  45. * @param string $longitude
  46. * @return array
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function getStoreList(array $where, array $field, int $page = 0, int $limit = 0, string $latitude = '', string $longitude = '')
  52. {
  53. return $this->search($where)->when($longitude && $latitude, function ($query) use ($longitude, $latitude) {
  54. $query->field(['*', $this->distance($latitude, $longitude)])->order('distance ASC');
  55. })->when($page && $limit, function ($query) use ($page, $limit) {
  56. $query->page($page, $limit);
  57. })->field($field)->order('id desc')->select()->toArray();
  58. }
  59. /**
  60. * 获取门店不分页
  61. * @param array $where
  62. * @return array
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function getStore(array $where)
  68. {
  69. return $this->search($where)->order('add_time DESC')->field(['id', 'name'])->select()->toArray();
  70. }
  71. }