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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
 
namespace app\common\model\shop;
 
use app\common\exception\BaseException;
use think\facade\Cache;
use app\common\model\BaseModel;
use app\common\model\agent\BalanceLog as BalanceLogModel;
 
/**
 * 应用模型
 */
class Agent extends BaseModel
{
    protected $name = 'dl_agent';
    protected $pk = 'agent_id';
 
    /**
     * 关联客户表
     */
    public function client()
    {
        return $this->hasMany('app\\common\\model\\agent\\Client','agent_id','agent_id');
    }
 
    /**
     * 获取应用信息
     */
    public static function detail($agent_id)
    {
        return (new static())->find($agent_id);
    }
 
    /**
     * 从缓存中获取app信息
     */
    public static function getAgentCache($agent_id = null)
    {
        if (is_null($agent_id)) {
            $self = new static();
            $agent_id = $self::$agent_id;
        }
        if (!$data = Cache::get('app_' . $agent_id)) {
            $data = self::detail($agent_id);
            if (empty($data)) throw new BaseException(['msg' => '未找到当前应用信息']);
            Cache::tag('cache')->set('app_' . $agent_id, $data);
        }
        return $data;
    }
 
    /**
     * 启用商城
     * @return bool
     */
    public function updateStatus()
    {
        return $this->save([
            'status' => !$this['status'],
        ]);
    }
 
    /**
     * 所有商城
     */
    public static function getAll(){
        return (new self())->where('is_delete', '=', 0)
            ->where('is_recycle', '=', 0)
            ->select();
    }
    
    /**
     * 用户充值
     */
    public function recharge($storeUserName, $source, $data)
    {
         
        if ($source == 0) {
            return $this->rechargeToBalance($storeUserName, $data['balance']);
        } elseif ($source == 1) {
            return $this->rechargeToPoints($storeUserName, $data['points']);
        }
        return false;
    }
    
    /**
     * 用户充值:余额
     */
    private function rechargeToBalance($storeUserName, $data)
    {
        if (!isset($data['money']) || $data['money'] === '' || $data['money'] < 0) {
            $this->error = '请输入正确的金额';
            return false;
        }
        // 判断充值方式,计算最终金额
        $money = 0;
        if ($data['mode'] === 'inc') {
            $diffMoney = $this['balance'] + $data['money'];
            $money = $data['money'];
        } elseif ($data['mode'] === 'dec') {
            $diffMoney = $this['balance'] - $data['money'] <= 0 ? 0 : $this['balance'] - $data['money'];
            $money = -$data['money'];
        } else {
            $diffMoney = $data['money'];
            $money = $diffMoney - $this['balance'];
        }
        // 更新记录
        $this->transaction(function () use ($storeUserName, $data, $diffMoney, $money) {
            // 更新账户余额
            $this->where('agent_id', '=', $this['agent_id'])->update(['balance' => $diffMoney]);
            // 新增余额变动记录
            BalanceLogModel::add(30, [
                'agent_id' => $this['agent_id'],
                'money' => $money,
                'remark' => $data['remark'],
            ], [$storeUserName]);
        });
        return true;
    }
}