-
Notifications
You must be signed in to change notification settings - Fork 20
/
Currency.php
153 lines (132 loc) · 4.4 KB
/
Currency.php
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Currency extends Model
{
protected $table = 'currency';
public $timestamps = false;
protected $appends = ['to_pb_price'];
protected $hidden = ['key'];
/**
* 定义一对多关系
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function quotation()
{
return $this->hasMany(CurrencyMatch::class, 'legal_id', 'id');
}
public function microNumbers()
{
return $this->hasMany(MicroNumber::class)->orderBy('number', 'asc');
}
// public function getExRateAttribute()
// {
// return Setting::getValueByKey('ExRate', 6.5);
// }
public function getCreateTimeAttribute()
{
return date('Y-m-d H:i:s', $this->attributes['create_time']);
}
public static function getNameById($currency_id)
{
$currency = self::find($currency_id);
return $currency->name;
}
// public function getUsdtPriceAttribute()
// {
// $last_price = 1;
// $currency_id = $this->attributes['id'];
// $last = TransactionComplete::orderBy('id', 'desc')
// ->where("currency", $currency_id)
// ->where("legal", 1)->first();//1是PB
// if (!empty($last)) {
// $last_price = $last->price;
// }
// if ($currency_id == 1) {
// $last_price = 1;
// }
// return $last_price;
// }
//获取币种相对于人民币的价格
public static function getCnyPrice($currency_id)
{
$rate = Setting::getValueByKey('USDTRate', 7.08);
$usdt = Currency::where('name', 'USDT')->select(['id'])->first();
$last = MarketHour::orderBy('id', 'desc')
->where("currency_id", $currency_id)
->where("legal_id", $usdt->id)->first();
if (!empty($last)) {
$cny_Price = $last->highest * $rate; //行情表里面最近的数据的最高值
} else {
//$cny_Price = 1; //如果不存在交易对,默认为1
//如果不存在行情,取币种默认价格
$currency = Currency::where('id', $usdt->id)->first();
$cny_Price = $currency->price * $rate;
}
if ($currency_id == $usdt->id) {
$cny_Price = 1 * $rate;
}
return $cny_Price;
}
public function getRmbRelationAttribute()
{
$rate = Setting::getValueByKey('USDTRate', 7.08);
return $rate;
}
public function getToPbPriceAttribute()
{
$currency_id = $this->id;
$ptb = Currency::where('name', UsersWallet::CURRENCY_DEFAULT)->first();
$last = MarketHour::orderBy('id', 'desc')
->where("currency_id", $currency_id)
->where("legal_id", $ptb->id)->first();
if (!empty($last)) {
$Price = $last->highest; //行情表里面最近的数据的最高值
} else {
$Price = $ptb->price; //如果不存在交易对,默认为1
}
if ($currency_id == $ptb->id) {
$Price = 1;
}
$to_pb_price = bcdiv($this->price, $Price, 8);
return $to_pb_price;
}
//获取币种相对于平台币的价格
public static function getPbPrice($currency_id)
{
$ptb = Currency::where('name', UsersWallet::CURRENCY_DEFAULT)->first();
$last = MarketHour::orderBy('id', 'desc')
->where("currency_id", $currency_id)
->where("legal_id", $ptb->id)->first();
if (!empty($last)) {
$Price = $last->highest; //行情表里面最近的数据的最高值
} else {
$Price = $ptb->price; //如果不存在交易对,默认为1
}
if ($currency_id == $ptb->id) {
$Price = 1;
}
return $Price;
}
public function getOriginKeyAttribute($value)
{
$private_key = $this->attributes['key'] ?? '';
return $private_key != '' ? decrypt($private_key) : '';
}
public function getKeyAttribute($value)
{
return $value == '' ?: '********';
}
public function setKeyAttribute($value)
{
if ($value != '') {
return $this->attributes['key'] = encrypt($value);
}
}
public static function getIdByName($currency_name)
{
$currency = self::where('name',strtoupper($currency_name))->first();
return $currency;
}
}