<?php

namespace Shop\Lib;

use Dever;

class Goods
{
    # 根据店铺获取商品列表
    public function getList()
    {
        $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);
                $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')->find($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_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);
                }
            }
        }
    }
}