php 实现店铺装修8

    /**
     * @title 店铺装修--根据分类获取商品列表
     * @param source 是 int 来源(1--h5、2--app)
     * @param type 是 string 店铺类型--首页进去(other)、我的蜂店进去(me)
     * @param store_id 是 string 店铺id
     * @param type_id 是 int 分类类型(0-全部、1-平台、2-特色)
     * @param sort 否 int 排序字段(1销量由高到低、2价格由低到高、3价格由高到低)--不传默认1
     * @param keywords 否 string 搜索字段(商品名称)
     * @param cate_type 是 string 商品所属分类id--格式:13,20,24,16
     * @param brand_id 否 string 品牌id--格式:3,4,5,6
     * @param page 否 int 页码(不传默认1)
     * @param pagesize 否 int 每页显示条数(不传默认10)
     * @example FlagShipShopDecorate.getGoodsListByCate? 调用参数说明:{"method":"FlagShipShopDecorate.getGoodsListByCate","username":"17721355485","check_code":"123456","type_id":"0","sort":"1","keywords":"","cate_type":"1","brand_id":"","page":"1","pagesize":"10","store_id":"115075399495199"}
     * @return_param_explain 分类入口搜索返回字段说明: select_status:选中状态(1是、2否) img:宫格图片 seascapes:海景图 sell_nums:已卖数量 sku_id:商品属性id sku_no:商品属性编号 title:商品标题 sell_price:零售价 store_id:店铺id  is_zero_goods:是否为0元购(1:0元购,0:不是0元购) restriction:限购数量  yuding:是否可预定  market_price:市场价 cost_price:成本价 profit:利润  spec_name:规格名称 activity_info--- activityId:活动编码 activityType:活动类型 productPrice:活动售价 costPrice:成本价 title:活动标题 isAct:是否是活动(true:是、false:不是) >>>foot: current_page:当前页 pagesize:每页显示条数 total_page:总页数 >>>cate_info: id:分类id name:分类名称 list_status:1选中、2未选中 >>>brand_info: id:品牌id brand_name:品牌名称 list_status:1选中、2未选中
     * @method POST
     * @author 邹柯
     */
    public function getGoodsListByCate($res){
        $userId=session('user.user_id');
        //商品类型
        $type_id=$res['type_id'];
        if(empty($type_id)){
            $type_id=0;
        }
        if($type_id !=0 && $type_id !=1 && $type_id !=2){
            E('700416');
        }
        $store_id=$res['store_id'];
        //品牌id
        $brand_id=$res['brand_id'];
        //搜索--宝贝名称
        $keywords=$res['keywords'];
        //排序字段(1销量由高到低、2价格由低到高、3价格由高到低)
        $sort=$res['sort'];
        if(empty($sort)){
            $sort=1;
        }
        if(!empty($sort)){
            if($sort !=1 && $sort !=2 && $sort !=3){
                E('700410');
            }
        }
        //商品所属分类
        $cate_type=$res['cate_type'];
        if(empty($cate_type)){
            $cate_type=0;
        }
        //页码
        $page = $res['page'];
        if(empty($page)){
            $page=1;
        }

        //每页显示条数
        $pageSize = $res['pagesize'];
        if(empty($pageSize)){
            $pageSize=10;
        }

        $proStoreGoods = new FlagShopCategoryModel();
        $list=$proStoreGoods->getGoodsListByCate($userId,$sort,$cate_type,$brand_id,$page,$pageSize,$keywords,$type_id,$store_id);
        return $list;
    }

返回结果

{
    "status":"0",
    "errorCode":"0",
    "msg":"成功",
    "result":{
        "goods_info":[
            {
                "product_id":"P000873",
                "brand_id":"3",
                "search_name":null,
                "seascapes":[

                ],
                "img":"",
                "sell_nums":"1",
                "title":"【0元购返399元】斐讯K2 1200M智能双频无线路由器 WIFI穿墙 PSG1218",
                "sku_id":"8032715893",
                "soft_text":null,
                "is_zero_goods":"0",
                "restriction":null,
                "yuding":"1",
                "sku_no":"P0028792",
                "market_price":null,
                "sell_price":"0.01",
                "cost_price":"0.01",
                "product_num":"1",
                "profit":"0.00",
                "spec_name":": : ",
                "store_id":"115075399495199",
                "activity_info":{
                    "productPrice":"00.00",
                    "costPrice":"00.00",
                    "activityId":null,
                    "activityType":null,
                    "title":null,
                    "isAct":false
                },
                "select_status":2
            }
        ],
        "foot":{
            "current_page":"1",
            "pagesize":"10",
            "total_page":1
        },
        "brand_info":[
            {
                "id":"3",
                "brand_name":"斐讯",
                "list_status":1
            },
            {
                "id":"6",
                "brand_name":"迅捷",
                "list_status":2
            }
        ],
        "cate_info":[
            {
                "id":"109",
                "name":"斐讯K3C+E1组合套装",
                "list_status":2
            },
            {
                "id":"108",
                "name":"斐讯N1预售",
                "list_status":2
            }
        ]
    }
}
    //根据分类获取商品列表
    public function getGoodsListByCate($userId,$sort,$cate_type,$brand_id,$page,$pageSize,$keywords,$type_id,$store_id){
        $store=M('store');
        $s_where['id']=$store_id;
        $store_user_id=$store->field('id')->where($s_where)->getField('user_id');
        //排序
        switch ($sort) {
            case 1:
                //销量由高到低排序
                $order = 'sell_nums desc';
                break;
            case 2:
                //价格从低到高
                $order = 'sell_price asc';
                break;
            default:
                //价格从高到低
                $order = 'sell_price desc';
                break;
        }
        $origin=1; //1搜索、2店铺首页或商品管理
        $res2=$this->getGoodsInfoNew($keywords,$cate_type,$page,$pageSize,$order,$brand_id,$store_id,'me',2,$origin,$type_id,$userId,$store_user_id);
        if(empty($res2['goods_info'])){
            $res2=null;
        }
        foreach($res2['goods_info'] as $k=>$v){
            $arrs[]=array(
                'productId'=>$v['product_id'],
                'skuNo'=>$v['sku_no']
            );
        }
        $home_special_session=new HomeSpecialSessionModel();
        $res_info=$home_special_session->getGoodsActInfo($arrs);
        foreach($res_info as $k=>$v){
            unset($res_info[$k]['productId']);
            unset($res_info[$k]['skuNo']);
            unset($res_info[$k]['status']);
        }
        foreach($res2['goods_info'] as $k=>$v){
            $res2['goods_info'][$k]['activity_info']=$res_info[$v['sku_no']];
        }
        $search_info= $res2;
        $ob=new FlagShipShopDecorateModel();
        $sku_ids=array_column($res2['goods_info'],'sku_id');
        $tt=$ob->getSelectStatus($userId,$type_id);
        foreach($sku_ids as $k=>$v){
            if(in_array($v,$tt)){
                $atr=1;
            }else{
                $atr=2;
            }
            $search_info['goods_info'][$k]['select_status']=$atr;
        }
        return $search_info;
    }

    //组装商品信息--新(修改商品列表sku显示为商品显示)
    private function getGoodsInfoNew($keywords,$cate_type,$page,$pageSize,$order,$brand_id,$store_id,$store_type,$source,$origin,$type_id,$user_id,$store_user_id){
        $img_base=C('img_base');
        //获取平台商品的ID
        $pingtai_goods=C('pingtai_goods');
        //获取特色商品的ID
        $tese_goods=C('tese_goods');
        $store_goods=M('store_goods sg');

        $ob=new FlagShipShopDecorateModel();
        $res=$ob->isParentUserId($user_id,$store_user_id);
        if($type_id==1){ //平台商品
            $where2 ="gs.goods_type=$pingtai_goods";
        }elseif($type_id==2){//特色商品
            $where2 ="gs.goods_type=$tese_goods";
        }else{
            $where2 ="1=1";
            if($res===false){
                $where2 .=" and goods_type=2";
            }
        }
        
        if(!empty($keywords)){
            $where2 .=" and (gs.title like '%$keywords%' or gd.search_name like '%$keywords%')";
        }
        $where3=$where2;
        //品牌
        if(!empty($brand_id)){            
            $where2 .=" and gd.brand_id in ($brand_id)";
        }
        if(!empty($cate_type)){
            $where ="sgc.id in ($cate_type)";
        }else{
            $where ="1=1";
        }
        
        if($type_id==1){ //平台商品
            $where .=" and type_id=$pingtai_goods";
        }elseif($type_id==2){ //特色商品
            $where .=" and sg.store_id='".$store_id."' and type_id=$tese_goods";
        }else{
            if($res===false){
                $where .=" and (sg.store_id='".$store_id."' and type_id=$tese_goods)";
            }else{
                $where .=" and ((sg.store_id='".$store_id."' and type_id=$tese_goods) or type_id=$pingtai_goods)";
            }
        }
        
        $where .=" and sg.is_deleted=1 and sg.is_open=1 ";
        $store_goods_info=$store_goods->field('sg.sku_id,sg.g_cats,sgc.id')
                ->join('left join lc_store_goods_category sgc on sg.g_cats=sgc.path')
                ->where($where)
                ->select();
        $sku_ids =implode(",",array_column($store_goods_info,'sku_id'));
        $goods_sku_m = M('goods_sku gs');  
        if(!empty($store_goods_info)){   
            //根据sku_id查找对应的商品
            $sku_ids = "'".str_replace(",","','",$sku_ids)."'";
            $where2 .=' and gs.sku_id in ('.$sku_ids.') and gs.status=1 and gs.is_deleted=0 and gd.is_deleted =0 and gd.is_online =1 and gs.is_show=0';
            $where4=$where3;
            $where4 .=' and gs.status=1 and gs.is_deleted=0 and gd.is_deleted =0 and gd.is_online =1 and gs.is_show=0';
            $gd_info = $goods_sku_m
                ->join('left join __GOODS__ gd on gd.product_id=gs.product_id')
                ->field("gd.brand_id,gs.sku_id")
                ->where($where4)
                ->select();
             //选中品牌,品牌分类不变
            $where3 .=' and gs.sku_id in ('.$sku_ids.') and gs.status=1 and gs.is_deleted=0 and gd.is_deleted =0 and gd.is_online =1 and gs.is_show=0';
            $gd_info2 = $goods_sku_m
                 ->join('left join __GOODS__ gd on gd.product_id=gs.product_id')
                 ->field("gd.brand_id,gs.sku_id")
                 ->where($where3)
                 ->select();
            if(!empty($gd_info2)){
                $brand_ids2=array_unique(array_column($gd_info2,'brand_id'));
            }else{
                $brand_ids2=null;
            }
            if(!empty($brand_ids2)){
                $brands=implode(',',$brand_ids2);
            }else{
                $brands=null;
            }
            $brand_info=$this->getBrandInfo($brands,$brand_id);
            if(!empty($gd_info2)){
                $sku_ids2 =implode(",",array_unique(array_column($gd_info,'sku_id')));
                $sku_ids2 = "'".str_replace(",","','",$sku_ids2)."'";
                $where5 ="sg.sku_id in ($sku_ids2) and concat(sg.g_cats,'/') like '".$pingtai_goods."/%' and sg.is_deleted=1 and sg.is_open=1";
                $store_goods_info5=$store_goods->field('sgc.id,sg.g_cats')
                        ->join('left join lc_store_goods_category sgc on sg.g_cats=sgc.path')
                        ->where($where5)
                        ->select();
                $cate_infos=array_unique(array_column($store_goods_info5,'id'));
                if(!empty($cate_infos)){
                    foreach($cate_infos as $k=>$v){
                        if(empty($v)){
                            unset($cate_infos[$k]);
                        }
                    }
                    $cate_infos=array_merge($cate_infos);
                }else{
                    $cate_infos=null;
                }
                if(!empty($cate_infos)){
                    $cate_infos=implode(',',$cate_infos);
                    $cate_info=$this->getCateInfo($cate_type,$cate_infos);
                }
            }else{
                $cate_info=null;
            }


            $count=$goods_sku_m->join('left join __GOODS__ gd on gd.product_id=gs.product_id')->where($where2)->count('DISTINCT gs.product_id');
            //组装分页信息
            $total_page=ceil($count/$pageSize);
            if($page > $total_page){
                $page=1;
            }
            $goods_info = $goods_sku_m
                     ->join('left join __GOODS__ gd on gd.product_id=gs.product_id')
                     ->field("gd.product_id,gd.brand_id,gd.search_name,gd.seascapes,gs.img,sum(gs.sell_nums) as sell_nums,gs.title")
                     ->where($where2)
                     ->group('gs.product_id')
                     ->order($order)
                     ->page($page,$pageSize)
                     ->select();
            $product_ids=implode(",",array_unique(array_column($goods_info,'product_id')));
            $product_ids = "'".str_replace(",","','",$product_ids)."'";
            $sku_where="product_id in ($product_ids) and gs.status=1 and gs.is_deleted=0 and gs.is_show=0";
            $sku_info = $goods_sku_m
                     ->field("gs.product_id,gs.properties,gs.sku_id,gs.soft_text,gs.is_zero_goods,gs.restriction,gs.yuding,gs.img,gs.sku_no,gs.market_price,gs.sell_price,gs.cost_price")
                     ->where($sku_where)
                     ->order('sell_price asc,gs.sell_nums desc')
                     ->select();
            $sku_info=$this->remove_duplicate($sku_info);
            foreach($sku_info as $k=>$v){
                $sku[$v['product_id']]['properties']=$v['properties'];
                $sku[$v['product_id']]['sku_id']=$v['sku_id'];
                $sku[$v['product_id']]['soft_text']=$v['soft_text'];
                $sku[$v['product_id']]['is_zero_goods']=$v['is_zero_goods'];
                $sku[$v['product_id']]['restriction']=$v['restriction'];
                $sku[$v['product_id']]['yuding']=$v['yuding'];
                $sku[$v['product_id']]['img']=$v['img'];
                $sku[$v['product_id']]['sku_no']=$v['sku_no'];
                $sku[$v['product_id']]['market_price']=$v['market_price'];
                $sku[$v['product_id']]['sell_price']=$v['sell_price'];
                $sku[$v['product_id']]['cost_price']=$v['cost_price'];
            }
            foreach($goods_info as $k=>$v){
                $goods_info[$k]['properties']= $sku[$v['product_id']]['properties'];
                $goods_info[$k]['sku_id']= $sku[$v['product_id']]['sku_id'];
                $goods_info[$k]['soft_text']= $sku[$v['product_id']]['soft_text'];
                $goods_info[$k]['is_zero_goods']= $sku[$v['product_id']]['is_zero_goods'];
                $goods_info[$k]['restriction']= $sku[$v['product_id']]['restriction'];
                $goods_info[$k]['yuding']= $sku[$v['product_id']]['yuding'];
                $goods_info[$k]['img']= $sku[$v['product_id']]['img'];
                $goods_info[$k]['sku_no']= $sku[$v['product_id']]['sku_no'];
                $goods_info[$k]['market_price']= $sku[$v['product_id']]['market_price'];
                $goods_info[$k]['sell_price']= $sku[$v['product_id']]['sell_price'];
                $goods_info[$k]['cost_price']= $sku[$v['product_id']]['cost_price'];
            }
            $public = new PublicModel();

            //查询商品是否有多个sku
            if(empty($goods_info)){
                foreach ($goods_info as $key=>$value){
                    $goods_info[$key]['product_num']='';
                }
            }else{
                $product_info=$public->isMoreSku(array_column($goods_info,'product_id'));
                $new_data=[];
                foreach ($product_info as $key=>$value){
                    $new_data[$value['product_id']]=$value['num'];
                }
                foreach ($goods_info as $key=>$value){
                    $goods_info[$key]['product_num']=empty($new_data[$value['product_id']])?'':$new_data[$value['product_id']];
                }
            }
            if(!empty($goods_info)){
                 foreach ($goods_info as $k =>$v) {
                    //利润:
                    $profit=$v['sell_price']-$v['cost_price'];
                    $goods_info[$k]['profit']=$profit?number_format($profit, 2, '.', ','):'0.00';
                    $seascapes=explode(',',$v['seascapes']);
                    if(!empty($seascapes)){
                        $seascapes_t= array();
                        foreach($seascapes as $v1){
                            if(!empty($v1)){
                                $seascapes_t[]=$img_base.$v1;
                            }
                        }
                    }
                    $goods_info[$k]['seascapes']=$seascapes_t;
                    $goods_info[$k]['img']=empty($v['img'])?'':$img_base.$v['img'];
                    //单个产品相关的规格
                    $spec = $public->getSkuPropName('',$v['properties']);
                    $goods_info[$k]['spec_name'] = $spec;
                    
                    //app
                    if($source==2){
                        $inf2=$public->shopDeal($store_id);
                    }else{ //h5
                        $inf2=$public->shopDealH5($store_id,$store_type);
                    }
                    $inf=$public->getTeSeGoods($v['sku_id']);
                    if(empty($inf)){//平台商品
                         $store_id=$inf2;
                    }else{
                         $store_id=$inf['store_id'];
                    }       
                    $goods_info[$k]['store_id']=$store_id;   
                    unset($goods_info[$k]['properties']);
                }
            }               
        }else{
            $goods_info=null;
            if($origin==1){
                $brand_info=null;
                $cate_info=null;
            }
        }   
        $foot=array('current_page'=>$page,'pagesize'=>$pageSize,'total_page'=>$total_page);
        $data['goods_info']=$goods_info;
        $data['foot']=$foot;
        if($origin==1){
            //组装品牌信息
            $data['brand_info']=$brand_info;
            //组装分类信息
            $data['cate_info']=$cate_info;
        }        
        return $data;
    }
   //二维数组去重
   public function remove_duplicate($array){
        $result=array();
        foreach ($array as $key => $value) {
            $has = false;
            foreach($result as $val){
                if($val['product_id']==$value['product_id']){
                $has = true;
                break;
            }
        }
        if(!$has)
            $result[]=$value;
        }
        return $result;
    }
    //获取品牌列表
    //$brand_ids为搜索后产品所对应的品牌 $brand_id为选中的品牌列表
    public function getBrandInfo($brand_ids,$brand_id){
        if(!empty($brand_ids)){
            $where="id in ($brand_ids)";
        }else{
            $where="1=1";
        }
        $brand_info=M('brand')->field('id,brand_name')->where($where)->select();
        foreach($brand_info as $k=>$v){
            if(empty($brand_id)){
                $brand_info[$k]['list_status']=2; //未选中
            }else{
                if(in_array($v['id'],explode(',',$brand_id))){
                    $brand_info[$k]['list_status']=1; //选中
                }else{
                    $brand_info[$k]['list_status']=2; //未选中
                }    
            }      
        }  
        return $brand_info;
    }
    //获取分类列表
    //$cate_type选中的分类  $cate_infos搜索出的分类
    public function getCateInfo($cate_type,$cate_infos){
        //获取平台商品的id
        $pingtai_goods=C('pingtai_goods');
        $where ="id in ($cate_infos)";
        $cate_info=M('store_goods_category')->field('id,name,path')->where($where)->order('sort desc')->select();
        if(!empty($cate_info)){
            foreach($cate_info as $k=>$v){
                if(empty($cate_type)){
                    $cate_info[$k]['list_status']=2; //未选中
                }else{
                    if(in_array($v['id'],explode(',',$cate_type))){
                        $cate_info[$k]['list_status']=1; //选中
                    }else{
                        $cate_info[$k]['list_status']=2; //未选中
                    }
                } 
                unset($cate_info[$k]['path']);
            }
            $cate_info=array_merge($cate_info);
        }else{
            $cate_info=null;
        }        
        return $cate_info;
    }
    //根据sku_ids获取分类选中状态
    public function getSelectStatus($userId,$type_id){
        $user_shop_decorate=M('user_shop_decorate');
        $usd_where['flagship_shop']=1;
        $usd_where['create_id']=$userId;
        if($type_id==1){
            $usd_info=$user_shop_decorate->field('plate_id,plate_content_draft')->where($usd_where)->order('create_time desc')->find();
        }else{
            $usd_info=$user_shop_decorate->field('plate_id_features plate_id,plate_content_draft_features plate_content_draft')->where($usd_where)->order('create_time desc')->find();
        }

        $shop_decorate_template=M('shop_decorate_template');
        $us_where['is_deleted']=0;
        $us_where['id']=$usd_info['plate_id'];
        $us_info=$shop_decorate_template->field('goods_nums')->where($usd_where)->find();
        if(empty($usd_info)){
            $goods_info=$this->getDefaultGoodsInfo($us_info['goods_nums'],"sell_nums desc");
            foreach($goods_info as $k=>$v){
                $arr[]=$v['sku_id'];
            }
        }else{
            $plate_content=json_decode($usd_info['plate_content_draft'],true);
            foreach($plate_content as $k=>$v){
                foreach($v['goods'] as $k2=>$v2){
                    if($v2['operate_type']==1){
                        $arr[]=$v2['sku_id'];
                    }
                }
            }
        }
        return $arr;
    }
原文地址:https://www.cnblogs.com/zouke1220/p/9298604.html