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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
 
namespace app\job\event;
 
use think\facade\Cache;
use app\job\model\plus\vip\User as VipUserModel;
 
/**
 * VIP用户事件管理
 */
class VipUser
{
    // 模型
    private $model;
 
    /**
     * 执行函数
     */
    public function handle()
    {
        try {
            $this->model = new VipUserModel();
            $cacheKey = "task_space_VipUser";
            if (!Cache::has($cacheKey)) {
                // 处理用户升级逻辑
                $this->processUserUpgrades();
                
                // 处理用户降级逻辑
                $this->processUserDowngrades();
                
                Cache::set($cacheKey, time(), 60);
            }
        } catch (\Throwable $e) {
            echo 'ERROR VipUser: ' . $e->getMessage() . PHP_EOL;
            log_write('VipUser TASK : ' . '__ ' . $e->getMessage(), 'task');
        }
        return true;
    }
 
    /**
     * 处理用户升级
     */
    private function processUserUpgrades()
    {
        // 获取需要升级的用户列表
        $list = $this->model->getUpgradeList();
        if ($list->isEmpty()) return false;
        
        // 处理逻辑...
        $processedIds = [];
        foreach ($list as $item) {
            // 具体处理逻辑
            $processedIds[] = $item['user_id'];
        }
        
        // 标记已处理
        if (!empty($processedIds)) {
            $this->model->processGradeChange($processedIds, true);
        }
 
        // 记录日志
        $this->dologs('processUserUpgrades', ['Count' => count($processedIds)]);
        return true;
    }
 
    /**
     * 处理用户降级
     */
    private function processUserDowngrades()
    {
        // 获取需要降级的用户列表
        $list = $this->model->getDowngradeList();
        if ($list->isEmpty()) return false;
        
        // 处理逻辑...
        $processedIds = [];
        foreach ($list as $item) {
            // 具体处理逻辑
            $processedIds[] = $item['user_id'];
        }
        
        // 标记已处理
        if (!empty($processedIds)) {
            $this->model->processGradeChange($processedIds, false);
        }
 
        // 记录日志
        $this->dologs('processUserDowngrades', ['Count' => count($processedIds)]);
        return true;
    }
 
    /**
     * 记录日志
     */
    private function dologs($method, $params = [])
    {
        $value = 'behavior VipUser --' . $method;
        foreach ($params as $key => $val) {
            $value .= ' --' . $key . ' ' . (is_array($val) ? json_encode($val) : $val);
        }
        return log_write($value, 'task');
    }
}