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