<?php

namespace Factory\Lib;

use Dever;

class Goods
{
    # 获取店铺的商品SKU列表
    public function getSkuList($factory)
    {
        $table = 'factory/goods_sku';
        $where['factory_id'] = isset($factory['id']) ? $factory['id'] : $factory;
        $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']);
                if ($data[$k]['status'] == 1) {
                    if (!isset($result[$v['id']])) {
                        $result[$v['id']]['id'] = $v['id'];
                        $result[$v['id']]['name'] = $v['name'];
                        $result[$v['id']]['min'] = $data[$k]['min'];
                        $result[$v['id']]['price'] = $v['p_price'] ? $v['p_price'] : 0;
                        $result[$v['id']]['c_price'] = $data[$k]['c_price'] ? $data[$k]['c_price'] : 0;
                        $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'],
                            'min' => $data[$k]['min'],
                            'price' => $v['p_price'] ? $v['p_price'] : 0,
                            'c_price' => $data[$k]['c_price'] ? $data[$k]['c_price'] : 0,
                            'end' => true,
                        );
                    }

                    if (!$result[$v['id']]['children']) {
                        $result[$v['id']]['end'] = true;
                    }
                }
                
            }
        }

        return array_values($result);
    }
}