Last active
July 4, 2020 08:25
-
-
Save rubanraj7/fab121c2a600be2d815496277d4d7608 to your computer and use it in GitHub Desktop.
Syncing links to garments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function handle() | |
{ | |
colorgarment::query()->update(['has_links' => 0]); | |
ini_set('max_execution_time', 0); | |
$counter = 0; | |
$links = link::select('name','color','category','retailer')->get(); | |
$colorGarments = colorgarment::all(); | |
$colors = color::select('id','name')->get(); | |
$garments = garment::join('categories','categories.id','=','garments.category_id')->select('garments.id as id','garments.name as name','categories.name as category')->get(); | |
$retailers = $links->pluck('retailer')->unique(); | |
$counter = 0; | |
foreach ($retailers as $retailer) { | |
$links = $links->where('retailer',$retailer); | |
if(file_exists(public_path('links/'.$retailer.'/garment_dictionary.json'))){ | |
$jsonString = file_get_contents(base_path('public/links/'.$retailer.'/garment_dictionary.json')); | |
$garmentData = json_decode($jsonString, true); | |
} | |
if(file_exists(public_path('links/'.$retailer.'/category_dictionary.json'))){ | |
$jsonString = file_get_contents(base_path('public/links/'.$retailer.'/category_dictionary.json')); | |
$categoryData = json_decode($jsonString, true); | |
}; | |
if(file_exists(public_path('links/'.$retailer.'/colors_dictionary.json'))){ | |
$jsonString = file_get_contents(base_path('public/links/'.$retailer.'/colors_dictionary.json')); | |
$colorsData = json_decode($jsonString, true); | |
}; | |
foreach ($colorGarments as $item) { | |
$garment = $garments->where('id',$item->garment_id)->first(); | |
$garmentName = $garment->name; | |
$color = $colors->where('id',$item->color_id)->first(); | |
$colorName = $color->name; | |
$category = $garment->category; | |
try { | |
$linkCategory = $categoryData[$garmentName]; | |
$garmentTypes = $garmentData[$garmentName]; | |
$colors2 = $colorsData[$colorName]; | |
$links_filtered = $links->filter(function ($value, $key) use ($colors2) { | |
return in_array($value->color, $colors2); | |
}); | |
foreach ($garmentTypes as $garmentType ){ | |
// $link = $links->where('color',$color)->filter(function ($value, $key) use ($garmentType, $linkCategory) { | |
// return (strpos($value->name, $garmentType) !== false && strpos($value->category, $linkCategory) !== false); | |
// })->first(); | |
foreach ($links_filtered as $lf) { | |
if (strpos($lf->name, $garmentType) !== false && strpos($lf->category, $linkCategory) !== false) { | |
$item->has_links = true; | |
$item->save(); | |
break 2; | |
} | |
} | |
} | |
} catch (\Exception $e) { | |
logger('fail'); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment