AdoptGoodsListForm.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\adopt\models\client;
  8. use app\models\Cat;
  9. use app\models\Goods;
  10. use app\models\GoodsCat;
  11. use app\models\GoodsPic;
  12. use app\models\Mch;
  13. use app\models\Md;
  14. use app\models\Order;
  15. use app\models\OrderComment;
  16. use app\models\OrderDetail;
  17. use app\plugins\adopt\models\AdoptCat;
  18. use app\plugins\adopt\models\AdoptGoods;
  19. use yii\base\BaseObject;
  20. use yii\base\Model;
  21. use yii\data\Pagination;
  22. use app\models\MchGoodsCat;
  23. use app\modules\client\models\v1\common\CommonGoods;
  24. use app\models\MdGoods;
  25. class AdoptGoodsListForm extends Model
  26. {
  27. public $store_id;
  28. public $keyword;
  29. public $cat_id;
  30. public $page;
  31. public $limit;
  32. public $sort;
  33. public $sort_type;
  34. public $goods_id;
  35. public $pic_url;
  36. public $recommend_count;
  37. public $mch_id;
  38. public function rules()
  39. {
  40. return [
  41. [['keyword'], 'trim'],
  42. [['store_id', 'cat_id', 'mch_id', 'page', 'limit',], 'integer'],
  43. [['limit'], 'integer',],
  44. [['page',], 'default', 'value' => 1],
  45. [['limit',], 'default', 'value' => 12],
  46. [['sort', 'sort_type', 'recommend_count'], 'integer',],
  47. [['sort',], 'default', 'value' => 0],
  48. [['goods_id',], 'string'],
  49. ];
  50. }
  51. public function getMchId()
  52. {
  53. return empty($this->mch_id) ? 0 : $this->mch_id;
  54. }
  55. private function numToW($sales)
  56. {
  57. if ($sales < 10000) {
  58. return $sales;
  59. } else {
  60. return round($sales / 10000, 2) . 'W';
  61. }
  62. }
  63. public function GoodsSearch()
  64. {
  65. if (!$this->validate())
  66. return [
  67. 'code' => 1,
  68. 'msg' => $this->getErrorSummary(false)[0]
  69. ];
  70. $query = Goods::find()->alias('g')
  71. ->leftJoin(['ag' => AdoptGoods::tableName()], 'g.id = ag.goods_id')
  72. ->where([
  73. 'g.store_id' => $this->store_id,
  74. 'g.status' => 1,
  75. 'g.is_delete' => 0,
  76. 'g.md_food_id' => 0,
  77. 'g.product_type' => 5,
  78. ]);
  79. if ($this->keyword) {
  80. $query->andWhere([
  81. 'or',
  82. ['like', 'g.name', $this->keyword],
  83. ['like', 'g.key_word', $this->keyword]
  84. ]);
  85. }
  86. if ($this->cat_id && $this->getMchId() == 0) {
  87. // $cat = Cat::find()->select('id')->where(['is_delete' => 0,])->andWhere(['OR', ['parent_id' => $this->cat_id], ['id' => $this->cat_id],])->column();
  88. // $query->leftJoin(['gc' => GoodsCat::tableName()], 'gc.goods_id=g.id');
  89. // $query->andWhere(['or', ['gc.is_delete' => 0], 'isnull(gc.id)']);
  90. // $query->andWhere(
  91. // [
  92. // 'OR',
  93. // ['g.cat_id' => $cat],
  94. // ['gc.cat_id' => $cat],
  95. // ]
  96. // );
  97. $query->leftJoin(['gc' => GoodsCat::tableName()], 'gc.goods_id=g.id');
  98. $query->andWhere(['or', ['gc.is_delete' => 0], 'isnull(gc.id)']);
  99. $query->andWhere(
  100. [
  101. 'OR',
  102. ['g.cat_id' => $this->cat_id],
  103. ['gc.cat_id' => $this->cat_id],
  104. ]
  105. );
  106. // $query->leftJoin(['gc' => GoodsCat::tableName()], 'g.id = gc.goods_id')->andWhere([
  107. // 'gc.cat_id' => $arr['cat_id']
  108. // ])->groupBy('gc.goods_id');
  109. }
  110. // if (get_md_id()) {
  111. // $md = Md::findOne(get_md_id());
  112. // if ($md->is_single) {
  113. // $query->leftJoin(['mdg' => MdGoods::tableName()], 'mdg.goods_id=g.id');
  114. // $query->andWhere(['g.product_type' => 0, 'mdg.status' => 1, 'mdg.md_id' => get_md_id()]);
  115. // }
  116. // }
  117. $count = $query->count();
  118. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  119. if ($this->sort == 0) {
  120. //综合,自定义排序+时间最新
  121. $query->orderBy('g.sort DESC, g.created_at DESC');
  122. }
  123. if ($this->sort == 1) {
  124. //时间最新
  125. $query->orderBy('g.created_at DESC');
  126. }
  127. if ($this->sort == 2) {
  128. if (get_md_id()) {
  129. //价格
  130. if ($this->sort_type == 0) {
  131. $query->orderBy('mdg.price ASC');
  132. } else {
  133. $query->orderBy('mdg.price DESC');
  134. }
  135. } else {
  136. //价格
  137. if ($this->sort_type == 0) {
  138. $query->orderBy('g.price ASC');
  139. } else {
  140. $query->orderBy('g.price DESC');
  141. }
  142. }
  143. }
  144. if ($this->sort == 3) {
  145. // if (get_md_id()) {
  146. // //销量
  147. // if ($md->is_single) {
  148. // $query->orderBy([
  149. // '( IF(gn.num, gn.num, 0) + mdg.virtual_sales)' => SORT_DESC,
  150. // 'g.created_at' => SORT_DESC,
  151. // ]);
  152. // } else {
  153. // $query->orderBy([
  154. // '( IF(gn.num, gn.num, 0) + g.virtual_sales)' => SORT_DESC,
  155. // 'g.created_at' => SORT_DESC,
  156. // ]);
  157. // }
  158. // } else {
  159. // //销量
  160. $query->orderBy([
  161. '( IF(gn.num, gn.num, 0) + g.virtual_sales)' => SORT_DESC,
  162. 'g.created_at' => SORT_DESC,
  163. ]);
  164. // }
  165. }
  166. $od_query = OrderDetail::find()->alias('od')
  167. ->leftJoin(['o' => Order::tableName()], 'od.order_id=o.id')
  168. ->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');
  169. $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';
  170. // if (get_md_id()) {
  171. // if ($md->is_single) {
  172. // $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';
  173. // }
  174. // }
  175. $list = $query
  176. ->leftJoin(['gn' => $od_query], 'gn.goods_id=g.id')
  177. ->select($select)
  178. ->limit($pagination->limit)
  179. ->offset($pagination->offset)
  180. ->asArray()->all();
  181. $md_id = get_md_id();
  182. foreach ($list as $i => $item) {
  183. if (!$item['pic_url']) {
  184. $list[$i]['pic_url'] = Goods::getGoodsPicStatic($item['id']);
  185. }
  186. // $list[$i]['sales'] = $this->numToW($item['num'] + $item['virtual_sales']) . $item['unit'];
  187. $list[$i]['sales'] = $this->numToW($item['num'] + $item['virtual_sales']);
  188. $list[$i]['remaining_num'] = $this->numToW($item['goods_num'] - $list[$i]['sales']);
  189. $list[$i]['comment'] = OrderComment::find()->where(['store_id' => $this->store_id, 'goods_id' => $item['id'],
  190. 'is_hide' => 0, 'is_delete' => OrderComment::IS_DELETE_FALSE])->count();
  191. $goods_comment_count = OrderComment::find()->where(['store_id' => $this->store_id, 'goods_id' => $item['id'],
  192. 'is_hide' => 0, 'is_delete' => OrderComment::IS_DELETE_FALSE]);
  193. $goods_count = $goods_comment_count->andWhere(['>', 'score', 2])->count();
  194. $list[$i]['rank_good_percent'] = $goods_count > 0 ? round(($goods_count / $list[$i]['comment']) * 100) . '%' : 0 . '%';
  195. if ($md_id && $md->is_single) {
  196. $list[$i]['price'] = $item['mdg_price'];
  197. $list[$i]['virtual_sales'] = $item['mdg_virtual_sales'];
  198. $list[$i]['goods_num'] = $item['mdg_goods_num'];
  199. $list[$i]['attr'] = $item['mdg_attr'];
  200. $list[$i]['status'] = $item['mdg_status'];
  201. }
  202. }
  203. $cat_list = AdoptCat::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'is_show' => 1])->orderBy(['sort' => SORT_DESC])->all();
  204. $data = [
  205. 'row_count' => $count,
  206. 'page_count' => $pagination->pageCount,
  207. 'list' => $list,
  208. // 'sql' => $query->createCommand()->getRawSql(),
  209. 'cat_list' => $cat_list
  210. ];
  211. return [
  212. 'code' => 0,
  213. 'data' => $data
  214. ];
  215. }
  216. }