quanwei
19 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
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 think\facade\Cache;
use app\job\model\plus\agent\Order as AgentOrderModel;
 
/**
 * 分销商订单事件管理
 */
class AgentOrder
{
    // 模型
    private $model;
 
    /**
     * 执行函数
     */
    public function handle()
    {
        try {
            $this->model = new AgentOrderModel();
            $cacheKey = "task_space_AgentOrder";
            if (!Cache::has($cacheKey)) {
                 $this->model->startTrans();
                try {
                    // 发放分销订单佣金
                    $this->grantMoney();
                    $this->model->commit();
                } catch (\Exception $e) {
                    $this->model->rollback();
                }
                Cache::set($cacheKey, time(), 60);
            }
        } catch (\Throwable $e) {
            echo 'ERROR AgentOrder: ' . $e->getMessage() . PHP_EOL;
            log_write('AgentOrder TASK : ' . '__ ' . $e->getMessage(), 'task');
        }
        return true;
    }
 
    /**
     * 发放分销订单佣金
     */
    private function grantMoney()
    {
        // 获取未结算佣金的订单列表
        $list = $this->model->getUnSettledList();
        if ($list->isEmpty()) return false;
        // 整理id集
        $invalidIds = [];
        $grantIds = [];
        // 发放分销订单佣金
        foreach ($list->toArray() as $item) {
            // 已失效的订单
            if ($item['order_master']['order_status']['value'] == 20) {
                $invalidIds[] = $item['id'];
            }
            // 已完成的订单
            if ($item['order_master']['order_status']['value'] == 30) {
                $grantIds[] = $item['id'];
                AgentOrderModel::grantMoney($item['order_master'], $item['order_type']['value']);
            }
            
        }
        
        // 标记已失效的订单
        $this->model->setInvalid($invalidIds);
 
        // 记录日志
        $this->dologs('invalidIds', ['Ids' => $invalidIds]);
        $this->dologs('grantMoney', ['Ids' => $grantIds]);
        return true;
    }
 
    /**
     * 记录日志
     */
    private function dologs($method, $params = [])
    {
        $value = 'behavior AgentOrder --' . $method;
        foreach ($params as $key => $val) {
            $value .= ' --' . $key . ' ' . (is_array($val) ? json_encode($val) : $val);
        }
        return log_write($value, 'task');
    }
 
}