Info.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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)
  44. {
  45. $table = 'shop/goods';
  46. $where['shop_id'] = isset($shop['id']) ? $shop['id'] : $shop;
  47. $where['column'] = $column;
  48. $method = 'getData';
  49. $data = Dever::db($table)->$method($where);
  50. if ($data) {
  51. foreach ($data as $k => $v) {
  52. $data[$k] = $this->getGoodsInfo($where['shop_id'], $v);
  53. }
  54. }
  55. return $data;
  56. }
  57. # 获取详细信息
  58. private function getInfo($data)
  59. {
  60. if ($data['worktime']) {
  61. $time = date('Hi');
  62. $worktime = str_replace(':', '', $data['worktime']);
  63. $temp = explode('~', $worktime);
  64. if ($time < $temp[0] || $time > $temp[1]) {
  65. $data['open'] = 2;
  66. }
  67. }
  68. return $data;
  69. }
  70. # 获取距离
  71. private function fetch($id, $city, $lng, $lat, $type = 1, $name = '', $method = 'fetch')
  72. {
  73. $page = array();
  74. if ($method == 'fetchAll') {
  75. $page['template'] = 'list';
  76. $page['num'] = 10;
  77. } else {
  78. $page = false;
  79. }
  80. $where = 'type = '.$type.' and status = 1 and state = 1';
  81. $county = Dever::db('area/county')->find($city);
  82. if ($county) {
  83. $city = $county['id'];
  84. }
  85. if ($type == 1 && $city) {
  86. $where .= ' and city = ' . $city;
  87. }
  88. if ($name) {
  89. $where .= ' and name like("%'.$name.'%")';
  90. }
  91. if ($id) {
  92. $where .= ' and id = ' . $id;
  93. }
  94. $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';
  95. $data = Dever::db('shop/info')->$method($sql, array(), $page);
  96. return $data;
  97. }
  98. # 获取库存
  99. public function getGoodsInfo($shop_id, $info, $sku_id = false, $attr = true)
  100. {
  101. $where['shop_id'] = $shop_id;
  102. $where['goods_id'] = isset($info['goods_id']) ? $info['goods_id'] : $info;
  103. $other = Dever::db('shop/goods_sku')->getData($where);
  104. $data = Dever::load('goods/lib/info')->getInfo($info, $attr, array($other, array('total')));
  105. if($data) {
  106. $sku_id = $sku_id ? $sku_id : $data['sku_id'];
  107. $data['total'] = 0;
  108. if ($data['price_type'] == 4) {
  109. if (isset($data['goods']) && is_array($data['goods'])) {
  110. foreach ($data['goods'] as $k => $v) {
  111. $data['goods'][$k]['total'] = $this->getTotal($other, -1);
  112. if ($data['total'] > $data['goods'][$k]['total']) {
  113. $data['total'] = $data['goods'][$k]['total'];
  114. }
  115. }
  116. }
  117. } else {
  118. $data['total'] = $this->getTotal($other, $sku_id);
  119. }
  120. }
  121. return $data;
  122. }
  123. # 验证库存
  124. public function checkTotal(&$num, $goods_id, $shop_id, $sku_id, $state = 1)
  125. {
  126. $info = $this->getGoodsInfo($shop_id, $goods_id, $sku_id, false);
  127. if (!$info) {
  128. Dever::alert('商品不存在');
  129. }
  130. $total = $info['total'];
  131. if ($num > $total) {
  132. if ($state == 2) {
  133. Dever::alert('库存不足');
  134. }
  135. $num = $total;
  136. }
  137. return $total;
  138. }
  139. # 获取库存
  140. public function getTotal($other, $sku_id)
  141. {
  142. if (isset($other[$sku_id])) {
  143. return $other[$sku_id]['total'];
  144. } else {
  145. return 0;
  146. }
  147. }
  148. }