Sku.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php namespace Api\Lib;
  2. use Dever;
  3. class Sku
  4. {
  5. # 获取当前价格
  6. public function getPrice($where, $spec_type, $app)
  7. {
  8. /*
  9. $result['price'] = 0;
  10. $result['id'] = 0;
  11. $result['name'] = '';
  12. */
  13. $result = array();
  14. if ($spec_type == 2) {
  15. $where['key'] = '-1';
  16. $sku = Dever::db('sku', $app)->find($where);
  17. } elseif ($spec_type == 3) {
  18. $sku = Dever::db('sku', $app)->find($where, array('order' => 'price asc,id desc'));
  19. }
  20. if (isset($sku) && $sku) {
  21. $result['price'] = $sku['price'];
  22. $result['id'] = $sku['id'];
  23. $result['key'] = str_replace(',', '_', $sku['key']);
  24. $result['name'] = $this->getName($sku['key'], $app);
  25. }
  26. return $result;
  27. }
  28. # 多规格获取多个价格
  29. public function getList($where, $sku_id, $app)
  30. {
  31. $result = array();
  32. $sku = Dever::db('sku', $app)->select($where, array('order' => 'price asc,id desc'));
  33. if ($sku) {
  34. $result['info'] = array();
  35. $result['spec'] = Dever::load('spec', 'api')->getList($where, $app);
  36. foreach ($sku as $k => $v) {
  37. $t = array();
  38. $k = str_replace(',', '_', $v['key']);
  39. $t['id'] = $v['id'];
  40. $t['pic'] = $v['pic'];
  41. $t['value'] = $v['price'];
  42. $result['price'][$k] = $t;
  43. if ($sku_id && $sku_id == $v['id']) {
  44. $t['name'] = $this->getName($v['key'], $app);
  45. $result['info'] = $t;
  46. }
  47. }
  48. }
  49. return $result;
  50. }
  51. # 获取某个sku的名称
  52. public function getName($key, $app)
  53. {
  54. if (!$key || $key == '-1') {
  55. return '';
  56. } else {
  57. $name = array();
  58. $where['id'] = array('in', $key);
  59. $data = Dever::db('spec_value', $app)->select($where);
  60. if ($data) {
  61. foreach ($data as $k => $v) {
  62. $info = Dever::db('spec', $app)->find($v['spec_id']);
  63. $name[] = $info['name'] . ':' . $v['value'];
  64. }
  65. }
  66. return implode(',', $name);
  67. }
  68. }
  69. }