| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769 |
- <?php
- // +----------------------------------------------------------------------
- // | likeshop100%开源免费商用商城系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 开源版本可自由商用,可去除界面版权logo
- // | 商业版本务必购买商业授权,以免引起法律纠纷
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | gitee下载:https://gitee.com/likeshop_gitee
- // | github下载:https://github.com/likeshop-github
- // | 访问官网:https://www.likeshop.cn
- // | 访问社区:https://home.likeshop.cn
- // | 访问手册:http://doc.likeshop.cn
- // | 微信公众号:likeshop技术社区
- // | likeshop团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeshopTeam
- // +----------------------------------------------------------------------
- namespace app\common\service;
- use app\common\{enum\ActivityEnum,
- enum\GoodsEnum,
- enum\LiveEnum,
- enum\PresellEnum,
- enum\SeckillEnum,
- enum\TeamEnum,
- model\Cart,
- model\CouponList,
- model\Goods,
- model\GoodsItem,
- model\Order,
- model\Coupon,
- enum\CouponEnum,
- model\Presell,
- model\PresellGoods,
- model\PresellGoodsItem,
- model\SeckillActivity,
- model\SeckillGoodsItem,
- model\ShopNotice,
- enum\ThemePageEnum,
- model\GoodsCategoryIndex,
- model\TeamActivity,
- model\TeamGoodsItem};
- use function JmesPath\search;
- /**
- * 主题功能类
- * Class ThemeService
- * @package app\common\service
- */
- class ThemeService
- {
- public static array $params = [];
- /**
- * @notes 替换组件内容
- * @param array $content
- * @return array
- * @author cjhao
- * @date 2021/8/19 18:52
- */
- public static function getModuleData(array $content,array $config = []):array
- {
- self::$params = $config;
- $isDistribution = self::$params['is_distribution'] ?? false; //是否分销
- $isVerifier = self::$params['is_verifier'] ?? false; //是否核销员
- $source = self::$params['source'] ?? 'shop'; //后台组件替换或商城组件替换
- $userId = self::$params['user_id'] ?? '';
- $moduleList = array_column($content,'name');
- foreach ($moduleList as $moduleKey => $moduleName) {
- //需要拼接数据的组件
- switch ($moduleName) {
- //商品组件
- case ThemePageEnum::GOODS:
- $goods_type = $content[$moduleKey]['content']['goods_type'] ?? 2;
- //商品分类
- $limit = false;
- if(2 == $goods_type){
- if('admin' == $source){
- $content[$moduleKey]['content']['data'] = [];
- break;
- }
- $categoryId = $content[$moduleKey]['content']['category']['id'] ?? 0;
- $limit = $content[$moduleKey]['content']['category']['num'] ?? false;
- $goodsIds = GoodsCategoryIndex::where(['category_id'=>$categoryId])->column('goods_id');
- }else{
- $goodsIds = array_column($content[$moduleKey]['content']['data'], 'id');
- }
- //如果id都是空,直接返回数组
- if (empty($goodsIds)) {
- $content[$moduleKey]['content']['data'] = [];
- break;
- // $goods_info = Goods::where(['status'=>1])->field('id')->select()->toArray();
- // $goods_id_arr = array_column($goods_info,'id');
- // $goodsIds = array_rand($goods_id_arr,1);
- }
- //todo 商品需要根据顺序排序
- $orderField = implode(',', $goodsIds);
- $goodsList = Goods::where(['id' => $goodsIds,'status'=>GoodsEnum::STATUS_SELL])
- ->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')
- ->orderRaw("field(id,$orderField)")
- ->limit($limit)
- ->order('sort desc')
- ->select()
- ->toArray();
- foreach($goodsList as &$gv){
- $goods_item_count = GoodsItem::where(['goods_id'=>$gv['id']])->count();
- if($goods_item_count > 1){
- $gv['is_multi_gauge'] = 1;
- }else{
- $gv['is_multi_gauge'] = 0;
- }
- }
- //是否显示划线加
- $showPrice = ConfigService::get('goods_set', 'show_price', 1);
- if(0 == $showPrice){
- foreach ($goodsList as $goodsKey => $goodsVal){
- $goodsList[$goodsKey]['lineation_price'] = 0;
- }
- }
- dump($goodsList);die;
- $content[$moduleKey]['content']['data'] = $goodsList;
- break;
- //选项卡组件 todo 选项卡的data是多维数据
- case ThemePageEnum::TABS:
- $dataList = $content[$moduleKey]['content']['data'];
- foreach ($dataList as $dataKey => $dataVal){
- $goods_type = $dataVal['goods_type'] ?? 1;
- $limit = false;
- if(2 == $goods_type){
- if('admin' == $source){
- $content[$moduleKey]['content']['data'][$dataKey]['data'] = [];
- break;
- }
- $categoryId = $dataVal['category']['id'] ?? 0;
- $limit = $dataVal['category']['num'] ?? false;
- $goodsIds = GoodsCategoryIndex::where(['category_id'=>$categoryId])->column('goods_id');
- }else{
- $goodsIds = array_column($dataVal['data'], 'id');
- }
- //如果id都是空,直接返回数组
- if (empty($goodsIds)) {
- $content[$moduleKey]['content']['data'][$dataKey]['data'] = [];
- break;
- // $goods_info = Goods::where(['status'=>1])->field('id')->select()->toArray();
- // $goods_id_arr = array_column($goods_info,'id');
- // $goodsIds = array_rand($goods_id_arr,1);
- }
- //todo 商品需要根据顺序排序
- $orderField = implode(',', $goodsIds);
- $goodsList = Goods::where(['id' => $goodsIds,'status'=>GoodsEnum::STATUS_SELL])
- ->field('id,name,image,virtual_sales_num+sales_num as sales_num,min_price as sell_price,min_lineation_price as lineation_price')
- ->orderRaw("field(id,$orderField)")
- ->limit($limit)
- ->select()
- ->toArray();
- //是否显示划线加
- $showPrice = ConfigService::get('goods_set', 'show_price', 1);
- if(0 == $showPrice){
- foreach ($goodsList as $goodsKey => $goodsVal){
- $goodsList[$goodsKey]['lineation_price'] = 0;
- }
- }
- $content[$moduleKey]['content']['data'][$dataKey]['data'] = $goodsList;
- }
- break;
- //优惠券组件
- case ThemePageEnum::COUPON:
- $couponIds = array_column($content[$moduleKey]['content']['data'], 'id');
- //如果id都是空,直接返回数组
- if (empty($couponIds)) {
- $content[$moduleKey]['content']['data'] = [];
- break;
- }
- //todo 优惠券需要根据顺序排序
- $orderField = implode(',', $couponIds);
- $couponList = Coupon::where(['id' => $couponIds,'status'=>[CouponEnum::COUPON_STATUS_NOT,CouponEnum::COUPON_STATUS_CONDUCT]])
- ->append([ 'use_type', 'discount_content', 'status_desc', 'condition' ])
- ->field('id,name,money,discount_ratio,condition_type,get_num_type,condition_money,use_goods_type,get_num,status')
- ->orderRaw("field(id,$orderField)")
- ->select();
- $myCouponIds = [];
- if($couponList && $userId){
- $myCouponIds = CouponList::where(['user_id'=>$userId,'status'=>CouponEnum::USE_STATUS_NOT])
- ->column('coupon_id');
- }
- $list = [];
- foreach ($couponList as $coupon) {
- //是否已领取
- $isReceive = 0;
- if(in_array($coupon->id,$myCouponIds)){
- $isReceive = 1;
- }
- $isAvailable = 0;
- //是否可领取
- switch ($coupon->get_num_type) {
- case CouponEnum::GET_NUM_TYPE_LIMIT:
- $total = CouponList::where(['coupon_id' => $coupon->id])
- ->where(['user_id' => $userId])
- ->count();
- $isAvailable = $total >= $coupon->get_num ? 1 : 0;
- break;
- case CouponEnum::GET_NUM_TYPE_DAY:
- $total = CouponList::where(['coupon_id' => $coupon->id])
- ->where(['user_id' =>$userId])
- ->where('create_time', '>=', TimeService::today()[0])
- ->where('create_time', '<=', TimeService::today()[1])
- ->count();
- $isAvailable = $total >= $coupon->get_num ? 1 : 0;
- break;
- }
-
- $coupon->is_receive = $isReceive;
- $coupon->is_available = $isAvailable;
- $list[] = $coupon->toArray();
- }
- $content[$moduleKey]['content']['data'] = $list;
- break;
- //公告组件
- case ThemePageEnum::NOTICE:
- $limit = $content[$moduleKey]['content']['num'] ?? 1;//默认拿一条
- $noticeList = ShopNotice::field('id,name')
- ->where(['status'=>1])
- ->limit($limit)
- ->order('sort asc,id desc')
- ->select()
- ->toArray();
- $content[$moduleKey]['content']['data'] = $noticeList;
- break;
- //会员中心-我的服务组件、微页面-导航组件
- case ThemePageEnum::USERSERVE:
- case ThemePageEnum::NAVIGATION:
- if('admin' == $source){
- break;
- }
- $linksData = $content[$moduleKey]['content']['data'];
- foreach ($linksData as $linkKey => $link){
- // 非分销用户,屏蔽分销菜单,index == 17时分销菜单,
- if(false === $isDistribution && isset($link['link']['index']) && 17 == $link['link']['index']){
- unset($linksData[$linkKey]);
- }
- //非核销用户,屏蔽分销菜单
- if(false === $isVerifier && isset($link['link']['index']) && 19 == $link['link']['index']){
- unset($linksData[$linkKey]);
- }
- }
- $content[$moduleKey]['content']['data'] = array_values($linksData);
- break;
- //商品拼团
- case ThemePageEnum::SPELLGROUP:
- if('admin' == $source){
- break;
- }
- $dataType = $content[$moduleKey]['content']['data_type'];
- $data = $content[$moduleKey]['content']['data'];
- $limit = false;
- $where = [];
- if(1 == $dataType) {
- //获取数量
- $limit = $content[$moduleKey]['content']['num'];
- $orderField = 'TA.id desc';
- }else{
- if(empty($data)){
- break;
- }
- $goods_ids = array_column($data,'goods_id');
- $where = ['goods_id'=>$goods_ids];
- $goodsIds = implode(',',array_column($data,'goods_id'));
- $orderField = "field(goods_id,$goodsIds)";
- }
- $goodsList = TeamActivity::alias('TA')
- ->join('team_goods TG','TA.id = TG.team_id')
- ->join('goods G','TG.goods_id = G.id')
- ->where([
- ['TA.status','=',TeamEnum::TEAM_STATUS_CONDUCT],
- ['start_time', '<=', time()],
- ['end_time', '>=', time()],
- ])
- ->where($where)
- ->field('TA.id as activity_id,TG.id,goods_id,goods_snap,min_team_price,people_num,G.image')
- ->limit($limit)
- ->orderRaw($orderField)
- ->select()
- ->toArray();
- if(empty($goodsList)){
- $content[$moduleKey]['content']['data'] = [];
- break;
- }
- $activityIds = array_column($goodsList,'activity_id');
- $goodsIds = array_column($goodsList,'goods_id');
- $salesList = TeamGoodsItem::where(['team_id'=>$activityIds,'goods_id'=>$goodsIds])
- ->group('goods_id')
- ->column('sum(sales_volume)','goods_id');
- $data = [];
- foreach ($goodsList as $goods){
- $goods_snap = json_decode($goods['goods_snap'],true);
- $data[] = [
- 'id' => $goods['id'],
- 'activity_id' => $goods['activity_id'],
- 'activity_type' => ActivityEnum::TEAM,
- 'goods_id' => $goods['goods_id'],
- 'name' => $goods_snap['name'],
- 'image' => FileService::getFileUrl($goods['image']),
- 'people_num' => $goods['people_num'],
- 'sell_price' => $goods_snap['min_price'],
- 'activity_price' => $goods['min_team_price'],
- 'activity_sales' => $salesList[$goods['goods_id']] ?? 0,
- ];
- }
- $content[$moduleKey]['content']['data'] = $data;
- break;
- //商品秒杀
- case ThemePageEnum::SECKILL:
- if('admin' == $source){
- break;
- }
- $dataType = $content[$moduleKey]['content']['data_type'];
- $data = $content[$moduleKey]['content']['data'];
- $limit = false;
- $where = [];
- //自动获取商品
- if(1 == $dataType) {
- //获取数量
- $limit = $content[$moduleKey]['content']['num'];
- $orderField = 'SA.id desc';
- }else{
- if(empty($data)){
- break;
- }
- $goods_ids = array_column($data,'goods_id');
- $where = ['goods_id'=>$goods_ids];
- $goodsIds = implode(',',array_column($data,'goods_id'));
- $orderField = "field(goods_id,$goodsIds)";
- }
- $goodsList = SeckillActivity::alias('SA')
- ->join('seckill_goods SG','SA.id = SG.seckill_id')
- ->join('goods G','SG.goods_id = G.id')
- ->where([
- ['SA.status','=',SeckillEnum::SECKILL_STATUS_CONDUCT],
- ['start_time', '<=', time()],
- ['end_time', '>=', time()],
- ])
- ->where($where)
- ->field('SA.id as activity_id,SG.id,goods_id,goods_snap,min_seckill_price,G.image')
- ->limit($limit)
- ->orderRaw($orderField)
- ->select()
- ->toArray();
- if(empty($goodsList)){
- $content[$moduleKey]['content']['data'] = [];
- break;
- }
- //销量
- $activityIds = array_column($goodsList,'activity_id');
- $goodsIds = array_column($goodsList,'goods_id');
- $salesList = SeckillGoodsItem::where(['seckill_id'=>$activityIds,'goods_id'=>$goodsIds])
- ->group('goods_id')
- ->column('sum(sales_volume)','goods_id');
- $data = [];
- foreach ($goodsList as $goods){
- $goodsSnap = json_decode($goods['goods_snap'],true);
- $data[] = [
- 'id' => $goods['id'],
- 'activity_id' => $goods['activity_id'],
- 'activity_type' => ActivityEnum::SECKILL,
- 'goods_id' => $goods['goods_id'],
- 'name' => $goodsSnap['name'],
- 'image' => FileService::getFileUrl($goods['image']),
- 'sell_price' => $goodsSnap['min_price'],
- 'activity_price' => $goods['min_seckill_price'],
- 'activity_sales' => $salesList[$goods['goods_id']] ?? 0,
- ];
- }
- $content[$moduleKey]['content']['data'] = $data;
- break;
-
- // 预售
- case ThemePageEnum::PRESELL:
- if('admin' == $source){
- break;
- }
- $dataType = $content[$moduleKey]['content']['data_type'];
- $data = $content[$moduleKey]['content']['data'];
- $limit = false;
- $where = [];
- //自动获取商品
- if(1 == $dataType) {
- //获取数量
- $limit = $content[$moduleKey]['content']['num'];
- $orderField = 'pg.id desc';
-
- }else{
- if(empty($data)){
- break;
- }
- $goods_ids = array_column($data,'goods_id');
- $where = ['pg.goods_id'=>$goods_ids];
- $goodsIds = implode(',',array_column($data,'goods_id'));
- $orderField = "field(goods_id,$goodsIds)";
- }
-
- $lists = PresellGoods::alias('pg')
- ->join('presell p', 'p.id=pg.presell_id')
- ->where('p.status', PresellEnum::STATUS_START)
- ->where('p.start_time', '<=', time())
- ->where('p.end_time', '>=', time())
- ->where($where)
- ->limit($limit)
- ->orderRaw($orderField)
- ->field([
- 'pg.id',
- 'p.id as presell_id', 'p.name', 'p.type',
- 'p.start_time', 'p.end_time', 'p.remark',
- 'p.send_type', 'p.send_type_day', 'p.buy_limit', 'p.buy_limit_num',
- 'p.status',
- 'pg.goods_id',
- 'pg.content',
- 'pg.min_price',
- 'pg.virtual_sale',
- ])
- ->with([
- 'items' => function ($query) {
- $query->field([ 'presell_goods_id', 'sale_nums' ]);
- }
- ])
- ->select()
- ->toArray();
- $data = [];
-
- foreach ($lists as $presell) {
- $data[] = [
- 'id' => $presell['goods_id'],
- 'activity_id' => $presell['presell_id'],
- 'activity_type' => ActivityEnum::PRESELL,
- 'goods_id' => $presell['goods_id'],
- 'name' => $presell['content']['name'],
- 'image' => FileService::getFileUrl($presell['content']['image']),
- 'sell_price' => $presell['content']['min_price'],
- 'activity_price' => $presell['min_price'],
- 'activity_sales' => array_sum(array_column($presell['items'], 'sale_nums')) + ($presell['virtual_sale']),
- ];
- }
-
- $content[$moduleKey]['content']['data'] = $data;
-
- break;
- //商品推荐
- case ThemePageEnum::GOODSRECOM:
- $show = $content[$moduleKey]['show'];
- if('admin' == $source || 0 == $show){
- break;
- }
- $content[$moduleKey]['content']['data'] = self::recommend();
- break;
- //小程序直播
- case ThemePageEnum::MNPLIVE:
- if('admin' == $source){
- break;
- }
- $num = $content[$moduleKey]['content']['num'] ?? 1;
- $result = WeChatService::getLiveRoom(0,$num);
- if (!is_array($result)) {
- break;
- }
- $data = [];
- foreach ($result['room_info'] as $item) {
- $data[] = [
- 'name' => $item['name'],
- 'room_id' => $item['roomid'],
- 'cover_img' => $item['cover_img'],
- 'anchor_name' => $item['anchor_name'],
- 'status' => $item['live_status'],
- 'live_status' => LiveEnum::getLiveStatus($item['live_status']),
- 'goods' => count($item['goods']),
- 'start_time' => date('Y-m-d H:i:s', $item['start_time']),
- 'end_time' => date('Y-m-d H:i:s', $item['end_time'])
- ];
- }
- $content[$moduleKey]['content']['data'] = $data;
- break;
- }
- }
- return $content;
- }
- public static function getPCModuleData(array $content,array $config = []):array
- {
- self::$params = $config;
- $isDistribution = self::$params['is_distribution'] ?? false; //是否分销
- $isVerifier = self::$params['is_verifier'] ?? false; //是否核销员
- $source = self::$params['source'] ?? 'shop'; //后台组件替换或商城组件替换
- $userId = self::$params['user_id'] ?? '';
- $moduleList = array_column($content,'name');
- foreach ($moduleList as $moduleKey => $moduleName){
- //需要拼接数据的组件
- switch ($moduleName) {
- case ThemePageEnum::GOODS:
- $goods_type = $content[$moduleKey]['content']['goods_type'] ?? 2;
- //商品分类
- $limit = false;
- if(2 == $goods_type){
- if('admin' == $source){
- $content[$moduleKey]['content']['data'] = [];
- break;
- }
- $categoryId = $content[$moduleKey]['content']['category']['id'] ?? 0;
- $limit = $content[$moduleKey]['content']['category']['num'] ?? false;
- $goodsIds = GoodsCategoryIndex::where(['category_id'=>$categoryId])->column('goods_id');
- }else{
- $goodsIds = array_column($content[$moduleKey]['content']['data'], 'id');
- }
- //如果id都是空,直接返回数组
- if (empty($goodsIds)) {
- $content[$moduleKey]['content']['data'] = [];
- break;
- }
- //todo 商品需要根据顺序排序
- $orderField = implode(',', $goodsIds);
- $goodsList = Goods::where(['id' => $goodsIds,'status'=>GoodsEnum::STATUS_SELL])
- ->field('id,name,image,virtual_sales_num+sales_num as sales_num,min_price as sell_price,min_lineation_price as lineation_price')
- ->orderRaw("field(id,$orderField)")
- ->limit($limit)
- ->select()
- ->toArray();
- //是否显示划线加
- $showPrice = ConfigService::get('goods_set', 'show_price', 1);
- if(0 == $showPrice){
- foreach ($goodsList as $goodsKey => $goodsVal){
- $goodsList[$goodsKey]['lineation_price'] = 0;
- }
- }
- $content[$moduleKey]['content']['data'] = $goodsList;
- break;
- case ThemePageEnum::SECKILL:
- if('admin' == $source){
- break;
- }
- $dataType = $content[$moduleKey]['content']['data_type'];
- $data = $content[$moduleKey]['content']['data'];
- $limit = false;
- $where = [];
- //自动获取商品
- if(1 == $dataType) {
- //获取数量
- $limit = $content[$moduleKey]['content']['num'];
- $orderField = 'SA.id desc';
- }else{
- if(empty($data)){
- break;
- }
- $goods_ids = array_column($data,'goods_id');
- $where = ['goods_id'=>$goods_ids];
- $goodsIds = implode(',',array_column($data,'goods_id'));
- $orderField = "field(goods_id,$goodsIds)";
- }
- $goodsList = SeckillActivity::alias('SA')
- ->join('seckill_goods SG','SA.id = SG.seckill_id')
- ->where([
- ['status','=',SeckillEnum::SECKILL_STATUS_CONDUCT],
- ['start_time', '<=', time()],
- ['end_time', '>=', time()],
- ])
- ->where($where)
- ->field('SA.id as activity_id,SG.id,goods_id,goods_snap,min_seckill_price')
- ->limit($limit)
- ->orderRaw($orderField)
- ->select()
- ->toArray();
- if(empty($goodsList)){
- $content[$moduleKey]['content']['data'] = [];
- break;
- }
- //销量
- $activityIds = array_column($goodsList,'activity_id');
- $goodsIds = array_column($goodsList,'goods_id');
- $salesList = SeckillGoodsItem::where(['seckill_id'=>$activityIds,'goods_id'=>$goodsIds])
- ->group('goods_id')
- ->column('sum(sales_volume)','goods_id');
- $data = [];
- foreach ($goodsList as $goods){
- $goodsSnap = json_decode($goods['goods_snap'],true);
- $data[] = [
- 'id' => $goods['id'],
- 'activity_id' => $goods['activity_id'],
- 'activity_type' => ActivityEnum::SECKILL,
- 'goods_id' => $goods['goods_id'],
- 'name' => $goodsSnap['name'],
- 'image' => FileService::getFileUrl($goodsSnap['image']),
- 'sell_price' => $goodsSnap['min_price'],
- 'activity_price' => $goods['min_seckill_price'],
- 'activity_sales' => $salesList[$goods['goods_id']] ?? 0,
- ];
- }
- $content[$moduleKey]['content']['data'] = $data;
- break;
- }
- }
- return $content;
- }
- /**
- * @notes 获取推荐商品
- * @param string $orderRaw 推荐商品排序;默认按销量和排序,传空则随机排序
- * @return array $type 页面类型
- * @return array $goodsIds 排除的商品id
- * @author cjhao
- * @date 2021/8/23 11:07
- */
- public static function recommend(string $orderRaw = 'virtual_sales_num+sales_num desc,sort desc',int $type=0,array $goodsIds = []):array
- {
- $pageType = self::$params['page_type'] ?? $type; //页面类型
- $goodsList = [];
- $categoryIds = [];
- $notGoodsIds = self::$params['goods_id'] ?? $goodsIds;
- //如果传空,则使用随机排序
- if(empty($orderRaw)){
- $orderRaw = 'rand()';
- }
- //获取推荐商品:
- switch ($pageType){
- case ThemePageEnum::TYPE_GOODS_DETAIL://商品详情推荐商品
- $goodsId = self::$params['goods_id'] ?? '';
- $categoryIds = GoodsCategoryIndex::where(['goods_id'=>$goodsId])->column('category_id');
- break;
- case ThemePageEnum::TYPE_CART: //购物车推荐商品
- $userId = self::$params['user_id'] ?? '';
- //购物车商品的分类
- $cartList = Cart::alias('C')
- ->join('goods_category_index GCI','C.goods_id = GCI.goods_id')
- ->where(['user_id'=>$userId])
- ->column('C.goods_id,category_id');
- $notGoodsIds = array_unique(array_column($cartList,'goods_id'));
- $categoryIds = array_unique(array_column($cartList,'category_id'));
- if(empty($notGoodsIds) || empty($categoryIds)){
- $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();
- $key_Arr = array_keys($goods_info);
- $gc_key = array_rand($key_Arr,1);
- $goods_card_info = $goods_info[$gc_key];
- $notGoodsIds[] = $goods_card_info['id'];
- if((count($goods_card_info['category_id'])>1)){
- $key_Arr1 = array_keys($goods_card_info['category_id']);
- $gc_key1 = array_rand($key_Arr1,1);
- $categoryIds[]=$goods_card_info['category_id'][$gc_key1];
- }else{
- $categoryIds[]=$goods_card_info['category_id'][0];
- }
- }
- break;
- case ThemePageEnum::TYPE_MEMBER_CENTRE://个人中心推荐商品
- $userId = self::$params['user_id'] ?? '';
- //订单商品的分类
- $orderList = Order::alias('O')
- ->join('order_goods OG','O.id = OG.order_id')
- ->join('goods_category_index GCI','OG.goods_id = GCI.goods_id')
- ->where(['user_id'=>$userId])
- ->column('OG.goods_id,category_id');
- $notGoodsIds = array_unique(array_column($orderList,'goods_id'));
- $categoryIds = array_unique(array_column($orderList,'category_id'));
- break;
- }
- if($categoryIds){
- $where[] = ['status','=',GoodsEnum::STATUS_SELL];
- $where[] = ['category_id','in',$categoryIds];
- if($notGoodsIds){
- $where[] = ['G.id','not in',$notGoodsIds];
- }
- //找到推荐商品
- $goodsList = Goods::alias('G')
- ->join('goods_category_index GCI','G.id = GCI.goods_id')
- ->where($where)
- ->field('G.id,name,image,min_price as sell_price,min_lineation_price as lineation_price')
- ->group("G.id")
- ->orderRaw($orderRaw)
- ->limit(9)
- ->select()->toarray();
- //是否显示划线加
- $showPrice = ConfigService::get('goods_set', 'show_price', 1);
- if(0 == $showPrice){
- foreach ($goodsList as $goodsKey => $goodsVal){
- $goodsList[$goodsKey]['lineation_price'] = 0;
- }
- }
- }
- if(empty($goodsList)){
- $goodsList = [];
- $goodsLists = Goods::alias('G')
- ->join('goods_category_index GCI','G.id = GCI.goods_id')
- ->where('G.status = 1')
- ->field('G.id,name,image,min_price as sell_price,min_lineation_price as lineation_price')
- ->group("G.id")
- ->orderRaw($orderRaw)
- ->limit(20)
- ->select()->toarray();
- $key_Arr = array_keys($goodsLists);
- if(count($goodsLists)>4){
- $gc_key = array_rand($key_Arr,4);
- }else{
- $gc_key = $key_Arr;
- }
- foreach ($goodsLists as $k=>$v){
- foreach($gc_key as $gk=>$gv){
- if($k==$gk){
- $goodsList[]=$v;
- }
- }
- }
- }
- return $goodsList;
- }
- }
|