SystemStoreDao.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\store;
  12. use app\dao\BaseDao;
  13. use app\model\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. * @param array $where
  31. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  32. */
  33. public function search(array $where = [])
  34. {
  35. return parent::search($where)->when(isset($where['ids']) && $where['ids'], function ($query) use ($where) {
  36. $query->whereIn('id', $where['ids']);
  37. })->when(isset($where['province']) && $where['province'], function ($query) use ($where) {
  38. $query->where('province', $where['province']);
  39. })->when(isset($where['city']) && $where['city'], function ($query) use ($where) {
  40. $query->where('city', $where['city']);
  41. })->when(isset($where['area']) && $where['area'], function ($query) use ($where) {
  42. $query->where('area', $where['area']);
  43. })->when(isset($where['street']) && $where['street'], function ($query) use ($where) {
  44. $query->where('street', $where['street']);
  45. });
  46. }
  47. /**
  48. * 经纬度排序计算
  49. * @param string $latitude
  50. * @param string $longitude
  51. * @return string
  52. */
  53. public function distance(string $latitude, string $longitude, bool $type = false)
  54. {
  55. if ($type) {
  56. return "(round(6378137 * 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)))))";
  57. } else {
  58. return "(round(6378137 * 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";
  59. }
  60. }
  61. /**
  62. * 获取列表
  63. * @param array $where
  64. * @param array $field
  65. * @param int $page
  66. * @param int $limit
  67. * @return array
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function getList(array $where, array $field = ['*'], int $page = 0, int $limit = 0)
  73. {
  74. return $this->search($where)->when($page && $limit, function ($query) use ($page, $limit) {
  75. $query->page($page, $limit);
  76. })->order('id desc')->field($field)->select()->toArray();
  77. }
  78. /**
  79. * 获取
  80. * @param array $where
  81. * @param array $field
  82. * @param int $page
  83. * @param int $limit
  84. * @param string $latitude
  85. * @param string $longitude
  86. * @return array
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. */
  91. public function getStoreList(array $where, array $field = ['*'], int $page = 0, int $limit = 0, array $with = [], string $latitude = '', string $longitude = '', int $order = 0)
  92. {
  93. return $this->search($where)->when($with, function ($query) use ($with) {
  94. $query->with($with);
  95. })->when($latitude && $longitude, function ($query) use ($longitude, $latitude, $order) {
  96. $query->field(['*', $this->distance($latitude, $longitude)]);
  97. })->when($page && $limit, function ($query) use ($page, $limit) {
  98. $query->page($page, $limit);
  99. })->when(isset($order), function ($query) use ($order) {
  100. if ($order == 1) {
  101. $query->order('distance ASC');
  102. } else {
  103. $query->order('id desc');
  104. }
  105. })->field($field)->select()->toArray();
  106. }
  107. /**
  108. * 获取有效门店
  109. * @param array $where
  110. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  111. */
  112. public function getValidSerch(array $where = [])
  113. {
  114. $validWhere = [
  115. 'is_show' => 1,
  116. 'is_del' => 0,
  117. ];
  118. return $this->search($where)->where($validWhere);
  119. }
  120. /**
  121. * 获取最近距离距离内的一个门店
  122. * @param string $latitude
  123. * @param string $longitude
  124. * @return array|\think\Model|null
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\DbException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. */
  129. public function getDistanceShortStore(string $latitude = '', string $longitude = '')
  130. {
  131. return $this->getValidSerch()->when($longitude && $longitude, function ($query) use ($longitude, $latitude) {
  132. $query->field(['*', $this->distance($latitude, $longitude)])->order('distance ASC');
  133. })->order('id desc')->find();
  134. }
  135. /**
  136. * 距离排序符合配送范围门店
  137. * @param string $latitude
  138. * @param string $longitude
  139. * @param string $field
  140. * @param int $limit
  141. * @return array
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\DbException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. */
  146. public function getDistanceShortStoreList(string $latitude = '', string $longitude = '', string $field = '*', int $limit = 0)
  147. {
  148. return $this->getValidSerch()->field($field)->when($longitude && $longitude, function ($query) use ($longitude, $latitude, $field) {
  149. $query->field([$field, $this->distance($latitude, $longitude)])->where('valid_range', 'EXP', '>' . $this->distance($latitude, $longitude, true))->order('distance ASC');
  150. })->when($limit, function ($query) use ($limit) {
  151. $query->limit($limit);
  152. })->order('id desc')->select()->toArray();
  153. }
  154. /**
  155. * 根据地址区、街道信息获取门店列表
  156. * @param string $addressInfo
  157. * @param array $where
  158. * @param string $field
  159. * @param int $page
  160. * @param int $limit
  161. * @return array
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\DbException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. */
  166. public function getStoreByAddressInfo(string $addressInfo = '', array $where = [], string $field = '*', int $page = 0, int $limit = 0)
  167. {
  168. return $this->getValidSerch($where)->field($field)->when($addressInfo, function ($query) use ($addressInfo) {
  169. $query->whereLike('address', '%' . $addressInfo . '%');
  170. })->when($page && $limit, function ($query) use ($page, $limit) {
  171. $query->page($page, $limit);
  172. })->when(!$page && $limit, function ($query) use ($limit) {
  173. $query->limit($limit);
  174. })->order('id desc')->select()->toArray();
  175. }
  176. /**
  177. * 获取门店不分页
  178. * @param array $where
  179. * @return array
  180. * @throws \think\db\exception\DataNotFoundException
  181. * @throws \think\db\exception\DbException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. */
  184. public function getStore(array $where)
  185. {
  186. return $this->search($where)->order('add_time DESC')->field(['id', 'name'])->select()->toArray();
  187. }
  188. /**
  189. * 获取ERP店铺
  190. * @param array $where
  191. * @return array
  192. * @throws \think\db\exception\DataNotFoundException
  193. * @throws \think\db\exception\DbException
  194. * @throws \think\db\exception\ModelNotFoundException
  195. */
  196. public function getErpStore(array $where, array $field = ['id','name','erp_shop_id'])
  197. {
  198. return $this->search(['type' => 0])->where($where)->field($field)->select()->toArray();
  199. }
  200. }