123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace app\model\system;
- use library\basic\BaseModel;
- use library\traits\ModelTrait;
- /**
- * TODO 商品Model
- * Class Product
- * @package app\model\system
- */
- class Product extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'product';
- use ModelTrait;
- /**
- * 获取商品列表
- * @param $where array
- * @return array
- *
- */
- public static function ProductList($where)
- {
- $model = (new Product)->getWhere($where)->field([
- '*',
- '(SELECT SUM(stock) FROM table_product_attr_value WHERE product_id = table_product.id AND type = 0) as stock',
- '(SELECT SUM(sales) FROM table_product_attr_value WHERE product_id = table_product.id AND type = 0) as sales',
- '(SELECT name FROM table_merchant WHERE id = table_product.factory_id) as factory'
- ]);
- $model = $model->page((int)$where['page'], (int)$where['limit']);
- $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
- foreach ($data as &$item) {
- $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');
- $item['cate_name'] = [];
- foreach ($cateName as $k => $v) {
- $item['cate_name'][] = $v['one'] . '/' . $v['two'];
- }
- $item['cate_name'] = is_array($item['cate_name']) ? implode(',', $item['cate_name']) : '';
- $item['stock_attr'] = $item['stock'] > 0 ? true : false;//库存
- }
- unset($item);
- $count = (new Product)->getWhere($where)->count();
- return ['count' => $count, 'list' => $data];
- }
- /**
- * 查询条件
- * @param $model
- * @return object
- */
- public function getWhere($where = [])
- {
- $model = new self();
- if (!empty($where)) {
- $type = $where['type'] ?? 0;
- switch ((int)$type) {
- case 1:
- $model = $model->where(['is_show' => 1, 'is_del' => 0]);
- break;
- case 2:
- $model = $model->where(['is_show' => 0, 'is_del' => 0]);
- break;
- case 3:
- $model = $model->where(['is_del' => 0]);
- break;
- case 4:
- $model = $model->where(['is_del' => 0])->whereIn('id', function ($query) {
- $query->name('product_attr_value')->where('stock', 0)->where('type', 0)->field('product_id')->select();
- })->where(function ($query) {
- $query->whereOr('stock', 0);
- });
- break;
- case 5:
- $model = $model->where(['is_show' => 1, 'is_del' => 0])->where('stock', '<=', 3)->where('stock', '>', 0);
- break;
- case 6:
- $model = $model->where(['is_del' => 1]);
- break;
- case 7:
- $model = $model->where(['is_one' => 1, 'is_del' => 0]);
- break;
- };
- if (isset($where['mer_id']) && $where['mer_id'] != '') {
- $model = $model->where('mer_id', $where['mer_id']);
- }
- if (isset($where['store_name']) && $where['store_name'] != '') {
- $model = $model->where('store_name|keyword|id', 'LIKE', "%$where[store_name]%");
- }
- if (isset($where['cate_id']) && $where['cate_id'] != '') {
- $model = $model->where('cate_id', $where['cate_id']);
- }
- if (isset($where['factory_id']) && $where['factory_id'] != '') {
- $model = $model->where('factory_id', $where['factory_id']);
- }
- if (isset($where['label']) && $where['label'] != '') {
- if($where['label'] == '4'){
- $model = $model->where('is_new', 1);
- }else{
- $model = $model->where('stars', $where['label']);
- }
- }
- $model = $model->order('sort desc,id desc');
- }
- return $model;
- }
- }
|