| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\models\Favorite;
- use app\models\Goods;
- use app\models\Mch;
- use app\models\Worker;
- use app\models\WorkerCat;
- use app\models\WorkerCatExt;
- use yii\base\Model;
- use yii\data\Pagination;
- use yii\helpers\VarDumper;
- use app\modules\client\models\ApiModel;
- class FavoriteListForm extends ApiModel
- {
- public $store_id;
- public $user_id;
- public $page;
- public $limit;
- public $type;
- public function rules()
- {
- return [
- [['type'], 'default', 'value' => 0],
- ['type', 'in', 'range' => [Favorite::FAVORITE_GOODS, Favorite::FAVORITE_SHOP, Favorite::FAVORITE_WORKER]],
- [['page', 'limit',], 'integer',],
- [['page'], 'default', 'value' => 1],
- [['limit'], 'default', 'value' => 20],
- ];
- }
- public function search()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- if ($this->type == Favorite::FAVORITE_GOODS) {
- $query = Favorite::find()->where(['store_id' => $this->store_id, 'type' => Favorite::FAVORITE_GOODS, 'is_delete' => 0, 'user_id' => $this->user_id]);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
- $list = $query->select('*')->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray()->all();
- $new_list = [];
- foreach ($list as $i => $favorite) {
- $goods = Goods::findOne(['store_id' => $this->store_id, 'is_delete' => 0, 'status' => Goods::STATUS_NORMAL, 'id' => $favorite['goods_id']]);
- if (!$goods) {
- continue;
- }
- if ($goods->is_negotiable) {
- $goods->price = Goods::GOODS_NEGOTIABLE;
- }
- $new_list[] = (object)[
- 'id' => $favorite['id'],
- 'goods_id' => $goods->id,
- 'name' => $goods->name,
- 'price' => $goods->price,
- 'goods_pic' => $goods->getGoodsPic(0)->pic_url,
- 'is_negotiable' => $goods->is_negotiable,
- ];
- }
- return [
- 'code' => 0,
- 'data' => (object)[
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $new_list,
- ],
- ];
- }
- if ($this->type == Favorite::FAVORITE_SHOP) {
- $query = Favorite::find()->where(['store_id' => $this->store_id, 'type' => Favorite::FAVORITE_SHOP, 'is_delete' => 0, 'user_id' => $this->user_id]);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
- $list = $query->select('*')->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray()->all();
- $new_list = [];
- foreach ($list as $key => $shop) {
- $mch = Mch::find()->where(['id' => $shop['goods_id'], 'store_id' => $this->store_id, 'is_delete' => 0, 'is_open' => 1, 'is_lock' => 0])->one();
- if (!$mch) {
- continue;
- }
- $new_list[] = (object)[
- 'id' => $shop['id'],
- 'created_at' => $shop['created_at'],
- 'name' => $mch['name'],
- 'logo' => $mch['logo'],
- 'goods_list' => Goods::find()->where(['store_id' => $this->store_id, 'status' => Goods::STATUS_NORMAL, 'is_delete' => 0, 'mch_id' => $shop['goods_id']])->select('id, price, cover_pic, mch_id')->limit(4)->orderBy('created_at DESC')->asArray()->all()
- ];
- }
- return [
- 'code' => 0,
- 'data' => (object)[
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $new_list,
- ],
- ];
- }
- if ($this->type == Favorite::FAVORITE_WORKER) {
- $query = Favorite::find()->where(['store_id' => $this->store_id, 'type' => Favorite::FAVORITE_WORKER, 'is_delete' => 0, 'user_id' => $this->user_id]);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
- $list = $query->select('*')->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray()->all();
- $new_list = [];
- foreach ($list as $key => $item) {
- $worker = Worker::findOne(['id' => $item['worker_id'], 'store_id' => $this->store_id, 'status' => 1]);
- if (!$worker) {
- continue;
- }
- $worker_cat_id = WorkerCatExt::find()->where(['worker_id' => $worker->id, 'is_delete' => 0])->select('cat_id')->column();
- $worker_cat = [];
- if ($worker_cat_id) {
- $worker_cat = WorkerCat::find()->where(['id' => $worker_cat_id])->select('name')->column();
- }
- $new_list[] = (object)[
- 'id' => $item['worker_id'],
- 'created_at' => $item['created_at'],
- 'name' => $worker->name,
- 'logo' => $worker->logo,
- 'worker_cat' => $worker_cat
- ];
- }
- return [
- 'code' => 0,
- 'data' => (object)[
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $new_list,
- ],
- ];
- }
- }
- }
|