SystemStore.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace app\models\system;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\DbException;
  7. use think\db\exception\ModelNotFoundException;
  8. /**
  9. * 门店自提 model
  10. * Class SystemStore
  11. * @package app\model\system
  12. */
  13. class SystemStore extends BaseModel
  14. {
  15. use ModelTrait;
  16. /**
  17. * 数据表主键
  18. * @var string
  19. */
  20. protected $pk = 'id';
  21. /**
  22. * 模型名称
  23. * @var string
  24. */
  25. protected $name = 'system_store';
  26. public static function merSet($mer_id)
  27. {
  28. return $mer_id ? self::where('mer_id', $mer_id) : new self;
  29. }
  30. public static function getLatlngAttr($value, $data)
  31. {
  32. return $data['latitude'] . ',' . $data['longitude'];
  33. }
  34. public static function verificWhere($mer_id = false)
  35. {
  36. return self::merSet($mer_id)->where('is_show', 1)->where('is_del', 0);
  37. }
  38. /**
  39. * 获取门店信息
  40. * @param int $id
  41. * @param string $felid
  42. * @param bool $mer_id
  43. * @return array|mixed|null|string|\think\Model
  44. * @throws DataNotFoundException
  45. * @throws DbException
  46. * @throws ModelNotFoundException
  47. */
  48. public static function getStoreDispose($id = 0, $felid = '', $mer_id = false)
  49. {
  50. if ($id)
  51. $storeInfo = self::verificWhere($mer_id)->where('id', $id)->find();
  52. else
  53. $storeInfo = self::verificWhere($mer_id)->find();
  54. if ($storeInfo) {
  55. $storeInfo['latlng'] = self::getLatlngAttr(null, $storeInfo);
  56. $storeInfo['dataVal'] = $storeInfo['valid_time'] ? explode(' - ', $storeInfo['valid_time']) : [];
  57. $storeInfo['timeVal'] = $storeInfo['day_time'] ? explode(' - ', $storeInfo['day_time']) : [];
  58. $storeInfo['address2'] = $storeInfo['address'] ? explode(',', $storeInfo['address']) : [];
  59. if ($felid) return $storeInfo[$felid] ?? '';
  60. }
  61. return $storeInfo;
  62. }
  63. /**
  64. * 获取商品所属的门店
  65. */
  66. public static function getProductStore($id)
  67. {
  68. $res = SystemProductStore::alias('a')->where('a.product_id', $id)->join('system_store s', 's.id = a.store_id')->where('s.is_del', 0)->where('s.is_show', 1)->field('s.name')->select()->toArray();
  69. $res = array_column($res, 'name');
  70. $res = implode(',', $res);
  71. return $res;
  72. }
  73. /**
  74. * 获取门店详情
  75. */
  76. public static function getStoreInfo($id)
  77. {
  78. $storeInfo = self::where('id', $id)->find();
  79. if ($storeInfo) {
  80. $storeInfo['latlng'] = self::getLatlngAttr(null, $storeInfo);
  81. $storeInfo['dataVal'] = $storeInfo['valid_time'] ? explode(' - ', $storeInfo['valid_time']) : [];
  82. $storeInfo['timeVal'] = $storeInfo['day_time'] ? explode(' - ', $storeInfo['day_time']) : [];
  83. $storeInfo['address2'] = $storeInfo['address'] ? explode(',', $storeInfo['address']) : [];
  84. $storeInfo['products'] = SystemProductStore::alias('a')->where('store_id', $id)->join('store_product s', 's.id = a.product_id')->column('a.product_id as id, s.image');
  85. }
  86. return $storeInfo;
  87. }
  88. /**
  89. * 获取排序sql
  90. * @param $latitude
  91. * @param $longitude
  92. * @return mixed
  93. */
  94. public static function distanceSql($latitude, $longitude)
  95. {
  96. $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";
  97. return $field;
  98. }
  99. /**
  100. * 门店列表
  101. * @param $latitude
  102. * @param $longitude
  103. * @param $page
  104. * @param $limit
  105. * @param string $keywords
  106. * @param int $type
  107. * @param bool $mer_id
  108. * @return mixed
  109. */
  110. public static function lst($latitude, $longitude, $page, $limit, $keywords = '', $type = 0, $mer_id = false, $product_id = false)
  111. {
  112. if(!$product_id){
  113. $model = self::merSet($mer_id);
  114. if ($type == 0) {
  115. $model = $model->where(['is_del' => 0, 'is_show' => 1]);
  116. } elseif ($type == 1) {
  117. $model = $model->where('is_show', 0);
  118. } else {
  119. $model = $model->where('is_del', 1);
  120. }
  121. if (isset($keywords) && $keywords != '') {
  122. $model = $model->where('id|name|introduction', 'like', '%' . $keywords . '%');
  123. }
  124. if ($latitude && $longitude) {
  125. $model = $model->field(['*', self::distanceSql($latitude, $longitude)]);
  126. }
  127. $count = $model->count('id');
  128. $list = $model->page((int)$page, (int)$limit)->select()->toArray();
  129. if ($latitude && $longitude) {
  130. foreach ($list as &$value) {
  131. //计算距离
  132. $value['distance'] = sqrt((pow((($latitude - $value['latitude']) * 111000), 2)) + (pow((($longitude - $value['longitude']) * 111000), 2)));
  133. //转换单位
  134. $value['range'] = bcdiv($value['distance'], 1000, 1);
  135. $value['total_address'] = $value['address']. $value['detailed_address'];
  136. }
  137. }else {
  138. foreach ($list as &$value) {
  139. $value['total_address'] = $value['address']. $value['detailed_address'];
  140. }
  141. }
  142. return compact('list', 'count');
  143. }else{
  144. $product_id = explode(',', $product_id);
  145. $model = self::merSet($mer_id);
  146. if ($type == 0) {
  147. $model = $model->where(['is_del' => 0, 'is_show' => 1]);
  148. } elseif ($type == 1) {
  149. $model = $model->where('is_show', 0);
  150. } else {
  151. $model = $model->where('is_del', 1);
  152. }
  153. if (isset($keywords) && $keywords != '') {
  154. $model = $model->where('id|name|introduction', 'like', '%' . $keywords . '%');
  155. }
  156. if ($latitude && $longitude) {
  157. $model = $model->field(['*', self::distanceSql($latitude, $longitude)]);
  158. }
  159. $list = $model->select()->toArray();
  160. $result = SystemProductStore::alias('a')->where('a.product_id', 'in', $product_id)->where('s.is_del', 0)->where('s.is_show', 1)->join('system_store s', 's.id = a.store_id')->field('s.*')->select()->toArray();
  161. $list = $result ? $result : $list;
  162. $count = count($list);
  163. if ($latitude && $longitude) {
  164. foreach ($list as &$value) {
  165. //计算距离
  166. $value['distance'] = sqrt((pow((($latitude - $value['latitude']) * 111000), 2)) + (pow((($longitude - $value['longitude']) * 111000), 2)));
  167. //转换单位
  168. $value['range'] = bcdiv($value['distance'], 1000, 1);
  169. $value['total_address'] = $value['address']. $value['detailed_address'];
  170. }
  171. }else {
  172. foreach ($list as &$value) {
  173. $value['total_address'] = $value['address']. $value['detailed_address'];
  174. }
  175. }
  176. return compact('list', 'count');
  177. }
  178. }
  179. /**
  180. * 获取门店下的商品
  181. */
  182. public static function getStoreProduct($page, $limit, $mer_id = false)
  183. {
  184. $list = self::merSet($mer_id)->where('is_del', 0)->where('is_show', 1)->page((int)$page, (int)$limit)->select()->each(function ($item) {
  185. $item['productList'] = SystemProductStore::alias('a')->where('a.store_id', $item['id'])->where('p.is_del', 0)->where('p.is_show', 1)->join('store_product p', 'p.id = a.product_id', 'right')->field('p.*')->page(1, 10)->select();
  186. })->toArray();
  187. return $list;
  188. }
  189. /**
  190. * 导出数据
  191. * @return mixed
  192. */
  193. public static function exportData($where)
  194. {
  195. $model = new self();
  196. if ($where['type'] == 0) {
  197. $model = $model->where(['is_del' => 0, 'is_show' => 1]);
  198. } elseif ($where['type'] == 1) {
  199. $model = $model->where('is_show', 0);
  200. } else {
  201. $model = $model->where('is_del', 1);
  202. }
  203. if (isset($where['mer_id']) && $where['mer_id'] != '') {
  204. $model = $model->where('mer_id', $where['mer_id']);
  205. }
  206. if (isset($where['keywords']) && $where['keywords'] != '') {
  207. $model = $model->where('id|name|introduction', 'like', '%' . $where['keywords'] . '%');
  208. }
  209. $list = $model->select()->toArray();
  210. return $list;
  211. }
  212. /**
  213. * 店员添加门店列表
  214. * @return array
  215. * @throws DataNotFoundException
  216. * @throws DbException
  217. * @throws ModelNotFoundException
  218. */
  219. public static function dropList($mer_id = '')
  220. {
  221. return self::where(['is_show' => 1, 'is_del' => 0, 'mer_id' => $mer_id])->select()->toArray();
  222. }
  223. function array_unique_fb($array2D){
  224. foreach ($array2D as $v){
  225. $v=join(',',$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
  226. $temp[]=$v;
  227. }
  228. $temp=array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
  229. foreach ($temp as $k => $v){
  230. $temp[$k]=explode(',',$v); //再将拆开的数组重新组装
  231. }
  232. return $temp;
  233. }
  234. }