= 2; $page--) {
$pageData = GongApiRequest::get_goods_list($page, 100);
if ($pageData && !empty($pageData['list'])) {
$allData = array_merge($allData, $pageData['list']); // 数据在list键下
}
}
}
// 3、获取 微唯宝 商品表 SPUId集合
$goods_id_list = [];
$spu_id_list = Db::name('douhuomall')->column('spu_id');
// 4、
// 同步到数据库
foreach ($allData as $key => $value) {
sleep(1);
// if($key != 0) continue;
// 4.1 获取商品详情
$goodsInfo = GongApiRequest::get_goods_info($value['goods_id']);
if (!$goodsInfo) continue;
// 4.2 图片存放路径
$targetDir = public_path() . 'images/gong/' . $value['goods_id'] . '/';
// 4.3 清空路径下的文件
$this->deleteFilesInDirectory($targetDir);
// 4.4 获取图片地址 spuId_info商品详细信息处理
$goodsInfo['cover_url'] = $this->seveImg($value['goods_id'], $goodsInfo['main_img'], $targetDir);
$goodsInfo['spu_id'] = $goodsInfo['goods_id'];
$goodsInfo['detail'] = "";
$detail_img = $this->seveImg($value['goods_id'], $goodsInfo['detail_img'], $targetDir);
foreach ($detail_img as $img_url) {
$img_url = "
";
$goodsInfo['detail'] = $goodsInfo['detail'] . $img_url;
}
//sku信息处理
$skuInfo = $goodsInfo['sku_list'];
foreach ($skuInfo as $k => $v) {
if (empty($v['plat_price'])) {
continue;
}
$skuInfo[$k]['img_url'] = $this->seveImg($value['goods_id'], $v['main_img'], $targetDir);//规格图片
$skuInfo[$k]['cost_price'] = bcadd($v['plat_price'], bcmul($v['plat_price'], 0.03, 2), 2);//成本价格 原来进价的基础上加百分之3
$skuInfo[$k]['market_price'] = bcadd($skuInfo[$k]['cost_price'], bcmul($skuInfo[$k]['cost_price'], 0.2, 2), 2);//销售价格 售价在成本价格的基础上加百分20
$skuInfo[$k]['profit'] = bcdiv($skuInfo[$k]['market_price'], $skuInfo[$k]['cost_price'], 2);
$skuInfo[$k]['mer_profit'] = bcsub($skuInfo[$k]['market_price'], $skuInfo[$k]['cost_price'], 2);
$skuInfo[$k]['yanglaojin'] = '0.00';
if (count($v['attr'])) {
$skuInfo[$k]['attribute_json'] = $v['attr'];
} else {
$skuInfo[$k]['attribute_json'] = [['val' => '默认', 'name' => '默认']];
}
}
unset($goodsInfo['sku_list']);
$seveData = [
'spu_id' => $goodsInfo['spu_id'],
'skuId_info' => $skuInfo,
'sys_id' => '112',
'goods_id' => $goodsInfo['goods_id'],
'spuId_info' => $goodsInfo,
'sku_id' => $skuInfo[0]['sku_id'] ? $skuInfo[0]['sku_id'] : 0,
'status' => $goodsInfo['status'],
'cate_ids' => $goodsInfo['cate_ids'],
'title' => $goodsInfo['spu_name'],
'cxb_cate_id' => 0,
];
// 4.5 保存商品信心到本地
$this->add($seveData);
$goods_id_list[] = $goodsInfo['spu_id'];
// 4.6 如果上架到商品表 则更新商品表的图片
$this->updata_product_img($goodsInfo['spu_id']);
}
// 5、将 未同步到的商品 进行下架
sort($spu_id_list);
sort($goods_id_list);
$goodsIds = array_diff($spu_id_list, $goods_id_list);
if (!empty($goodsIds)) {
Db::name('log')->insert(['data' => '供应链拉取数据下架商品记录:' . json_encode(['spu_id_list' => $spu_id_list, 'goods_id_list' => $goods_id_list, 'goodsIds' => $goodsIds])]);
$this->remove($goodsIds);
}
return 'sussess';
} catch (Exception $e) {
Log::error('供应链拉取数据:' . $e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage() . '】');
echo '供应链拉取数据:' . $e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage() . '】';
Db::name('log')->insert(['data' => '供应链拉取数据:' . $e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage() . '】']);
return 'error';
}
}
public function updata_product_img($spu_id)
{
// 1、验证该 SPUId 是否上架了 如果上架了 需要去更新商品 图片
$product_id = Db::name('store_product')->where('spu_id', $spu_id)->value('product_id');
if (!$product_id) return true;
// 2、查找微唯宝 商品
$data = Db::name('douhuomall')->where('spu_id', $spu_id)->find();
// 3、获取 商品主图
$spuId_info = json_decode($data['spuId_info'], true);
$slider_image = $spuId_info['detail_img_list'] ?? '';
$image = $spuId_info['cover_url'] ?? '';
try {
$slider_image = implode(',', json_decode($slider_image, true));
} catch (\Exception $exception) {
$slider_image = $image;
}
//更新产品图片数据
$product_insert_data = [
'image' => $image,
'slider_image' => $slider_image,
];
Db::name('store_product')->where('product_id', $product_id)->update($product_insert_data);
// echo '更新产品图片数据成功' . json_encode($product_insert_data);
$detail = $spuId_info['detail'];
if (!is_null(json_decode($detail))) {
$detail_list = json_decode($detail);
$detail = '';
foreach ($detail_list as $value) {
$detail .= "
";
}
}
Db::name('store_product_content')->where('product_id', $product_id)->update(['content' => $detail]);
return true;
}
public function seveImg($goods_id, $img_url, $targetDir)
{
return $img_url;
if (!$img_url) return $img_url;
if (is_array($img_url)) {
$new_img_url = [];
foreach ($img_url as $value) {
$new_img_url[] = $this->seveImgUrl($goods_id, $value, $targetDir);
}
} else {
$new_img_url = $this->seveImgUrl($goods_id, $img_url, $targetDir);
}
return $new_img_url;
}
public function seveImgUrl($goods_id, $remoteImageUrl, $targetDir)
{
// 目标路径(确保目录存在)
$targetDirTow = rand(1000, 10000) . $this->upImgName(basename($remoteImageUrl));
$targetPath = $targetDir . $targetDirTow; // 使用basename获取文件名
// 检查目标目录是否存在,如果不存在则创建
if (!is_dir($targetDir)) {
mkdir($targetDir, 0777, true); // 第二个参数是权限,第三个参数是递归创建
}
ini_set("max_execution_time", 2);
// 获取远程图片内容
$imageContent = @file_get_contents($remoteImageUrl);
// 检查是否成功获取内容
if ($imageContent !== false) {
// 保存图片到指定路径
if (file_put_contents($targetPath, $imageContent)) {
$new_img_url = env('APP_URL') . '/images/gong/' . $goods_id . '/' . $targetDirTow;
return $new_img_url;
} else {
echo "保存图片时发生错误。";
return $remoteImageUrl;
}
} else {
echo "无法获取远程图片内容。";
return $remoteImageUrl;
}
}
public function upImgName($filename)
{
$position = strpos($filename, '?');
if ($position !== false) {
$filename = substr($filename, 0, $position);
}
return $filename;
}
public function deleteFilesInDirectory($dir)
{
if (!is_dir($dir)) return true;
// 打开目录
$files = scandir($dir);
// 遍历目录中的所有文件
foreach ($files as $file) {
// 如果是文件(排除 . 和 ..)
if ($file !== '.' && $file !== '..') {
$filePath = $dir . '/' . $file;
// 删除文件
if (is_file($filePath)) {
unlink($filePath);
}
}
}
return true;
}
public function add($data)
{
$findData = Db::name('douhuomall')->where('spu_id', $data['spu_id'])->find();
try {
$data['create_time'] = date('Y-m-d H:i:s');
$data['update_time'] = date('Y-m-d H:i:s');
$skuInfo = $this->arrSort($data['skuId_info'], 'cost_price', SORT_ASC);
$data['cost_price'] = isset($skuInfo[0]['cost_price']) ? $skuInfo[0]['cost_price'] : 0;
$data['market_price'] = isset($skuInfo[0]['market_price']) ? $skuInfo[0]['market_price'] : 0;
$data['ot_price'] = isset($skuInfo[0]['retail_price']) ? $skuInfo[0]['retail_price'] : 0;
$data['mer_profit'] = isset($skuInfo[0]['mer_profit']) ? $skuInfo[0]['mer_profit'] : 0;
$data['pension'] = isset($skuInfo[0]['yanglaojin']) ? $skuInfo[0]['yanglaojin'] : 0;
$data['profit'] = empty($data['cost_price']) || empty($data['market_price']) ? 0 : bcdiv($data['market_price'], $data['cost_price'], 2);
if (empty($findData)) {
$data['skuId_info'] = json_encode($data['skuId_info']);
$data['spuId_info'] = json_encode($data['spuId_info']);
Db::name('douhuomall')->insert($data);
} else {
// 如果货物 属于已被删除状态,并且新更新进来的状态 非删除状态,并且成本价、零售价、市场价、利润率没有变动的话,
// if ($findData['status'] == 3
// && $data['status'] !== 3
// && $data['cost_price'] == $findData['cost_price']
// && $data['ot_price'] == $findData['ot_price']
// && $data['market_price'] == $findData['market_price']
// && $data['profit'] == $findData['profit']) {
// $count = Db::name('store_product')
// ->where('spu_id', '=', $data['spu_id'])
// ->where('is_show', '=', 0)
// ->where('is_status', '=', 0)
// ->count();
// if ($count) {
// Db::name('store_product')->where('spu_id', '=', $data['spu_id'])->update([
// 'is_show' => 1
// ]);
// }
// }
Db::name('douhuomall')->where('spu_id', $findData['spu_id'])->update([
'status' => $data['status'],
// 'create_time' => date('Y-m-d H:i:s'),
'update_time' => date('Y-m-d H:i:s'),
'cost_price' => $data['cost_price'],
'market_price' => $data['market_price'],
'ot_price' => $data['ot_price'],
'mer_profit' => $data['mer_profit'],
'pension' => $data['pension'],
'profit' => $data['profit'],
'skuId_info' => json_encode($data['skuId_info']),
'spuId_info' => json_encode($data['spuId_info']),
'title' => $data['title'],
'cate_ids' => $data['cate_ids'],
'cxb_cate_id' => $data['cxb_cate_id']
]);
}
return true;
} catch (Exception $e) {
echo '供应链产品添加:' . $e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage() . '】';
return false;
}
}
public function remove($goodsIds)
{
Db::startTrans();
try {
// 删除选品库对应的数据
Db::name('douhuomall')->whereIn('spu_id', $goodsIds)->update([
'status' => 3,
'update_time' => date('Y-m-d H:i:s')
]);
// 删除商品管理对应的数据
Db::name('store_product')->whereIn('spu_id', $goodsIds)->update([
// 'is_del' => 1,
'is_show' => 0
]);
Db::commit();
return true;
} catch (Exception $e) {
Db::rollback();
Log::channel("gong")->error('供应链取消商品失败:' . $e->getMessage());
return false;
}
}
/**
* 二维数组根据某个字段排序
* @param array $array 要排序的数组
* @param string $keys 要排序的键字段
* @param string $sort 排序类型 SORT_ASC SORT_DESC
* @return array 排序后的数组
*/
function arrSort($array, $keys, $sort = SORT_DESC)
{
$keysValue = [];
foreach ($array as $k => $v) {
$keysValue[$k] = $v[$keys];
}
array_multisort($keysValue, $sort, $array);
return $array;
}
}