You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/command/UpdateCollectGoodsPrice.php

110 lines
4.3 KiB

8 months ago
<?php
declare (strict_types=1);
namespace app\command;
use think\console\Command;
use think\console\Output;
use think\console\Input;
use think\facade\Db;
use app\common\model\Goods;
use app\common\model\GoodsSku;
// /www/server/php/74/bin/php /server/wwwroot/yanzong/think test
class UpdateCollectGoodsPrice extends Command
{
protected function configure()
{
// 指令配置
$this->setName('UpdateCollectGoodsPrice')->setDescription('自动更新商品的价格');
$this->addArgument("goods_id");
$this->addArgument("div");
$this->addArgument("mod");
}
protected function execute(Input $input, Output $output)
{
$goods_id = $input->getArgument("goods_id");
$div = $input->getArgument("div");
$mod = $input->getArgument("mod");
8 months ago
$limit = 100;
8 months ago
$service = new \app\job\service\goods\Collector();
while (TRUE) {
8 months ago
//人工导入的数据,价格更新,导入的数据有链接地址
$sql = "SELECT goods_id,link,goods_price_min,cost_price_min,profit,profit_rate,store_id FROM `yoshop_goods` WHERE goods_id > " . $goods_id . " AND goods_id % ". $div . " = ". $mod ." AND store_id = 0 AND is_delete = 0 AND status = 10 AND link != '' and data_type = 1 ";
8 months ago
$list = Db::query($sql);
if (!$list) {
return false;
}
foreach ($list as $item) {
try {
$sku = GoodsSku::where('goods_id', $item['goods_id'])->field('goods_sku_no,cost_price,goods_price')->find();
if (!$sku || !$sku['goods_sku_no']) {
echo $item['link'].":sku不存在".PHP_EOL;
continue;
}
$data = $service->collector($item['link'], (int)$item['store_id']);
if (!$data || !isset($data['specData']['skuList']) || !$data['specData']['skuList']) {
echo $item['link'].":没有抓取到sku信息".PHP_EOL;
continue;
}
$skuList = array_column($data['specData']['skuList'], null, "goods_sku_no");
var_dump($skuList);
$goods_price = $skuList[$sku['goods_sku_no']]['goods_price'] ?? 0;
var_dump($goods_price);
//exit();
if (!$goods_price) {
echo $item['link'].":商品价格没有抓取到".PHP_EOL;
continue;
}
$cost_price = $item['cost_price_min'];
$profit = $goods_price - $cost_price;
$profit_rate = (float)$goods_price > 0 ? ($goods_price - $cost_price) / $goods_price : 0.00;
$profit_rate = $profit_rate > 0.0001 ? bcmul($profit_rate, 100, 2) : 0.00;
//更新商品价格
$goodsData = [
'goods_price_min' => $goods_price,
'profit' => $profit,
'profit_rate' => $profit_rate,
'update_time' => time(),
];
Goods::where('goods_id', $item['goods_id'])->update($goodsData);
//更新sku价格
$skuData = [
'goods_price' => $goods_price,
'update_time' => time(),
];
Goods::where('goods_id', $item['goods_id'])->update($goodsData);
} catch (Exception $e) {
echo $e->getMessage().PHP_EOL;
continue;
}
}
// var_dump($list);
// exit();
}
// $page = 1;
// $limit = 1000;
// while (TRUE) {
// $list = Goods::where('spu_id',0)->field('goods_id')->page($page)->limit($limit)->select();
// foreach ($list as $key => $value) {
// $ret = Goods::where('goods_id', $value['goods_id'])->update(['spu_id' => $value['goods_id']]);
// $ret = GoodsSku::where('goods_id', $value['goods_id'])->update(['spu_id' => $value['goods_id']]);
// var_dump($ret);
// }
// $page++;
// }
//var_dump($list);
}
}