Goods.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Shop\Lib;
  3. use Dever;
  4. class Goods
  5. {
  6. # 根据店铺获取商品列表
  7. public function getList()
  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. # 这里要加一下缓存
  15. $data['shop'] = Dever::load('shop/lib/info')->getOne($shop_id, $lng, $lat);
  16. if ($column) {
  17. foreach ($column as $k => $v) {
  18. $v['data'] = Dever::load('shop/lib/info')->getGoods($shop_id, $v['id']);
  19. $data['cate'][] = $v;
  20. }
  21. }
  22. $data['banner'] = Dever::load('push/lib/data')->get('list_banner', '商品列表焦点图', 5);
  23. return $data;
  24. }
  25. # 处理库存操作 1是增加,2是减少 1是总库存,2是销量
  26. public function oper($order, $type, $col, $data)
  27. {
  28. if (!$data) {
  29. return;
  30. }
  31. if ($type == 1) {
  32. $method = 'inc';
  33. $otherMethod = 'dec';
  34. } else {
  35. $method = 'dec';
  36. $otherMethod = 'inc';
  37. }
  38. if ($col == 1) {
  39. $method .= 'Total';
  40. $otherMethod .= 'Total';
  41. $num = 'total_num';
  42. } else {
  43. $method .= 'Sell';
  44. $otherMethod .= 'Sell';
  45. $num = 'sell_num';
  46. }
  47. $up = array();
  48. $table = 'shop';
  49. if (isset($order['shop_id'])) {
  50. $up['where_shop_id'] = $order['shop_id'];
  51. } elseif (isset($order['type'])) {
  52. if ($order['type'] == 1) {
  53. $up['where_shop_id'] = $order['type_id'];
  54. } elseif ($order['type'] == 2) {
  55. $table = 'store';
  56. $up['where_store_id'] = $order['type_id'];
  57. }
  58. } else {
  59. return false;
  60. }
  61. foreach ($data as $k => $v) {
  62. if (!$v['sku_id']) {
  63. $v['sku_id'] = -1;
  64. }
  65. $goods = Dever::db('goods/info')->find($v['goods_id']);
  66. if (!$goods) {
  67. continue;
  68. }
  69. if ($goods['price_type'] == 4) {
  70. $goods['goods'] = Dever::array_decode($goods['goods']);
  71. if ($goods['goods']) {
  72. foreach ($goods['goods'] as $k1 => $v1) {
  73. if ($v1['goods_id']) {
  74. if (!isset($v1['sku_id'])) {
  75. $v1['sku_id'] = -1;
  76. }
  77. $v1['num'] = $v1['num'] * $v['num'];
  78. $this->set($up, $order, $num, $table, $method, $otherMethod, $type, $col, $v1);
  79. }
  80. }
  81. }
  82. } else {
  83. $this->set($up, $order, $num, $table, $method, $otherMethod, $type, $col, $v);
  84. }
  85. }
  86. }
  87. private function set($up, $order, $num, $table, $method, $otherMethod, $type, $col, $v)
  88. {
  89. $up['where_goods_id'] = $v['goods_id'];
  90. $up[$num] = $v['num'];
  91. $state = Dever::db($table . '/goods')->$method($up);
  92. if ($state) {
  93. $upSku = $up;
  94. $upSku['where_sku_id'] = $v['sku_id'];
  95. $state = Dever::db($table . '/goods_sku')->$method($upSku);
  96. if (!$state) {
  97. Dever::db($table . '/goods')->$otherMethod($up);
  98. }
  99. if (isset($order['source_type']) && $order['source_type'] == 2 && $type == 1 && $col == 1) {
  100. # 如果是仓库发货的,要减少仓库库存
  101. $sup['where_store_id'] = $order['source_id'];
  102. $sup['where_goods_id'] = $v['goods_id'];
  103. $sup[$num] = $v['num'];
  104. $state = Dever::db('store/goods')->$otherMethod($sup);
  105. if ($state) {
  106. $supSku = $sup;
  107. $supSku['where_sku_id'] = $v['sku_id'];
  108. $state = Dever::db('store/goods_sku')->$otherMethod($supSku);
  109. }
  110. }
  111. }
  112. }
  113. }