93 lines
4.0 KiB
PHP
93 lines
4.0 KiB
PHP
<?php
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\OptionPair;
|
|
use App\OptionOrder;
|
|
use App\UsersWallet;
|
|
use App\AccountLog;
|
|
use App\CurrencyMatch;
|
|
use App\Currency;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class OptionController extends Controller
|
|
{
|
|
public function orderIndex() { return view('manages.option.orders'); }
|
|
|
|
public function orderList(Request $request)
|
|
{
|
|
$query = OptionOrder::query()
|
|
->leftJoin('users', 'option_order.user_id', '=', 'users.id')
|
|
->leftJoin('option_pair', 'option_order.pair_id', '=', 'option_pair.id')
|
|
->select('option_order.*', 'users.phone as user_phone', 'users.email as user_email', 'option_pair.coinname');
|
|
|
|
$keyword = $request->input('keyword');
|
|
if ($keyword) {
|
|
$query->where(function ($q) use ($keyword) {
|
|
$q->where('option_order.user_id', $keyword)
|
|
->orWhere('users.email', 'like', "%{$keyword}%")
|
|
->orWhere('users.phone', 'like', "%{$keyword}%");
|
|
});
|
|
}
|
|
$status = $request->input('status');
|
|
if ($status !== null && $status !== '') {
|
|
$query->where('option_order.status', intval($status));
|
|
}
|
|
|
|
$data = $query->orderBy('option_order.id', 'desc')->paginate($request->input('limit', 20));
|
|
$statusMap = ['0'=>'待交割','1'=>'盈利','2'=>'亏损','3'=>'已取消'];
|
|
foreach ($data as $item) {
|
|
$item->status_text = $statusMap[$item->status] ?? '';
|
|
$item->direction_text = $item->direction == 'up' ? '买涨' : '买跌';
|
|
}
|
|
return $this->layuiData($data);
|
|
}
|
|
|
|
public function settle(Request $request)
|
|
{
|
|
$ids = $request->input('ids');
|
|
$force = $request->input('force', '');
|
|
if (!$ids) return $this->error('请先选择订单');
|
|
$orders = OptionOrder::whereIn('id', explode(',', $ids))->where('status', 0)->get();
|
|
if ($orders->isEmpty()) return $this->error('没有待结算订单');
|
|
|
|
$settled = 0;
|
|
DB::beginTransaction();
|
|
try {
|
|
foreach ($orders as $order) {
|
|
$pair = OptionPair::find($order->pair_id);
|
|
$match = $pair ? CurrencyMatch::find($pair->currency_match_id) : null;
|
|
$currency = $match ? Currency::find($match->currency_id) : null;
|
|
$close_price = $currency ? floatval($currency->price) : 0;
|
|
|
|
if ($force === 'win') $win = true;
|
|
elseif ($force === 'lose') $win = false;
|
|
else $win = $order->direction == 'up' ? $close_price > $order->open_price : $close_price < $order->open_price;
|
|
|
|
$income = $win ? sprintf("%.6f", $order->amount * $order->profit_rate / 100) : -$order->amount;
|
|
$order->update(['close_price' => $close_price, 'income' => $income, 'status' => $win ? 1 : 2]);
|
|
|
|
$wallet = UsersWallet::where('user_id', $order->user_id)->where('currency', 3)->first();
|
|
if ($wallet) {
|
|
$return_amount = $win ? $order->amount + abs($income) : 0;
|
|
if ($return_amount > 0) {
|
|
$before = $wallet->legal_balance;
|
|
$wallet->legal_balance = bc_add($wallet->legal_balance, $return_amount, 6);
|
|
$wallet->save();
|
|
AccountLog::insertLog(
|
|
['user_id'=>$order->user_id,'value'=>$return_amount,'info'=>'Option '.($win?'Win':'Refund').' - '.$order->symbol,'type'=>AccountLog::MICRO_TRADE_CLOSE_SETTLE,'currency'=>3],
|
|
['balance_type'=>1,'wallet_id'=>$wallet->id,'lock_type'=>0,'before'=>$before,'change'=>$return_amount,'after'=>$wallet->legal_balance]
|
|
);
|
|
}
|
|
}
|
|
$settled++;
|
|
}
|
|
DB::commit();
|
|
return $this->success("已结算 {$settled} 个订单");
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
}
|