| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\plugins\adopt\models\client;
- use app\models\Cat;
- use app\models\Goods;
- 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\plugins\adopt\models\AdoptCat;
- use app\plugins\adopt\models\AdoptGoods;
- 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;
- class AdoptGoodsListForm 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 function rules()
- {
- return [
- [['keyword'], 'trim'],
- [['store_id', 'cat_id', 'mch_id', 'page', 'limit',], 'integer'],
- [['limit'], 'integer',],
- [['page',], 'default', 'value' => 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
- ];
- }
- }
|