-
Notifications
You must be signed in to change notification settings - Fork 20
/
MicroOrder.php
240 lines (204 loc) · 6.03 KB
/
MicroOrder.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
class MicroOrder extends Model
{
const TYPE_RISE = 1; //买涨
const TYPE_FALL = 2; //买跌
const STATUS_OPENED = 1; //交易中
const STATUS_CLOSING = 2; //平仓中
const STATUS_CLOSED = 3; //已平仓
const RESULT_LOSS = -1; //亏
const RESULT_BALANCE = 0; //平
const RESULT_PROFIT = 1; //盈
protected $dates = [
'handled_at',
'complete_at',
];
protected $attributes = [
'profit_result' => 0,
];
protected $hidden = [
'pre_profit_result', //隐藏掉预设的盈利结果
];
protected $appends = [
'currency_name',
'symbol_name',
'account',
'type_name',
'status_name',
'real_name',
'profit_result_name',
'show_micro_id',//显示订单号
'parent_agent_name',
];
protected static $typeList = [
'',
self::TYPE_RISE => '涨',
self::TYPE_FALL => '跌',
];
protected static $statusList = [
'',
self::STATUS_OPENED => '交易中',
self::STATUS_CLOSING => '平仓中',
self::STATUS_CLOSED => '已平仓',
];
protected static $resultList = [
self::RESULT_LOSS => '亏损',
self::RESULT_BALANCE => '无',
self::RESULT_PROFIT => '盈利',
];
//伪订单号
public function getShowMicroIdAttribute()
{
$id = $this->attributes['id'] ?? 0;
$uid = $id + 10000;
return $uid;
}
public function getCurrencyNameAttribute()
{
return $this->currency()->value('name');
}
public function getSymbolNameAttribute()
{
return $this->currencyMatch->symbol ?? '';
}
public function getTypeNameAttribute()
{
$value = $this->attributes['type'] ?? 0;
return self::$typeList[$value] ?? '';
}
public function getStatusNameAttribute()
{
$value = $this->attributes['status'] ?? 0;
return self::$statusList[$value] ?? '';
}
public function getProfitResultNameAttribute()
{
$value = $this->attributes['profit_result'] ?? 0;
return self::$resultList[$value] ?? '';
}
public function getPreProfitResultNameAttribute()
{
$value = $this->attributes['pre_profit_result'] ?? 0;
return self::$resultList[$value] ?? '';
}
public function getAccountAttribute()
{
$user = $this->user();
return $user->value('phone') ?? $user->value('email');
}
public function setPreProfitResultAttribute($value)
{
if (!$this->exists) {
$user_id = $this->attributes['user_id']; //下单用户id
$currency_id = $this->attributes['currency_id']; //币种id
$number = $this->attributes['number']; //下单数量
$result = self::getRisk($user_id, $currency_id, $number);
$this->attributes['pre_profit_result'] = $result;
} else {
$this->attributes['pre_profit_result'] = $value;
}
}
/**
* 返回风控结果
*
* @return void
*/
public static function getRisk($user_id, $currency_id)
{
//检测用户是否在预设的盈利和亏损名单中
//$risk = $this->user()->value('risk');
//return $risk;
return 0;
}
/**
* 根据价格计算当前的盈利结果
*
* @return integer
*/
public function getProfitTypeAttribute()
{
$type = $this->attributes['type'] ?? 0;
$open_price = $this->attributes['open_price'] ?? 0;
$end_price = $this->attributes['end_price'] ?? 0;
if (empty($type) || empty($open_price) || empty($end_price)) {
return 0;
}
$profit_type = bc_comp($end_price, $open_price);
if ($type == self::TYPE_FALL) {
$profit_type = -1 * $profit_type;
}
return $profit_type;
}
public function getCostPriceAttribute()
{
return $this->currency()->value('price') ?? 0;
}
public function getLossPriceAttribute()
{
return $this->currency()->value('price') ?? 0;
}
public function getCostAttribute()
{
$number = $this->attributes['number'] ?? 0;
return bc_mul($number, $this->getAttribute('cost_price'));
}
public function getCostWithProfitAttribute()
{
$profit_ratio = $this->getAttribute('profit_ratio');
$number = $this->attributes['number'] ?? 0;
$cost_with_profit = bc_mul($number, bc_div(bc_add(100, $profit_ratio), 100));
return bc_mul($cost_with_profit, $this->getAttribute('cost_price'));
}
public function getRemainMilliSecondsAttribute()
{
$handled_at = $this->getAttribute('handled_at');
$now = Carbon::now();
if ($now >= $handled_at) {
return 0;
}
return ($handled_at->diffInSeconds($now) + 1.3) * 1000;
}
public function getRealNameAttribute()
{
$user_profile = $this->user->userReal->first();
if($user_profile){
return $user_profile->name ?? '--';
}else{
return '--';
}
//return $this->user->userProfile->name ?? '--';
}
public function user()
{
return $this->belongsTo(Users::class)->withDefault();
}
public function userProfile()
{
return $this->user->userProfile();
}
public function currency()
{
return $this->belongsTo(Currency::class);
}
public function currencyMatch()
{
return $this->belongsTo(CurrencyMatch::class, 'match_id');
}
//所属代理商
public function getParentAgentNameAttribute() {
$user = $this->user()->getResults();
if ($user) {
if ($user->agent_note_id == 0){
return '无';
}else{
$agent = Agent::where('id' , $user->agent_note_id)->first();
return $agent->username;
}
} else {
return '';
}
}
}