ThemeService.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\service;
  20. use app\common\{enum\ActivityEnum,
  21. enum\GoodsEnum,
  22. enum\LiveEnum,
  23. enum\PresellEnum,
  24. enum\SeckillEnum,
  25. enum\TeamEnum,
  26. model\Cart,
  27. model\CouponList,
  28. model\Goods,
  29. model\Order,
  30. model\Coupon,
  31. enum\CouponEnum,
  32. model\Presell,
  33. model\PresellGoods,
  34. model\PresellGoodsItem,
  35. model\SeckillActivity,
  36. model\SeckillGoodsItem,
  37. model\ShopNotice,
  38. enum\ThemePageEnum,
  39. model\GoodsCategoryIndex,
  40. model\TeamActivity,
  41. model\TeamGoodsItem};
  42. use function JmesPath\search;
  43. /**
  44. * 主题功能类
  45. * Class ThemeService
  46. * @package app\common\service
  47. */
  48. class ThemeService
  49. {
  50. public static array $params = [];
  51. /**
  52. * @notes 替换组件内容
  53. * @param array $content
  54. * @return array
  55. * @author cjhao
  56. * @date 2021/8/19 18:52
  57. */
  58. public static function getModuleData(array $content,array $config = []):array
  59. {
  60. self::$params = $config;
  61. $isDistribution = self::$params['is_distribution'] ?? false; //是否分销
  62. $isVerifier = self::$params['is_verifier'] ?? false; //是否核销员
  63. $source = self::$params['source'] ?? 'shop'; //后台组件替换或商城组件替换
  64. $userId = self::$params['user_id'] ?? '';
  65. $moduleList = array_column($content,'name');
  66. foreach ($moduleList as $moduleKey => $moduleName) {
  67. //需要拼接数据的组件
  68. switch ($moduleName) {
  69. //商品组件
  70. case ThemePageEnum::GOODS:
  71. $goods_type = $content[$moduleKey]['content']['goods_type'] ?? 2;
  72. //商品分类
  73. $limit = false;
  74. if(2 == $goods_type){
  75. if('admin' == $source){
  76. $content[$moduleKey]['content']['data'] = [];
  77. break;
  78. }
  79. $categoryId = $content[$moduleKey]['content']['category']['id'] ?? 0;
  80. $limit = $content[$moduleKey]['content']['category']['num'] ?? false;
  81. $goodsIds = GoodsCategoryIndex::where(['category_id'=>$categoryId])->column('goods_id');
  82. }else{
  83. $goodsIds = array_column($content[$moduleKey]['content']['data'], 'id');
  84. }
  85. //如果id都是空,直接返回数组
  86. if (empty($goodsIds)) {
  87. $content[$moduleKey]['content']['data'] = [];
  88. break;
  89. }
  90. //todo 商品需要根据顺序排序
  91. $orderField = implode(',', $goodsIds);
  92. $goodsList = Goods::where(['id' => $goodsIds,'status'=>GoodsEnum::STATUS_SELL])
  93. ->field('id,name,image,virtual_sales_num+sales_num as sales_num,min_price as sell_price,min_lineation_price as lineation_price')
  94. ->orderRaw("field(id,$orderField)")
  95. ->limit($limit)
  96. ->order('sort desc')
  97. ->select()
  98. ->toArray();
  99. //是否显示划线加
  100. $showPrice = ConfigService::get('goods_set', 'show_price', 1);
  101. if(0 == $showPrice){
  102. foreach ($goodsList as $goodsKey => $goodsVal){
  103. $goodsList[$goodsKey]['lineation_price'] = 0;
  104. }
  105. }
  106. $content[$moduleKey]['content']['data'] = $goodsList;
  107. break;
  108. //选项卡组件 todo 选项卡的data是多维数据
  109. case ThemePageEnum::TABS:
  110. $dataList = $content[$moduleKey]['content']['data'];
  111. foreach ($dataList as $dataKey => $dataVal){
  112. $goods_type = $dataVal['goods_type'] ?? 1;
  113. $limit = false;
  114. if(2 == $goods_type){
  115. if('admin' == $source){
  116. $content[$moduleKey]['content']['data'][$dataKey]['data'] = [];
  117. break;
  118. }
  119. $categoryId = $dataVal['category']['id'] ?? 0;
  120. $limit = $dataVal['category']['num'] ?? false;
  121. $goodsIds = GoodsCategoryIndex::where(['category_id'=>$categoryId])->column('goods_id');
  122. }else{
  123. $goodsIds = array_column($dataVal['data'], 'id');
  124. }
  125. //如果id都是空,直接返回数组
  126. if (empty($goodsIds)) {
  127. $content[$moduleKey]['content']['data'][$dataKey]['data'] = [];
  128. break;
  129. }
  130. //todo 商品需要根据顺序排序
  131. $orderField = implode(',', $goodsIds);
  132. $goodsList = Goods::where(['id' => $goodsIds,'status'=>GoodsEnum::STATUS_SELL])
  133. ->field('id,name,image,virtual_sales_num+sales_num as sales_num,min_price as sell_price,min_lineation_price as lineation_price')
  134. ->orderRaw("field(id,$orderField)")
  135. ->limit($limit)
  136. ->select()
  137. ->toArray();
  138. //是否显示划线加
  139. $showPrice = ConfigService::get('goods_set', 'show_price', 1);
  140. if(0 == $showPrice){
  141. foreach ($goodsList as $goodsKey => $goodsVal){
  142. $goodsList[$goodsKey]['lineation_price'] = 0;
  143. }
  144. }
  145. $content[$moduleKey]['content']['data'][$dataKey]['data'] = $goodsList;
  146. }
  147. break;
  148. //优惠券组件
  149. case ThemePageEnum::COUPON:
  150. $couponIds = array_column($content[$moduleKey]['content']['data'], 'id');
  151. //如果id都是空,直接返回数组
  152. if (empty($couponIds)) {
  153. $content[$moduleKey]['content']['data'] = [];
  154. break;
  155. }
  156. //todo 优惠券需要根据顺序排序
  157. $orderField = implode(',', $couponIds);
  158. $couponList = Coupon::where(['id' => $couponIds,'status'=>[CouponEnum::COUPON_STATUS_NOT,CouponEnum::COUPON_STATUS_CONDUCT]])
  159. ->append([ 'use_type', 'discount_content', 'status_desc', 'condition' ])
  160. ->field('id,name,money,discount_ratio,condition_type,get_num_type,condition_money,use_goods_type,get_num,status')
  161. ->orderRaw("field(id,$orderField)")
  162. ->select();
  163. $myCouponIds = [];
  164. if($couponList && $userId){
  165. $myCouponIds = CouponList::where(['user_id'=>$userId,'status'=>CouponEnum::USE_STATUS_NOT])
  166. ->column('coupon_id');
  167. }
  168. $list = [];
  169. foreach ($couponList as $coupon) {
  170. //是否已领取
  171. $isReceive = 0;
  172. if(in_array($coupon->id,$myCouponIds)){
  173. $isReceive = 1;
  174. }
  175. $isAvailable = 0;
  176. //是否可领取
  177. switch ($coupon->get_num_type) {
  178. case CouponEnum::GET_NUM_TYPE_LIMIT:
  179. $total = CouponList::where(['coupon_id' => $coupon->id])
  180. ->where(['user_id' => $userId])
  181. ->count();
  182. $isAvailable = $total >= $coupon->get_num ? 1 : 0;
  183. break;
  184. case CouponEnum::GET_NUM_TYPE_DAY:
  185. $total = CouponList::where(['coupon_id' => $coupon->id])
  186. ->where(['user_id' =>$userId])
  187. ->where('create_time', '>=', TimeService::today()[0])
  188. ->where('create_time', '<=', TimeService::today()[1])
  189. ->count();
  190. $isAvailable = $total >= $coupon->get_num ? 1 : 0;
  191. break;
  192. }
  193. $coupon->is_receive = $isReceive;
  194. $coupon->is_available = $isAvailable;
  195. $list[] = $coupon->toArray();
  196. }
  197. $content[$moduleKey]['content']['data'] = $list;
  198. break;
  199. //公告组件
  200. case ThemePageEnum::NOTICE:
  201. $limit = $content[$moduleKey]['content']['num'] ?? 1;//默认拿一条
  202. $noticeList = ShopNotice::field('id,name')
  203. ->where(['status'=>1])
  204. ->limit($limit)
  205. ->order('sort asc,id desc')
  206. ->select()
  207. ->toArray();
  208. $content[$moduleKey]['content']['data'] = $noticeList;
  209. break;
  210. //会员中心-我的服务组件、微页面-导航组件
  211. case ThemePageEnum::USERSERVE:
  212. case ThemePageEnum::NAVIGATION:
  213. if('admin' == $source){
  214. break;
  215. }
  216. $linksData = $content[$moduleKey]['content']['data'];
  217. foreach ($linksData as $linkKey => $link){
  218. // 非分销用户,屏蔽分销菜单,index == 17时分销菜单,
  219. if(false === $isDistribution && isset($link['link']['index']) && 17 == $link['link']['index']){
  220. unset($linksData[$linkKey]);
  221. }
  222. //非核销用户,屏蔽分销菜单
  223. if(false === $isVerifier && isset($link['link']['index']) && 19 == $link['link']['index']){
  224. unset($linksData[$linkKey]);
  225. }
  226. }
  227. $content[$moduleKey]['content']['data'] = array_values($linksData);
  228. break;
  229. //商品拼团
  230. case ThemePageEnum::SPELLGROUP:
  231. if('admin' == $source){
  232. break;
  233. }
  234. $dataType = $content[$moduleKey]['content']['data_type'];
  235. $data = $content[$moduleKey]['content']['data'];
  236. $limit = false;
  237. $where = [];
  238. if(1 == $dataType) {
  239. //获取数量
  240. $limit = $content[$moduleKey]['content']['num'];
  241. $orderField = 'TA.id desc';
  242. }else{
  243. if(empty($data)){
  244. break;
  245. }
  246. $goods_ids = array_column($data,'goods_id');
  247. $where = ['goods_id'=>$goods_ids];
  248. $goodsIds = implode(',',array_column($data,'goods_id'));
  249. $orderField = "field(goods_id,$goodsIds)";
  250. }
  251. $goodsList = TeamActivity::alias('TA')
  252. ->join('team_goods TG','TA.id = TG.team_id')
  253. ->join('goods G','TG.goods_id = G.id')
  254. ->where([
  255. ['TA.status','=',TeamEnum::TEAM_STATUS_CONDUCT],
  256. ['start_time', '<=', time()],
  257. ['end_time', '>=', time()],
  258. ])
  259. ->where($where)
  260. ->field('TA.id as activity_id,TG.id,goods_id,goods_snap,min_team_price,people_num,G.image')
  261. ->limit($limit)
  262. ->orderRaw($orderField)
  263. ->select()
  264. ->toArray();
  265. if(empty($goodsList)){
  266. $content[$moduleKey]['content']['data'] = [];
  267. break;
  268. }
  269. $activityIds = array_column($goodsList,'activity_id');
  270. $goodsIds = array_column($goodsList,'goods_id');
  271. $salesList = TeamGoodsItem::where(['team_id'=>$activityIds,'goods_id'=>$goodsIds])
  272. ->group('goods_id')
  273. ->column('sum(sales_volume)','goods_id');
  274. $data = [];
  275. foreach ($goodsList as $goods){
  276. $goods_snap = json_decode($goods['goods_snap'],true);
  277. $data[] = [
  278. 'id' => $goods['id'],
  279. 'activity_id' => $goods['activity_id'],
  280. 'activity_type' => ActivityEnum::TEAM,
  281. 'goods_id' => $goods['goods_id'],
  282. 'name' => $goods_snap['name'],
  283. 'image' => FileService::getFileUrl($goods['image']),
  284. 'people_num' => $goods['people_num'],
  285. 'sell_price' => $goods_snap['min_price'],
  286. 'activity_price' => $goods['min_team_price'],
  287. 'activity_sales' => $salesList[$goods['goods_id']] ?? 0,
  288. ];
  289. }
  290. $content[$moduleKey]['content']['data'] = $data;
  291. break;
  292. //商品秒杀
  293. case ThemePageEnum::SECKILL:
  294. if('admin' == $source){
  295. break;
  296. }
  297. $dataType = $content[$moduleKey]['content']['data_type'];
  298. $data = $content[$moduleKey]['content']['data'];
  299. $limit = false;
  300. $where = [];
  301. //自动获取商品
  302. if(1 == $dataType) {
  303. //获取数量
  304. $limit = $content[$moduleKey]['content']['num'];
  305. $orderField = 'SA.id desc';
  306. }else{
  307. if(empty($data)){
  308. break;
  309. }
  310. $goods_ids = array_column($data,'goods_id');
  311. $where = ['goods_id'=>$goods_ids];
  312. $goodsIds = implode(',',array_column($data,'goods_id'));
  313. $orderField = "field(goods_id,$goodsIds)";
  314. }
  315. $goodsList = SeckillActivity::alias('SA')
  316. ->join('seckill_goods SG','SA.id = SG.seckill_id')
  317. ->join('goods G','SG.goods_id = G.id')
  318. ->where([
  319. ['SA.status','=',SeckillEnum::SECKILL_STATUS_CONDUCT],
  320. ['start_time', '<=', time()],
  321. ['end_time', '>=', time()],
  322. ])
  323. ->where($where)
  324. ->field('SA.id as activity_id,SG.id,goods_id,goods_snap,min_seckill_price,G.image')
  325. ->limit($limit)
  326. ->orderRaw($orderField)
  327. ->select()
  328. ->toArray();
  329. if(empty($goodsList)){
  330. $content[$moduleKey]['content']['data'] = [];
  331. break;
  332. }
  333. //销量
  334. $activityIds = array_column($goodsList,'activity_id');
  335. $goodsIds = array_column($goodsList,'goods_id');
  336. $salesList = SeckillGoodsItem::where(['seckill_id'=>$activityIds,'goods_id'=>$goodsIds])
  337. ->group('goods_id')
  338. ->column('sum(sales_volume)','goods_id');
  339. $data = [];
  340. foreach ($goodsList as $goods){
  341. $goodsSnap = json_decode($goods['goods_snap'],true);
  342. $data[] = [
  343. 'id' => $goods['id'],
  344. 'activity_id' => $goods['activity_id'],
  345. 'activity_type' => ActivityEnum::SECKILL,
  346. 'goods_id' => $goods['goods_id'],
  347. 'name' => $goodsSnap['name'],
  348. 'image' => FileService::getFileUrl($goods['image']),
  349. 'sell_price' => $goodsSnap['min_price'],
  350. 'activity_price' => $goods['min_seckill_price'],
  351. 'activity_sales' => $salesList[$goods['goods_id']] ?? 0,
  352. ];
  353. }
  354. $content[$moduleKey]['content']['data'] = $data;
  355. break;
  356. // 预售
  357. case ThemePageEnum::PRESELL:
  358. if('admin' == $source){
  359. break;
  360. }
  361. $dataType = $content[$moduleKey]['content']['data_type'];
  362. $data = $content[$moduleKey]['content']['data'];
  363. $limit = false;
  364. $where = [];
  365. //自动获取商品
  366. if(1 == $dataType) {
  367. //获取数量
  368. $limit = $content[$moduleKey]['content']['num'];
  369. $orderField = 'pg.id desc';
  370. }else{
  371. if(empty($data)){
  372. break;
  373. }
  374. $goods_ids = array_column($data,'goods_id');
  375. $where = ['pg.goods_id'=>$goods_ids];
  376. $goodsIds = implode(',',array_column($data,'goods_id'));
  377. $orderField = "field(goods_id,$goodsIds)";
  378. }
  379. $lists = PresellGoods::alias('pg')
  380. ->join('presell p', 'p.id=pg.presell_id')
  381. ->where('p.status', PresellEnum::STATUS_START)
  382. ->where('p.start_time', '<=', time())
  383. ->where('p.end_time', '>=', time())
  384. ->where($where)
  385. ->limit($limit)
  386. ->orderRaw($orderField)
  387. ->field([
  388. 'pg.id',
  389. 'p.id as presell_id', 'p.name', 'p.type',
  390. 'p.start_time', 'p.end_time', 'p.remark',
  391. 'p.send_type', 'p.send_type_day', 'p.buy_limit', 'p.buy_limit_num',
  392. 'p.status',
  393. 'pg.goods_id',
  394. 'pg.content',
  395. 'pg.min_price',
  396. 'pg.virtual_sale',
  397. ])
  398. ->with([
  399. 'items' => function ($query) {
  400. $query->field([ 'presell_goods_id', 'sale_nums' ]);
  401. }
  402. ])
  403. ->select()
  404. ->toArray();
  405. $data = [];
  406. foreach ($lists as $presell) {
  407. $data[] = [
  408. 'id' => $presell['goods_id'],
  409. 'activity_id' => $presell['presell_id'],
  410. 'activity_type' => ActivityEnum::PRESELL,
  411. 'goods_id' => $presell['goods_id'],
  412. 'name' => $presell['content']['name'],
  413. 'image' => FileService::getFileUrl($presell['content']['image']),
  414. 'sell_price' => $presell['content']['min_price'],
  415. 'activity_price' => $presell['min_price'],
  416. 'activity_sales' => array_sum(array_column($presell['items'], 'sale_nums')) + ($presell['virtual_sale']),
  417. ];
  418. }
  419. $content[$moduleKey]['content']['data'] = $data;
  420. break;
  421. //商品推荐
  422. case ThemePageEnum::GOODSRECOM:
  423. $show = $content[$moduleKey]['show'];
  424. if('admin' == $source || 0 == $show){
  425. break;
  426. }
  427. $content[$moduleKey]['content']['data'] = self::recommend();
  428. break;
  429. //小程序直播
  430. case ThemePageEnum::MNPLIVE:
  431. if('admin' == $source){
  432. break;
  433. }
  434. $num = $content[$moduleKey]['content']['num'] ?? 1;
  435. $result = WeChatService::getLiveRoom(0,$num);
  436. if (!is_array($result)) {
  437. break;
  438. }
  439. $data = [];
  440. foreach ($result['room_info'] as $item) {
  441. $data[] = [
  442. 'name' => $item['name'],
  443. 'room_id' => $item['roomid'],
  444. 'cover_img' => $item['cover_img'],
  445. 'anchor_name' => $item['anchor_name'],
  446. 'status' => $item['live_status'],
  447. 'live_status' => LiveEnum::getLiveStatus($item['live_status']),
  448. 'goods' => count($item['goods']),
  449. 'start_time' => date('Y-m-d H:i:s', $item['start_time']),
  450. 'end_time' => date('Y-m-d H:i:s', $item['end_time'])
  451. ];
  452. }
  453. $content[$moduleKey]['content']['data'] = $data;
  454. break;
  455. }
  456. }
  457. return $content;
  458. }
  459. public static function getPCModuleData(array $content,array $config = []):array
  460. {
  461. self::$params = $config;
  462. $isDistribution = self::$params['is_distribution'] ?? false; //是否分销
  463. $isVerifier = self::$params['is_verifier'] ?? false; //是否核销员
  464. $source = self::$params['source'] ?? 'shop'; //后台组件替换或商城组件替换
  465. $userId = self::$params['user_id'] ?? '';
  466. $moduleList = array_column($content,'name');
  467. foreach ($moduleList as $moduleKey => $moduleName){
  468. //需要拼接数据的组件
  469. switch ($moduleName) {
  470. case ThemePageEnum::GOODS:
  471. $goods_type = $content[$moduleKey]['content']['goods_type'] ?? 2;
  472. //商品分类
  473. $limit = false;
  474. if(2 == $goods_type){
  475. if('admin' == $source){
  476. $content[$moduleKey]['content']['data'] = [];
  477. break;
  478. }
  479. $categoryId = $content[$moduleKey]['content']['category']['id'] ?? 0;
  480. $limit = $content[$moduleKey]['content']['category']['num'] ?? false;
  481. $goodsIds = GoodsCategoryIndex::where(['category_id'=>$categoryId])->column('goods_id');
  482. }else{
  483. $goodsIds = array_column($content[$moduleKey]['content']['data'], 'id');
  484. }
  485. //如果id都是空,直接返回数组
  486. if (empty($goodsIds)) {
  487. $content[$moduleKey]['content']['data'] = [];
  488. break;
  489. }
  490. //todo 商品需要根据顺序排序
  491. $orderField = implode(',', $goodsIds);
  492. $goodsList = Goods::where(['id' => $goodsIds,'status'=>GoodsEnum::STATUS_SELL])
  493. ->field('id,name,image,virtual_sales_num+sales_num as sales_num,min_price as sell_price,min_lineation_price as lineation_price')
  494. ->orderRaw("field(id,$orderField)")
  495. ->limit($limit)
  496. ->select()
  497. ->toArray();
  498. //是否显示划线加
  499. $showPrice = ConfigService::get('goods_set', 'show_price', 1);
  500. if(0 == $showPrice){
  501. foreach ($goodsList as $goodsKey => $goodsVal){
  502. $goodsList[$goodsKey]['lineation_price'] = 0;
  503. }
  504. }
  505. $content[$moduleKey]['content']['data'] = $goodsList;
  506. break;
  507. case ThemePageEnum::SECKILL:
  508. if('admin' == $source){
  509. break;
  510. }
  511. $dataType = $content[$moduleKey]['content']['data_type'];
  512. $data = $content[$moduleKey]['content']['data'];
  513. $limit = false;
  514. $where = [];
  515. //自动获取商品
  516. if(1 == $dataType) {
  517. //获取数量
  518. $limit = $content[$moduleKey]['content']['num'];
  519. $orderField = 'SA.id desc';
  520. }else{
  521. if(empty($data)){
  522. break;
  523. }
  524. $goods_ids = array_column($data,'goods_id');
  525. $where = ['goods_id'=>$goods_ids];
  526. $goodsIds = implode(',',array_column($data,'goods_id'));
  527. $orderField = "field(goods_id,$goodsIds)";
  528. }
  529. $goodsList = SeckillActivity::alias('SA')
  530. ->join('seckill_goods SG','SA.id = SG.seckill_id')
  531. ->where([
  532. ['status','=',SeckillEnum::SECKILL_STATUS_CONDUCT],
  533. ['start_time', '<=', time()],
  534. ['end_time', '>=', time()],
  535. ])
  536. ->where($where)
  537. ->field('SA.id as activity_id,SG.id,goods_id,goods_snap,min_seckill_price')
  538. ->limit($limit)
  539. ->orderRaw($orderField)
  540. ->select()
  541. ->toArray();
  542. if(empty($goodsList)){
  543. $content[$moduleKey]['content']['data'] = [];
  544. break;
  545. }
  546. //销量
  547. $activityIds = array_column($goodsList,'activity_id');
  548. $goodsIds = array_column($goodsList,'goods_id');
  549. $salesList = SeckillGoodsItem::where(['seckill_id'=>$activityIds,'goods_id'=>$goodsIds])
  550. ->group('goods_id')
  551. ->column('sum(sales_volume)','goods_id');
  552. $data = [];
  553. foreach ($goodsList as $goods){
  554. $goodsSnap = json_decode($goods['goods_snap'],true);
  555. $data[] = [
  556. 'id' => $goods['id'],
  557. 'activity_id' => $goods['activity_id'],
  558. 'activity_type' => ActivityEnum::SECKILL,
  559. 'goods_id' => $goods['goods_id'],
  560. 'name' => $goodsSnap['name'],
  561. 'image' => FileService::getFileUrl($goodsSnap['image']),
  562. 'sell_price' => $goodsSnap['min_price'],
  563. 'activity_price' => $goods['min_seckill_price'],
  564. 'activity_sales' => $salesList[$goods['goods_id']] ?? 0,
  565. ];
  566. }
  567. $content[$moduleKey]['content']['data'] = $data;
  568. break;
  569. }
  570. }
  571. return $content;
  572. }
  573. /**
  574. * @notes 获取推荐商品
  575. * @param string $orderRaw 推荐商品排序;默认按销量和排序,传空则随机排序
  576. * @return array $type 页面类型
  577. * @return array $goodsIds 排除的商品id
  578. * @author cjhao
  579. * @date 2021/8/23 11:07
  580. */
  581. public static function recommend(string $orderRaw = 'virtual_sales_num+sales_num desc,sort desc',int $type=0,array $goodsIds = []):array
  582. {
  583. $pageType = self::$params['page_type'] ?? $type; //页面类型
  584. $goodsList = [];
  585. $categoryIds = [];
  586. $notGoodsIds = self::$params['goods_id'] ?? $goodsIds;
  587. //如果传空,则使用随机排序
  588. if(empty($orderRaw)){
  589. $orderRaw = 'rand()';
  590. }
  591. //获取推荐商品:
  592. switch ($pageType){
  593. case ThemePageEnum::TYPE_GOODS_DETAIL://商品详情推荐商品
  594. $goodsId = self::$params['goods_id'] ?? '';
  595. $categoryIds = GoodsCategoryIndex::where(['goods_id'=>$goodsId])->column('category_id');
  596. break;
  597. case ThemePageEnum::TYPE_CART: //购物车推荐商品
  598. $userId = self::$params['user_id'] ?? '';
  599. //购物车商品的分类
  600. $cartList = Cart::alias('C')
  601. ->join('goods_category_index GCI','C.goods_id = GCI.goods_id')
  602. ->where(['user_id'=>$userId])
  603. ->column('C.goods_id,category_id');
  604. $notGoodsIds = array_unique(array_column($cartList,'goods_id'));
  605. $categoryIds = array_unique(array_column($cartList,'category_id'));
  606. break;
  607. case ThemePageEnum::TYPE_MEMBER_CENTRE://个人中心推荐商品
  608. $userId = self::$params['user_id'] ?? '';
  609. //订单商品的分类
  610. $orderList = Order::alias('O')
  611. ->join('order_goods OG','O.id = OG.order_id')
  612. ->join('goods_category_index GCI','OG.goods_id = GCI.goods_id')
  613. ->where(['user_id'=>$userId])
  614. ->column('OG.goods_id,category_id');
  615. $notGoodsIds = array_unique(array_column($orderList,'goods_id'));
  616. $categoryIds = array_unique(array_column($orderList,'category_id'));
  617. break;
  618. }
  619. if($categoryIds){
  620. $where[] = ['status','=',GoodsEnum::STATUS_SELL];
  621. $where[] = ['category_id','in',$categoryIds];
  622. if($notGoodsIds){
  623. $where[] = ['G.id','not in',$notGoodsIds];
  624. }
  625. //找到推荐商品
  626. $goodsList = Goods::alias('G')
  627. ->join('goods_category_index GCI','G.id = GCI.goods_id')
  628. ->where($where)
  629. ->field('G.id,name,image,min_price as sell_price,min_lineation_price as lineation_price')
  630. ->group("G.id")
  631. ->orderRaw($orderRaw)
  632. ->limit(9)
  633. ->select()->toarray();
  634. //是否显示划线加
  635. $showPrice = ConfigService::get('goods_set', 'show_price', 1);
  636. if(0 == $showPrice){
  637. foreach ($goodsList as $goodsKey => $goodsVal){
  638. $goodsList[$goodsKey]['lineation_price'] = 0;
  639. }
  640. }
  641. }
  642. return $goodsList;
  643. }
  644. }