12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php namespace Api\Lib;
- use Dever;
- class Sku
- {
- # 获取当前价格
- public function getPrice($where, $spec_type, $app)
- {
- /*
- $result['price'] = 0;
- $result['id'] = 0;
- $result['name'] = '';
- */
- $result = array();
- if ($spec_type == 2) {
- $where['key'] = '-1';
- $sku = Dever::db('sku', $app)->find($where);
- } elseif ($spec_type == 3) {
- $sku = Dever::db('sku', $app)->find($where, array('order' => 'price asc,id desc'));
- }
- if (isset($sku) && $sku && $sku['key']) {
- $result['price'] = $sku['price'];
- $result['id'] = $sku['id'];
- if (isset($sku['num'])) {
- $result['num'] = $sku['num'];
- }
- $result['key'] = str_replace(',', '_', $sku['key']);
- $result['name'] = $this->getName($sku['key'], $app);
- } else {
- Dever::error('价格传入错误');
- }
- return $result;
- }
- # 多规格获取多个价格
- public function getList($where, $sku_id, $app)
- {
- $result = array();
- $sku = Dever::db('sku', $app)->select($where, array('order' => 'price asc,id desc'));
- if ($sku) {
- $result['sku'] = array();
- $result['spec'] = Dever::load('spec', 'api')->getList($where, $app);
- foreach ($sku as $k => $v) {
- $t = array();
- $k = str_replace(',', '_', $v['key']);
- $t['id'] = $v['id'];
- $t['pic'] = $v['pic'];
- $t['price'] = $v['price'];
- if (isset($v['num'])) {
- $t['num'] = $v['num'];
- }
- $result['price'][$k] = $t;
- if ($sku_id && $sku_id == $v['id']) {
- $t['name'] = $this->getName($v['key'], $app);
- $result['sku'] = $t;
- }
- }
- }
- return $result;
- }
- # 获取某个sku的名称
- public function getName($key, $app)
- {
- if (!$key || $key == '-1') {
- return '无规格';
- } else {
- $name = array();
- $where['id'] = array('in', $key);
- $data = Dever::db('spec_value', $app)->select($where);
- if ($data) {
- foreach ($data as $k => $v) {
- $info = Dever::db('spec', $app)->find($v['spec_id']);
- $name[] = $info['name'] . ':' . $v['value'];
- }
- }
- return implode(',', $name);
- }
- }
- }
|