GoodsListForm.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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\Cart;
  9. use app\models\Cat;
  10. use app\models\Goods;
  11. use app\models\GoodsBook;
  12. use app\models\GoodsBrowse;
  13. use app\models\GoodsCat;
  14. use app\models\GoodsPic;
  15. use app\models\Mch;
  16. use app\models\MchCat;
  17. use app\models\Md;
  18. use app\models\Order;
  19. use app\models\OrderComment;
  20. use app\models\OrderDetail;
  21. use yii\base\BaseObject;
  22. use yii\base\Model;
  23. use yii\data\Pagination;
  24. use app\models\MchGoodsCat;
  25. use app\modules\client\models\v1\common\CommonGoods;
  26. use app\models\MdGoods;
  27. use yii\helpers\Json;
  28. use app\constants\OptionSetting;
  29. use app\models\Option;
  30. class GoodsListForm extends Model
  31. {
  32. public $store_id;
  33. public $keyword;
  34. public $cat_id;
  35. public $page;
  36. public $limit;
  37. public $sort;
  38. public $sort_type;
  39. public $goods_id;
  40. public $pic_url;
  41. public $recommend_count;
  42. public $mch_id;
  43. public $type;
  44. public $start_time;
  45. public $end_time;
  46. public $rate;
  47. public $user_id;
  48. public $brand_id;
  49. // 是否要查询支持装配的商品
  50. public $is_accessories = false;
  51. // 是否要查询视频号小店的商品
  52. public $is_videoShopGoods = false;
  53. public function rules()
  54. {
  55. return [
  56. [['keyword'], 'trim'],
  57. [['store_id', 'cat_id', 'page', 'limit', 'type', 'user_id'], 'integer'],
  58. [['limit', 'brand_id'], 'integer',],
  59. [['page',], 'default', 'value' => 1],
  60. [['limit',], 'default', 'value' => 12],
  61. [['sort', 'sort_type', 'recommend_count'], 'integer',],
  62. [['sort',], 'default', 'value' => 0],
  63. [['goods_id', 'start_time', 'end_time'], 'string'],
  64. [['is_videoShopGoods', 'rate'], 'safe'],
  65. [['mch_id'], 'safe'],
  66. ];
  67. }
  68. public function getMchId()
  69. {
  70. return empty($this->mch_id) ? 0 : $this->mch_id;
  71. }
  72. public function search()
  73. {
  74. if (!$this->validate())
  75. return [
  76. 'code' => 1,
  77. 'msg' => $this->getErrorSummary(false)[0]
  78. ];
  79. $userLevel = get_user()->level;
  80. $query = Goods::find()->alias('g')->leftJoin(['gb' => GoodsBrowse::tableName()],'g.id=gb.goods_id')->where([
  81. 'g.status' => 1,
  82. 'g.is_delete' => 0,
  83. 'g.md_food_id' => 0
  84. ])->andWhere(['!=', 'g.product_type', Goods::GOODS_TYPE_ADOPT])->andWhere(['!=', 'g.product_type', Goods::GOODS_TYPE_INTEGRAL]);
  85. // 这里判断商品浏览权限
  86. if(empty($userLevel)){
  87. $query->andWhere(['gb.goods_id' => null]);
  88. }else{
  89. $query->andWhere(['or',['gb.goods_id' => null],['gb.level' => $userLevel]]);
  90. }
  91. if (get_md_id()) {
  92. $md = Md::findOne(get_md_id());
  93. if ($md->is_single) {
  94. $query->leftJoin(['mdg' => MdGoods::tableName()], 'mdg.goods_id=g.id');
  95. $query->andWhere(['g.product_type' => 0, 'mdg.status' => 1, 'mdg.md_id' => get_md_id()]);
  96. }
  97. }
  98. if ($this->mch_id){
  99. $query->andWhere(['g.mch_id' => $this->mch_id]);
  100. }
  101. if ($this->store_id)
  102. $query->andWhere(['g.store_id' => $this->store_id]);
  103. if ($this->cat_id) {
  104. if (!$this->mch_id) {
  105. $cat_id = $this->getCatId($this->cat_id);
  106. array_push($cat_id, $this->cat_id);
  107. $cat = Cat::find()->where(['is_delete' => 0, 'id' => $cat_id])->select('id')->column();
  108. $query->leftJoin(['gc' => GoodsCat::tableName()], 'gc.goods_id=g.id');
  109. $query->andWhere(['or', ['gc.is_delete' => 0], 'isnull(gc.id)']);
  110. $query->andWhere(
  111. [
  112. 'OR',
  113. ['g.cat_id' => $cat],
  114. ['gc.cat_id' => $cat],
  115. ]
  116. );
  117. }else{
  118. $query->andWhere(['g.id' => MchGoodsCat::find()->select('goods_id')->where([
  119. 'cat_id' => MchCat::find()->select('id')->where([
  120. 'or',
  121. ['id' => $this->cat_id],
  122. ['parent_id' => $this->cat_id],
  123. ])->andWhere(['is_show' => 1])
  124. ])]);
  125. }
  126. }
  127. if ($this->goods_id) {
  128. $arr = explode(',', $this->goods_id);
  129. $query->andWhere(['in', 'g.id', $arr]);
  130. }
  131. if ($this->brand_id) {
  132. $query->andWhere(['g.brand_id' => $this->brand_id]);
  133. }
  134. if ($this->keyword)
  135. $query->andWhere(['LIKE', 'g.name', $this->keyword]);
  136. if($this->is_accessories){
  137. //$query->andWhere(['not',['g.accessories_image' => null]]);
  138. $query->andWhere(['<>','g.accessories_image', '']);
  139. }
  140. $count = $query->groupBy('g.id')->count();
  141. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  142. if ($this->sort == 0) {
  143. //综合,自定义排序+时间最新
  144. $query->orderBy('g.sort DESC, g.created_at DESC');
  145. }
  146. if ($this->sort == 1) {
  147. //时间最新
  148. $query->orderBy('g.created_at DESC');
  149. }
  150. if ($this->sort == 2) {
  151. //价格
  152. if ($this->sort_type == 0) {
  153. $query->orderBy('g.price DESC');
  154. } else {
  155. $query->orderBy('g.price ASC');
  156. }
  157. }
  158. if ($this->sort == 3) {
  159. //销量
  160. if ($this->sort_type == 0) {
  161. $query->orderBy([
  162. '( IF(gn.num, gn.num, 0) + virtual_sales)' => SORT_DESC,
  163. 'g.created_at' => SORT_DESC,
  164. ]);
  165. } else {
  166. $query->orderBy([
  167. '( IF(gn.num, gn.num, 0) + virtual_sales)' => SORT_ASC,
  168. 'g.created_at' => SORT_ASC,
  169. ]);
  170. }
  171. }
  172. $od_query = OrderDetail::find()->alias('od')
  173. ->leftJoin(['o' => Order::tableName()], 'od.order_id=o.id')
  174. ->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');
  175. $select = 'g.goods_num, g.order_min_count, 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,g.accessories_image';
  176. if (get_md_id() && $md->is_single) {
  177. $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';
  178. }
  179. $list = $query
  180. ->leftJoin(['gn' => $od_query], 'gn.goods_id=g.id')
  181. ->select($select)
  182. ->limit($pagination->limit)
  183. ->offset($pagination->offset)
  184. ->asArray()->groupBy('g.id')->all();
  185. $md_id = get_md_id();
  186. foreach ($list as $i => $item) {
  187. if (!$item['pic_url']) {
  188. $list[$i]['pic_url'] = Goods::getGoodsPicStatic($item['id']);
  189. }
  190. if ($item['is_negotiable']) {
  191. $list[$i]['price'] = Goods::GOODS_NEGOTIABLE;
  192. }
  193. $list[$i]['sales'] = $this->numToW($item['num'] + $item['virtual_sales']) . $item['unit'];
  194. $res = CommonGoods::getMemberPrice([
  195. 'attr' => $item['attr'],
  196. 'price' => $item['price'],
  197. 'mch_id' => $item['mch_id'],
  198. 'is_level' => $item['is_level'],
  199. ]);
  200. $goodsMemberPrice = $res['min_member_price'] ? $res['min_member_price'] : $item['price'];
  201. $list[$i]['goods_member_price'] = sprintf("%.2f", $goodsMemberPrice);
  202. if (!$item['is_negotiable']) {// 说让分类页面要显示会员价
  203. $list[$i]['price'] = $list[$i]['goods_member_price'];
  204. }
  205. $list[$i]['comment'] = OrderComment::find()->where(['store_id' => $this->store_id, 'goods_id' => $item['id'],
  206. 'is_hide' => 0, 'is_delete' => OrderComment::IS_DELETE_FALSE])->count();
  207. $goods_comment_count = OrderComment::find()->where(['store_id' => $this->store_id, 'goods_id' => $item['id'],
  208. 'is_hide' => 0, 'is_delete' => OrderComment::IS_DELETE_FALSE]);
  209. $goods_count = $goods_comment_count->andWhere(['>', 'score', 2])->count();
  210. $list[$i]['rank_good_percent'] = $goods_count > 0 ? round(($goods_count / $list[$i]['comment']) * 100) . '%' : 0 . '%';
  211. if ($md_id && $md->is_single) {
  212. $list[$i]['attr'] = $item['mdg_attr'];
  213. $list[$i]['virtual_sales'] = $item['mdg_virtual_sales'];
  214. $list[$i]['goods_num'] = $item['mdg_goods_num'];
  215. $list[$i]['price'] = $item['mdg_price'];
  216. $list[$i]['status'] = $item['mdg_status'];
  217. }
  218. // 转换price为字符串
  219. $list[$i]['price'] = (string)$list[$i]['price'];
  220. $list[$i]['cart_num'] = 0;
  221. if ($this->user_id) {
  222. if ($md_id <= 0) {
  223. $md_id = [0, -1];
  224. }
  225. $list[$i]['cart_num'] = Cart::find()->where(['type'=>Cart::TYPE_DEFAULT,'user_id' => $this->user_id, 'is_delete' => 0, 'goods_id' => $item['id'], 'md_id' => $md_id])
  226. ->sum('num') ?: 0;
  227. }
  228. }
  229. $data = [
  230. 'row_count' => $count,
  231. 'page_count' => $pagination->pageCount,
  232. 'list' => $list,
  233. ];
  234. return [
  235. 'code' => 0,
  236. 'data' => $data
  237. ];
  238. }
  239. public function getCatId($cat_id) {
  240. $arr = [];
  241. $cat = Cat::find()->where(['id' => $cat_id, 'store_id' => $this->store_id])->select('id')->asArray()->one();
  242. if ($cat) {
  243. $child_cat = Cat::find()->where(['parent_id' => $cat_id, 'store_id' => $this->store_id])->select('id')->asArray()->all();
  244. foreach ($child_cat as $item) {
  245. $arr[] = $item['id'];
  246. $this->getCatId($item['id']);
  247. }
  248. }
  249. return $arr;
  250. }
  251. public function recommend()
  252. {
  253. if (!$this->validate())
  254. return $this->errorResponse;
  255. $goods_id = $this->goods_id;
  256. if (!$goods_id) {
  257. return [
  258. 'code' => 1,
  259. 'msg' => 'err'
  260. ];
  261. }
  262. $cat_ids = [];
  263. $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();
  264. $cat_id = $goods->cat_id;
  265. if ($goods->mch_id > 0) {
  266. if ($cat_id == 0) {
  267. $goodsCat = MchGoodsCat::find()->select('cat_id')->where(['goods_id' => $goods_id])->all();
  268. $goods_cat = [];
  269. foreach ($goodsCat as $v) {
  270. $goods_cat[] = $v->cat_id;
  271. }
  272. } else {
  273. $goods_cat = array(intval($cat_id));
  274. }
  275. $cat_ids = $goods_cat;
  276. //查询
  277. $goodscat_list = MchGoodsCat::find()->select(['goods_id'])->andWhere(['in', 'cat_id', $cat_ids])->all();
  278. }else {
  279. if ($cat_id == 0) {
  280. $goodsCat = GoodsCat::find()->select('cat_id')->where(['store_id' => $this->store_id, 'goods_id' => $goods_id, 'is_delete' => 0])->all();
  281. $goods_cat = [];
  282. foreach ($goodsCat as $v) {
  283. $goods_cat[] = $v->cat_id;
  284. }
  285. } else {
  286. $goods_cat = array(intval($cat_id));
  287. }
  288. $cat_ids = $goods_cat;
  289. //查询
  290. $goodscat_list = GoodsCat::find()->select(['goods_id'])->where(['store_id' => $this->store_id, 'is_delete' => 0])->andWhere(['in', 'cat_id', $cat_ids])->all();
  291. }
  292. $cats = [];
  293. foreach ($goodscat_list as $v) {
  294. $cats[] = $v->goods_id;
  295. }
  296. $query = Goods::find()->alias('g')
  297. ->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]])
  298. ->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]])
  299. ->andWhere(['g.type' => get_plugin_type()])
  300. ->andWhere(['not like', 'g.name', '当面付'])
  301. ->leftJoin(['m' => Mch::tableName()], 'm.id=g.mch_id')
  302. ->andWhere([
  303. 'or',
  304. ['g.mch_id' => 0],
  305. ['m.is_delete' => 0]
  306. ]);
  307. $count = $query->count();
  308. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  309. $query->orderBy('g.sort DESC');
  310. $od_query = OrderDetail::find()->alias('od')
  311. ->leftJoin(['o' => Order::tableName()], 'od.order_id=o.id')
  312. ->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');
  313. $limit = $pagination->limit;
  314. $offset = $pagination->offset;
  315. $recommend_count = $this->recommend_count;
  316. if ($offset > $recommend_count) {
  317. return [
  318. 'code' => 1,
  319. 'msg' => 'err'
  320. ];
  321. } else if ($offset + $limit > $recommend_count) {
  322. $limit = $recommend_count - $offset;
  323. }
  324. $list = $query
  325. ->leftJoin(['gn' => $od_query], 'gn.goods_id=g.id')
  326. ->select('g.id,g.name,g.price,g.original_price,g.cover_pic pic_url,gn.num,g.virtual_sales,g.unit')
  327. ->limit($limit)
  328. ->offset($pagination->offset)
  329. ->asArray()->groupBy('g.id')->all();
  330. foreach ($list as $i => $item) {
  331. if (!$item['pic_url']) {
  332. $list[$i]['pic_url'] = Goods::getGoodsPicStatic($item['id'])->pic_url;
  333. }
  334. $list[$i]['sales'] = $this->numToW($item['num'] + $item['virtual_sales']) . $item['unit'];
  335. }
  336. $data = [
  337. 'row_count' => $count,
  338. 'page_count' => $pagination->pageCount,
  339. 'list' => $list,
  340. ];
  341. return [
  342. 'code' => 0,
  343. 'data' => $data
  344. ];
  345. }
  346. private function numToW($sales)
  347. {
  348. if ($sales < 10000) {
  349. return $sales;
  350. } else {
  351. return round($sales / 10000, 2) . 'W';
  352. }
  353. }
  354. public function couponSearch()
  355. {
  356. $arr = explode(",", $this->goods_id);
  357. $query = Goods::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'status' => 1])->andWhere(['in', 'id', $arr]);
  358. $count = $query->count();
  359. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  360. if ($this->sort == 0) {
  361. //综合,自定义排序+时间最新
  362. $query->orderBy('sort ASC,created_at DESC');
  363. }
  364. if ($this->sort == 1) {
  365. //时间最新
  366. $query->orderBy('created_at DESC');
  367. }
  368. if ($this->sort == 2) {
  369. //价格
  370. if ($this->sort_type == 0) {
  371. $query->orderBy('price ASC');
  372. } else {
  373. $query->orderBy('price DESC');
  374. }
  375. }
  376. if ($this->sort == 3) {
  377. //销量
  378. $query->orderBy([
  379. 'virtual_sales' => SORT_DESC,
  380. 'created_at' => SORT_DESC,
  381. ]);
  382. }
  383. $list = $query
  384. ->select(['id', 'name', 'cover_pic as pic_url', 'price', 'original_price', 'virtual_sales as sales', 'unit'])
  385. ->limit($pagination->limit)
  386. ->offset($pagination->offset)
  387. ->asArray()->all();
  388. return [
  389. 'code' => 0,
  390. 'msg' => 'success',
  391. 'data' => [
  392. 'row_count' => $count,
  393. 'page_count' => $pagination->pageCount,
  394. 'list' => $list,
  395. ],
  396. ];
  397. }
  398. public function newSearch()
  399. {
  400. if (!$this->validate())
  401. return [
  402. 'code' => 1,
  403. 'msg' => $this->getErrorSummary(false)[0]
  404. ];
  405. $is_shenhe = Option::get(OptionSetting::WECHAT_IS_SHENHE, $this->store_id, 'wechat', '0')['value'];
  406. $query = Goods::find()->alias('g')->leftJoin(['gb' => GoodsBrowse::tableName()],'g.id=gb.goods_id')->where([
  407. 'g.store_id' => $this->store_id,
  408. 'g.status' => 1,
  409. 'g.is_delete' => 0,
  410. 'g.md_food_id' => 0
  411. ])->andWhere(['AND', ['!=', 'g.product_type',Goods::GOODS_TYPE_ADOPT], ['!=', 'g.product_type',Goods::GOODS_TYPE_INTEGRAL]]); //, ['!=', 'g.product_type',Goods::GOODS_TYPE_WORKER]
  412. $userLevel = get_user()->level;
  413. // 这里判断商品浏览权限
  414. if(empty($userLevel)){
  415. $query->andWhere(['gb.goods_id' => null]);
  416. }else{
  417. $query->andWhere(['or',['gb.goods_id' => null],['gb.level' => $userLevel]]);
  418. }
  419. if (in_array($this->type, Goods::$validProductType)) {
  420. $query->andWhere(['g.product_type' => $this->type]);
  421. }
  422. if (show_shenhe()) {
  423. $shenhe_goods = Option::get(OptionSetting::ACTIVITY_MINI_AUDIT_GOODS, $this->store_id, 'wechat', '')['value'];
  424. if (!empty($shenhe_goods)) {
  425. $this->goods_id = $shenhe_goods;
  426. }
  427. }
  428. if ($this->goods_id) {
  429. $arr = explode(',', $this->goods_id);
  430. $query->andWhere(['in', 'g.id', $arr]);
  431. }
  432. if ($this->mch_id) {
  433. $query->andWhere(['mch_id' => $this->mch_id]);
  434. }
  435. if ($this->keyword) {
  436. $query->andWhere([
  437. 'or',
  438. ['like', 'g.name', $this->keyword],
  439. ['like', 'g.sub_name', $this->keyword],
  440. ['like', 'g.key_word', $this->keyword],
  441. ['like', 'g.goods_no', $this->keyword]
  442. ]);
  443. }
  444. if (get_md_id()) {
  445. $md = Md::findOne(get_md_id());
  446. if ($md->is_single) {
  447. $query->leftJoin(['mdg' => MdGoods::tableName()], 'mdg.goods_id=g.id');
  448. // $query->andWhere(['g.product_type' => 0, 'mdg.status' => 1, 'mdg.md_id' => get_md_id()]);
  449. $query->andWhere(['OR', [
  450. 'g.product_type' => 0,
  451. 'mdg.status' => 1,
  452. 'mdg.md_id' => get_md_id()
  453. ], [
  454. 'g.product_type' => [
  455. 1, 2
  456. ]
  457. ]]);
  458. }
  459. }
  460. if($this->is_videoShopGoods){
  461. $query->leftJoin(['vsg' => \app\models\VideoShopGoodsExt::tableName()], 'vsg.goods_id = g.id');
  462. $query->leftJoin(['sm' => \app\models\StoreMini::tableName()], 'vsg.mini_id = sm.id');
  463. $query->andWhere(['vsg.wx_status' => 5, 'vsg.is_delete' => 0, 'sm.is_cancle' => 0]);
  464. $query->andWhere(['>', 'vsg.id','0']);
  465. }
  466. $count = $query->count();
  467. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  468. if ($this->sort == 0) {
  469. //综合,自定义排序+时间最新
  470. $query->orderBy('g.sort DESC, g.created_at DESC');
  471. }
  472. if ($this->sort == 1) {
  473. //时间最新
  474. $query->orderBy('g.created_at DESC');
  475. }
  476. if ($this->sort == 2) {
  477. if (get_md_id()) {
  478. //价格
  479. if ($this->sort_type == 0) {
  480. $query->orderBy('mdg.price ASC');
  481. } else {
  482. $query->orderBy('mdg.price DESC');
  483. }
  484. } else {
  485. //价格
  486. if ($this->sort_type == 0) {
  487. $query->orderBy('g.price ASC');
  488. } else {
  489. $query->orderBy('g.price DESC');
  490. }
  491. }
  492. }
  493. if ($this->sort == 3) {
  494. if (get_md_id()) {
  495. //销量
  496. if ($md->is_single) {
  497. $query->orderBy([
  498. '( IF(gn.num, gn.num, 0) + mdg.virtual_sales)' => SORT_DESC,
  499. 'g.created_at' => SORT_DESC,
  500. ]);
  501. } else {
  502. $query->orderBy([
  503. '( IF(gn.num, gn.num, 0) + g.virtual_sales)' => SORT_DESC,
  504. 'g.created_at' => SORT_DESC,
  505. ]);
  506. }
  507. } else {
  508. //销量
  509. $query->orderBy([
  510. '( IF(gn.num, gn.num, 0) + g.virtual_sales)' => SORT_DESC,
  511. 'g.created_at' => SORT_DESC,
  512. ]);
  513. }
  514. }
  515. // if ($this->is_videoShopGoods && $this->sort == 5) {
  516. // $query->orderBy(['( IF(g.rate_type, g.rate, g.price * g.rate / 100))' => SORT_DESC]);
  517. // }
  518. $od_query = OrderDetail::find()->alias('od')
  519. ->leftJoin(['o' => Order::tableName()], 'od.order_id=o.id')
  520. ->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');
  521. $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.product_type, g.attr, g.is_level ';
  522. if (get_md_id()) {
  523. if ($md->is_single) {
  524. $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';
  525. }
  526. }
  527. if ($this->is_videoShopGoods) {
  528. $select .= ',vsg.mini_id video_shop_mini_id, vsg.product_id wx_product_id, g.rate_type, g.rate';
  529. }
  530. $list = $query
  531. ->leftJoin(['gn' => $od_query], 'gn.goods_id=g.id')
  532. ->select($select)
  533. ->limit($pagination->limit)
  534. ->offset($pagination->offset)
  535. ->asArray()->all();
  536. $md_id = get_md_id();
  537. foreach ($list as $i => $item) {
  538. if (!$item['pic_url']) {
  539. $list[$i]['pic_url'] = Goods::getGoodsPicStatic($item['id']);
  540. }
  541. if ($item['rate']) {
  542. $list[$i]['rate_price'] = sprintf("%.2f", $item['rate_type'] ? $item['rate'] : ($item['rate'] * $item['price'] / 100));
  543. }
  544. $list[$i]['sales'] = $this->numToW($item['num'] + $item['virtual_sales']) . $item['unit'];
  545. $list[$i]['comment'] = OrderComment::find()->where(['store_id' => $this->store_id, 'goods_id' => $item['id'],
  546. 'is_hide' => 0, 'is_delete' => OrderComment::IS_DELETE_FALSE])->count();
  547. $goods_comment_count = OrderComment::find()->where(['store_id' => $this->store_id, 'goods_id' => $item['id'],
  548. 'is_hide' => 0, 'is_delete' => OrderComment::IS_DELETE_FALSE]);
  549. $goods_count = $goods_comment_count->andWhere(['>', 'score', 2])->count();
  550. $list[$i]['rank_good_percent'] = $goods_count > 0 ? round(($goods_count / $list[$i]['comment']) * 100) . '%' : 0 . '%';
  551. if ($md_id && $md->is_single && !in_array($list[$i]['product_type'], [1, 2])) {
  552. $list[$i]['price'] = $item['mdg_price'];
  553. $list[$i]['virtual_sales'] = $item['mdg_virtual_sales'];
  554. $list[$i]['goods_num'] = $item['mdg_goods_num'];
  555. $list[$i]['attr'] = $item['mdg_attr'];
  556. $list[$i]['status'] = $item['mdg_status'];
  557. }
  558. $list[$i]['product_type'] = intval($list[$i]['product_type']);
  559. if ($this->is_videoShopGoods) {
  560. $rebate = (new \app\modules\client\models\v1\GoodsForm())->getShareCommissionMoney(Goods::findone($item['id']), get_user_id());
  561. $list[$i]['rebate'] = floatval(sprintf('%.2f', $rebate));
  562. }
  563. $res = CommonGoods::getMemberPrice([
  564. 'attr' => $item['attr'],
  565. 'price' => $item['price'],
  566. 'mch_id' =>0,
  567. 'is_level' => $item['is_level'],
  568. ]);
  569. $goodsMemberPrice = $res['min_member_price'] ?: $item['price'];
  570. $list[$i]['price'] = sprintf("%.2f", $goodsMemberPrice);
  571. }
  572. $data = [
  573. 'row_count' => $count,
  574. 'page_count' => $pagination->pageCount,
  575. 'list' => $list,
  576. ];
  577. return [
  578. 'code' => 0,
  579. 'data' => $data
  580. ];
  581. }
  582. /**
  583. * 酒店商品搜索
  584. */
  585. public function searchHotel() {
  586. if (!$this->validate())
  587. return [
  588. 'code' => 1,
  589. 'msg' => $this->getErrorSummary(false)[0]
  590. ];
  591. $query = Goods::find()->alias('g')->where([
  592. 'g.store_id' => $this->store_id,
  593. 'g.status' => 1,
  594. 'g.is_delete' => 0,
  595. 'g.md_food_id' => 0,
  596. 'g.product_type' => Goods::GOODS_TYPE_DATE
  597. ])->leftJoin(['gb' => GoodsBook::tableName()], 'gb.goods_id = g.id');
  598. if ($this->keyword) {
  599. $query->andWhere([
  600. 'or',
  601. ['like', 'g.name', $this->keyword],
  602. ['like', 'g.key_word', $this->keyword]
  603. ]);
  604. }
  605. if ($this->sort == 0) {
  606. //综合,自定义排序+时间最新
  607. $query->orderBy('g.sort DESC, g.created_at DESC');
  608. }
  609. if ($this->sort == 1) {
  610. //时间最新
  611. $query->orderBy('g.created_at DESC');
  612. }
  613. if ($this->sort == 2) {
  614. //价格
  615. if ($this->sort_type == 0) {
  616. $query->orderBy('g.price ASC');
  617. } else {
  618. $query->orderBy('g.price DESC');
  619. }
  620. }
  621. if ($this->sort == 3) {
  622. //销量
  623. $query->orderBy([
  624. '( IF(gn.num, gn.num, 0) + g.virtual_sales)' => SORT_DESC,
  625. 'g.created_at' => SORT_DESC,
  626. ]);
  627. }
  628. $od_query = OrderDetail::find()->alias('od')
  629. ->leftJoin(['o' => Order::tableName()], 'od.order_id=o.id')
  630. ->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');
  631. $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';
  632. $list = $query
  633. ->leftJoin(['gn' => $od_query], 'gn.goods_id=g.id')
  634. ->select($select)
  635. ->asArray()->all();
  636. foreach ($list as $i => $item) {
  637. if ($this->start_time && $this->end_time) {
  638. $date_book = Json::decode($item['date_book']);
  639. if (is_array($date_book)) {
  640. $date_book = array_column($date_book, 'date');
  641. $first_date = array_shift($date_book);
  642. $last_date = array_pop($date_book);
  643. if ((strtotime($this->start_time) < strtotime($first_date) && strtotime($this->end_time) < strtotime($first_date))
  644. || (strtotime($this->start_time) > strtotime($last_date) && strtotime($this->end_time) > strtotime($last_date))) {
  645. unset($list[$i]);
  646. continue;
  647. }
  648. }
  649. }
  650. if (!$item['pic_url']) {
  651. $list[$i]['pic_url'] = Goods::getGoodsPicStatic($item['id']);
  652. }
  653. $list[$i]['sales'] = $this->numToW($item['num'] + $item['virtual_sales']) . $item['unit'];
  654. $list[$i]['comment'] = OrderComment::find()->where(['store_id' => $this->store_id, 'goods_id' => $item['id'],
  655. 'is_hide' => 0, 'is_delete' => OrderComment::IS_DELETE_FALSE])->count();
  656. $goods_comment_count = OrderComment::find()->where(['store_id' => $this->store_id, 'goods_id' => $item['id'],
  657. 'is_hide' => 0, 'is_delete' => OrderComment::IS_DELETE_FALSE]);
  658. $goods_count = $goods_comment_count->andWhere(['>', 'score', 2])->count();
  659. $list[$i]['rank_good_percent'] = $goods_count > 0 ? round(($goods_count / $list[$i]['comment']) * 100) . '%' : 0 . '%';
  660. $list[$i]['product_type'] = Goods::GOODS_TYPE_DATE;
  661. }
  662. $data = array_slice($list, ($this->page - 1) * $this->limit, $this->limit);
  663. return [
  664. 'code' => 0,
  665. 'msg' => 'success',
  666. 'data' => [
  667. 'row_count' => count($list),
  668. 'page_count' => ceil(count($list) / $this->limit),
  669. 'list' => $data,
  670. ]
  671. ];
  672. }
  673. }