ThemeService.php 37 KB

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