| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\models\Cart;
- use app\models\Cat;
- use app\models\Goods;
- use app\models\GoodsBook;
- use app\models\GoodsBrowse;
- use app\models\GoodsCat;
- use app\models\GoodsPic;
- use app\models\Mch;
- use app\models\MchCat;
- use app\models\Md;
- use app\models\Order;
- use app\models\OrderComment;
- use app\models\OrderDetail;
- use yii\base\BaseObject;
- use yii\base\Model;
- use yii\data\Pagination;
- use app\models\MchGoodsCat;
- use app\modules\client\models\v1\common\CommonGoods;
- use app\models\MdGoods;
- use yii\helpers\Json;
- use app\constants\OptionSetting;
- use app\models\Option;
- class GoodsListForm extends Model
- {
- public $store_id;
- public $keyword;
- public $cat_id;
- public $page;
- public $limit;
- public $sort;
- public $sort_type;
- public $goods_id;
- public $pic_url;
- public $recommend_count;
- public $mch_id;
- public $type;
- public $start_time;
- public $end_time;
- public $rate;
- public $user_id;
- public $brand_id;
- // 是否要查询支持装配的商品
- public $is_accessories = false;
- // 是否要查询视频号小店的商品
- public $is_videoShopGoods = false;
- public function rules()
- {
- return [
- [['keyword'], 'trim'],
- [['store_id', 'cat_id', 'page', 'limit', 'type', 'user_id'], 'integer'],
- [['limit', 'brand_id'], 'integer',],
- [['page',], 'default', 'value' => 1],
- [['limit',], 'default', 'value' => 12],
- [['sort', 'sort_type', 'recommend_count'], 'integer',],
- [['sort',], 'default', 'value' => 0],
- [['goods_id', 'start_time', 'end_time'], 'string'],
- [['is_videoShopGoods', 'rate'], 'safe'],
- [['mch_id'], 'safe'],
- ];
- }
- public function getMchId()
- {
- return empty($this->mch_id) ? 0 : $this->mch_id;
- }
- public function search()
- {
- if (!$this->validate())
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- $userLevel = get_user()->level;
- $query = Goods::find()->alias('g')->leftJoin(['gb' => GoodsBrowse::tableName()],'g.id=gb.goods_id')->where([
- 'g.status' => 1,
- 'g.is_delete' => 0,
- 'g.md_food_id' => 0
- ])->andWhere(['!=', 'g.product_type', Goods::GOODS_TYPE_ADOPT])->andWhere(['!=', 'g.product_type', Goods::GOODS_TYPE_INTEGRAL]);
- // 这里判断商品浏览权限
- if(empty($userLevel)){
- $query->andWhere(['gb.goods_id' => null]);
- }else{
- $query->andWhere(['or',['gb.goods_id' => null],['gb.level' => $userLevel]]);
- }
- 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()]);
- }
- }
-
- if ($this->mch_id){
- $query->andWhere(['g.mch_id' => $this->mch_id]);
- }
- if ($this->store_id)
- $query->andWhere(['g.store_id' => $this->store_id]);
- if ($this->cat_id) {
- if (!$this->mch_id) {
- $cat_id = $this->getCatId($this->cat_id);
- array_push($cat_id, $this->cat_id);
- $cat = Cat::find()->where(['is_delete' => 0, 'id' => $cat_id])->select('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],
- ]
- );
- }else{
- $query->andWhere(['g.id' => MchGoodsCat::find()->select('goods_id')->where([
- 'cat_id' => MchCat::find()->select('id')->where([
- 'or',
- ['id' => $this->cat_id],
- ['parent_id' => $this->cat_id],
- ])->andWhere(['is_show' => 1])
- ])]);
- }
- }
- if ($this->goods_id) {
- $arr = explode(',', $this->goods_id);
- $query->andWhere(['in', 'g.id', $arr]);
- }
- if ($this->brand_id) {
- $query->andWhere(['g.brand_id' => $this->brand_id]);
- }
- if ($this->keyword)
- $query->andWhere(['LIKE', 'g.name', $this->keyword]);
- if($this->is_accessories){
- //$query->andWhere(['not',['g.accessories_image' => null]]);
- $query->andWhere(['<>','g.accessories_image', '']);
- }
- $count = $query->groupBy('g.id')->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 ($this->sort_type == 0) {
- $query->orderBy('g.price DESC');
- } else {
- $query->orderBy('g.price ASC');
- }
- }
- if ($this->sort == 3) {
- //销量
- if ($this->sort_type == 0) {
- $query->orderBy([
- '( IF(gn.num, gn.num, 0) + virtual_sales)' => SORT_DESC,
- 'g.created_at' => SORT_DESC,
- ]);
- } else {
- $query->orderBy([
- '( IF(gn.num, gn.num, 0) + virtual_sales)' => SORT_ASC,
- 'g.created_at' => SORT_ASC,
- ]);
- }
- }
- $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.goods_num, g.order_min_count, g.status, g.id, g.name,g.use_attr,g.price,g.original_price,g.cover_pic pic_url,gn.num,g.virtual_sales,g.unit,g.is_negotiable,g.attr,g.mch_id,g.is_level,g.product_type,g.accessories_image';
- if (get_md_id() && $md->is_single) {
- $select .= ',mdg.virtual_sales as mdg_virtual_sales,mdg.attr as mdg_attr,mdg.price as mdg_price, mdg.status as mdg_status, mdg.goods_num as mdg_goods_num';
- }
- $list = $query
- ->leftJoin(['gn' => $od_query], 'gn.goods_id=g.id')
- ->select($select)
- ->limit($pagination->limit)
- ->offset($pagination->offset)
- ->asArray()->groupBy('g.id')->all();
- $md_id = get_md_id();
- foreach ($list as $i => $item) {
- if (!$item['pic_url']) {
- $list[$i]['pic_url'] = Goods::getGoodsPicStatic($item['id']);
- }
- if ($item['is_negotiable']) {
- $list[$i]['price'] = Goods::GOODS_NEGOTIABLE;
- }
- $list[$i]['sales'] = $this->numToW($item['num'] + $item['virtual_sales']) . $item['unit'];
- $res = CommonGoods::getMemberPrice([
- 'attr' => $item['attr'],
- 'price' => $item['price'],
- 'mch_id' => $item['mch_id'],
- 'is_level' => $item['is_level'],
- ]);
- $goodsMemberPrice = $res['min_member_price'] ? $res['min_member_price'] : $item['price'];
- $list[$i]['goods_member_price'] = sprintf("%.2f", $goodsMemberPrice);
- if (!$item['is_negotiable']) {// 说让分类页面要显示会员价
- $list[$i]['price'] = $list[$i]['goods_member_price'];
- }
- $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]['attr'] = $item['mdg_attr'];
- $list[$i]['virtual_sales'] = $item['mdg_virtual_sales'];
- $list[$i]['goods_num'] = $item['mdg_goods_num'];
- $list[$i]['price'] = $item['mdg_price'];
- $list[$i]['status'] = $item['mdg_status'];
- }
- // 转换price为字符串
- $list[$i]['price'] = (string)$list[$i]['price'];
- $list[$i]['cart_num'] = 0;
- if ($this->user_id) {
- if ($md_id <= 0) {
- $md_id = [0, -1];
- }
- $list[$i]['cart_num'] = Cart::find()->where(['type'=>Cart::TYPE_DEFAULT,'user_id' => $this->user_id, 'is_delete' => 0, 'goods_id' => $item['id'], 'md_id' => $md_id])
- ->sum('num') ?: 0;
- }
- }
- $data = [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $list,
- ];
- return [
- 'code' => 0,
- 'data' => $data
- ];
- }
- public function getCatId($cat_id) {
- $arr = [];
- $cat = Cat::find()->where(['id' => $cat_id, 'store_id' => $this->store_id])->select('id')->asArray()->one();
- if ($cat) {
- $child_cat = Cat::find()->where(['parent_id' => $cat_id, 'store_id' => $this->store_id])->select('id')->asArray()->all();
- foreach ($child_cat as $item) {
- $arr[] = $item['id'];
- $this->getCatId($item['id']);
- }
- }
- return $arr;
- }
- public function recommend()
- {
- if (!$this->validate())
- return $this->errorResponse;
- $goods_id = $this->goods_id;
- if (!$goods_id) {
- return [
- 'code' => 1,
- 'msg' => 'err'
- ];
- }
- $cat_ids = [];
- $goods = Goods::find()->select('*')->where(['store_id' => $this->store_id, 'is_delete' => 0, 'type' => get_plugin_type(), 'md_food_id' => 0])->andWhere('id=:id', [':id' => $goods_id])->one();
- $cat_id = $goods->cat_id;
- if ($goods->mch_id > 0) {
- if ($cat_id == 0) {
- $goodsCat = MchGoodsCat::find()->select('cat_id')->where(['goods_id' => $goods_id])->all();
- $goods_cat = [];
- foreach ($goodsCat as $v) {
- $goods_cat[] = $v->cat_id;
- }
- } else {
- $goods_cat = array(intval($cat_id));
- }
- $cat_ids = $goods_cat;
- //查询
- $goodscat_list = MchGoodsCat::find()->select(['goods_id'])->andWhere(['in', 'cat_id', $cat_ids])->all();
- }else {
- if ($cat_id == 0) {
- $goodsCat = GoodsCat::find()->select('cat_id')->where(['store_id' => $this->store_id, 'goods_id' => $goods_id, 'is_delete' => 0])->all();
- $goods_cat = [];
- foreach ($goodsCat as $v) {
- $goods_cat[] = $v->cat_id;
- }
- } else {
- $goods_cat = array(intval($cat_id));
- }
- $cat_ids = $goods_cat;
- //查询
- $goodscat_list = GoodsCat::find()->select(['goods_id'])->where(['store_id' => $this->store_id, 'is_delete' => 0])->andWhere(['in', 'cat_id', $cat_ids])->all();
- }
- $cats = [];
- foreach ($goodscat_list as $v) {
- $cats[] = $v->goods_id;
- }
- $query = Goods::find()->alias('g')
- ->where(['and', "g.id!=$goods_id", 'cat_id=0', "g.store_id=$this->store_id", 'g.is_delete=0', 'g.status=1', ['in', 'g.id', $cats]])
- ->orWhere(['and', "g.id!=$goods_id", "g.store_id=$this->store_id", 'g.is_delete=0', 'g.status=1', ['in', 'g.cat_id', $cat_ids]])
- ->andWhere(['g.type' => get_plugin_type()])
- ->andWhere(['not like', 'g.name', '当面付'])
- ->leftJoin(['m' => Mch::tableName()], 'm.id=g.mch_id')
- ->andWhere([
- 'or',
- ['g.mch_id' => 0],
- ['m.is_delete' => 0]
- ]);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
- $query->orderBy('g.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');
- $limit = $pagination->limit;
- $offset = $pagination->offset;
- $recommend_count = $this->recommend_count;
- if ($offset > $recommend_count) {
- return [
- 'code' => 1,
- 'msg' => 'err'
- ];
- } else if ($offset + $limit > $recommend_count) {
- $limit = $recommend_count - $offset;
- }
- $list = $query
- ->leftJoin(['gn' => $od_query], 'gn.goods_id=g.id')
- ->select('g.id,g.name,g.price,g.original_price,g.cover_pic pic_url,gn.num,g.virtual_sales,g.unit')
- ->limit($limit)
- ->offset($pagination->offset)
- ->asArray()->groupBy('g.id')->all();
- foreach ($list as $i => $item) {
- if (!$item['pic_url']) {
- $list[$i]['pic_url'] = Goods::getGoodsPicStatic($item['id'])->pic_url;
- }
- $list[$i]['sales'] = $this->numToW($item['num'] + $item['virtual_sales']) . $item['unit'];
- }
- $data = [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $list,
- ];
- return [
- 'code' => 0,
- 'data' => $data
- ];
- }
- private function numToW($sales)
- {
- if ($sales < 10000) {
- return $sales;
- } else {
- return round($sales / 10000, 2) . 'W';
- }
- }
- public function couponSearch()
- {
- $arr = explode(",", $this->goods_id);
- $query = Goods::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'status' => 1])->andWhere(['in', 'id', $arr]);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
- if ($this->sort == 0) {
- //综合,自定义排序+时间最新
- $query->orderBy('sort ASC,created_at DESC');
- }
- if ($this->sort == 1) {
- //时间最新
- $query->orderBy('created_at DESC');
- }
- if ($this->sort == 2) {
- //价格
- if ($this->sort_type == 0) {
- $query->orderBy('price ASC');
- } else {
- $query->orderBy('price DESC');
- }
- }
- if ($this->sort == 3) {
- //销量
- $query->orderBy([
- 'virtual_sales' => SORT_DESC,
- 'created_at' => SORT_DESC,
- ]);
- }
- $list = $query
- ->select(['id', 'name', 'cover_pic as pic_url', 'price', 'original_price', 'virtual_sales as sales', 'unit'])
- ->limit($pagination->limit)
- ->offset($pagination->offset)
- ->asArray()->all();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $list,
- ],
- ];
- }
- public function newSearch()
- {
- if (!$this->validate())
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- $is_shenhe = Option::get(OptionSetting::WECHAT_IS_SHENHE, $this->store_id, 'wechat', '0')['value'];
- $query = Goods::find()->alias('g')->leftJoin(['gb' => GoodsBrowse::tableName()],'g.id=gb.goods_id')->where([
- 'g.store_id' => $this->store_id,
- 'g.status' => 1,
- 'g.is_delete' => 0,
- 'g.md_food_id' => 0
- ])->andWhere(['AND', ['!=', 'g.product_type',Goods::GOODS_TYPE_ADOPT], ['!=', 'g.product_type',Goods::GOODS_TYPE_INTEGRAL]]); //, ['!=', 'g.product_type',Goods::GOODS_TYPE_WORKER]
- $userLevel = get_user()->level;
- // 这里判断商品浏览权限
- if(empty($userLevel)){
- $query->andWhere(['gb.goods_id' => null]);
- }else{
- $query->andWhere(['or',['gb.goods_id' => null],['gb.level' => $userLevel]]);
- }
- if (in_array($this->type, Goods::$validProductType)) {
- $query->andWhere(['g.product_type' => $this->type]);
- }
- if (show_shenhe()) {
- $shenhe_goods = Option::get(OptionSetting::ACTIVITY_MINI_AUDIT_GOODS, $this->store_id, 'wechat', '')['value'];
- if (!empty($shenhe_goods)) {
- $this->goods_id = $shenhe_goods;
- }
- }
- if ($this->goods_id) {
- $arr = explode(',', $this->goods_id);
- $query->andWhere(['in', 'g.id', $arr]);
- }
- if ($this->mch_id) {
- $query->andWhere(['mch_id' => $this->mch_id]);
- }
- if ($this->keyword) {
- $query->andWhere([
- 'or',
- ['like', 'g.name', $this->keyword],
- ['like', 'g.sub_name', $this->keyword],
- ['like', 'g.key_word', $this->keyword],
- ['like', 'g.goods_no', $this->keyword]
- ]);
- }
- 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()]);
- $query->andWhere(['OR', [
- 'g.product_type' => 0,
- 'mdg.status' => 1,
- 'mdg.md_id' => get_md_id()
- ], [
- 'g.product_type' => [
- 1, 2
- ]
- ]]);
- }
- }
- if($this->is_videoShopGoods){
- $query->leftJoin(['vsg' => \app\models\VideoShopGoodsExt::tableName()], 'vsg.goods_id = g.id');
- $query->leftJoin(['sm' => \app\models\StoreMini::tableName()], 'vsg.mini_id = sm.id');
- $query->andWhere(['vsg.wx_status' => 5, 'vsg.is_delete' => 0, 'sm.is_cancle' => 0]);
- $query->andWhere(['>', 'vsg.id','0']);
- }
- $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,
- ]);
- }
-
- }
- // if ($this->is_videoShopGoods && $this->sort == 5) {
- // $query->orderBy(['( IF(g.rate_type, g.rate, g.price * g.rate / 100))' => 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, g.product_type, g.attr, g.is_level ';
- 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';
- }
- }
- if ($this->is_videoShopGoods) {
- $select .= ',vsg.mini_id video_shop_mini_id, vsg.product_id wx_product_id, g.rate_type, g.rate';
- }
- $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']);
- }
- if ($item['rate']) {
- $list[$i]['rate_price'] = sprintf("%.2f", $item['rate_type'] ? $item['rate'] : ($item['rate'] * $item['price'] / 100));
- }
- $list[$i]['sales'] = $this->numToW($item['num'] + $item['virtual_sales']) . $item['unit'];
- $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 && !in_array($list[$i]['product_type'], [1, 2])) {
- $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'];
- }
- $list[$i]['product_type'] = intval($list[$i]['product_type']);
- if ($this->is_videoShopGoods) {
- $rebate = (new \app\modules\client\models\v1\GoodsForm())->getShareCommissionMoney(Goods::findone($item['id']), get_user_id());
- $list[$i]['rebate'] = floatval(sprintf('%.2f', $rebate));
- }
- $res = CommonGoods::getMemberPrice([
- 'attr' => $item['attr'],
- 'price' => $item['price'],
- 'mch_id' =>0,
- 'is_level' => $item['is_level'],
- ]);
- $goodsMemberPrice = $res['min_member_price'] ?: $item['price'];
- $list[$i]['price'] = sprintf("%.2f", $goodsMemberPrice);
- }
- $data = [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $list,
- ];
- return [
- 'code' => 0,
- 'data' => $data
- ];
- }
- /**
- * 酒店商品搜索
- */
- public function searchHotel() {
- if (!$this->validate())
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- $query = Goods::find()->alias('g')->where([
- 'g.store_id' => $this->store_id,
- 'g.status' => 1,
- 'g.is_delete' => 0,
- 'g.md_food_id' => 0,
- 'g.product_type' => Goods::GOODS_TYPE_DATE
- ])->leftJoin(['gb' => GoodsBook::tableName()], 'gb.goods_id = g.id');
- if ($this->keyword) {
- $query->andWhere([
- 'or',
- ['like', 'g.name', $this->keyword],
- ['like', 'g.key_word', $this->keyword]
- ]);
- }
- 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 ($this->sort_type == 0) {
- $query->orderBy('g.price ASC');
- } else {
- $query->orderBy('g.price DESC');
- }
- }
- if ($this->sort == 3) {
- //销量
- $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 = 'gb.date_book, 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';
- $list = $query
- ->leftJoin(['gn' => $od_query], 'gn.goods_id=g.id')
- ->select($select)
- ->asArray()->all();
- foreach ($list as $i => $item) {
- if ($this->start_time && $this->end_time) {
- $date_book = Json::decode($item['date_book']);
- if (is_array($date_book)) {
- $date_book = array_column($date_book, 'date');
- $first_date = array_shift($date_book);
- $last_date = array_pop($date_book);
- if ((strtotime($this->start_time) < strtotime($first_date) && strtotime($this->end_time) < strtotime($first_date))
- || (strtotime($this->start_time) > strtotime($last_date) && strtotime($this->end_time) > strtotime($last_date))) {
- unset($list[$i]);
- continue;
- }
- }
- }
- 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]['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 . '%';
- $list[$i]['product_type'] = Goods::GOODS_TYPE_DATE;
- }
- $data = array_slice($list, ($this->page - 1) * $this->limit, $this->limit);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'row_count' => count($list),
- 'page_count' => ceil(count($list) / $this->limit),
- 'list' => $data,
- ]
- ];
- }
- }
|