multi-store/process/Task.php

63 lines
2.4 KiB
PHP

<?php
namespace process;
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\user_recharge\UserRecharge;
use Webman\RedisQueue\Redis;
use Workerman\Crontab\Crontab;
class Task
{
public function onWorkerStart()
{
// 每10分钟执行一次
new Crontab('0 */10 * * * *', function () {
$where = ['paid' => 0];
$where[] = ['create_time', '<', time() - 600]; // 10分钟前创建的订单
// 删除10分钟未支付的订单
$oid = StoreOrder::where($where)->column('id'); // 删除时间设置为当前时间,即删除
if ($oid) {
StoreOrder::where('id', 'in', $oid)->update(['delete_time' => time()]);
$arr = StoreOrderCartInfo::where('oid', 'in', $oid)->field('store_id,product_id,cart_num')->select();
$updateData = [];
$updateDataTwo = [];
foreach ($arr as $v) {
$updateData[] = [
'id' => $v['branch_product_id'],
'sales' => ['dec', $v['cart_num']]
];
$updateDataTwo[] = [
'id' => $v['product_id'],
'sales' => ['dec', $v['cart_num']]
];
}
(new StoreBranchProduct())->saveAll($updateData);
(new StoreProduct())->saveAll($updateDataTwo);
}
// 获取当前时间
$now = time();
// 计算一个小时前的时间戳
$oneHourAgo = $now - 3600;
//删除未充值的订单
UserRecharge::where('paid', 0)->where('status', 1)->where('create_time', '<', $oneHourAgo)->update(['delete_time' => time()]);
});
new Crontab('0 */1 * * * *', function () {
$endTime = time();
// 计算10分钟前的时间戳
$startTime = $endTime - 10 * 60;
$arr = UserRecharge::where('paid', 0)->where('status', 1)->whereBetweenTime('create_time', $startTime, $endTime)->column('order_id');
foreach ($arr as $v) {
Redis::send('task-recharge-query', ['order_id' => $v]);
}
});
}
}