feat: 后台一键平仓功能
- AppContract.php: 新增closeall()方法, 遍历所有status=1持仓订单按当前价平仓 - index.html: 工具栏新增一键平仓按钮 - app_contract.js: 按钮事件, 二次确认后POST请求, 刷新表格 - 非超管只能平自己团队的仓位
This commit is contained in:
+261
@@ -0,0 +1,261 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\admin\controller\app;
|
||||||
|
|
||||||
|
use app\common\controller\Backend;
|
||||||
|
use think\Config;
|
||||||
|
use think\Db;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 合约管理
|
||||||
|
*
|
||||||
|
* @icon fa fa-circle-o
|
||||||
|
*/
|
||||||
|
class AppContract extends Backend
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AppContract模型对象
|
||||||
|
* @var \app\admin\model\app\AppContract
|
||||||
|
*/
|
||||||
|
protected $model = null;
|
||||||
|
|
||||||
|
public function _initialize()
|
||||||
|
{
|
||||||
|
parent::_initialize();
|
||||||
|
$this->model = new \app\admin\model\app\AppContract;
|
||||||
|
$this->view->assign("typeList", $this->model->getTypeList());
|
||||||
|
$this->view->assign("statusList", $this->model->getStatusList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 一键平仓 - 关闭所有持仓中的合约订单
|
||||||
|
*/
|
||||||
|
public function closeall()
|
||||||
|
{
|
||||||
|
if (!$this->request->isPost()) {
|
||||||
|
$this->error('请求方式错误');
|
||||||
|
}
|
||||||
|
$admin_id = $this->auth->id;
|
||||||
|
|
||||||
|
// 构建查询条件
|
||||||
|
$where = ['a.status' => '1'];
|
||||||
|
|
||||||
|
// 非超级管理员只能平仓自己团队的订单
|
||||||
|
if ($admin_id > 1) {
|
||||||
|
$user = Db::name("user")->where("admin_id", $admin_id)->find();
|
||||||
|
if ($user) {
|
||||||
|
$where['u.path'] = ['like', $user['path'] . '%'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询所有持仓中的合约
|
||||||
|
$contracts = Db::name('app_contract')
|
||||||
|
->alias('a')
|
||||||
|
->field('a.*,b.close,b.symbol')
|
||||||
|
->join('app_rate b', 'a.coin_id = b.id', 'left')
|
||||||
|
->join('fa_user u', 'a.user_id = u.id')
|
||||||
|
->where($where)
|
||||||
|
->select();
|
||||||
|
|
||||||
|
if (empty($contracts)) {
|
||||||
|
$this->error('没有持仓中的订单');
|
||||||
|
}
|
||||||
|
|
||||||
|
$shizhi = Config::get('site.shizhi');
|
||||||
|
$success_count = 0;
|
||||||
|
$fail_count = 0;
|
||||||
|
|
||||||
|
foreach ($contracts as $contract) {
|
||||||
|
Db::startTrans();
|
||||||
|
try {
|
||||||
|
$pc_price = $contract['close'];
|
||||||
|
$diffnum = $pc_price - $contract['price'];
|
||||||
|
|
||||||
|
// 计算收益
|
||||||
|
if ($contract['type'] == 'more') {
|
||||||
|
$income = $diffnum * $contract['num'] * $shizhi;
|
||||||
|
} else {
|
||||||
|
if ($diffnum < 0) {
|
||||||
|
$income = abs($diffnum) * $contract['num'] * $shizhi;
|
||||||
|
} else {
|
||||||
|
$income = 0 - $diffnum * $contract['num'] * $shizhi;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$income = sprintf('%.6f', $income);
|
||||||
|
|
||||||
|
// 获取用户钱包
|
||||||
|
$currency = Db::name('app_currency_user')
|
||||||
|
->field('num,id')
|
||||||
|
->where('user_id', $contract['user_id'])
|
||||||
|
->where('curr_id', 1)->find();
|
||||||
|
|
||||||
|
if (!$currency) {
|
||||||
|
$fail_count++;
|
||||||
|
Db::rollback();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 记录资金明细
|
||||||
|
$detailed = [
|
||||||
|
'user_id' => $contract['user_id'],
|
||||||
|
'curr_id' => 1,
|
||||||
|
'price' => abs($income),
|
||||||
|
'cart' => $income > 0 ? '1' : '2',
|
||||||
|
'type' => '6',
|
||||||
|
'serial_no' => time() . mt_rand(1000, 9999),
|
||||||
|
'order_result' => 'result',
|
||||||
|
'description' => $income > 0 ? '合约交易平仓盈利' : '合约交易平仓亏损',
|
||||||
|
'createtime' => time(),
|
||||||
|
'notice' => '管理员一键平仓',
|
||||||
|
'before_num' => $currency['num'],
|
||||||
|
'after_num' => $currency['num'] + $income,
|
||||||
|
];
|
||||||
|
|
||||||
|
$detaileds = [$detailed];
|
||||||
|
$detaileds[] = [
|
||||||
|
'user_id' => $contract['user_id'],
|
||||||
|
'curr_id' => 1,
|
||||||
|
'price' => $contract['promise_fee'],
|
||||||
|
'cart' => '1',
|
||||||
|
'type' => '6',
|
||||||
|
'serial_no' => time() . mt_rand(1000, 9999),
|
||||||
|
'order_result' => 'result',
|
||||||
|
'description' => '合约交易保证金退还',
|
||||||
|
'createtime' => time(),
|
||||||
|
'notice' => '管理员一键平仓',
|
||||||
|
'before_num' => $detailed['after_num'],
|
||||||
|
'after_num' => $detailed['after_num'] + $contract['promise_fee'],
|
||||||
|
];
|
||||||
|
|
||||||
|
$lostnum = sprintf("%.6f", ($currency['num'] + $income + $contract['promise_fee']));
|
||||||
|
|
||||||
|
// 更新钱包余额
|
||||||
|
Db::name('app_currency_user')
|
||||||
|
->where('id', $currency['id'])
|
||||||
|
->update(['num' => $lostnum, 'updatetime' => time()]);
|
||||||
|
|
||||||
|
// 更新订单状态
|
||||||
|
Db::name('app_contract')
|
||||||
|
->where('id', $contract['id'])
|
||||||
|
->update(['status' => '2', 'pc_price' => $pc_price, 'pctime' => time(), 'income' => $income]);
|
||||||
|
|
||||||
|
// 写入资金明细
|
||||||
|
Db::name('app_detailed')->insertAll($detaileds);
|
||||||
|
|
||||||
|
Db::commit();
|
||||||
|
$success_count++;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Db::rollback();
|
||||||
|
$fail_count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->success("一键平仓完成:成功 {$success_count} 笔,失败 {$fail_count} 笔");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function import()
|
||||||
|
{
|
||||||
|
parent::import();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||||||
|
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||||||
|
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查看
|
||||||
|
*/
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
//设置过滤方法
|
||||||
|
$this->request->filter(['strip_tags']);
|
||||||
|
if ($this->request->isAjax())
|
||||||
|
{
|
||||||
|
//如果发送的来源是Selectpage,则转发到Selectpage
|
||||||
|
if ($this->request->request('keyField'))
|
||||||
|
{
|
||||||
|
return $this->selectpage();
|
||||||
|
}
|
||||||
|
// list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||||
|
$offset = $this->request->get("offset", 0);
|
||||||
|
$limit = $this->request->get("limit", 0);
|
||||||
|
$params = json_decode(input('filter'),true);
|
||||||
|
$op = json_decode(input('op'),true);
|
||||||
|
if(count($params) > 0){
|
||||||
|
$new_params = $new_op = [];
|
||||||
|
foreach ($params as $key => $value) {
|
||||||
|
if($key == 'mobile')
|
||||||
|
{
|
||||||
|
$new_params['u.' . $key] = $value;
|
||||||
|
$new_op['u.' . $key] = $op[$key];
|
||||||
|
}else if($key == 'user_name'){
|
||||||
|
$new_params['u.nickname'] = $value;
|
||||||
|
$new_op['u.nickname'] = $op[$key];
|
||||||
|
}elseif ($key == 'daili_name') {
|
||||||
|
$new_params['e.nickname'] = $value;
|
||||||
|
$new_op['e.nickname'] = $op[$key];
|
||||||
|
}else {
|
||||||
|
$new_params['a.' . $key] = $value;
|
||||||
|
$new_op['a.' . $key] = $op[$key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$w = $this->rewriteQuery($new_params, $new_op);
|
||||||
|
}else{
|
||||||
|
$w['a.id'] = array('>', 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
$admin_id = $this->auth->id;
|
||||||
|
if($admin_id > 1){
|
||||||
|
$user = Db::name("user")->where("admin_id",$admin_id)->find();
|
||||||
|
if($user){
|
||||||
|
$w['u.path'] = array("like",$user['path']."%");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$total = Db::name('app_contract')
|
||||||
|
->alias('a')
|
||||||
|
->field('a.*,u.nickname as user_name,e.nickname as daili_name')
|
||||||
|
->join('fa_user u', 'a.user_id = u.id')
|
||||||
|
->join('admin e', 'u.p_admin_id = e.id' , 'left')
|
||||||
|
->where($w)
|
||||||
|
->count();
|
||||||
|
$list = Db::name('app_contract')
|
||||||
|
->alias('a')
|
||||||
|
->field('a.*,u.nickname as user_name,b.coinname,b.symbol,b.close,e.nickname as daili_name')
|
||||||
|
->join('app_rate b','a.coin_id = b.id','left')
|
||||||
|
->join('fa_user u', 'a.user_id = u.id')
|
||||||
|
->join('admin e', 'u.p_admin_id = e.id' , 'left')
|
||||||
|
->where($w)
|
||||||
|
->order('a.id', 'desc')
|
||||||
|
->limit($offset, $limit)
|
||||||
|
->select();
|
||||||
|
foreach ($list as $key=>$value)
|
||||||
|
{
|
||||||
|
if($value['status'] == '1') {
|
||||||
|
$diffnum = $value['close'] - $value['price'];
|
||||||
|
if ($value['type'] == 'more') {
|
||||||
|
//计算收益
|
||||||
|
$income = $diffnum / 100 * $value['num'] * $value['multiple'] * Config::get('site.shizhi');
|
||||||
|
} else {
|
||||||
|
if ($diffnum < 0) {
|
||||||
|
$income = abs($diffnum) / 100 * $value['num'] * $value['multiple'] * Config::get('site.shizhi');
|
||||||
|
} else {
|
||||||
|
$income = 0 - $diffnum / 100 * $value['num'] * $value['multiple'] * Config::get('site.shizhi');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$value['income'] = sprintf('%.6f', $income);
|
||||||
|
$list[$key] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = array("total" => $total, "rows" => $list);
|
||||||
|
|
||||||
|
return json($result);
|
||||||
|
}
|
||||||
|
return $this->view->fetch();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+46
@@ -0,0 +1,46 @@
|
|||||||
|
<div class="panel panel-default panel-intro">
|
||||||
|
|
||||||
|
<div class="panel-heading">
|
||||||
|
{:build_heading(null,FALSE)}
|
||||||
|
<ul class="nav nav-tabs" data-field="status">
|
||||||
|
<li class="{:$Think.get.status === null ? 'active' : ''}"><a href="#t-all" data-value="" data-toggle="tab">{:__('All')}</a></li>
|
||||||
|
{foreach name="statusList" item="vo"}
|
||||||
|
<li class="{:$Think.get.status === (string)$key ? 'active' : ''}"><a href="#t-{$key}" data-value="{$key}" data-toggle="tab">{$vo}</a></li>
|
||||||
|
{/foreach}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<div class="panel-body">
|
||||||
|
<div id="myTabContent" class="tab-content">
|
||||||
|
<div class="tab-pane fade active in" id="one">
|
||||||
|
<div class="widget-body no-padding">
|
||||||
|
<div id="toolbar" class="toolbar">
|
||||||
|
<a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
|
||||||
|
<a href="javascript:;" class="btn btn-success btn-add {:$auth->check('app/app_contract/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>
|
||||||
|
<a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('app/app_contract/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a>
|
||||||
|
<a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('app/app_contract/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>
|
||||||
|
<a href="javascript:;" class="btn btn-warning btn-closeall {:$auth->check('app/app_contract/closeall')?'':'hide'}" title="一键平仓" ><i class="fa fa-gavel"></i> 一键平仓</a>
|
||||||
|
<!-- <a href="javascript:;" class="btn btn-danger btn-import {:$auth->check('app/app_contract/import')?'':'hide'}" title="{:__('Import')}" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="fa fa-upload"></i> {:__('Import')}</a>
|
||||||
|
|
||||||
|
<div class="dropdown btn-group {:$auth->check('app/app_contract/multi')?'':'hide'}">
|
||||||
|
<a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>
|
||||||
|
<ul class="dropdown-menu text-left" role="menu">
|
||||||
|
<li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=normal"><i class="fa fa-eye"></i> {:__('Set to normal')}</a></li>
|
||||||
|
<li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=hidden"><i class="fa fa-eye-slash"></i> {:__('Set to hidden')}</a></li>
|
||||||
|
</ul>
|
||||||
|
</div>-->
|
||||||
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<table id="table" class="table table-striped table-bordered table-hover table-nowrap"
|
||||||
|
data-operate-edit="{:$auth->check('app/app_contract/edit')}"
|
||||||
|
data-operate-del="{:$auth->check('app/app_contract/del')}"
|
||||||
|
width="100%">
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
Executable
+95
@@ -0,0 +1,95 @@
|
|||||||
|
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
|
||||||
|
|
||||||
|
var Controller = {
|
||||||
|
index: function () {
|
||||||
|
// 初始化表格参数配置
|
||||||
|
Table.api.init({
|
||||||
|
extend: {
|
||||||
|
index_url: 'app/app_contract/index' + location.search,
|
||||||
|
add_url: 'app/app_contract/add',
|
||||||
|
edit_url: 'app/app_contract/edit',
|
||||||
|
del_url: 'app/app_contract/del',
|
||||||
|
// multi_url: 'app/app_contract/multi',
|
||||||
|
// import_url: 'app/app_contract/import',
|
||||||
|
table: 'app_contract',
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var table = $("#table");
|
||||||
|
|
||||||
|
// 初始化表格
|
||||||
|
table.bootstrapTable({
|
||||||
|
url: $.fn.bootstrapTable.defaults.extend.index_url,
|
||||||
|
pk: 'id',
|
||||||
|
sortName: 'id',
|
||||||
|
columns: [
|
||||||
|
[
|
||||||
|
{checkbox: true},
|
||||||
|
{field: 'id', title: __('Id')},
|
||||||
|
{field: 'daili_name', title: __('所属代理'), operate: 'LIKE'},
|
||||||
|
{field: 'user_id', title: __('用户ID')},
|
||||||
|
{field: 'user_name', title: __('用户')},
|
||||||
|
{field: 'coin_id', title: __('Coin_id')},
|
||||||
|
{field: 'symbol', title: __('Symbol'), operate: 'LIKE'},
|
||||||
|
{field: 'type', title: __('Type'), searchList: {"more":__('Type more'),"free":__('Type free')}, formatter: Table.api.formatter.normal},
|
||||||
|
{field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
|
||||||
|
{field: 'price', title: __('Price'), operate:'BETWEEN'},
|
||||||
|
{field: 'pc_price', title: __('Pc_price'), operate:'BETWEEN'},
|
||||||
|
{field: 'num', title: __('Num')},
|
||||||
|
{field: 'multiple', title: __('Multiple')},
|
||||||
|
{field: 'promise_fee', title: __('Promise_fee'), operate:'BETWEEN'},
|
||||||
|
{field: 'fee', title: __('Fee'), operate:'BETWEEN'},
|
||||||
|
{field: 'zy_price', title: __('Zy_price'), operate:'BETWEEN'},
|
||||||
|
{field: 'zs_price', title: __('Zs_price'), operate:'BETWEEN'},
|
||||||
|
{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
|
||||||
|
{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
|
||||||
|
{field: 'pctime', title: __('Pctime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
|
||||||
|
{field: 'income', title: __('Income'), operate:'BETWEEN'},
|
||||||
|
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
// 为表格绑定事件
|
||||||
|
Table.api.bindevent(table);
|
||||||
|
|
||||||
|
// 一键平仓按钮事件
|
||||||
|
$(document).on('click', '.btn-closeall', function () {
|
||||||
|
Layer.confirm('确定要一键平仓所有持仓中的合约订单吗?此操作不可撤销!', {
|
||||||
|
icon: 0,
|
||||||
|
title: '一键平仓确认'
|
||||||
|
}, function (index) {
|
||||||
|
Layer.close(index);
|
||||||
|
$.ajax({
|
||||||
|
url: 'app/app_contract/closeall',
|
||||||
|
type: 'POST',
|
||||||
|
dataType: 'json',
|
||||||
|
success: function (ret) {
|
||||||
|
if (ret.code === 1) {
|
||||||
|
Layer.alert(ret.msg, {icon: 1});
|
||||||
|
table.bootstrapTable('refresh');
|
||||||
|
} else {
|
||||||
|
Layer.alert(ret.msg, {icon: 2});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function () {
|
||||||
|
Layer.alert('请求失败', {icon: 2});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
add: function () {
|
||||||
|
Controller.api.bindevent();
|
||||||
|
},
|
||||||
|
edit: function () {
|
||||||
|
Controller.api.bindevent();
|
||||||
|
},
|
||||||
|
api: {
|
||||||
|
bindevent: function () {
|
||||||
|
Form.api.bindevent($("form[role=form]"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return Controller;
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user