2 changed files with 159 additions and 14 deletions
@ -0,0 +1,144 @@ |
|||
<?php |
|||
namespace app\model; |
|||
|
|||
|
|||
class MoneyTradeModel extends BaseModel |
|||
{ |
|||
protected $name = 'money_trade'; |
|||
protected $pk = 'trade_id'; |
|||
|
|||
const STATUS_HOLD = 1;// 持仓 |
|||
const STATUS_PLACE = 0;// 挂单 |
|||
const STATUS_BACK = 2;// 撤单 |
|||
const STATUS_CLEAR = 3;// 平仓 |
|||
|
|||
|
|||
/** |
|||
*获取持仓成本 |
|||
*/ |
|||
public static function getTradeOrderBuyNum(int $user_id,string $contract_id,int $trade_type) |
|||
{ |
|||
$num=0; |
|||
$money=0; |
|||
//查询持仓订单总成本 |
|||
$list=self::where([ |
|||
'user_id'=>$user_id, |
|||
'status'=>1, |
|||
'contract_id'=>$contract_id, |
|||
'trade_type'=>$trade_type |
|||
])->field('order_number,deal_price,service_cost')->select(); |
|||
$con_list=ContractListMode::getMarketFaceList(1); |
|||
if(!empty($list)) { |
|||
$list_arr=$list->toArray(); |
|||
foreach ($list_arr as $val){ |
|||
$order_num=$val['order_number']*$val['deal_price'] ;//+ $val['service_cost']; |
|||
$money+=$order_num; |
|||
$num+=$val['order_number']; |
|||
} |
|||
} |
|||
return [ |
|||
'buy_num'=>$num, |
|||
'face_value'=>isset($con_list[$contract_id]) ? $con_list[$contract_id]:0, |
|||
'buy_money'=>$money |
|||
]; |
|||
} |
|||
public static function getUserEarnestMmoney(int $user_id) |
|||
{ |
|||
$num=self::where([ |
|||
'user_id'=>$user_id, |
|||
'status'=>1 |
|||
])->sum('earnest_money'); |
|||
return $num >0 ? $num:0; |
|||
} |
|||
public static function getUserTradeNum(int $user_id) |
|||
{ |
|||
//查询持仓订单总成本 |
|||
$count=self::where([ |
|||
['user_id','=',$user_id], |
|||
['status','>',0] |
|||
])->count(); |
|||
return $count>0; |
|||
} |
|||
|
|||
/** |
|||
* 获取累计盈亏 |
|||
*/ |
|||
public static function getTradeOrderDailyNum(int $user_id) |
|||
{ |
|||
$num=0; |
|||
//查询持仓订单总成本 |
|||
$list=self::where([ |
|||
'user_id'=>$user_id, |
|||
'status'=>3 |
|||
])->field('order_number,closing_price,deal_price,closing_cost,service_cost,trade_type,contract_id,face_value')->select(); |
|||
|
|||
//$con_list=ContractListMode::getMarketFaceList(1); |
|||
if(!empty($list)) { |
|||
$list_arr=$list->toArray(); |
|||
foreach ($list_arr as $val){ |
|||
if(empty($val['face_value'])){ |
|||
$face_value=1; |
|||
}else{ |
|||
$face_value=$val['face_value']; |
|||
} |
|||
if($val['trade_type']==1){ |
|||
//$order_num=$val['order_number']*($val['closing_price']-$val['deal_price'])-$val['closing_cost']-$val['service_cost']; |
|||
$order_num=$val['order_number']*($val['closing_price']-$val['deal_price'])*$face_value; |
|||
}else{ |
|||
//$order_num=$val['order_number']*($val['deal_price']-$val['closing_price'])-$val['closing_cost']-$val['service_cost']; |
|||
$order_num=$val['order_number']*($val['deal_price']-$val['closing_price'])*$face_value; |
|||
} |
|||
$num+=$order_num; |
|||
} |
|||
} |
|||
return $num; |
|||
} |
|||
public static function getTradeOrderFee(int $user_id) |
|||
{ |
|||
$info=self::where([ |
|||
['user_id','=',$user_id], |
|||
['status','in',[1,3]] |
|||
])->field('sum(service_cost+closing_cost) as fee')->find(); |
|||
if(empty($info)){ |
|||
return 0; |
|||
}else{ |
|||
$info=$info->toArray(); |
|||
return empty($info['fee']) ? 0 : $info['fee']; |
|||
} |
|||
} |
|||
public static function getTradeList($trade_name,$num) |
|||
{ |
|||
$trade_list=[]; |
|||
$list=self::where([ |
|||
['status','in','1,3'], |
|||
['contract_id','=',strtoupper($trade_name)] |
|||
])->field('deal_price,order_number,closing_price,open_time,closing_time,status')->order('trade_id desc')->limit($num)->select(); |
|||
if(!empty($list)) { |
|||
$list_arr=$list->toArray(); |
|||
foreach ($list_arr as $val){ |
|||
if($val['status']==1){ |
|||
$trade_data['order_number']=$val['order_number']; |
|||
$trade_data['deal_price']=$val['deal_price']; |
|||
$trade_data['order_time']=strtotime($val['open_time'])*1000; |
|||
$trade_data['trade_type']=1; |
|||
array_push($trade_list,$trade_data); |
|||
} |
|||
if($val['status']==3){ |
|||
$trade_data['order_number']=$val['order_number']; |
|||
$trade_data['deal_price']=$val['deal_price']; |
|||
$trade_data['order_time']=strtotime($val['open_time'])*1000; |
|||
$trade_data['trade_type']=1; |
|||
array_push($trade_list,$trade_data); |
|||
|
|||
$sale_data['order_number']=$val['order_number']; |
|||
$sale_data['deal_price']=$val['closing_price']; |
|||
$sale_data['order_time']=strtotime($val['closing_time'])*1000; |
|||
$sale_data['trade_type']=2; |
|||
array_push($trade_list,$sale_data); |
|||
|
|||
} |
|||
} |
|||
} |
|||
return $trade_list; |
|||
} |
|||
} |
Loading…
Reference in new issue