ThemeService.php 35 KB

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