FavoriteListForm.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\Favorite;
  9. use app\models\Goods;
  10. use app\models\Mch;
  11. use app\models\Worker;
  12. use app\models\WorkerCat;
  13. use app\models\WorkerCatExt;
  14. use yii\base\Model;
  15. use yii\data\Pagination;
  16. use yii\helpers\VarDumper;
  17. use app\modules\client\models\ApiModel;
  18. class FavoriteListForm extends ApiModel
  19. {
  20. public $store_id;
  21. public $user_id;
  22. public $page;
  23. public $limit;
  24. public $type;
  25. public function rules()
  26. {
  27. return [
  28. [['type'], 'default', 'value' => 0],
  29. ['type', 'in', 'range' => [Favorite::FAVORITE_GOODS, Favorite::FAVORITE_SHOP, Favorite::FAVORITE_WORKER]],
  30. [['page', 'limit',], 'integer',],
  31. [['page'], 'default', 'value' => 1],
  32. [['limit'], 'default', 'value' => 20],
  33. ];
  34. }
  35. public function search()
  36. {
  37. if (!$this->validate()) {
  38. return [
  39. 'code' => 1,
  40. 'msg' => $this->getErrorSummary(false)[0]
  41. ];
  42. }
  43. if ($this->type == Favorite::FAVORITE_GOODS) {
  44. $query = Favorite::find()->where(['store_id' => $this->store_id, 'type' => Favorite::FAVORITE_GOODS, 'is_delete' => 0, 'user_id' => $this->user_id]);
  45. $count = $query->count();
  46. $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
  47. $list = $query->select('*')->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray()->all();
  48. $new_list = [];
  49. foreach ($list as $i => $favorite) {
  50. $goods = Goods::findOne(['store_id' => $this->store_id, 'is_delete' => 0, 'status' => Goods::STATUS_NORMAL, 'id' => $favorite['goods_id']]);
  51. if (!$goods) {
  52. continue;
  53. }
  54. if ($goods->is_negotiable) {
  55. $goods->price = Goods::GOODS_NEGOTIABLE;
  56. }
  57. $new_list[] = (object)[
  58. 'id' => $favorite['id'],
  59. 'goods_id' => $goods->id,
  60. 'name' => $goods->name,
  61. 'price' => $goods->price,
  62. 'goods_pic' => $goods->getGoodsPic(0)->pic_url,
  63. 'is_negotiable' => $goods->is_negotiable,
  64. ];
  65. }
  66. return [
  67. 'code' => 0,
  68. 'data' => (object)[
  69. 'row_count' => $count,
  70. 'page_count' => $pagination->pageCount,
  71. 'list' => $new_list,
  72. ],
  73. ];
  74. }
  75. if ($this->type == Favorite::FAVORITE_SHOP) {
  76. $query = Favorite::find()->where(['store_id' => $this->store_id, 'type' => Favorite::FAVORITE_SHOP, 'is_delete' => 0, 'user_id' => $this->user_id]);
  77. $count = $query->count();
  78. $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
  79. $list = $query->select('*')->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray()->all();
  80. $new_list = [];
  81. foreach ($list as $key => $shop) {
  82. $mch = Mch::find()->where(['id' => $shop['goods_id'], 'store_id' => $this->store_id, 'is_delete' => 0, 'is_open' => 1, 'is_lock' => 0])->one();
  83. if (!$mch) {
  84. continue;
  85. }
  86. $new_list[] = (object)[
  87. 'id' => $shop['id'],
  88. 'created_at' => $shop['created_at'],
  89. 'name' => $mch['name'],
  90. 'logo' => $mch['logo'],
  91. '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()
  92. ];
  93. }
  94. return [
  95. 'code' => 0,
  96. 'data' => (object)[
  97. 'row_count' => $count,
  98. 'page_count' => $pagination->pageCount,
  99. 'list' => $new_list,
  100. ],
  101. ];
  102. }
  103. if ($this->type == Favorite::FAVORITE_WORKER) {
  104. $query = Favorite::find()->where(['store_id' => $this->store_id, 'type' => Favorite::FAVORITE_WORKER, 'is_delete' => 0, 'user_id' => $this->user_id]);
  105. $count = $query->count();
  106. $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
  107. $list = $query->select('*')->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray()->all();
  108. $new_list = [];
  109. foreach ($list as $key => $item) {
  110. $worker = Worker::findOne(['id' => $item['worker_id'], 'store_id' => $this->store_id, 'status' => 1]);
  111. if (!$worker) {
  112. continue;
  113. }
  114. $worker_cat_id = WorkerCatExt::find()->where(['worker_id' => $worker->id, 'is_delete' => 0])->select('cat_id')->column();
  115. $worker_cat = [];
  116. if ($worker_cat_id) {
  117. $worker_cat = WorkerCat::find()->where(['id' => $worker_cat_id])->select('name')->column();
  118. }
  119. $new_list[] = (object)[
  120. 'id' => $item['worker_id'],
  121. 'created_at' => $item['created_at'],
  122. 'name' => $worker->name,
  123. 'logo' => $worker->logo,
  124. 'worker_cat' => $worker_cat
  125. ];
  126. }
  127. return [
  128. 'code' => 0,
  129. 'data' => (object)[
  130. 'row_count' => $count,
  131. 'page_count' => $pagination->pageCount,
  132. 'list' => $new_list,
  133. ],
  134. ];
  135. }
  136. }
  137. }