You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
7.8 KiB
194 lines
7.8 KiB
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Actions\ContractPosition\Flat;
|
|
use App\Admin\Actions\ContractPosition\OnekeyFlatPosition;
|
|
use App\Handlers\ContractTool;
|
|
use App\Models\Agent;
|
|
use App\Models\AgentGrade;
|
|
use App\Models\ContractPair;
|
|
use App\Models\ContractPosition;
|
|
use App\Models\ContractStrategy;
|
|
use App\Models\SustainableAccount;
|
|
use App\Models\UserAuth;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Show;
|
|
use Dcat\Admin\Controllers\AdminController;
|
|
use GatewayWorker\Lib\Db;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class ContractPositionController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
$builder = ContractPosition::query()->where('hold_position', '>', 0);
|
|
return Grid::make($builder, function (Grid $grid) {
|
|
$grid->model()->orderByDesc('id');
|
|
$grid->disableRowSelector();
|
|
$grid->disableCreateButton();
|
|
$grid->actions(function (Grid\Displayers\Actions $actions) {
|
|
$actions->disableDelete();
|
|
$actions->disableQuickEdit();
|
|
$actions->disableEdit();
|
|
$actions->disableView();
|
|
|
|
$actions->append(new Flat());
|
|
});
|
|
|
|
$grid->tools([new OnekeyFlatPosition()]);
|
|
|
|
$grid->column('id')->sortable();
|
|
$grid->column('user_id');
|
|
$grid->column('usermail', '邮箱')->display(function () {
|
|
//$realtime_price = Cache::store('redis')->get('swap:' . 'trade_detail_' . $this->symbol)['price'] ?? null;
|
|
// dd(ContractTool::getUserMail($this->user_id));
|
|
return ContractTool::getUserMail($this->user_id);
|
|
});
|
|
$grid->column('agname', '代理邮箱')->display(function () {
|
|
$var = ContractTool::GetProxyEmail($this->user_id);
|
|
// dd($var['username'],$this->user_id);
|
|
return $var['username'];
|
|
});
|
|
$grid->column('symbol');
|
|
$grid->column('side')->using([1 => '多', 2 => '空'])->label([1 => 'info', 2 => 'danger']);
|
|
// $grid->column('contract_id');
|
|
// $grid->column('unit_amount');
|
|
$grid->column('lever_rate');
|
|
$grid->column('hold_position');
|
|
$grid->column('avail_position');
|
|
$grid->column('freeze_position');
|
|
$grid->column('position_margin');
|
|
$grid->column('avg_price');
|
|
|
|
$grid->column('LatestPrice', '最新价格')->display(function () {
|
|
|
|
return $realtime_price = Cache::store('redis')->get('swap:' . 'trade_detail_' . $this->symbol)['price'] ?? null;
|
|
//return ContractTool::unRealProfit($this, ['unit_amount' => $this->unit_amount], $realtime_price);
|
|
});
|
|
$grid->column('unRealProfit', '未实现盈亏')->display(function () {
|
|
$realtime_price = Cache::store('redis')->get('swap:' . 'trade_detail_' . $this->symbol)['price'] ?? null;
|
|
return ContractTool::unRealProfit($this, ['unit_amount' => $this->unit_amount], $realtime_price);
|
|
});
|
|
$grid->column('ProfitAndLossRatio', '盈亏比')->display(function () {
|
|
$realtime_price = Cache::store('redis')->get('swap:' . 'trade_detail_' . $this->symbol)['price'] ?? null;
|
|
if ($this->side == 1) {
|
|
$aa = ($realtime_price - $this->avg_price) / $this->avg_price * $this->lever_rate;
|
|
} else {
|
|
$aa = ($this->avg_price - $realtime_price) / $this->avg_price * $this->lever_rate;
|
|
}
|
|
|
|
$aa = sprintf("%.2f", $aa);
|
|
return $aa * 100 . '%';
|
|
});
|
|
$grid->column('estimatedLiquidationValue', '预估强平值')->display(function () {
|
|
|
|
$account = SustainableAccount::getContractAccount($this->user_id);
|
|
$contract = ContractPair::query()->find($this->contract_id);
|
|
//$realtime_price = Cache::store('redis')->get('swap:' . 'trade_detail_' . $this->symbol)['price'] ?? null;
|
|
//dd(ContractTool::getUserMail($this->user_id));
|
|
//return ContractTool::getUserMail($this->user_id);
|
|
return ContractTool::getFlatPrice($account, $contract);
|
|
});
|
|
$grid->column('zhiying','止盈/止损')->display(function(){
|
|
$strategy = ContractStrategy::query()
|
|
->where('user_id', $this->user_id)
|
|
->where('status', 1)
|
|
->where('contract_id', $this->contract_id)
|
|
->where('position_side', $this->side)
|
|
->first();
|
|
// $abababa =$strategy['created_at'];
|
|
$abababa = $strategy['tp_trigger_price'] ?? '--' ;
|
|
$abababa .= "/";
|
|
$abababa .=$strategy['sl_trigger_price'] ?? '--';
|
|
|
|
return $abababa;
|
|
});
|
|
$grid->column('updated_at');
|
|
$grid->column('forCurrencyBalances', '对于币种余额')->display(function () {
|
|
$conac = $account = SustainableAccount::getContractAccount($this->user_id);
|
|
return $conac['usable_balance'];
|
|
});
|
|
|
|
|
|
// $grid->column('created_at');
|
|
// $grid->column('updated_at')->sortable();
|
|
|
|
$grid->filter(function (Grid\Filter $filter) {
|
|
$filter->equal('user_id')->width(3);
|
|
$filter->where('username', function ($q) {
|
|
$username = $this->input;
|
|
$aa = $q->whereHas('user', function ($q) use ($username) {
|
|
$q->where('username', $username)->orWhere('phone', $username)->orWhere('email', $username);
|
|
});
|
|
}, "用户名/手机/邮箱")->width(3);
|
|
$filter->equal('symbol')->width(3);
|
|
$filter->where('acc', function ($q) {
|
|
$username = $this->input;
|
|
$aa = $q->whereHas('user', function ($q) use ($username) {
|
|
$q->where('whethertopromote', $username);
|
|
});
|
|
}, "账号类型")->select(UserAuth::$typeMap)->width(2);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new ContractPosition(), function (Show $show) {
|
|
$show->field('id');
|
|
$show->field('user_id');
|
|
$show->field('side');
|
|
$show->field('contract_id');
|
|
$show->field('symbol');
|
|
$show->field('unit_amount');
|
|
$show->field('lever_rate');
|
|
$show->field('hold_position');
|
|
$show->field('avail_position');
|
|
$show->field('freeze_position');
|
|
$show->field('position_margin');
|
|
$show->field('avg_price');
|
|
$show->field('created_at');
|
|
$show->field('updated_at');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
return Form::make(new ContractPosition(), function (Form $form) {
|
|
$form->display('id');
|
|
$form->text('user_id');
|
|
$form->text('side');
|
|
$form->text('contract_id');
|
|
$form->text('symbol');
|
|
$form->text('unit_amount');
|
|
$form->text('lever_rate');
|
|
$form->text('hold_position');
|
|
$form->text('avail_position');
|
|
$form->text('freeze_position');
|
|
$form->text('position_margin');
|
|
$form->text('avg_price');
|
|
|
|
$form->display('created_at');
|
|
$form->display('updated_at');
|
|
});
|
|
}
|
|
}
|
|
|