<?php

namespace Store\Lib;

use Dever;

class Goods
{
    # 处理库存操作 1是增加，2是减少 1是总库存，2是销量
    public function oper($store_id, $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';
        }

        foreach ($data as $k => $v) {
            if (!$v['sku_id']) {
                $v['sku_id'] = -1;
            }

            $up = array();
            $up['where_store_id'] = $store_id;
            $up['where_goods_id'] = $v['goods_id'];
            $up[$num] = $v['num'];
            $state = Dever::db('store/goods')->$method($up);
            if ($state) {
                $upSku = $up;
                $upSku['where_sku_id'] = $v['sku_id'];
                $state = Dever::db('store/goods_sku')->$method($upSku);
                if (!$state) {
                    Dever::db('store/goods')->$otherMethod($up);
                }
            }
        }

        return $state;
    }

    # 获取店铺的商品列表
    public function getGoodsSku($store)
    {
        $table = 'store/goods_sku';
        $where['store_id'] = isset($store['id']) ? $store['id'] : $store;
        $method = 'getDataPage';
        
        $name = Dever::input('name');
        if ($name) {
            $where['name'] = $name;
        }
        $total = Dever::input('total');
        if ($total) {
            $where['total'] = $total;
        }
        
        $where['status'] = 1;
        $where['state'] = 1;
        $where['state_1'] = 1;
        $data = Dever::db($table)->$method($where);

        $result = array();
        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id'], 2);
                $data[$k]['total'] = $v['total'];
            }
        }

        return $data;
    }

    # 获取店铺的商品SKU列表
    public function getSkuList($store)
    {
        $table = 'store/goods_sku';
        $where['store_id'] = isset($store['id']) ? $store['id'] : $store;
        $method = 'getDataPage';
        
        $name = Dever::input('name');
        if ($name) {
            $where['name'] = $name;
        }
        $where['status'] = 1;
        $where['state'] = 1;
        $where['state_1'] = 1;
        $data = Dever::db($table)->$method($where);

        $result = array();
        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id'], 2);
                if ($data[$k]['price_type'] == 4) {
                    continue;
                }
                $data[$k]['total'] = $v['total'] ? $v['total'] : 0;
                if ($data[$k]['total'] <= 0) {
                    continue;
                }
                if (!isset($result[$v['id']])) {
                    $result[$v['id']]['id'] = $v['id'];
                    $result[$v['id']]['name'] = $v['name'];
                    $result[$v['id']]['price'] = $data[$k]['price'];
                    $result[$v['id']]['total'] = $data[$k]['total'];
                    $result[$v['id']]['children'] = array();
                }
                if (isset($data[$k]['attr']) && $data[$k]['attr']) {
                    $result[$v['id']]['children'][] = array
                    (
                        'id' => $v['id'] . '-' . $data[$k]['sku_id'],
                        'name' => $v['name'] . '-' . $data[$k]['sku_name'] . '[剩余'.$data[$k]['total'].'个]',
                        'price' => $data[$k]['price'],
                        'total' => $data[$k]['total'],
                        'end' => true,
                    );
                }

                if (!$result[$v['id']]['children']) {
                    $result[$v['id']]['name'] = $v['name'] . '[剩余'.$data[$k]['total'].'个]';
                    $result[$v['id']]['end'] = true;
                }
            }
        }

        return array_values($result);
    }

    # 获取库存
    public function getInfo($store_id, $info, $sku_id = false, $attr = true)
    {
        $where['store_id'] = $store_id;
        $where['goods_id'] = isset($info['goods_id']) ? $info['goods_id'] : $info;
        $other = Dever::db('store/goods_sku')->getData($where);
        $data = Dever::load('goods/lib/info')->getInfo($info, $attr, array($other, array('total')));

        if($data) {

            $sku_id = $sku_id ? $sku_id : $data['sku_id'];
            $data['total'] = 0;
            if ($data['price_type'] == 4) {
                if (isset($data['goods']) && is_array($data['goods'])) {
                    foreach ($data['goods'] as $k => $v) {
                        $data['goods'][$k]['total'] = $this->getTotal($other, -1);

                        if ($data['total'] > $data['goods'][$k]['total']) {
                            $data['total'] = $data['goods'][$k]['total'];
                        }
                    }
                }
            } else {
                $data['total'] = $this->getTotal($other, $sku_id);
            }
        }

        return $data;
    }

    # 验证库存
    public function checkTotal(&$num, $goods_id, $store_id, $sku_id, $state = 1)
    {
        $info = $this->getInfo($store_id, $goods_id, $sku_id, false);
        if (!$info) {
            Dever::alert('商品不存在');
        }
        $total = $info['total'];

        if ($num > $total) {
            if ($state == 2) {
                Dever::alert('库存不足');
            }
            $num = $total;
        }

        return $total;
    }

    # 获取库存
    public function getTotal($other, $sku_id)
    {
        if (isset($other[$sku_id])) {
            return $other[$sku_id]['total'];
        } else {
            return 0;
        }
    }
}