ExtendInfo.php 3.5 KB
<?php
/**
 * @remark :
 * @name   :ExtendInfo.php
 * @author :lyh
 * @method :post
 * @time   :2023/11/9 14:19
 */

namespace App\Models\Product;

use App\Models\Base;

class ExtendInfo extends Base
{
    //设置关联表名
    protected $table = 'gl_product_extend_info';
    //连接数据库
    protected $connection = 'custom_mysql';

    /**
     * 搜索模块-广州万天实业有限公司-项目ID:194,获取产品品牌和型号扩展字段的值
     * key值固定为:pd_extended_field_1:品牌,pd_extended_field_2:型号
     */
    public static function getProductsAllBrandAndModel($projectId): array
    {
        $brandAndModel = [];
        $brandArr = [];
        $productIds = [];
        $productsExtendInfo = self::where("project_id",$projectId)->get();
        if (!empty($productsExtendInfo)){
            $productsExtendBrandInfo = $productsExtendInfo->where("key","pd_extended_field_1");
            if (!empty($productsExtendBrandInfo)){
                foreach ($productsExtendBrandInfo as $productsExtendBrandInfoItem){
                    if (!empty($productsExtendBrandInfoItem->values)){
                        $brand = explode(",",$productsExtendBrandInfoItem->values);
                        $brands = array_filter($brand);
                        if (!empty($brands)){
                            foreach ($brands as $brandsItem){
                                $brandArr[] = $brandsItem;
                            }
                        }
                    }
                }
            }
        }
        //临时数据
        if (!empty($brandArr)){
            $brandArr = array_unique($brandArr);
            $key = 0;
            foreach ($brandArr as $brandArrItem){
                $brandKeys = $productsExtendInfo->where("key","pd_extended_field_1");
                if (!empty($brandKeys)){
                    foreach ($brandKeys as $brandKeysItem){
                        if ($brandKeysItem->values){
                            if (strpos($brandKeysItem->values, $brandArrItem) !== false){
                                $brandAndModel[$key]["ids"][] = $brandKeysItem->product_id;
                                $brandAndModel[$key]["brand"] = $brandArrItem;
                            }
                        }
                    }
                }
                $key++;
            }
        }

        if (!empty($brandAndModel)){
            foreach ($brandAndModel as $keyItem=>$brandAndModelItem){
                if (!empty($brandAndModelItem["ids"])){
                    $productIds = array_unique($brandAndModelItem["ids"]);
                    $modelKeys = $productsExtendInfo->where("key","pd_extended_field_2")->whereIn("product_id",$productIds);
                    $models = [];
                    if (!empty($modelKeys)){
                        foreach ($modelKeys as $modelKeysItem){
                            if (!empty($modelKeysItem->values)){
                                $modelArr = array_filter(explode(",",$modelKeysItem->values));
                                foreach ($modelArr as $modelArrItem){
                                    $models[] = $modelArrItem;
                                }
                            }
                        }
                    }
                    if (!empty($models)){
                        $models = array_unique($models);
                        $brandAndModel[$keyItem]["model"] = $models;
                    }
                }
            }
        }
        return $brandAndModel;
    }
}