Goods.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace Shop\Lib;
  3. use Dever;
  4. class Goods
  5. {
  6. # 根据店铺获取商品列表
  7. public function getColumn($uid = -1)
  8. {
  9. $shop_id = Dever::input('shop_id');
  10. $lng = Dever::input('lng');
  11. $lat = Dever::input('lat');
  12. $column = Dever::db('goods/column')->select();
  13. $data = array();
  14. $data['shop'] = Dever::load('shop/lib/info')->getOne($shop_id, $lng, $lat);
  15. if ($column) {
  16. foreach ($column as $k => $v) {
  17. $data['cate'][] = $v;
  18. }
  19. }
  20. $data['banner'] = Dever::load('push/lib/data')->get('list_banner', '商品列表焦点图', 5);
  21. return $data;
  22. }
  23. # 根据店铺获取商品列表
  24. public function getColumnGoods($uid = -1)
  25. {
  26. $shop_id = Dever::input('shop_id');
  27. $column_id = Dever::input('column_id');
  28. $data['data'] = Dever::load('shop/lib/info')->getGoods($shop_id, $column_id, false, 1, false, $uid);
  29. return $data;
  30. }
  31. # 根据店铺获取商品列表
  32. public function getList($uid = -1)
  33. {
  34. $shop_id = Dever::input('shop_id');
  35. $lng = Dever::input('lng');
  36. $lat = Dever::input('lat');
  37. $column = Dever::db('goods/column')->select();
  38. $data = array();
  39. # 这里要加一下缓存
  40. $data['shop'] = Dever::load('shop/lib/info')->getOne($shop_id, $lng, $lat);
  41. if ($column) {
  42. foreach ($column as $k => $v) {
  43. $v['data'] = Dever::load('shop/lib/info')->getGoods($shop_id, $v['id'], false, 1, false, $uid);
  44. if ($v['data']) {
  45. $data['cate'][] = $v;
  46. }
  47. }
  48. }
  49. $data['banner'] = Dever::load('push/lib/data')->get('list_banner', '商品列表焦点图', 5);
  50. return $data;
  51. }
  52. # 处理库存操作 1是增加,2是减少 1是总库存,2是销量
  53. public function oper($order, $type, $col, $data)
  54. {
  55. if (!$data) {
  56. return;
  57. }
  58. if ($type == 1) {
  59. $method = 'inc';
  60. $otherMethod = 'dec';
  61. } else {
  62. $method = 'dec';
  63. $otherMethod = 'inc';
  64. }
  65. if ($col == 1) {
  66. $method .= 'Total';
  67. $otherMethod .= 'Total';
  68. $num = 'total_num';
  69. } else {
  70. $method .= 'Sell';
  71. $otherMethod .= 'Sell';
  72. $num = 'sell_num';
  73. }
  74. $up = array();
  75. $table = 'shop';
  76. if (isset($order['shop_id'])) {
  77. $up['where_shop_id'] = $order['shop_id'];
  78. /*
  79. # 如果是仓库直发
  80. if (isset($order['store_id']) && $order['store_id'] && isset($order['store']) && $order['store'] == 1) {
  81. $table = 'store';
  82. $up['where_store_id'] = $order['store_id'];
  83. } elseif (isset($order['shop_type']) && $order['shop_type'] == 2) {
  84. # 如果是平台店下单,就不扣库存了
  85. return;
  86. }
  87. */
  88. } elseif (isset($order['type'])) {
  89. if ($order['type'] == 1) {
  90. $up['where_shop_id'] = $order['type_id'];
  91. } elseif ($order['type'] == 2) {
  92. $table = 'store';
  93. $up['where_store_id'] = $order['type_id'];
  94. }
  95. } else {
  96. return false;
  97. }
  98. foreach ($data as $k => $v) {
  99. if (!$v['sku_id']) {
  100. $v['sku_id'] = -1;
  101. }
  102. $goods = Dever::db('goods/info')->one($v['goods_id']);
  103. if (!$goods) {
  104. continue;
  105. }
  106. if ($goods['price_type'] == 4) {
  107. $goods['goods'] = Dever::array_decode($goods['goods']);
  108. if ($goods['goods']) {
  109. foreach ($goods['goods'] as $k1 => $v1) {
  110. if ($v1['goods_id']) {
  111. if (!isset($v1['sku_id'])) {
  112. $v1['sku_id'] = -1;
  113. }
  114. $v1['num'] = $v1['num'] * $v['num'];
  115. $this->set($up, $order, $num, $table, $method, $otherMethod, $type, $col, $v1);
  116. }
  117. }
  118. }
  119. } else {
  120. $this->set($up, $order, $num, $table, $method, $otherMethod, $type, $col, $v);
  121. }
  122. }
  123. }
  124. private function set($up, $order, $num, $table, $method, $otherMethod, $type, $col, $v)
  125. {
  126. # 先验证有没有信息吧
  127. foreach ($up as $k1 => $v1) {
  128. $k1 = str_replace('where_', '', $k1);
  129. $w[$k1] = $v1;
  130. }
  131. $w['goods_id'] = $v['goods_id'];
  132. $info = Dever::db($table . '/goods')->one($w);
  133. if (!$info) {
  134. Dever::db($table . '/goods')->insert($w);
  135. } else {
  136. Dever::db($table . '/goods')->update(array('where_id' => $info['id'], 'state' => 1));
  137. }
  138. $w['sku_id'] = $v['sku_id'];
  139. $info = Dever::db($table . '/goods_sku')->one($w);
  140. if (!$info) {
  141. Dever::db($table . '/goods_sku')->insert($w);
  142. } else {
  143. Dever::db($table . '/goods_sku')->update(array('where_id' => $info['id'], 'state' => 1));
  144. }
  145. $up['where_sku_id'] = $w['sku_id'];
  146. $up['where_goods_id'] = $v['goods_id'];
  147. $up[$num] = $v['num'];
  148. $state = Dever::db($table . '/goods')->$method($up);
  149. if ($state) {
  150. $upSku = $up;
  151. $upSku['where_sku_id'] = $v['sku_id'];
  152. $state = Dever::db($table . '/goods_sku')->$method($upSku);
  153. if (!$state) {
  154. Dever::db($table . '/goods')->$otherMethod($up);
  155. }
  156. if (isset($order['source_type']) && $order['source_type'] == 2 && $order['type'] != 2 && $type == 1 && $col == 1) {
  157. # 如果是仓库发货的,要减少仓库库存
  158. $sup['where_store_id'] = $order['source_id'];
  159. $sup['where_goods_id'] = $v['goods_id'];
  160. $sup[$num] = $v['num'];
  161. $state = Dever::db('store/goods')->$otherMethod($sup);
  162. if ($state) {
  163. $supSku = $sup;
  164. $supSku['where_sku_id'] = $v['sku_id'];
  165. $state = Dever::db('store/goods_sku')->$otherMethod($supSku);
  166. }
  167. }
  168. }
  169. }
  170. }