123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- namespace Shop\Lib;
- use Dever;
- class Goods
- {
- # 根据店铺获取商品列表
- public function getColumn($uid = -1)
- {
- $shop_id = Dever::input('shop_id');
- $lng = Dever::input('lng');
- $lat = Dever::input('lat');
- $column = Dever::db('goods/column')->select();
- $data = array();
- $data['shop'] = Dever::load('shop/lib/info')->getOne($shop_id, $lng, $lat);
- if ($column) {
- foreach ($column as $k => $v) {
- $data['cate'][] = $v;
- }
- }
- $data['banner'] = Dever::load('push/lib/data')->get('list_banner', '商品列表焦点图', 5);
- return $data;
- }
- # 根据店铺获取商品列表
- public function getColumnGoods($uid = -1)
- {
- $shop_id = Dever::input('shop_id');
- $column_id = Dever::input('column_id');
- $data['data'] = Dever::load('shop/lib/info')->getGoods($shop_id, $column_id, false, 1, false, $uid);
- return $data;
- }
- # 根据店铺获取商品列表
- public function getList($uid = -1)
- {
- $shop_id = Dever::input('shop_id');
- $lng = Dever::input('lng');
- $lat = Dever::input('lat');
- $column = Dever::db('goods/column')->select();
- $data = array();
- # 这里要加一下缓存
- $data['shop'] = Dever::load('shop/lib/info')->getOne($shop_id, $lng, $lat);
- if ($column) {
- foreach ($column as $k => $v) {
- $v['data'] = Dever::load('shop/lib/info')->getGoods($shop_id, $v['id'], false, 1, false, $uid);
- if ($v['data']) {
- $data['cate'][] = $v;
- }
- }
- }
- $data['banner'] = Dever::load('push/lib/data')->get('list_banner', '商品列表焦点图', 5);
- return $data;
- }
- # 处理库存操作 1是增加,2是减少 1是总库存,2是销量
- public function oper($order, $type, $col, $data)
- {
- if (!$data) {
- return;
- }
- if ($type == 1) {
- $method = 'inc';
- $otherMethod = 'dec';
- } else {
- $method = 'dec';
- $otherMethod = 'inc';
- }
- if ($col == 1) {
- $method .= 'Total';
- $otherMethod .= 'Total';
- $num = 'total_num';
- } else {
- $method .= 'Sell';
- $otherMethod .= 'Sell';
- $num = 'sell_num';
- }
- $up = array();
- $table = 'shop';
- if (isset($order['shop_id'])) {
- $up['where_shop_id'] = $order['shop_id'];
- /*
- # 如果是仓库直发
- if (isset($order['store_id']) && $order['store_id'] && isset($order['store']) && $order['store'] == 1) {
- $table = 'store';
- $up['where_store_id'] = $order['store_id'];
- } elseif (isset($order['shop_type']) && $order['shop_type'] == 2) {
- # 如果是平台店下单,就不扣库存了
- return;
- }
- */
- } elseif (isset($order['type'])) {
- if ($order['type'] == 1) {
- $up['where_shop_id'] = $order['type_id'];
- } elseif ($order['type'] == 2) {
- $table = 'store';
- $up['where_store_id'] = $order['type_id'];
- }
- } else {
- return false;
- }
- foreach ($data as $k => $v) {
- if (!$v['sku_id']) {
- $v['sku_id'] = -1;
- }
- $goods = Dever::db('goods/info')->one($v['goods_id']);
- if (!$goods) {
- continue;
- }
-
- if ($goods['price_type'] == 4) {
- $goods['goods'] = Dever::array_decode($goods['goods']);
- if ($goods['goods']) {
- foreach ($goods['goods'] as $k1 => $v1) {
- if ($v1['goods_id']) {
- if (!isset($v1['sku_id'])) {
- $v1['sku_id'] = -1;
- }
- $v1['num'] = $v1['num'] * $v['num'];
- $this->set($up, $order, $num, $table, $method, $otherMethod, $type, $col, $v1);
- }
- }
- }
- } else {
- $this->set($up, $order, $num, $table, $method, $otherMethod, $type, $col, $v);
- }
- }
- }
- private function set($up, $order, $num, $table, $method, $otherMethod, $type, $col, $v)
- {
- # 先验证有没有信息吧
- foreach ($up as $k1 => $v1) {
- $k1 = str_replace('where_', '', $k1);
- $w[$k1] = $v1;
- }
- $w['goods_id'] = $v['goods_id'];
- $info = Dever::db($table . '/goods')->one($w);
- if (!$info) {
- Dever::db($table . '/goods')->insert($w);
- } else {
- Dever::db($table . '/goods')->update(array('where_id' => $info['id'], 'state' => 1));
- }
- $w['sku_id'] = $v['sku_id'];
- $info = Dever::db($table . '/goods_sku')->one($w);
- if (!$info) {
- Dever::db($table . '/goods_sku')->insert($w);
- } else {
- Dever::db($table . '/goods_sku')->update(array('where_id' => $info['id'], 'state' => 1));
- }
- $up['where_sku_id'] = $w['sku_id'];
- $up['where_goods_id'] = $v['goods_id'];
- $up[$num] = $v['num'];
- $state = Dever::db($table . '/goods')->$method($up);
- if ($state) {
- $upSku = $up;
- $upSku['where_sku_id'] = $v['sku_id'];
- $state = Dever::db($table . '/goods_sku')->$method($upSku);
- if (!$state) {
- Dever::db($table . '/goods')->$otherMethod($up);
- }
- if (isset($order['source_type']) && $order['source_type'] == 2 && $order['type'] != 2 && $type == 1 && $col == 1) {
- # 如果是仓库发货的,要减少仓库库存
- $sup['where_store_id'] = $order['source_id'];
- $sup['where_goods_id'] = $v['goods_id'];
- $sup[$num] = $v['num'];
- $state = Dever::db('store/goods')->$otherMethod($sup);
- if ($state) {
- $supSku = $sup;
- $supSku['where_sku_id'] = $v['sku_id'];
- $state = Dever::db('store/goods_sku')->$otherMethod($supSku);
- }
- }
- }
- }
- }
|