| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\models\Cat;
- use app\models\Goods;
- use app\models\SaasCategory;
- use app\models\Store;
- use app\models\GoodsBook;
- use app\models\GoodsCat;
- use app\models\GoodsPic;
- use app\models\Mch;
- use app\models\Md;
- use app\models\Order;
- use app\models\OrderComment;
- use app\models\OrderDetail;
- use app\models\WechatConfig;
- use yii\base\BaseObject;
- use yii\base\Model;
- use yii\data\Pagination;
- use app\models\MchGoodsCat;
- use app\modules\alliance\models\common\CommonGoods;
- use app\models\MdGoods;
- use yii\helpers\Json;
- 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 $searchType;
- public function rules()
- {
- return [
- [['keyword'], 'trim'],
- [['store_id', 'cat_id', 'mch_id', 'page', 'limit', 'type'], 'integer'],
- [['limit'], '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', 'searchType'], 'string'],
- ];
- }
- 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]
- ];
- $query = Goods::find()->alias('g')->where([
- 'g.status' => 1,
- 'g.is_delete' => 0,
- 'g.md_food_id' => 0
- ])->andWhere(['!=', 'g.product_type', Goods::GOODS_TYPE_ADOPT]);
- 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->store_id)
- $query->andWhere(['g.store_id' => $this->store_id]);
- 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],
- ]
- );
- }
- if ($this->goods_id) {
- $arr = explode(',', $this->goods_id);
- $query->andWhere(['in', 'g.id', $arr]);
- }
- if ($this->keyword)
- $query->andWhere(['LIKE', 'g.name', $this->keyword]);
- $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 ($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) + 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.goods_num, 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';
- 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'] = round($goodsMemberPrice,2);
- $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'];
- }
- }
- $data = [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $list,
- ];
- return [
- 'code' => 0,
- 'data' => $data
- ];
- }
- 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]
- ];
- $start_time = $this->start_time;
- $end_time = $this->end_time;
- //查找出存在关键词商品的商城 只查找出商城页码 根据商城再通过关键词查出商城中商品
- $front_query = Goods::find()->alias('g')->where([
- 'g.status' => 1,
- 'g.is_delete' => 0,
- 'g.md_food_id' => 0,
- 's.is_delete' => 0,
- ])->andWhere(['!=', 'g.product_type', Goods::GOODS_TYPE_ADOPT]);
- $front_query->leftJoin(['s' => Store::tableName()], 's.id=g.store_id');
- $front_query->andWhere(['s.business_model' => [2, 3, 4], 's.is_delete' => 0]);
- if ($this->keyword) {
- if ($this->searchType === 'store') {
- $front_query->andWhere([
- 'like', 's.name', $this->keyword
- ]);
- }
- if ($this->searchType === 'goods') {
- $front_query->andWhere([
- 'or',
- ['like', 'g.name', $this->keyword],
- ['like', 'g.key_word', $this->keyword]
- ]);
- }
- }
- if ($this->store_id != '-1') {
- $front_query->andWhere(['g.store_id' => $this->store_id]);
- }
- if (in_array($this->type, Goods::$validProductType)) {
- $front_query->andWhere(['g.product_type' => $this->type]);
- }
- $front_query->groupBy('g.store_id');
- $count = $front_query->count();
- $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
- $od_query = OrderDetail::find()->alias('od')
- ->leftJoin(['o' => Order::tableName()], 'od.order_id=o.id')
- ->where(['od.is_delete' => 0, 'o.is_pay' => 1, 'o.is_delete' => 0])->groupBy('od.goods_id')->select('SUM(od.num) num, od.goods_id');
- $select = 'g.store_id';
- if ($this->type != Goods::GOODS_TYPE_DATE) {
- $front_query->leftJoin(['gn' => $od_query], 'gn.goods_id=g.id');
- }
- $list = $front_query
- ->select($select)
- ->limit($pagination->limit)
- ->offset($pagination->offset)
- ->column();
- $store_list = [];
- foreach ($list as $index => $item) {
- $store = Store::findOne($item);
- $wechatInfo = WechatConfig::find()->where(['store_id' => $item, 'is_delete' => 0, 'type' => 1])->asArray()->one();
- $wechat_app_id = '';
- $gh_wechat_app_id = '';
- if($wechatInfo){
- $wechat_app_id = $wechatInfo['app_id'];
- $gh_wechat_app_id = $wechatInfo['gh_wechat_app_id'];
- }
- $category_name = '';
- $category = SaasCategory::findOne($store->category_id);
- if ($category) {
- $category_name = $category->name;
- }
- $store_list[$index] = [
- 'id' => $store->id,
- 'logo' => $store->logo,
- 'name' => $store->name,
- 'address' => $store->address,
- 'business_model' => $store->business_model,
- 'category_id' => $store->category_id,
- 'category_name' => $category_name,
- 'coordinate' => $store->coordinate,
- 'distance' => 0,
- 'gh_wechat_app_id' => $gh_wechat_app_id,
- 'per_spend' => $store->per_spend,
- 'rank' => $store->rank,
- 'sales' => $store->sales,
- 'wechat_app_id' => $wechat_app_id,
- ];
- $query = Goods::find()->alias('g')->where([
- 'g.status' => 1,
- 'g.is_delete' => 0,
- 'g.md_food_id' => 0,
- 'g.store_id' => $item
- ])->andWhere(['!=', 'g.product_type', Goods::GOODS_TYPE_ADOPT]);
- $query->leftJoin(['s' => Store::tableName()], 's.id=g.store_id');
- $query->andWhere(['s.business_model' => 2]);
- if (in_array($this->type, Goods::$validProductType)) {
- $query->andWhere(['g.product_type' => $this->type]);
- }
- if ($this->keyword) {
- if ($this->searchType === 'store') {
- $query->andWhere([
- 'like', 's.name', $this->keyword
- ]);
- }
- if ($this->searchType === 'goods') {
- $query->andWhere([
- 'or',
- ['like', 'g.name', $this->keyword],
- ['like', 'g.key_word', $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()]);
- // }
- // }
- $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.is_pay' => 1, 'o.is_delete' => 0, 'o.store_id' => $item])->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.store_id';
- // 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)
- ->orderBy('g.sort DESC')
- ->asArray()->all();
- if (empty($list)) {
- $list = Goods::find()->alias('g')->leftJoin(['gn' => $od_query], 'gn.goods_id=g.id')->select($select)
- ->limit($pagination->limit)
- ->offset($pagination->offset)
- ->orderBy('g.sort DESC')
- ->asArray()->all();
- }
- if ($this->type === Goods::GOODS_TYPE_DATE) {
- foreach ($list as $goods_index => $goods_item) {
- $goods_id = $goods_item['id'];
- $goods = Goods::findOne($goods_id);
- $goods_del_open = true;
- $goodsDateNum = Goods::getGoodsNum($goods);
- if ($goodsDateNum['code'] === 0) {
- foreach ($goodsDateNum['data'] as $date_goods_item) {
- if (strtotime($date_goods_item['date']) === strtotime($start_time) && $date_goods_item['num'] > 0) {
- $goods_del_open = false;
- }
- }
- }
- if ($goods_del_open) {
- unset($list[$goods_index]);
- }
- }
- }
- if (in_array($store_list[$index]['business_model'], [3, 4])) {
- $list = [];
- }
- $store_list[$index]['goods_info'] = $list;
- // $store_list = [];
- // $md_id = get_md_id();
- // //排序,整合商城
- // $store_ids = array_column($list,'store_id');
- // $store_ids = array_values(array_unique($store_ids));
- //
- //
- // foreach ($store_ids as $index => $value) {
- // $store = Store::findOne($value);
- // $wechatInfo = WechatConfig::find()->where(['store_id' => $value, 'is_delete' => 0, 'type' => 1])->asArray()->one();
- // $wechat_app_id = '';
- // $gh_wechat_app_id = '';
- // if($wechatInfo){
- // $wechat_app_id = $wechatInfo['app_id'];
- // $gh_wechat_app_id = $wechatInfo['gh_wechat_app_id'];
- // }
- // $category_name = '';
- // $category = SaasCategory::findOne($store->category_id);
- // if ($category) {
- // $category_name = $category->name;
- // }
- // $store_list[$index] = [
- // 'id' => $store->id,
- // 'logo' => $store->logo,
- // 'name' => $store->name,
- // 'address' => $store->address,
- // 'business_model' => $store->business_model,
- // 'category_id' => $store->category_id,
- // 'category_name' => $category_name,
- // 'coordinate' => $store->coordinate,
- // 'distance' => 0,
- // 'gh_wechat_app_id' => $gh_wechat_app_id,
- // 'per_spend' => $store->per_spend,
- // 'rank' => $store->rank,
- // 'sales' => $store->sales,
- // 'wechat_app_id' => $wechat_app_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]['comment'] = OrderComment::find()->where(['goods_id' => $item['id'],
- // 'is_hide' => 0, 'is_delete' => OrderComment::IS_DELETE_FALSE])->count();
- // $goods_comment_count = OrderComment::find()->where(['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'];
- // }
- // if ((int)$item['store_id'] === (int)$value) {
- // $store_list[$index]['goods_info'][] = $list[$i];
- // }
- // }
- // }
- }
- $data = [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $store_list,
- // 'store_list' => $store_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;
- $list[$i]['goods_info'] = [];
- }
- $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,
- ]
- ];
- }
- }
|