地区传入多个查询数据

对于数据表地址格式为省市区全部的情况

地区传入多个查询数据

其实省市区分别存入或者分别以id存入
然后用in方法更好

/**
     * @param $params
     * @return mixed 获取名片
     */
    public function cardList($params)
    {
        
        $limit = $params['page_size'] ? : 20;
        $pageNo = $params['page_no'] ? $params['page_no'] : 1;
        $start =  ($pageNo-1) * $limit;



        /*$cards = app::get('syscompany')->model('card_product')->getList('*', ['product_id'=>$params['product_id']]);
        if($cards){
            $cardId = array_column($cards,'card_id');
            $filter = [
                'card_id|in'=>$cardId,
            ];
        }
        if($params['type_id']){
            $filter['type_id'] = $params['type_id'];
        }*/


        ##搜索
        $where = 1;
        if($params['area']){
            if(is_array($params['area'])){
                $params['area'] = $params['area'];
            }else{
                $params['area'] = explode(',',$params['area']);
            }

            if(count($params['area']) > 1){
                foreach ($params['area'] as $key=>$value){
                    if($key == 0){

                        $where .=  " and (area like '%".$value."%'" ;
                    }else{
                        $where .=  " or area like '%".$value."%'" ;
                        if($key == count($params['area'])-1 ) $where .= ')';
                    }
                }
            }else{
                $where .=  " and area like '%".$params['area'][0]."%'" ;
            }
        }


        if(!empty($params['type_id'])) {
            $type_id = intval($params['type_id']);
            $where = $where . " and type_id = $type_id ";
        }

        ##产品
        if($params['product_id']){
            $cards = app::get('syscompany')->model('card_product')->getList('*', ['product_id'=>$params['product_id']]);

            if(empty($cards)){
                $data['list'] = array();
                return $data;
            }
            $select = array_column($cards,'card_id');
            $ids = implode(',',$select);
            $where = $where. " and card_id in ($ids)";
        }


        //$cardData['list'] = app::get('syscompany')->model('card')->getList('*', $filter,$start,$limit);

        $qb = app::get('sysshop')->database()->createQueryBuilder();
        $cardData['list'] = $qb->select("*
            ")
            ->from('syscompany_card')
            ->where("{$where}")
            ->setFirstResult($start)->setMaxResults($limit)
            ->execute()
            ->fetchAll();

        //$cardData['count'] = app::get('syscompany')->model('card')->count($filter);

        if($cardData['list']){
            foreach ($cardData['list'] as $key=>$value){
                $typeInfo = app::get('syscompany')->model('type')->getRow('*', ['type_id'=>$value['type_id']]);
                $cardData['list'][$key]['type_name'] = $typeInfo['type_name'];

                $productInfo = app::get('syscompany')->model('card_product')->getList('*', ['card_id'=>$value['card_id']]);
                if($productInfo){

                    $productId = array_column($productInfo,'product_id');
                    $productName = app::get('syscompany')->model('product')->getList('product_id,product_name', ['product_id|in'=>$productId]);
                    $cardData['list'][$key]['product_name'] = $productName;
                }else{

                    $cardData['list'][$key]['product_name'] = array();
                }
            }
        }


        return $cardData;
    }

猜你喜欢

转载自blog.csdn.net/ahaotata/article/details/88296799
今日推荐