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.
162 lines
5.1 KiB
162 lines
5.1 KiB
<?php
|
|
namespace app\model;
|
|
/**
|
|
* @property int trade_id
|
|
* @property int order_id
|
|
* @property int user_id
|
|
* @property string digital_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 DigitalTradeModel extends BaseModel
|
|
{
|
|
protected $name = 'digital_trade';
|
|
protected $pk = 'trade_id';
|
|
|
|
const STATUS_PLACE = 0; // 挂单状态
|
|
const STATUS_BACk = 2; // 挂单状态
|
|
const STATUS_SUCCESS = 3; // 成功状态
|
|
|
|
const TRADE_TYPE_BUY = 1;
|
|
const TRADE_TYPE_SELL = 2;
|
|
/**
|
|
*获取持仓成本
|
|
*/
|
|
public static function getTradeOrderBuyNum(int $user_id)
|
|
{
|
|
$num=0;
|
|
//查询持仓订单总成本
|
|
$list=self::where([
|
|
'user_id'=>$user_id,
|
|
'status'=>1
|
|
])->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'];
|
|
$num+=$order_num;
|
|
}
|
|
}
|
|
return $num;
|
|
}
|
|
|
|
/**
|
|
* 买入成本
|
|
*/
|
|
public static function getTradeOrderPrice(int $user_id,string $digital_id,int $trade_type)
|
|
{
|
|
$num=0;
|
|
$buy_num=0;
|
|
//查询持仓订单总成本
|
|
$list=self::where([
|
|
'user_id'=>$user_id,
|
|
'status'=>3,
|
|
'digital_id'=>$digital_id,
|
|
'trade_type'=>$trade_type
|
|
])->field('order_number,closing_price,service_cost,closing_cost')->select();
|
|
if(!empty($list)) {
|
|
$list_arr=$list->toArray();
|
|
foreach ($list_arr as $val){
|
|
$order_num=$val['order_number']*$val['closing_price'] + $val['service_cost'];
|
|
$num+=$order_num;
|
|
$buy_num+=$val['order_number'];
|
|
}
|
|
}
|
|
return [
|
|
'order_money'=>$num, //订单总额
|
|
'order_num'=>$buy_num //订单数量
|
|
];
|
|
}
|
|
public static function getTradeOrderFee(int $user_id)
|
|
{
|
|
$info=self::where([
|
|
['user_id','=',$user_id],
|
|
['status','in',[1,3]]
|
|
])->sum('closing_cost');
|
|
return $info;
|
|
}
|
|
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')->select();
|
|
if(!empty($list)) {
|
|
$list_arr=$list->toArray();
|
|
foreach ($list_arr as $val){
|
|
$order_num=$val['order_number']*($val['closing_price']-$val['deal_price'])-$val['closing_cost']-$val['service_cost'];
|
|
$num+=$order_num;
|
|
}
|
|
}
|
|
return $num;
|
|
}
|
|
public static function getTradeList($trade_name,$num)
|
|
{
|
|
$trade_list=[];
|
|
$list=self::where([
|
|
['status','in','1,3'],
|
|
['digital_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['closing_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['closing_price'];
|
|
$trade_data['order_time']=strtotime($val['closing_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;
|
|
}
|
|
|
|
|
|
|
|
}
|