bourse stock
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.

171 lines
5.5 KiB

2 months ago
<?php
namespace app\model;
/**
* @property int trade_id
* @property int order_id
* @property int user_id
* @property string stock_id
* @property int trade_type
* @property int deal_type
* @property int limit_price
* @property int market_price
* @property int deal_price
* @property int closing_price
* @property int order_number
* @property int stop_type
* @property int stop_loss_price
* @property int stop_win_price
* @property int service_cost
* @property int market_money
* @property int order_money
* @property int status
* @property string create_time
* @property string update_time
* @property string open_time
* @property string closing_time
*/
class StockOptionInrTradeModel extends BaseModel
{
protected $name = 'stock_option_inr_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 $stock_id,int $trade_type)
{
$num=0;
$money=0;
//查询持仓订单总成本
$list=self::where([
'user_id'=>$user_id,
'status'=>1,
'stock_id'=>$stock_id,
'trade_type'=>$trade_type
])->field('order_number,deal_price,service_cost')->select();
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,
'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 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 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')->select();
if(!empty($list)) {
$list_arr=$list->toArray();
foreach ($list_arr as $val){
if($val['trade_type']==1){
$order_num=$val['order_number']*($val['closing_price']-$val['deal_price']);
}else{
$order_num=$val['order_number']*($val['deal_price']-$val['closing_price']);
}
$num+=$order_num;
}
}
return $num;
}
public static function getTradeList($trade_name,$num)
{
$trade_list=[];
$list=self::where([
['status','in','1,3'],
['stock_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;
}
// 获取 挂单 持仓的订单金额 股票价格 * 股数 之和 也就是 market_money 的和
public static function getAmountForMargin($user_id)
{
$trade = self::where('status', '=', '1')->where('user_id', $user_id)->field('sum(deal_price * order_number) as amount')->find();
if(empty($trade)){
return 0;
}
$trade = $trade->toArray();
return $trade['amount']?? 0;
}
public static function getRealFee($userId)
{
return self::where('user_id', $userId)->sum('service_cost');
}
}