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.
116 lines
3.5 KiB
116 lines
3.5 KiB
3 months ago
|
<?php
|
||
|
namespace app\model;
|
||
|
/**
|
||
|
* @property int id
|
||
|
* @property int user_id
|
||
|
* @property int contract_id
|
||
|
* @property float usable_num
|
||
|
* @property float frozen_num
|
||
|
* @property string create_time
|
||
|
* @property string update_time
|
||
|
*/
|
||
|
class UserForexModel extends BaseModel
|
||
|
{
|
||
|
protected $name = 'user_forex';
|
||
|
protected $pk = 'id';
|
||
|
|
||
|
public static function getUserForexByUserId(array $data):array
|
||
|
{
|
||
|
if(!empty($data['trade_name'])){
|
||
|
$contract_id=strtoupper($data['trade_name']);
|
||
|
}else{
|
||
|
$contract_id='USD';
|
||
|
}
|
||
|
$info = self::where([
|
||
|
'user_id'=>$data['user_id'],
|
||
|
'contract_id'=>$contract_id
|
||
|
])->field('usable_num,frozen_num,contract_id as name')->find();
|
||
|
if(empty($info)){
|
||
|
return [
|
||
|
'usable_num'=>0,
|
||
|
'frozen_num'=>0,
|
||
|
'name'=>$contract_id
|
||
|
];
|
||
|
}
|
||
|
return $info->toArray();
|
||
|
}
|
||
|
public static function getUserForexLock(int $user_id,string $contract_id='USD'):array
|
||
|
{
|
||
|
$info = self::where([
|
||
|
'user_id'=>$user_id,
|
||
|
'contract_id'=>$contract_id
|
||
|
])->lock(true)->find();
|
||
|
if(empty($info)){
|
||
|
$info = new self;
|
||
|
$info->user_id = $user_id;
|
||
|
$info->contract_id = $contract_id;
|
||
|
$info->usable_num = 0;
|
||
|
$info->frozen_num = 0;
|
||
|
$info->create_time = date('Y-m-d H:i:s');
|
||
|
$info->update_time = date('Y-m-d H:i:s');
|
||
|
$info->save();
|
||
|
}
|
||
|
return $info->toArray();
|
||
|
}
|
||
|
public static function getUserForexList(int $user_id):array
|
||
|
{
|
||
|
$list = self::where([
|
||
|
'user_id'=>$user_id,
|
||
|
])->field('usable_num,frozen_num,contract_id as name')->select();
|
||
|
if(empty($list)){
|
||
|
return [];
|
||
|
}
|
||
|
foreach ($list as $key=>$val){
|
||
|
if($val['name']=='USD'){
|
||
|
$list[$key]['buy_up']=[
|
||
|
'buy_num'=>0,
|
||
|
'buy_money'=>0
|
||
|
];
|
||
|
$list[$key]['buy_down']=[
|
||
|
'buy_num'=>0,
|
||
|
'buy_money'=>0
|
||
|
];
|
||
|
}else{
|
||
|
//获取持仓成本
|
||
|
$list[$key]['buy_up']=ContractTradeModel::getTradeOrderBuyNum($user_id,$val['name'],1);
|
||
|
$list[$key]['buy_down']=ContractTradeModel::getTradeOrderBuyNum($user_id,$val['name'],2);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
//获取平仓累计盈亏
|
||
|
$daily_num=ContractTradeModel::getTradeOrderDailyNum($user_id);
|
||
|
$earnest_money=ContractTradeModel::getUserEarnestMmoney($user_id);
|
||
|
$fee_num=ContractTradeModel::getTradeOrderFee($user_id);
|
||
|
return [
|
||
|
'earnest_money'=>$earnest_money,//保证金
|
||
|
'daily_num'=>$daily_num,//累计盈亏
|
||
|
'fee_num'=>$fee_num,//累计盈亏
|
||
|
'list'=>$list->toArray()
|
||
|
];
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
public static function InsertUserForex(array $data)
|
||
|
{
|
||
|
$self = new self;
|
||
|
$self->user_id = $data['user_id'];
|
||
|
$self->contract_id = $data['contract_id'];
|
||
|
$self->usable_num = $data['usable_num'];
|
||
|
$self->frozen_num = $data['frozen_num'];
|
||
|
$self->create_time = date('Y-m-d H:i:s');
|
||
|
$self->save();
|
||
|
}
|
||
|
public static function updateUserForex(array $update_data,array $where)
|
||
|
{
|
||
|
$update_data['update_time']=date('Y-m-d H:i:s');
|
||
|
$res=self::where($where)->save($update_data);
|
||
|
//echo self::where($where)->getLastSql();
|
||
|
return $res;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
}
|