GoodsListForm.php 31 KB

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