Sku.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 && $sku['key']) {
  21. $result['price'] = $sku['price'];
  22. $result['id'] = $sku['id'];
  23. if (isset($sku['num'])) {
  24. $result['num'] = $sku['num'];
  25. }
  26. $result['key'] = str_replace(',', '_', $sku['key']);
  27. $result['name'] = $this->getName($sku['key'], $app);
  28. } else {
  29. Dever::error('价格传入错误');
  30. }
  31. return $result;
  32. }
  33. # 多规格获取多个价格
  34. public function getList($where, $sku_id, $app)
  35. {
  36. $result = array();
  37. $sku = Dever::db('sku', $app)->select($where, array('order' => 'price asc,id desc'));
  38. if ($sku) {
  39. $result['sku'] = array();
  40. $result['spec'] = Dever::load('spec', 'api')->getList($where, $app);
  41. foreach ($sku as $k => $v) {
  42. $t = array();
  43. $k = str_replace(',', '_', $v['key']);
  44. $t['id'] = $v['id'];
  45. $t['pic'] = $v['pic'];
  46. $t['price'] = $v['price'];
  47. if (isset($v['num'])) {
  48. $t['num'] = $v['num'];
  49. }
  50. $result['price'][$k] = $t;
  51. if ($sku_id && $sku_id == $v['id']) {
  52. $t['name'] = $this->getName($v['key'], $app);
  53. $result['sku'] = $t;
  54. }
  55. }
  56. }
  57. return $result;
  58. }
  59. # 获取某个sku的名称
  60. public function getName($key, $app)
  61. {
  62. if (!$key || $key == '-1') {
  63. return '无规格';
  64. } else {
  65. $name = array();
  66. $where['id'] = array('in', $key);
  67. $data = Dever::db('spec_value', $app)->select($where);
  68. if ($data) {
  69. foreach ($data as $k => $v) {
  70. $info = Dever::db('spec', $app)->find($v['spec_id']);
  71. $name[] = $info['name'] . ':' . $v['value'];
  72. }
  73. }
  74. return implode(',', $name);
  75. }
  76. }
  77. }