<?php

namespace Goods\Lib;

use Dever;

class Set
{
    # 获取商品及其属性列表
    public function getGoodsList($id, $type, $table)
    {
        $where['status'] = 1;

        if ($type != 'factory_id') {
            //$where['price_type'] = '1,2,3';
        }
        $data = Dever::db('goods/info')->getData($where);

        $result = array();
        if ($data) {
            $i = 0;
            foreach ($data as $k => $v) {
                
                $sku = Dever::db('goods/info_sku')->select(array('info_id' => $v['id']));

                if ($sku) {
                    $other = Dever::db($table)->find(array($type => $id, 'goods_id' => $v['id'], 'key' => -1));
                    $result[$i]['id'] = $v['id'];
                    $result[$i]['name'] = $v['name'];
                    $result[$i]['price'] = $v['price'];
                    $result[$i]['price_type'] = $v['price_type'];
                    $result[$i]['select'] = 2;
                    $result[$i]['del'] = 1;
                    if ($type == 'factory_id') {
                        if ($other) {
                            $result[$i]['p_price'] = $other['p_price'];
                            $result[$i]['select'] = $other['state'];
                        } else {
                            $result[$i]['p_price'] = $v['price'];
                        }
                    } else {
                        if ($other) {
                            $result[$i]['total'] = $other['total_num'] - $other['sell_num'];
                            $result[$i]['select'] = $other['state'];
                        } else {
                            $result[$i]['total'] = 0;
                        }
                    }
                    $result[$i]['children'] = array();
                    $num = count($sku);
                    if ($num == 1 && $sku[0]['key'] == -1) {
                        $result[$i]['end'] = true;
                    } else {
                        foreach ($sku as $k1 => $v1) {
                            $v1['sku_name'] = '';
                            if ($v1['attr']) {

                                $v1['attr'] = Dever::json_decode($v1['attr']);
                                if ($v1['attr']) {
                                    $v1['attr'] = Dever::load('attr/api')->getInfoByJson($v1['attr']);
                                    $v1['sku_name'] = $v1['attr']['string'];
                                }
                            }

                            $name = $v['name'];
                            if ($v1['sku_name']) {
                                $name .= '-' . $v1['sku_name'];
                            }

                            $children = array
                            (
                                'id' => $v['id'] . '-' . $v1['id'],
                                'name' => $name,
                                'price' => $v1['price'] ? $v1['price'] : 0,
                                'price_type' => $v['price_type'],
                                'del' => 1,
                                'end' => true,
                            );

                            $other_sku = Dever::db($table)->find(array($type => $id, 'goods_id' => $v['id'], 'sku_id' => $v1['id']));

                            $children['select'] = 2;
                            if ($type == 'factory_id') {
                                $children['p_price'] = $v1['price'];
                                if ($other_sku) {
                                    $children['p_price'] = $other_sku['p_price'];
                                    $children['select'] = $other_sku['state'];
                                }
                            } else {
                                $children['total'] = 0;
                                if ($other_sku) {
                                    $children['total'] = $other_sku['total_num'] - $other_sku['sell_num'];
                                    $children['select'] = $other_sku['state'];
                                }
                            }

                            $result[$i]['children'][] = $children;
                        }
                    }
                    $i++;
                }
            }
        }

        return $result;
    }
}