Info.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace Shop\Lib;
  3. use Dever;
  4. class Info
  5. {
  6. # 获取店铺基本信息
  7. public function getOne($shop_id, $lng, $lat)
  8. {
  9. $shop = $this->fetch($shop_id, false, $lng, $lat);
  10. return $shop;
  11. }
  12. # 获取店铺基本信息
  13. public function get($city, $lng, $lat, $name = '', $method = 'fetch', $shop_id = false)
  14. {
  15. if (!$city) {
  16. Dever::alert('请传入城市');
  17. }
  18. if (!$lng || !$lat) {
  19. Dever::alert('请传入用户坐标');
  20. }
  21. $data = $this->fetch(false, $city, $lng, $lat, 1, $name, $method);
  22. # 验证本城市内有没有店
  23. if (!$data) {
  24. $data = $this->fetch(false, $city, $lng, $lat, 2, $name, $method);
  25. }
  26. if ($data) {
  27. if ($method == 'fetch') {
  28. $data = $this->getInfo($data);
  29. } else {
  30. foreach ($data as $k => $v) {
  31. $data[$k] = $this->getInfo($data[$k]);
  32. if ($shop_id == $v['id']) {
  33. $data[$k]['cur'] = 1;
  34. } else {
  35. $data[$k]['cur'] = 2;
  36. }
  37. }
  38. }
  39. }
  40. return $data;
  41. }
  42. # 获取店铺的商品列表
  43. public function getGoods($shop, $column = false)
  44. {
  45. $table = 'shop/goods';
  46. $where['shop_id'] = isset($shop['id']) ? $shop['id'] : $shop;
  47. if ($column) {
  48. $where['column'] = $column;
  49. $method = 'getData';
  50. } else {
  51. $method = 'getDataPage';
  52. }
  53. $name = Dever::input('name');
  54. if ($name) {
  55. $where['name'] = $name;
  56. }
  57. $data = Dever::db($table)->$method($where);
  58. if ($data) {
  59. foreach ($data as $k => $v) {
  60. $data[$k] = $this->getGoodsInfo($where['shop_id'], $v);
  61. }
  62. }
  63. return $data;
  64. }
  65. # 获取详细信息
  66. private function getInfo($data)
  67. {
  68. if ($data['worktime']) {
  69. $time = date('Hi');
  70. $worktime = str_replace(':', '', $data['worktime']);
  71. $temp = explode('~', $worktime);
  72. if ($time < $temp[0] || $time > $temp[1]) {
  73. $data['open'] = 2;
  74. }
  75. }
  76. return $data;
  77. }
  78. # 获取距离
  79. private function fetch($id, $city, $lng, $lat, $type = 1, $name = '', $method = 'fetch')
  80. {
  81. $page = array();
  82. if ($method == 'fetchAll') {
  83. $page['template'] = 'list';
  84. $page['num'] = 10;
  85. } else {
  86. $page = false;
  87. }
  88. $where = 'type = '.$type.' and status = 1 and state = 1';
  89. $county = Dever::db('area/county')->find($city);
  90. if ($county) {
  91. $city = $county['id'];
  92. }
  93. if ($type == 1 && $city) {
  94. $where .= ' and city = ' . $city;
  95. }
  96. if ($name) {
  97. $where .= ' and name like("%'.$name.'%")';
  98. }
  99. if ($id) {
  100. $where .= ' and id = ' . $id;
  101. }
  102. $sql = 'select id,name,`desc`,truename,mobile,lng,lat,address,open,worktime,method,gotime,pdesc,round((st_distance(point(lng, lat), point('.$lng.', '.$lat.'))*111195)/1000, 2) as distance from {table} where '.$where.' order by distance asc';
  103. $data = Dever::db('shop/info')->$method($sql, array(), $page);
  104. return $data;
  105. }
  106. # 获取库存
  107. public function getGoodsInfo($shop_id, $info, $sku_id = false, $attr = true)
  108. {
  109. $where['shop_id'] = $shop_id;
  110. $where['goods_id'] = isset($info['goods_id']) ? $info['goods_id'] : $info;
  111. $other = Dever::db('shop/goods_sku')->getData($where);
  112. $data = Dever::load('goods/lib/info')->getInfo($info, $attr, array($other, array('total')));
  113. if($data) {
  114. $sku_id = $sku_id ? $sku_id : $data['sku_id'];
  115. $data['total'] = 0;
  116. if ($data['price_type'] == 4) {
  117. if (isset($data['goods']) && is_array($data['goods'])) {
  118. foreach ($data['goods'] as $k => $v) {
  119. $data['goods'][$k]['total'] = $this->getTotal($other, -1);
  120. if ($data['total'] > $data['goods'][$k]['total']) {
  121. $data['total'] = $data['goods'][$k]['total'];
  122. }
  123. }
  124. }
  125. } else {
  126. $data['total'] = $this->getTotal($other, $sku_id);
  127. }
  128. }
  129. return $data;
  130. }
  131. # 验证库存
  132. public function checkTotal(&$num, $goods_id, $shop_id, $sku_id, $state = 1)
  133. {
  134. $info = $this->getGoodsInfo($shop_id, $goods_id, $sku_id, false);
  135. if (!$info) {
  136. Dever::alert('商品不存在');
  137. }
  138. $total = $info['total'];
  139. if ($num > $total) {
  140. if ($state == 2) {
  141. Dever::alert('库存不足');
  142. }
  143. $num = $total;
  144. }
  145. return $total;
  146. }
  147. # 获取库存
  148. public function getTotal($other, $sku_id)
  149. {
  150. if (isset($other[$sku_id])) {
  151. return $other[$sku_id]['total'];
  152. } else {
  153. return 0;
  154. }
  155. }
  156. }