1], [['limit',], 'default', 'value' => 12], [['sort', 'sort_type', 'recommend_count'], 'integer',], [['sort',], 'default', 'value' => 0], [['goods_id',], 'string'], ]; } public function getMchId() { return empty($this->mch_id) ? 0 : $this->mch_id; } private function numToW($sales) { if ($sales < 10000) { return $sales; } else { return round($sales / 10000, 2) . 'W'; } } public function GoodsSearch() { if (!$this->validate()) return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; $query = Goods::find()->alias('g') ->leftJoin(['ag' => AdoptGoods::tableName()], 'g.id = ag.goods_id') ->where([ 'g.store_id' => $this->store_id, 'g.status' => 1, 'g.is_delete' => 0, 'g.md_food_id' => 0, 'g.product_type' => 5, ]); if ($this->keyword) { $query->andWhere([ 'or', ['like', 'g.name', $this->keyword], ['like', 'g.key_word', $this->keyword] ]); } if ($this->cat_id && $this->getMchId() == 0) { // $cat = Cat::find()->select('id')->where(['is_delete' => 0,])->andWhere(['OR', ['parent_id' => $this->cat_id], ['id' => $this->cat_id],])->column(); // $query->leftJoin(['gc' => GoodsCat::tableName()], 'gc.goods_id=g.id'); // $query->andWhere(['or', ['gc.is_delete' => 0], 'isnull(gc.id)']); // $query->andWhere( // [ // 'OR', // ['g.cat_id' => $cat], // ['gc.cat_id' => $cat], // ] // ); $query->leftJoin(['gc' => GoodsCat::tableName()], 'gc.goods_id=g.id'); $query->andWhere(['or', ['gc.is_delete' => 0], 'isnull(gc.id)']); $query->andWhere( [ 'OR', ['g.cat_id' => $this->cat_id], ['gc.cat_id' => $this->cat_id], ] ); // $query->leftJoin(['gc' => GoodsCat::tableName()], 'g.id = gc.goods_id')->andWhere([ // 'gc.cat_id' => $arr['cat_id'] // ])->groupBy('gc.goods_id'); } // if (get_md_id()) { // $md = Md::findOne(get_md_id()); // if ($md->is_single) { // $query->leftJoin(['mdg' => MdGoods::tableName()], 'mdg.goods_id=g.id'); // $query->andWhere(['g.product_type' => 0, 'mdg.status' => 1, 'mdg.md_id' => get_md_id()]); // } // } $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]); if ($this->sort == 0) { //综合,自定义排序+时间最新 $query->orderBy('g.sort DESC, g.created_at DESC'); } if ($this->sort == 1) { //时间最新 $query->orderBy('g.created_at DESC'); } if ($this->sort == 2) { if (get_md_id()) { //价格 if ($this->sort_type == 0) { $query->orderBy('mdg.price ASC'); } else { $query->orderBy('mdg.price DESC'); } } else { //价格 if ($this->sort_type == 0) { $query->orderBy('g.price ASC'); } else { $query->orderBy('g.price DESC'); } } } if ($this->sort == 3) { // if (get_md_id()) { // //销量 // if ($md->is_single) { // $query->orderBy([ // '( IF(gn.num, gn.num, 0) + mdg.virtual_sales)' => SORT_DESC, // 'g.created_at' => SORT_DESC, // ]); // } else { // $query->orderBy([ // '( IF(gn.num, gn.num, 0) + g.virtual_sales)' => SORT_DESC, // 'g.created_at' => SORT_DESC, // ]); // } // } else { // //销量 $query->orderBy([ '( IF(gn.num, gn.num, 0) + g.virtual_sales)' => SORT_DESC, 'g.created_at' => SORT_DESC, ]); // } } $od_query = OrderDetail::find()->alias('od') ->leftJoin(['o' => Order::tableName()], 'od.order_id=o.id') ->where(['od.is_delete' => 0, 'o.store_id' => $this->store_id, 'o.is_pay' => 1, 'o.is_delete' => 0])->groupBy('od.goods_id')->select('SUM(od.num) num,od.goods_id'); $select = 'g.status, g.goods_num, gn.num, g.id,g.name,g.price,g.original_price,g.cover_pic pic_url,g.virtual_sales,g.unit,ag.desc,ag.yield,ag.varieties,ag.growth_cycle'; // if (get_md_id()) { // if ($md->is_single) { // $select .= ',mdg.price as mdg_price,mdg.virtual_sales as mdg_virtual_sales, mdg.status as mdg_status, mdg.goods_num as mdg_goods_num, mdg.attr as mdg_attr'; // } // } $list = $query ->leftJoin(['gn' => $od_query], 'gn.goods_id=g.id') ->select($select) ->limit($pagination->limit) ->offset($pagination->offset) ->asArray()->all(); $md_id = get_md_id(); foreach ($list as $i => $item) { if (!$item['pic_url']) { $list[$i]['pic_url'] = Goods::getGoodsPicStatic($item['id']); } // $list[$i]['sales'] = $this->numToW($item['num'] + $item['virtual_sales']) . $item['unit']; $list[$i]['sales'] = $this->numToW($item['num'] + $item['virtual_sales']); $list[$i]['remaining_num'] = $this->numToW($item['goods_num'] - $list[$i]['sales']); $list[$i]['comment'] = OrderComment::find()->where(['store_id' => $this->store_id, 'goods_id' => $item['id'], 'is_hide' => 0, 'is_delete' => OrderComment::IS_DELETE_FALSE])->count(); $goods_comment_count = OrderComment::find()->where(['store_id' => $this->store_id, 'goods_id' => $item['id'], 'is_hide' => 0, 'is_delete' => OrderComment::IS_DELETE_FALSE]); $goods_count = $goods_comment_count->andWhere(['>', 'score', 2])->count(); $list[$i]['rank_good_percent'] = $goods_count > 0 ? round(($goods_count / $list[$i]['comment']) * 100) . '%' : 0 . '%'; if ($md_id && $md->is_single) { $list[$i]['price'] = $item['mdg_price']; $list[$i]['virtual_sales'] = $item['mdg_virtual_sales']; $list[$i]['goods_num'] = $item['mdg_goods_num']; $list[$i]['attr'] = $item['mdg_attr']; $list[$i]['status'] = $item['mdg_status']; } } $cat_list = AdoptCat::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'is_show' => 1])->orderBy(['sort' => SORT_DESC])->all(); $data = [ 'row_count' => $count, 'page_count' => $pagination->pageCount, 'list' => $list, // 'sql' => $query->createCommand()->getRawSql(), 'cat_list' => $cat_list ]; return [ 'code' => 0, 'data' => $data ]; } }