1], [['limit'], 'default', 'value' => 20], [['status'], 'in', 'range' => [0, 1, 2]], [['cx_mch_id'], 'required'], [['start_time', 'end_time',], 'safe'], [['goods_id'], 'required', 'on' => 'detail'], ]; } public function search() { if (!$this->validate()) { return $this->getModelError(); } $cat_query = GoodsCat::find() ->where([ 'is_delete' => 0, ]) ->select('goods_hub_id,cat_id,cat1_id,cat2_id,cat3_id,cat4_id'); $query = $this->getQuery($cat_query); $query = $query->where([ 'g.cx_mch_id' => $this->cx_mch_id, // 'g.is_delete' => 0, // 'gh.is_delete' => 0, // 'g.plugin_sign' => $this->plugin_sign, ]) ->andFilterWhere([ 'OR', ['LIKE', 'gh.name', $this->keywords], // ['LIKE', 'gh.subtitle', $this->keywords], ]) ->andFilterWhere([ 'g.id' => $this->goods_id, 'gc.cat_id' => $this->cat_id ]); if ($this->status == 2) { $query = $query->andWhere(['g.goods_stock' => 0]);//售罄 } else { $query = $query->andFilterWhere(['g.status' => $this->status]); } $query = $this->switchTime($query); $query = $this->getFields($query); $count_query = clone $query; $count = $count_query->count(); $pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]); $list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['g.sort' => SORT_ASC, 'g.created_at' => SORT_DESC])->asArray()->all(); $list = $this->handleList($list); //是否已经全部加载 $end_flag = $this->page > $pagination->pageCount ? true : false; return [ 'code' => 0, 'msg' => 'ok', 'data' => $list, 'count' => $count, 'page_size' => $this->limit, 'page_count' => $pagination->pageCount, 'page_no' => $this->page, 'end_flag' => $end_flag ]; } protected function getQuery($cat_query) { $query = Goods::find()->alias('g') ->leftJoin(['gh' => GoodsHub::tableName()], 'gh.id=g.goods_hub_id') ->leftJoin(['gc' => $cat_query], 'gc.goods_hub_id=gh.id'); return $query; } public function setFields($fields) { $this->fields = $fields; } protected function getFields($query) { $fields = is_array($this->fields) ? implode(',', $this->fields) : $this->fields; return $query->select($fields); } protected function handleList($list) { foreach ($list as $index => $item) { $item['status_cn'] = $item['status'] == Goods::STATUS_ONLINE ? '正常' : '隐藏'; $item['created_at_cn'] = date('Y-m-d H:i:s', $item['created_at']); $item['date_at_cn'] = date('Y-m-d', $item['date']); $item['cat_list'] = $this->getCatList($item['goods_hub_id']); $item['cat1_list'] = $this->getCat1List($item['goods_hub_id']); $item['cat2_list'] = $this->getCat2List($item['goods_hub_id']); $item['cat3_list'] = $this->getCat3List($item['goods_hub_id']); $item['cat4_list'] = $this->getCat4List($item['goods_hub_id']); $item['cover_pic'] = SiteHelper::getFullUrl($item['cover_pic']); if ($this->scenario == 'detail') { $item['detail'] = SiteHelper::repairContent($item['detail']); $item['pic_urls'] = $this->fixPicUrls($item['pic_urls']); $item['video_url'] = SiteHelper::getFullUrl($item['video_url']); $item['goods_sales'] = $item['virtaul_sales'] + $item['sales']; $item['attr_groups'] = empty($item['attr_groups']) ? [] : json_decode($item['attr_groups'], true); $item['attr'] = $this->getAttr($item['id']); } $list[$index] = $item; } return $list; } protected function switchTime($query) { if (!empty($this->start_time)) { $query = $query->andWhere([ '>', 'g.created_at', strtotime($this->start_time) ]); } if (!empty($this->end_time)) { $query = $query->andWhere([ '<', 'g.created_at', strtotime($this->end_time) ]); } return $query; } protected function getCatList($goods_hub_id) { switch ($this->plugin_sign) { case SysConst::$cxPluginSceneMall: $type = Cat::TYPE_GOODS; break; case SysConst::$cxPluginSceneIntegralMall: $type = Cat::TYPE_INTEGRAL_GOODS; break; //@TODO 其他 default: $type = Cat::TYPE_GOODS; } $list = GoodsCat::find()->alias('gc') ->leftJoin(['c' => Cat::tableName()], 'c.id=gc.cat_id') ->where([ 'gc.is_delete' => 0, 'gc.goods_hub_id' => $goods_hub_id, 'c.is_delete' => 0, 'c.type' => $type, ]) ->select('c.name,c.id as cat_id')->asArray()->all(); return $list; } protected function getCat1List($goods_hub_id) { $list = GoodsCat::find()->alias('gc') ->leftJoin(['c' => Cat1::tableName()], 'c.id=gc.cat1_id') ->where([ 'gc.is_delete' => 0, 'gc.goods_hub_id' => $goods_hub_id, 'c.is_delete' => 0, ]) ->select('c.name,c.id as cat_id')->asArray()->all(); return $list; } protected function getCat2List($goods_hub_id) { $list = GoodsCat::find()->alias('gc') ->leftJoin(['c' => Cat2::tableName()], 'c.id=gc.cat2_id') ->where([ 'gc.is_delete' => 0, 'gc.goods_hub_id' => $goods_hub_id, 'c.is_delete' => 0, ]) ->select('c.name,c.id as cat_id')->asArray()->all(); return $list; } protected function getCat3List($goods_hub_id) { $list = GoodsCat::find()->alias('gc') ->leftJoin(['c' => Cat3::tableName()], 'c.id=gc.cat3_id') ->where([ 'gc.is_delete' => 0, 'gc.goods_hub_id' => $goods_hub_id, 'c.is_delete' => 0, ]) ->select('c.name,c.id as cat_id')->asArray()->all(); return $list; } protected function getCat4List($goods_hub_id) { $list = GoodsCat::find()->alias('gc') ->leftJoin(['c' => Cat4::tableName()], 'c.id=gc.cat4_id') ->where([ 'gc.is_delete' => 0, 'gc.goods_hub_id' => $goods_hub_id, 'c.is_delete' => 0, ]) ->select('c.name,c.id as cat_id')->asArray()->all(); return $list; } protected function fixPicUrls($pic_urls) { $pic_urls = is_string($pic_urls) && !empty($pic_urls) ? json_decode($pic_urls, true) : $pic_urls; foreach ($pic_urls as $index => $item) { $item = SiteHelper::getFullUrl($item); $pic_urls[$index] = $item; } return $pic_urls; } //获取商品规格 private function getAttr($goods_id) { $attr = GoodsAttr::find()->alias('gr') ->where([ 'gr.goods_id' => $goods_id, 'gr.is_delete' => 0 ]) ->select('gr.id,gr.goods_id,gr.price,gr.sign_id,gr.stock,gr.serial_no,gr.weight,gr.cover_pic') ->asArray()->all(); foreach ($attr as $index => $item) { $item['cover_pic'] = SiteHelper::getFullUrl($item['cover_pic']); $attr[$index] = $item; } return $attr; } }