0]; $where[] = ['create_time', '<', time() - 600]; // 10分钟前创建的订单 $where[] = ['source', '<=',1]; // 删除10分钟未支付的订单 $oid = StoreOrder::where($where)->column('id'); // 删除时间设置为当前时间,即删除 if ($oid) { StoreOrder::where('id', 'in', $oid)->update(['delete_time' => time()]); StoreOrderCartInfo::where('oid','in',$oid)->update(['status'=>OrderEnum::REFUND_STATUS_FINISH]); } // 获取当前时间 $now = time(); // 计算一个小时前的时间戳 $oneHourAgo = $now - 3600; //删除未充值的订单 UserRecharge::where('paid', 0)->where('status', 1)->where('create_time', '<', $oneHourAgo)->update(['delete_time' => time()]); }); $this->updateProductPrice(); $this->confirmProductPrice(); $this->setPurchase(); } /** * 确认商品改价 * @return void */ public function confirmProductPrice() { new Crontab('0 */10 * * * *', function () { $value = DictData::getDictValue('update_product_price_task', 'confirm'); if ($value == 1) { $list = StoreProductPrice::where('status', 0)->select()->toArray(); $update = []; foreach ($list as $item) { $update[] = [ 'id' => $item['id'], 'status' => 1, ]; } if (count($update) > 0) { (new StoreProduct())->saveAll($update); } } }); } /** * 将商品价格更改列表的价格同步至商品 * @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); } } }); } /** * 设置出库商品的供货价 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function setPurchase() { new Crontab('0 0 * * * *', function () { $list = WarehouseProduct::where('purchase', 0)->order('id desc')->limit(100)->select()->toArray(); $productIds = array_unique(array_column($list, 'product_id')); $products = StoreProduct::whereIn('id', $productIds)->field('id,purchase')->select()->toArray(); $products = reset_index($products, 'id'); $update = []; foreach ($list as $item) { $product = $products[$item['product_id']] ?? []; if (empty($product) || empty($product['purchase'])) { continue; } $update[] = [ 'id' => $item['id'], 'purchase' => min($product['purchase'], $item['price']), ]; } (new WarehouseProduct())->saveAll($update); }); } }