MchDiyGoods.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\diy;
  8. use app\models\common\CommonGoods;
  9. use app\models\Goods;
  10. use app\models\MchGoodsCat;
  11. use app\models\Order;
  12. use app\models\OrderDetail;
  13. use yii\data\Pagination;
  14. use yii\db\ActiveQuery;
  15. use yii\helpers\ArrayHelper;
  16. /**
  17. * @property Store $store
  18. */
  19. class MchDiyGoods
  20. {
  21. public $store;
  22. public $type;
  23. public $cat;
  24. public $page;
  25. public $limit = 8;
  26. public $idList;
  27. public $is_page = false;
  28. public $mch;
  29. public $keyword;
  30. // 初始化模板信息
  31. public static function getTemplate(&$template, $mch_id, $store)
  32. {
  33. // 获取需要查找的id
  34. $list = [
  35. 'goods' => [
  36. 'id_list' => [],
  37. 'list' => []
  38. ],
  39. 'waimai' => [
  40. 'id_list' => [],
  41. 'list' => []
  42. ],
  43. 'miaosha' => [
  44. 'id_list' => [],
  45. 'list' => []
  46. ],
  47. 'pintuan' => [
  48. 'id_list' => [],
  49. 'list' => []
  50. ],
  51. 'bargain' => [
  52. 'id_list' => [],
  53. 'list' => []
  54. ],
  55. 'book' => [
  56. 'id_list' => [],
  57. 'list' => []
  58. ],
  59. 'shop' => [
  60. 'id_list' => [],
  61. 'list' => []
  62. ],
  63. 'lottery' => [
  64. 'id_list' => [],
  65. 'list' => []
  66. ],
  67. 'mch' => [
  68. 'id_list' => [],
  69. 'list' => []
  70. ],
  71. 'integral' => [
  72. 'id_list' => [],
  73. 'list' => []
  74. ],
  75. 'topic' => [
  76. 'id_list' => [],
  77. 'list' => []
  78. ],
  79. ];
  80. foreach ($template as &$value) {
  81. if ($value['type'] == 'search') {
  82. $value['param']['search_url'] = '/pages/search/search?mch_id=' . $mch_id;
  83. }
  84. if (array_key_exists($value['type'], $list) && $value['param']['list']) {
  85. $type = $value['type'];
  86. if($type == 'shop') {
  87. foreach ($value['param']['list'] as $item) {
  88. if (!in_array($item['id'], $list[$type]['id_list'])) {
  89. $list[$type]['id_list'][] = $item['id'];
  90. }
  91. }
  92. } else if ($type == 'mch') {
  93. foreach ($value['param']['list'] as $item) {
  94. if (!in_array($item['id'], $list[$type]['id_list'])) {
  95. $list[$type]['id_list'][] = $item['id'];
  96. }
  97. if ($item['goods_style'] == 2) {
  98. foreach ($item['goods_list'] as $v) {
  99. if (!in_array($v['id'], $list['goods']['id_list'])) {
  100. $list['goods']['id_list'][] = $v['id'];
  101. }
  102. }
  103. }
  104. }
  105. } else {
  106. foreach ($value['param']['list'] as $item) {
  107. if ($item['goods_style'] == 2) {
  108. foreach ($item['goods_list'] as $v) {
  109. if (!in_array($v['id'], $list[$type]['id_list'])) {
  110. $list[$type]['id_list'][] = $v['id'];
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. $ok = false;
  119. foreach ($list as $key => &$value) {
  120. if(!empty($value['id_list'])) {
  121. $res = MchDiyGoods::getGoods($store, $key, $value['id_list']);
  122. $list[$key]['list'] = $res['goods_list'];
  123. $ok = true;
  124. } else {
  125. $list[$key]['list'] = [];
  126. }
  127. }
  128. if ($ok) {
  129. foreach ($template as &$value) {
  130. if (array_key_exists($value['type'], $list) && $value['param']['list']) {
  131. foreach ($value['param']['list'] as &$item) {
  132. if($value['type'] == 'shop') {
  133. if(is_array($list[$value['type']]['list'])) {
  134. foreach ($list[$value['type']]['list'] as $goods) {
  135. if ($goods['id'] == $item['id']) {
  136. $item = $goods;
  137. break;
  138. }
  139. }
  140. }
  141. } else if($value['type'] == 'mch') {
  142. if(is_array($list[$value['type']]['list'])) {
  143. foreach ($list[$value['type']]['list'] as $mch) {
  144. if ($mch['id'] == $item['id']) {
  145. $item['name'] = $mch['name'];
  146. $item['pic_url'] = $mch['pic_url'];
  147. $item['logo'] = $mch['pic_url'];
  148. $item['goods_count'] = $mch['goods_count'];
  149. break;
  150. }
  151. }
  152. }
  153. if ($item['goods_style'] == 2) {
  154. foreach ($item['goods_list'] as &$v) {
  155. if(is_array($list['goods']['list'])){
  156. foreach ($list['goods']['list'] as $goods) {
  157. if ($goods['id'] == $v['id']) {
  158. $v = $goods;
  159. break;
  160. }
  161. }
  162. }
  163. }
  164. unset($v);
  165. // $item['goods_list'] = $list['goods']['list'];
  166. }
  167. } else {
  168. if ($item['goods_style'] == 2) {
  169. $newList = [];
  170. foreach ($item['goods_list'] as $v) {
  171. if(is_array($list[$value['type']]['list'])){
  172. foreach ($list[$value['type']]['list'] as $goods) {
  173. if ($goods['id'] == $v['id']) {
  174. $newList[] = $goods;
  175. break;
  176. }
  177. }
  178. }
  179. }
  180. $item['goods_list'] = $newList;
  181. }
  182. }
  183. }
  184. unset($item);
  185. }
  186. }
  187. unset($value);
  188. }
  189. }
  190. // 获取详情(商城、秒杀、拼团、砍价、积分商城、多商户等)
  191. public static function getGoods($store, $type, $idList = [] , $cat = 0, $limit = 8, $isPage = false, $page = 1, $mch = false)
  192. {
  193. $form = new MchDiyGoods();
  194. $form->type = $type;
  195. $form->idList = $idList;
  196. $form->cat = $cat;
  197. $form->limit = $limit;
  198. $form->is_page = $isPage;
  199. $form->page = $page;
  200. $form->mch = $mch;
  201. $form->keyword = \Yii::$app->request->get('keyword');
  202. $form->store = $store;
  203. $res = $form->getDetail();
  204. return $res;
  205. }
  206. // 获取具体信息
  207. public function getDetail()
  208. {
  209. $res = [];
  210. $type = $this->type;
  211. if (method_exists($this, $type)) {
  212. $res = $this->$type();
  213. }
  214. return $res;
  215. }
  216. private function goods()
  217. {
  218. $query = Goods::find()->alias('g')
  219. ->where(['g.store_id' => get_store_id(), 'g.is_delete' => 0, 'g.type' => 0, 'g.status' => 1]);
  220. if ($this->cat != 0) {
  221. $query->leftJoin(['gc' => MchGoodsCat::tableName()], 'gc.goods_id=g.id')
  222. ->andWhere(['gc.cat_id' => $this->cat])
  223. ->groupBy('g.id')
  224. ->orderBy(['g.sort' => SORT_DESC, 'g.addtime' => SORT_DESC]);
  225. }
  226. if($this->mch) {
  227. $query->andWhere(['g.mch_id' => $this->mch]);
  228. }
  229. if($this->keyword) {
  230. $query->andWhere(['like', 'g.name', $this->keyword]);
  231. }
  232. if($this->idList) {
  233. $query->andWhere(['g.id' => $this->idList]);
  234. }
  235. $count = $query->count();
  236. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  237. if($this->is_page) {
  238. $query->limit($pagination->limit)->offset($pagination->offset);
  239. }
  240. $list = $query->orderBy(['g.sort' => SORT_DESC, 'g.id' => SORT_DESC])->all();
  241. $goodsList = [];
  242. /* @var Goods[] $list */
  243. foreach ($list as $key => $item) {
  244. $attrGroupList = json_decode(json_encode($item->getAttrGroupList()), true);
  245. $price = round($item->price, 2);
  246. $res = CommonGoods::getMemberPrice([
  247. 'attr' => $item['attr'],
  248. 'price' => $item['price'],
  249. 'mch_id' => $item['mch_id'],
  250. 'is_level' => $item['is_level'],
  251. ]);
  252. $goodsMemberPrice = $res['min_member_price'] ? $res['min_member_price'] : $item['price'];
  253. $goodsList[] = [
  254. 'attr' => json_decode($item['attr'], true),
  255. 'attr_group_list' => $attrGroupList,
  256. 'id' => $item->id,
  257. 'goods_id' => $item->id,
  258. 'pic_url' => $item->cover_pic,
  259. 'cover_pic' => $item->cover_pic,
  260. 'price' => $price ? $price : 0,
  261. 'original_price' => $item->original_price,
  262. 'name' => $item->name,
  263. 'page_url' => '/goods/goods/goods?id=' . $item->id,
  264. 'use_attr' => $item->use_attr,
  265. 'is_negotiable' => $item->is_negotiable,
  266. 'price_content' => $item->is_negotiable == 0 ? '¥'.$price : '价格面议',
  267. 'goods_member_price' => $goodsMemberPrice
  268. ];
  269. }
  270. return [
  271. 'goods_list' => $goodsList,
  272. 'pagination' => $pagination
  273. ];
  274. }
  275. private function pintuan()
  276. {
  277. $query = PtGoods::find()->where([
  278. 'store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1
  279. ])->orderBy(['is_hot' => SORT_DESC, 'addtime' => SORT_DESC]);
  280. if($this->mch) {
  281. $query->andWhere(['mch_id' => $this->mch]);
  282. }
  283. if($this->keyword) {
  284. $query->andWhere(['like', 'name', $this->keyword]);
  285. }
  286. if($this->idList) {
  287. $query->andWhere(['id' => $this->idList]);
  288. }
  289. $count = $query->count();
  290. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  291. if($this->is_page) {
  292. $query->limit($pagination->limit)->offset($pagination->offset);
  293. }
  294. $list = $query->all();
  295. $goodsList = [];
  296. /* @var PtGoods[] $list */
  297. foreach ($list as $key => $item) {
  298. $attr = json_decode($item['attr'], true);
  299. $goodsList[] = [
  300. 'attr' => $attr,
  301. 'attr_group' => json_decode(json_encode($item->getAttrGroupList()), true),
  302. 'id' => $item->id,
  303. 'goods_id' => $item->id,
  304. 'pic_url' => $item->cover_pic,
  305. 'cover_pic' => $item->cover_pic,
  306. 'price' => round($item->price, 2) ? round($item->price, 2) : 0,
  307. 'original_price' => $item->original_price,
  308. 'name' => $item->name,
  309. 'page_url' => '/pages/pt/details/details?gid=' . $item->id,
  310. 'use_attr' => $item->use_attr,
  311. 'is_negotiable' => 0,
  312. 'price_content' => $item->group_num . '人团',
  313. 'original_price_content' => '单买价:',
  314. ];
  315. }
  316. return [
  317. 'goods_list' => $goodsList,
  318. 'pagination' => $pagination
  319. ];
  320. }
  321. private function miaosha()
  322. {
  323. $date = date('Y-m-d', time());
  324. $time = date('H', time());
  325. $query = MiaoshaGoods::find()->alias('mg')->where([
  326. 'mg.store_id' => get_store_id(), 'mg.is_delete' => 0
  327. ])->joinWith('msGoods g')->andWhere(['g.is_delete' => 0]);
  328. if($this->mch) {
  329. $query->andWhere(['mg.mch_id' => $this->mch]);
  330. }
  331. if($this->keyword) {
  332. $query->andWhere(['like', 'g.name', $this->keyword]);
  333. }
  334. if($this->keyword) {
  335. $query->andWhere(['like', 'g.name', $this->keyword]);
  336. }
  337. if($this->idList) {
  338. $query->andWhere(['mg.id' => $this->idList]);
  339. } else {
  340. $query->andWhere([
  341. 'or',
  342. [
  343. 'and',
  344. ['mg.open_date' => $date],
  345. ['>=', 'mg.start_time', $time]
  346. ],
  347. [
  348. 'and',
  349. ['>', 'mg.open_date', $date],
  350. ]
  351. ]);
  352. }
  353. $count = $query->count();
  354. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  355. if($this->is_page) {
  356. $query->limit($pagination->limit)->offset($pagination->offset);
  357. }
  358. $list = $query->all();
  359. $goodsList = [];
  360. /* @var MiaoshaGoods[] $list */
  361. foreach ($list as $key => $item) {
  362. /* @var MsGoods $msGoods */
  363. $msGoods = $item->msGoods;
  364. $price = $msGoods->original_price;
  365. $attr = json_decode($item['attr'], true);
  366. foreach ($attr as &$value) {
  367. $value['num'] = $value['miaosh_num'];
  368. $value['price'] = $value['miaosh_price'];
  369. if ($price == 0 || ($price > 0 && round($price, 2) >= round($value['miaosha_price'], 2))) {
  370. $price = $value['miaosha_price'];
  371. }
  372. }
  373. unset($value);
  374. $goodsList[] = [
  375. 'attr' => $attr,
  376. 'attr_group' => json_decode(json_encode($msGoods->getAttrGroupList()), true),
  377. 'id' => $item->id,
  378. 'goods_id' => $msGoods->id,
  379. 'pic_url' => $msGoods->cover_pic,
  380. 'cover_pic' => $msGoods->cover_pic,
  381. 'price' => round($price, 2) ? round($price, 2) : 0,
  382. 'original_price' => $msGoods->original_price,
  383. 'name' => $msGoods->name,
  384. 'page_url' => '/pages/miaosha/details/details?id=' . $item->id,
  385. 'use_attr' => $msGoods->use_attr,
  386. 'open_date' => $item->open_date,
  387. 'start_time' => $item->start_time,
  388. 'is_negotiable' => 0,
  389. 'price_content' => '秒杀价:',
  390. 'original_price_content' => '售价:',
  391. ];
  392. }
  393. return [
  394. 'goods_list' => $goodsList,
  395. 'pagination' => $pagination
  396. ];
  397. }
  398. private function book ()
  399. {
  400. $query = YyGoods::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1]);
  401. if($this->mch) {
  402. $query->andWhere(['mch_id' => $this->mch]);
  403. }
  404. if($this->keyword) {
  405. $query->andWhere(['like', 'name', $this->keyword]);
  406. }
  407. if($this->idList) {
  408. $query->andWhere(['id' => $this->idList]);
  409. }
  410. $count = $query->count();
  411. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  412. if($this->is_page) {
  413. $query->limit($pagination->limit)->offset($pagination->offset);
  414. }
  415. $list = $query->all();
  416. $goodsList = [];
  417. /* @var YyGoods[] $list */
  418. foreach ($list as $key => $item) {
  419. $price = round($item->price, 2);
  420. $goodsList[] = [
  421. 'attr' => json_decode($item->attr, true),
  422. 'attr_group' => json_decode(json_encode($item->getAttrGroupList()), true),
  423. 'id' => $item->id,
  424. 'goods_id' => $item->id,
  425. 'pic_url' => $item->cover_pic,
  426. 'cover_pic' => $item->cover_pic,
  427. 'price' => $price ? $price : 0,
  428. 'original_price' => $item->original_price,
  429. 'name' => $item->name,
  430. 'page_url' => '/pages/book/details/details?id=' . $item->id,
  431. 'use_attr' => $item->use_attr,
  432. 'is_negotiable' => 0,
  433. 'price_content' => $price ? '¥'.$price : '免费'
  434. ];
  435. }
  436. return [
  437. 'goods_list' => $goodsList,
  438. 'pagination' => $pagination
  439. ];
  440. }
  441. private function bargain()
  442. {
  443. $query = Goods::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1,'type' => 2])->with('bargain');
  444. if($this->mch) {
  445. $query->andWhere(['mch_id' => $this->mch]);
  446. }
  447. if($this->keyword) {
  448. $query->andWhere(['like', 'name', $this->keyword]);
  449. }
  450. if($this->idList) {
  451. $query->andWhere(['id' => $this->idList]);
  452. }
  453. $count = $query->count();
  454. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  455. if($this->is_page) {
  456. $query->limit($pagination->limit)->offset($pagination->offset);
  457. }
  458. $list = $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])->all();
  459. $goodsList = [];
  460. /* @var Goods[] $list */
  461. foreach ($list as $key => $item) {
  462. /* @var BargainGoods $goods*/
  463. $goods = $item->bargain;
  464. $goodsList[] = [
  465. 'attr' => json_decode($item->attr, true),
  466. 'attr_group' => json_decode(json_encode($item->getAttrGroupList()), true),
  467. 'goods_id' => $item->id,
  468. 'pic_url' => $item->cover_pic,
  469. 'cover_pic' => $item->cover_pic,
  470. 'original_price' => $item->price,
  471. 'name' => $item->name,
  472. 'use_attr' => $item->use_attr,
  473. 'id' => $item->id,
  474. 'price' => round($goods->min_price, 2) ? round($goods->min_price, 2) : 0,
  475. 'page_url' => '/bargain/goods/goods?goods_id=' . $goods->goods_id,
  476. 'is_negotiable' => 0,
  477. 'begin_time' => $goods->begin_time,
  478. 'end_time' => $goods->end_time,
  479. 'begin_time_text' => $goods->getBeginTimeText(),
  480. 'end_time_text' => $goods->getEndTimeText(),
  481. 'price_content' => '最低价:',
  482. 'original_price_content' => '售价:',
  483. ];
  484. }
  485. return [
  486. 'goods_list' => $goodsList,
  487. 'pagination' => $pagination
  488. ];
  489. }
  490. private function waimai()
  491. {
  492. $query = Goods::find()->alias('g')
  493. ->where(['g.store_id' => get_store_id(), 'g.is_delete' => 0, 'g.type' => 0, 'g.status' => 1]);
  494. if ($this->cat != 0) {
  495. $query->leftJoin(['gc' => MchGoodsCat::tableName()], 'gc.goods_id=g.id')
  496. ->andWhere(['gc.cat_id' => $this->cat])
  497. ->groupBy('g.id')
  498. ->orderBy(['g.sort' => SORT_DESC, 'g.addtime' => SORT_DESC]);
  499. }
  500. if($this->mch) {
  501. $query->andWhere(['g.mch_id' => $this->mch]);
  502. }
  503. if($this->keyword) {
  504. $query->andWhere(['like', 'g.name', $this->keyword]);
  505. }
  506. if($this->idList) {
  507. $query->andWhere(['g.id' => $this->idList]);
  508. }
  509. $count = $query->count();
  510. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  511. if($this->is_page) {
  512. $query->limit($pagination->limit)->offset($pagination->offset);
  513. }
  514. $list = $query->orderBy(['g.sort' => SORT_DESC, 'g.id' => SORT_DESC])->all();
  515. $goodsList = [];
  516. /* @var Goods[] $list */
  517. foreach ($list as $key => $item) {
  518. $attrGroupList = json_decode(json_encode($item->getAttrGroupList()), true);
  519. $price = round($item->price, 2);
  520. $monthly_sales = $this->monthly_sales($item->mch_id,$item->id);
  521. $goodsList[] = [
  522. 'attr' => json_decode($item['attr'], true),
  523. 'attr_group_list' => $attrGroupList,
  524. 'id' => $item->id,
  525. 'goods_id' => $item->id,
  526. 'pic_url' => $item->cover_pic,
  527. 'cover_pic' => $item->cover_pic,
  528. 'price' => $price ? $price : 0,
  529. 'original_price' => $item->original_price,
  530. 'name' => $item->name,
  531. 'page_url' => '/goods/goods/goods?id=' . $item->id,
  532. 'use_attr' => $item->use_attr,
  533. 'is_negotiable' => $item->is_negotiable,
  534. 'price_content' => $item->is_negotiable == 0 ? '¥'.$price : '价格面议',
  535. 'monthly_sales' => $monthly_sales,
  536. 'num' => 0,
  537. ];
  538. }
  539. return [
  540. 'goods_list' => $goodsList,
  541. 'pagination' => $pagination
  542. ];
  543. }
  544. private function monthly_sales($mch_id,$goods_id){
  545. $beginThismonth = mktime(0,0,0,date('m'),1,date('Y'));
  546. $endThismonth = mktime(23,59,59,date('m'),date('t'),date('Y'));
  547. $monthly_sales = OrderDetail::find()->alias('od')
  548. ->leftJoin(['o' => Order::tableName()], 'od.order_id=o.id')
  549. ->where([
  550. 'od.is_delete' => 0,
  551. 'o.mch_id' => $mch_id,
  552. 'od.goods_id' => $goods_id,
  553. 'o.is_pay' => 1,
  554. 'o.is_delete' => 0,
  555. ])->andWhere([
  556. '<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL
  557. ])->andWhere([
  558. 'AND',
  559. ['>=', 'o.created_at', $beginThismonth],
  560. ['<=', 'o.created_at', $endThismonth],
  561. ])->sum('num');
  562. $monthly_sales = $monthly_sales ? $monthly_sales : 0;
  563. return $monthly_sales;
  564. }
  565. // diy组件权限
  566. public static function getDiyAuth()
  567. {
  568. return ['search', 'nav', 'banner', 'notice', 'link', 'rubik', 'video' ,'goods', 'waimai', 'time', 'line','pintuan','miaosha','book','bargain','coupon'];
  569. }
  570. }