添加定时更新商品价格

This commit is contained in:
lewis 2024-12-31 17:41:13 +08:00
parent ec78cd4953
commit 2666241796
2 changed files with 49 additions and 0 deletions

View File

@ -44,4 +44,14 @@ class DictData extends BaseModel
return $data['status'] ? '正常' : '停用';
}
public static function getDictValue($type, $key)
{
$dictType = DictType::where('type', $type)->where('status', 1)->find();
if ($dictType) {
$dictData = DictData::where('type_id', $dictType['id'])->where('name', $key)->where('status', 1)->value('value');
return empty($dictData) ? '' : $dictData;
}
return '';
}
}

View File

@ -4,11 +4,14 @@ namespace process;
use app\common\enum\OrderEnum;
use app\common\logic\PayNotifyLogic;
use app\common\model\dict\DictData;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use app\common\model\store_product\StoreProduct;
use app\common\model\store_product_price\StoreProductPrice;
use app\common\model\user_recharge\UserRecharge;
use support\Cache;
use think\facade\Db;
use Webman\RedisQueue\Redis;
use Workerman\Crontab\Crontab;
@ -62,5 +65,41 @@ class Task
}
}
});
$this->updateProductPrice();
}
/**
* 将商品价格更改列表的价格同步至商品
* @return void
*/
public function updateProductPrice()
{
new Crontab('0 */10 * * * *', function () {
$value = DictData::getDictValue('update_product_price_task', 'status');
if ($value == 1) {
$lastProductId = Cache::get('update_product_price_last_product_id', 0);
$productIds = StoreProduct::where('purchase', 0)->where('id', '>', $lastProductId)->limit(200)->column('id');
$lastProductId = end($productIds);
if (count($productIds) < 200) {
$lastProductId = 0;
}
Cache::set('update_product_price_last_product_id', $lastProductId);
$productPrices = StoreProductPrice::whereIn('product_id', $productIds)->where('status', 1)->distinct('product_id')->order('id desc')->select()->toArray();
$update = [];
foreach ($productPrices as $productPrice) {
$update[] = [
'id' => $productPrice['product_id'],
'purchase' => $productPrice['purchase'] ?? 0,
'cost' => $productPrice['cost'] ?? 0,
'price' => $productPrice['purchase'] ?? 0,
];
}
if (count($update) > 0) {
(new StoreProduct())->saveAll($update);
}
}
});
}
}