Product.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\model\system;
  3. use library\basic\BaseModel;
  4. use library\traits\ModelTrait;
  5. /**
  6. * TODO 商品Model
  7. * Class Product
  8. * @package app\model\system
  9. */
  10. class Product extends BaseModel
  11. {
  12. /**
  13. * 数据表主键
  14. * @var string
  15. */
  16. protected $pk = 'id';
  17. /**
  18. * 模型名称
  19. * @var string
  20. */
  21. protected $name = 'product';
  22. use ModelTrait;
  23. /**
  24. * 获取商品列表
  25. * @param $where array
  26. * @return array
  27. *
  28. */
  29. public static function ProductList($where)
  30. {
  31. $model = (new Product)->getWhere($where)->field([
  32. '*',
  33. '(SELECT SUM(stock) FROM table_product_attr_value WHERE product_id = table_product.id AND type = 0) as stock',
  34. '(SELECT SUM(sales) FROM table_product_attr_value WHERE product_id = table_product.id AND type = 0) as sales',
  35. '(SELECT name FROM table_merchant WHERE id = table_product.factory_id) as factory'
  36. ]);
  37. $model = $model->page((int)$where['page'], (int)$where['limit']);
  38. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  39. foreach ($data as &$item) {
  40. $cateName = Category::alias('c')->join('Category b', 'b.id = c.pid')->where('c.id', $item['cate_id'])->column('c.cate_name as two,b.cate_name as one', 'c.id');
  41. $item['cate_name'] = [];
  42. foreach ($cateName as $k => $v) {
  43. $item['cate_name'][] = $v['one'] . '/' . $v['two'];
  44. }
  45. $item['cate_name'] = is_array($item['cate_name']) ? implode(',', $item['cate_name']) : '';
  46. $item['stock_attr'] = $item['stock'] > 0 ? true : false;//库存
  47. }
  48. unset($item);
  49. $count = (new Product)->getWhere($where)->count();
  50. return ['count' => $count, 'list' => $data];
  51. }
  52. /**
  53. * 查询条件
  54. * @param $model
  55. * @return object
  56. */
  57. public function getWhere($where = [])
  58. {
  59. $model = new self();
  60. if (!empty($where)) {
  61. $type = $where['type'] ?? 0;
  62. switch ((int)$type) {
  63. case 1:
  64. $model = $model->where(['is_show' => 1, 'is_del' => 0]);
  65. break;
  66. case 2:
  67. $model = $model->where(['is_show' => 0, 'is_del' => 0]);
  68. break;
  69. case 3:
  70. $model = $model->where(['is_del' => 0]);
  71. break;
  72. case 4:
  73. $model = $model->where(['is_del' => 0])->whereIn('id', function ($query) {
  74. $query->name('product_attr_value')->where('stock', 0)->where('type', 0)->field('product_id')->select();
  75. })->where(function ($query) {
  76. $query->whereOr('stock', 0);
  77. });
  78. break;
  79. case 5:
  80. $model = $model->where(['is_show' => 1, 'is_del' => 0])->where('stock', '<=', 3)->where('stock', '>', 0);
  81. break;
  82. case 6:
  83. $model = $model->where(['is_del' => 1]);
  84. break;
  85. case 7:
  86. $model = $model->where(['is_one' => 1, 'is_del' => 0]);
  87. break;
  88. };
  89. if (isset($where['mer_id']) && $where['mer_id'] != '') {
  90. $model = $model->where('mer_id', $where['mer_id']);
  91. }
  92. if (isset($where['store_name']) && $where['store_name'] != '') {
  93. $model = $model->where('store_name|keyword|id', 'LIKE', "%$where[store_name]%");
  94. }
  95. if (isset($where['cate_id']) && $where['cate_id'] != '') {
  96. $model = $model->where('cate_id', $where['cate_id']);
  97. }
  98. if (isset($where['factory_id']) && $where['factory_id'] != '') {
  99. $model = $model->where('factory_id', $where['factory_id']);
  100. }
  101. if (isset($where['label']) && $where['label'] != '') {
  102. if($where['label'] == '4'){
  103. $model = $model->where('is_new', 1);
  104. }else{
  105. $model = $model->where('stars', $where['label']);
  106. }
  107. }
  108. $model = $model->order('sort desc,id desc');
  109. }
  110. return $model;
  111. }
  112. }