liyaozhi
2025-10-29 52b7c373601edbc5010471082eb88dadf70e382d
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
<?php
 
namespace app\job\event;
 
use app\job\model\branch\Branch as BranchModel;
use think\facade\Cache;
use app\common\library\helper;
 
/**
 * 分会事件管理
 */
class Branch
{
    // 模型
    private $model;
 
    // 应用id
    private $appId;
 
    /**
     * 执行函数
     */
    public function handle($app_id)
    {
        try {
            $this->appId = $app_id;
            $this->model = new BranchModel();
            // 执行已完成活动的结算
            $this->settled();
            // 未支付报名自动删除
            $this->deleteActivityUser();
        } catch (\Throwable $e) {
            echo 'ERROR ORDER: ' . $e->getMessage() . PHP_EOL;
            log_write('ORDER TASK : ' . $app_id . '__ ' . $e->getMessage(), 'task');
        }
        return true;
    }
 
    
    /**
     * 未支付报名自动删除
     */
    private function deleteActivityUser()
    {
        $key = "task_space__branch__activityUser__{$this->appId}";
        if (Cache::has($key)) return true;
        $orderIds = $this->model->getActivityUserIds($this->appId);
        $this->model->deleteActivityUser($orderIds);
        // 记录日志
        $this->dologs('close', [
            'orderIds' => json_encode($orderIds),
        ]);
        Cache::set($key, time(), 8 * 3600);
        return true;
    }
 
    /**
     * 已完成活动结算
     */
    private function settled()
    {
        $key = "task_space__branch__activity__{$this->appId}";
        if (Cache::has($key)) return true;
        // 查询活动列表
        $activityList = $this->model->getActivitySettledList($this->appId);
        // 活动id集
        $activityIds = helper::getArrayColumn($activityList, 'activity_id');
        $this->model->settledActivity($activityList);
        // 记录日志
        $this->dologs('settled', [
            'activityIds' => json_encode($activityIds),
        ]);
        Cache::set($key, time(), 1800);
        return true;
    }
 
    /**
     * 记录日志
     */
    private function dologs($method, $params = [])
    {
        $value = 'behavior Branch-Activity --' . $method;
        foreach ($params as $key => $val)
            $value .= ' --' . $key . ' ' . $val;
        return log_write($value, 'task');
    }
 
}