toDateString(); // 获取昨天的日期,格式为 YYYY-MM-DD // 查询所有今天有浏览量记录的商品 $products = Product::query()->where('today_has_view', 1)->get(); // 获取所有今天有浏览量的商品 // 遍历每个商品并更新浏览量 $products->map(function (Product $product) use ($yesterday) { // 从缓存中取出前一天的浏览量,默认值为 0 $viewCount = Cache::pull($product->getViewCountKey($yesterday), 0); // 更新商品的浏览量 $product->view_count += $viewCount; // 累加前一天的浏览量 $product->today_has_view = 0; // 重置今天的浏览量标记 $product->save(); // 保存更新后的商品信息 }); // 记录系统日志,记录同步操作的信息 createSystemLog("系统同步{$yesterday}商品浏览量", ['date' => $yesterday]); // 记录同步的日期 } }