111
liyaozhi
2025-11-09 c13b8914228e6a404bd60ee36bf2479383da8f23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
 
namespace app\job\event;
 
use think\facade\Cache;
use app\job\model\plus\fans\UserCoupon as UserCouponModel;
 
/**
 * 优惠券事件管理
 */
class UserCoupon
{
    // 模型
    private $model;
 
    /**
     * 执行函数
     */
    public function handle()
    {
        try {
            $this->model = new UserCouponModel();
            $cacheKey = "task_space_UserCoupon";
            if (!Cache::has($cacheKey)) {
                // 设置优惠券过期状态
                $this->setExpired();
                Cache::set($cacheKey, time(), 60);
            }
        } catch (\Throwable $e) {
            echo 'ERROR UserCoupon: ' . $e->getMessage() . PHP_EOL;
            log_write('UserCoupon TASK : ' . '__ ' . $e->getMessage(), 'task');
        }
        return true;
    }
 
    /**
     * 设置优惠券过期状态
     */
    private function setExpired()
    {
        // 获取已过期的优惠券ID集
        $couponIds = $this->model->getExpiredCouponIds();
        // 记录日志
        $this->dologs('setExpired', [
            'couponIds' => json_encode($couponIds),
        ]);
        // 更新已过期状态
        return $this->model->setIsExpire($couponIds);
    }
 
    /**
     * 记录日志
     */
    private function dologs($method, $params = [])
    {
        $value = 'UserCoupon --' . $method;
        foreach ($params as $key => $val)
            $value .= ' --' . $key . ' ' . $val;
        return log_write($value, 'task');
    }
 
}