Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
stock_new
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sugar
stock_new
Commits
34ed11aa
Commit
34ed11aa
authored
Jun 19, 2024
by
twj
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'dev' of
http://rungit.jxdsy.cn:10000/sugar/stock_new
into dev
parents
f4afec72
a732594e
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
14 additions
and
286 deletions
+14
-286
application/function.php
application/function.php
+2
-2
application/market/home/Index.php
application/market/home/Index.php
+2
-1
application/market/home/Trade.php
application/market/home/Trade.php
+1
-1
application/market/model/Deal_stock.php
application/market/model/Deal_stock.php
+2
-1
application/market/model/Delivery.php
application/market/model/Delivery.php
+2
-1
application/market/model/StockPosition.php
application/market/model/StockPosition.php
+0
-277
application/market/model/Trust.php
application/market/model/Trust.php
+4
-2
application/market/validate/Trade.php
application/market/validate/Trade.php
+1
-1
No files found.
application/function.php
View file @
34ed11aa
...
@@ -247,9 +247,9 @@ if (!function_exists('getEndDay')) {
...
@@ -247,9 +247,9 @@ if (!function_exists('getEndDay')) {
$res
=
\think\Db
::
name
(
'admin_config'
)
->
where
(
array
(
'name'
=>
'market_data_in'
))
->
value
(
'value'
);
$res
=
\think\Db
::
name
(
'admin_config'
)
->
where
(
array
(
'name'
=>
'market_data_in'
))
->
value
(
'value'
);
//判断股票前缀
//判断股票前缀
if
(
toMarket
(
$code
)){
/*
if(toMarket($code)){
$market = toMarket($code);
$market = toMarket($code);
};
};
*/
$code
=
strtolower
(
$market
)
.
$code
;
$code
=
strtolower
(
$market
)
.
$code
;
//判断股票是否
//判断股票是否
...
...
application/market/home/Index.php
View file @
34ed11aa
...
@@ -58,7 +58,7 @@ class Index extends Common
...
@@ -58,7 +58,7 @@ class Index extends Common
$map
[
'title'
]
=
[
'like'
,
"%
$key
%"
];
$map
[
'title'
]
=
[
'like'
,
"%
$key
%"
];
}
}
$map
[
'status'
]
=
1
;
$map
[
'status'
]
=
1
;
$data
=
Db
::
name
(
'stock_list'
)
->
field
(
'code,title,pinyin'
)
->
where
(
$map
)
->
limit
(
10
)
->
select
();
$data
=
Db
::
name
(
'stock_list'
)
->
field
(
'code,title,pinyin
,market
'
)
->
where
(
$map
)
->
limit
(
10
)
->
select
();
if
(
!
$data
)
{
if
(
!
$data
)
{
return
json
([
'data'
=>
$data
,
'status'
=>
0
,
'message'
=>
'操作失败'
]);
return
json
([
'data'
=>
$data
,
'status'
=>
0
,
'message'
=>
'操作失败'
]);
...
@@ -69,6 +69,7 @@ class Index extends Common
...
@@ -69,6 +69,7 @@ class Index extends Common
$res
[
$k
][
'code'
]
=
$v
[
'code'
];
$res
[
$k
][
'code'
]
=
$v
[
'code'
];
$res
[
$k
][
'name'
]
=
$v
[
'title'
];
$res
[
$k
][
'name'
]
=
$v
[
'title'
];
$res
[
$k
][
'pin'
]
=
$v
[
'pinyin'
];
$res
[
$k
][
'pin'
]
=
$v
[
'pinyin'
];
$res
[
$k
][
'market'
]
=
$v
[
'market'
];
}
}
}
}
return
json
([
'data'
=>
$res
,
'status'
=>
1
,
'message'
=>
'操作成功'
]);
return
json
([
'data'
=>
$res
,
'status'
=>
1
,
'message'
=>
'操作成功'
]);
...
...
application/market/home/Trade.php
View file @
34ed11aa
<?php
declare
(
strict_types
=
1
);
namespace
app\market\controller
;
use
app\market\home\Common
;
use
app\market\model\Position
;
use
app\market\model\StockSubAccount
;
use
app\market\model\SubAccountMoney
;
use
app\market\model\StockPosition
;
use
app\market\model\Delivery
;
use
app\market\model\StockSubAccountRisk
;
use
app\market\model\Trust
;
use
app\market\model\StockDealStock
;
use
app\market\model\Borrow
;
use
app\stock\model\Account
as
AccountModel
;
use
think\Log
;
use
think\Db
;
//use util\RedisUtil; class Trade extends Common { protected function _initialize(){ parent::_initialize(); $token = $this->request->param("token"); $mid = isLogin($token); } public function account_info() { $token = $this->request->param("token"); $mid = isLogin($token); if(!$mid) return json(['status' => 0, 'message' => '登陆后才能进行查看']); $subId = $this->request->param("id"); if(!$subId) return json(['status' => 0, 'message' => '缺少参数或参数获取错误']); $subModel = new SubAccountMoney(); $res = $subModel->get_account_money_inf($subId); if(!$res) return json(['status' => 0, 'message' => '操作失败']); $res['available_amount'] >= 0 ? $res['available_amount'] : 0; $res = $subModel->ftoy($res); $pos = Position::where(['sub_id'=>$subId, 'buying'=>0])->order('id desc')->select(); $res['return_money'] = 0; $res['market_value'] = 0; foreach ($pos as $k => $v ) { $data = z_market($v['gupiao_code']); if($data['Price'] == '') continue; $res['return_money'] += ($data['Price'] - $v['buy_average_price']) * $v['stock_count']; $res['market_value'] += $data['Price'] * $v['stock_count']; } // 提盈额度计算 $all = $res['market_value'] + $res['avail']; $profit = bcsub(strval($all),bcadd(strval($res['borrow_money']),strval($res['deposit_money'])),2); if($profit > 0){ $res['available_amount'] = $profit < $res['available_amount'] ? $profit : $res['available_amount']; }else{ $res['available_amount'] = 0; } //$res['available_amount'] = $res['available_amount'] > $res['avail'] ? $res['avail'] : $res['available_amount']; $res['available_amount'] = bcadd(strval($res['available_amount']),'0',2); // 去掉卖出委托的市值 $weituo = 0; $weituo = Db::name('stock_trust')->where(['sub_id'=>$res['stock_subaccount_id'],'flag2'=>'卖出委托','status'=>'已委托'])->sum('amount'); $res['return_money'] = bcadd(strval($res['return_money']),'0', 2); //加法,在原值上加0为了保留小数点后两位 $res['market_value'] = bcsub(strval($res['market_value']),strval($weituo), 2);//减法 $res['total_money'] = bcadd(strval($res['market_value']),bcadd(strval($res['avail']),strval($res['freeze_amount']),2), 2);//加法 return json(['data'=>$res,'status' => 1, 'message' => '操作成功']); } /* * 持仓查询 */ public function position() { $token = $this->request->param("token"); $mid = isLogin($token); if(!$mid) return json(['status' => 0, 'message' => '未登录']); $subid = $this->request->param("subid"); if(!$subid) return json(['status' => 0, 'message' => '参数不正确']); $submodel = new StockSubAccount(); $res = $submodel->get_account_by_id($subid); if (!$res) return json(['status' => 0, 'message' => '不存在的子账号']); if (empty($res['account_id'])) return json(['status' => 0, 'message' => '证券公司不存在']); $data = Position::where(['sub_id' => $subid,'buying' => 0])->where('stock_count','>',0)->order('id desc')->paginate(20, false, ['query' => request()->param()]); if (!$data || count($data) === 0) return ajaxmsg('没有数据',0,$data); foreach ($data as $k => $item){ //查询当天交易的数量 T+1 交易 $todayCount = Delivery::get_delivery_order($subid,$item["gupiao_code"]); $data[$k]['canbuy_count'] = $item['canbuy_count']; //查询股票最新行情 $Qdata = z_market($item["gupiao_code"],$item['market']); //查询子账户 $data[$k]['sub_account'] = $res['sub_account']; //提取当前价格 $data[$k]['now_price'] = $Qdata['Price']; //市值 = 当前价格*数量 $data[$k]['market_value'] = round((int)$Qdata['Price']*(int)$item['stock_count'],2); //参考成本价 $data[$k]['ck_price'] = Position::calculate($subid,$item["gupiao_code"],'price'); //买入均价 $data[$k]['buy_average_price'] = Position::calculate($subid,$item["gupiao_code"],'average'); //参考盈亏 //$data[$k]['ck_profit'] = $item['stock_count'] > 0 ? round(($Qdata['Price']-$data[$k]['buy_average_price'])*$item['stock_count'], 2) : 0; $data[$k]['ck_profit'] = $item['stock_count'] > 0 ? bcmul(strval((int)$Qdata["Price"]-(int)$data[$k]['buy_average_price']),strval($item['stock_count']),2) : 0;//参考浮动盈亏 //盈亏比例 //$data[$k]['profit_rate'] = $item['stock_count'] > 0 ? round(($data[$k]['ck_profit'] / ($data[$k]['buy_average_price'] * $item['stock_count'])) * 100, 2) : 0; //$data[$k]['profit_rate'] = $item['stock_count'] > 0 ? bcdiv(strval($data[$k]['ck_profit']),strval($data[$k]['buy_average_price']*$item['stock_count']*100),2) : 0;//盈亏比例 //当天可卖数量计算 //$data[$k]['canbuy_count'] = StockPosition::getCanbuyCount($subid,$item["gupiao_code"]); } return ajaxmsg('操作成功',1,$data); } /* * 证券买入 */ public function buy() { $token = $this->request->param("token"); $mid = isLogin($token); if(!$mid) return json(['status' => 0, 'message' => '登陆后才能进行查看']); if (!yan_time()) return json(['status' => 0, 'message' => '非交易时间']); if (!config('site_trade_buy')) return ajaxmsg('系统设置为禁买状态',0); $data = [ "sub_id" => input('id','', ['trim', FILTER_SANITIZE_NUMBER_INT]),//子账户ID, "code" => input('code' ,'', ['trim', FILTER_SANITIZE_NUMBER_INT]),//股票代码 "name" => input('name' ,''),//股票名称 "market" => input('market',''),//交易所代码 "count" => input('count', 0, ['trim', FILTER_SANITIZE_NUMBER_INT]),//购买数量 "price" => input('price', 0, ['trim']), 'model' => input('model', 1, ['trim', FILTER_SANITIZE_NUMBER_INT]),//1:是委托状态 ]; //验证数据 $result = $this->validate($data, 'Trade.trade'); if(!$result) return ajaxmsg($result,0); //检查其他买入条件是否符合 $Trust = new Trust; $trade_res = $Trust->executeData($data,'buy'); if(isset($trade_res['status'])&&!$trade_res['status']) return ajaxmsg($trade_res['message'],0); $trade_money = $trade_res['trade_money']; $moneyinfo = $trade_res['moneyinfo']; $price = $trade_res['price']; //计算佣金 commission_scale:佣金比例(单位:万分之几) 如:5 代表万分之五; min_commission:最低佣金(单位:元) $commission = commission($trade_money,$moneyinfo['commission_scale'],$moneyinfo['min_commission']); //计算过户费 $transfer = transfer($trade_money); //写入子账户资金变化表 Db::startTrans(); $effectMoney = $trade_money + $commission + $transfer; /*(子账户ID, 金额*100, 买卖方向[1:买入 2:卖出], 盈亏金额, 实盘相关, 股票代码)*/ $ret = SubAccountMoney::up_moneylog($data['subid'], $effectMoney, 3, 0, 0, $data['code']); if (!$ret){ Db::rollback(); return ajaxmsg('委托失败!',0); } $subres = AccountModel::getAccountById($data['sub_id']); $broker = AccountModel::getBroker($subres['account_id']); if (!$subres) return ajaxmsg('子账号或证券公司不存在!',0); $Trust_no = mt_rand(101010, 999999) . substr(strval(time()), 1); //添加到委托表 $Trust_res = $Trust->add_m_trust($data, $data['count'],$data['price'],$data['sub_id'],$broker['lid'],$broker['user'],$broker['stockjobber'],$Trust_no,$broker, $data['model']); if(!$Trust_res){ Db::rollback(); return ajaxmsg('委托失败!',0); } //持仓数量查询 $position = Db::name('stock_position')->where(['sub_id' => $data['subid'],'gupiao_code' => $data['code'],'buying' => 0])->find(); $amount = empty($position) ? $data['count'] : $position['canbuy_count'] + $data['count']; //提交交易费用信息 $Delivery = new Delivery; $avail = ($moneyinfo['avail']) - $effectMoney; //print_r("佣金: ".$commission." 过户费: ".$transfer." 总计:".$effectMoney);Db::rollback();exit; $del_res = $Delivery->add_m_delivery_order($data, $data['count'],$data['price'],$data['sub_id'],$broker['lid'],$broker['user'],$broker['stockjobber'],$commission,$transfer,$Trust_no,$avail,$amount, $data['model']); //print_r($del_res);Db::rollback();exit; if(!$del_res){ Db::rollback(); return ajaxmsg('委托失败',0); }else{ Db::commit(); return ajaxmsg('买入委托已提交',1); } } public function sell() { $token = $this->request->param("token"); $mid = isLogin($token); if(!$mid) return json(['status' => 0, 'message' => '登陆后才能进行查看']); if (!yan_time()) return json(['status' => 0, 'message' => '非交易时间']); if (config('site_trade_sell') == 0) return ajaxmsg('系统设置为不允许卖出股票',0); if (time() <= strtotime(date("Y-m-d 09:30:15"))) { return ajaxmsg('平台设置9点30分15秒后可卖出',0); } $data = [ "sub_id" => input('id','', ['trim', FILTER_SANITIZE_NUMBER_INT]),//子账户ID, "code" => input('code' ,'', ['trim', FILTER_SANITIZE_NUMBER_INT]),//股票代码 "name" => input('name' ,''),//股票名称 "market" => input('market', ''),//交易所代码 "count" => input('count', 0, ['trim', FILTER_SANITIZE_NUMBER_INT]),//购买数量 "price" => input('price', 0, ['trim']), 'model' => input('model', 1, ['trim', FILTER_SANITIZE_NUMBER_INT]),//1:是委托状态 ]; //验证数据 $result = $this->validate($data, 'Trade.trade'); if(!$result) return ajaxmsg($result,0); $Trust = new Trust; $trade_res = $Trust->executeData($data,'sell'); //print_r($trade_res['message']);exit; if(isset($trade_res['status'])&&!$trade_res['status']) return ajaxmsg($trade_res['message'],0); $trade_money = $trade_res['trade_money']; $moneyinfo = $trade_res['moneyinfo']; $price = $trade_res['price']; //计算佣金 $commission = commission($trade_money,$moneyinfo['commission_scale'],$moneyinfo['min_commission']); //印花税 $stamps = stamps($trade_money); //计算过户费 $transfer = transfer($trade_money); //写入子账户资金变化表 Db::startTrans(); $effectMoney = $trade_money - $transfer - $stamps - $commission; $ret = SubAccountMoney::up_moneylog($data['sub_id'], $effectMoney, 4); if(!$ret){ Db::rollback(); return ajaxmsg('委托失败!',0); } $Trust_no = mt_rand(101010, 999999).substr(strval(time()),1); $subres = AccountModel::getAccountById($data['sub_id']); $broker = AccountModel::getBroker($subres['account_id']); if (!$subres) return ajaxmsg('子账号或证券公司不存在!',0); $Trust_no = mt_rand(101010, 999999) . substr(strval(time()), 1); //添加到委托表 $Trust_res = $Trust->sell_m_trust($data, $data['count'],$data['price'],$data['sub_id'],$broker['lid'],$broker['user'],$broker['stockjobber'],$Trust_no,$broker, $data['model']); if(!$Trust_res){ Db::rollback(); return ajaxmsg('委托失败!',0); } //持仓数量查询 $position = Db::name('stock_position')->where(['sub_id' => $data['subid'],'gupiao_code' => $data['code'],'buying' => 0])->find(); $amount = $position['canbuy_count'] - $data['count']; //持仓减少 $pos_res = Db::name('stock_position')->where(['sub_id' => $data['subid'],'gupiao_code' => $data['code'],'buying' => 0])->dec('canbuy_count',intval($data['count']))->update(); $Delivery = new Delivery; $avail = $moneyinfo["avail"] + $effectMoney; $del_res = $Delivery->sell_m_delivery_order($data, $data['count'],$data['price'],$data['sub_id'],$broker['lid'],$broker['user'],$broker['stockjobber'],$commission,$transfer,$Trust_no,$avail,$amount, $data['model']); if(!$del_res || !$pos_res){ Db::rollback(); return ajaxmsg('委托失败',0); } Db::commit(); return ajaxmsg('卖出委托已提交',1); } /*委托记录*/ public function trust() { if(!$this->token || $this->userId == '') return ajaxmsg('未登录',500); $sub_id = input('sub_id', '', ['trim', FILTER_SANITIZE_NUMBER_INT]); $startDate = input('start_date', ''); $endDate = input('end_date', ''); $page = input('page', 1, ['trim', FILTER_SANITIZE_NUMBER_INT]); $submodel = new StockSubAccount; $res = $submodel->getAccountById($sub_id); if (!$res['account_id']) return ajaxmsg('不存在的子账号',0); $trust = new Trust; //print_r($startDate);print_r($endDate);exit; $data = $trust->get_trust($sub_id,$startDate,$endDate,$page); if(!$data) return ajaxmsg('没有数据',0); return ajaxmsg('操作成功',1,$data); } /* * 获取可撤单委托列表 */ public function cancel_trust() { if(!$this->token || $this->userId == '') return ajaxmsg('未登录',500); $sub_id = input('sub_id', '', ['trim', FILTER_SANITIZE_NUMBER_INT]); $submodel = new StockSubAccount; $res = $submodel->getAccountById($sub_id); if (!$res['account_id']) return ajaxmsg('不存在的子账号',0); $trust = new Trust; $res = $trust->get_cancel_trust($sub_id); if(!$res) return ajaxmsg('没有数据',0); return ajaxmsg('操作成功',1,$res); } /* * 当天撤销委托 */ public function cancel() { if(!$this->token || $this->userId == '') return ajaxmsg('未登录',500); $sub_id = input('sub_id', '', ['trim', FILTER_SANITIZE_NUMBER_INT]); $trust_no = input('trust_no', '', ['trim', FILTER_SANITIZE_NUMBER_INT]); $res = StockSubAccount::getAccountById($sub_id); if (!$res['account_id']) return ajaxmsg('不存在的子账号',0); Db::startTrans(); $yes = false; $tempinfo = Db::name('stock_trust')->where(['trust_no' => $trust_no])->lock(true)->find(); if (!$tempinfo){ Db::rollback(); return ajaxmsg('没找到对应委托,撤单失败',0); } $trust['status'] = '已撤'; $trust['cancel_order_flag'] = '1'; $trust['cancel_order_count'] = $tempinfo['trust_count']; $trust_res = Db::name('stock_trust')->where(['trust_no' => $trust_no])->update($trust); $affect_money = Db::name('stock_delivery_order')->where(array('trust_no' => $trust_no))->value('liquidation_amount'); if ($tempinfo['flag2'] == '买入委托'){ $subm_res = SubAccountMoney::upMoneyLog($sub_id, $affect_money, 8); $position = true; } if ($tempinfo['flag2'] == '卖出委托'){ $position = Db::name('stock_position')->where(['sub_id' => $sub_id,'gupiao_code' => $tempinfo['gupiao_code'],'buying' => 0])->find(); $position['canbuy_count'] = $position['canbuy_count'] + $tempinfo['trust_count']; $position = Db::name('stock_position')->where(['sub_id' => $sub_id,'gupiao_code' => $tempinfo['gupiao_code']])->update($position); $subm_res = SubAccountMoney::upMoneyLog($sub_id, $affect_money, 9); } $delivery = Db::name('stock_delivery_order')->where(array('trust_no' => $trust_no))->delete(); if($trust_res && $subm_res && $position && $delivery){ Db::commit(); return ajaxmsg('撤单成功',1); } Db::rollback(); return ajaxmsg('撤单失败',0); } /* * 成交记录 */ public function deal() { if(!$this->token || $this->userId == '') return ajaxmsg('未登录',500); $sub_id = input('sub_id', '', ['trim', FILTER_SANITIZE_NUMBER_INT]); $startDate = input('start_date', ''); $endDate = input('end_date', ''); $page = input('page', 1, ['trim', FILTER_SANITIZE_NUMBER_INT]); $submodel = new StockSubAccount; $res = $submodel->getAccountById($sub_id); if (!$res['account_id']) return ajaxmsg('不存在的子账号',0); $deal_stack = new StockDealStock; $data = $deal_stack->get_deal_stock($sub_id,$startDate,$endDate,$page); if(!$data) return ajaxmsg('没有数据',0); return ajaxmsg('操作成功',1,$data); } /* * 交割记录 */ public function comp() { if(!$this->token || $this->userId == '') return ajaxmsg('未登录',500); $sub_id = input('sub_id', '', ['trim', FILTER_SANITIZE_NUMBER_INT]); $startDate = input('start_date', ''); $endDate = input('end_date', ''); $page = input('page', 1, ['trim', FILTER_SANITIZE_NUMBER_INT]); $submodel = new StockSubAccount; $res = $submodel->getAccountById($sub_id); if (!$res['account_id']) return ajaxmsg('不存在的子账号',0); $deal_stack = new Delivery; $data = $deal_stack->get_delivery_order($sub_id,$startDate,$endDate,$page); if(!$data) return ajaxmsg('没有数据',0); return ajaxmsg('操作成功',1,$data); } }
<?php
namespace
app\market\home
;
use
app\market\model\Deal_stock
;
use
app\market\model\Position
;
use
app\market\model\StockSubAccount
;
use
app\market\model\SubAccountMoney
;
use
app\market\model\Delivery
;
use
app\market\model\StockSubAccountRisk
;
use
app\market\model\Trust
;
use
app\stock\model\Account
as
AccountModel
;
use
think\Log
;
use
think\Db
;
class
Trade
extends
Common
{
protected
function
_initialize
(){
parent
::
_initialize
();
$token
=
$this
->
request
->
param
(
"token"
);
$mid
=
isLogin
(
$token
);
}
public
function
account_info
()
{
$token
=
$this
->
request
->
param
(
"token"
);
$mid
=
isLogin
(
$token
);
if
(
!
$mid
)
return
json
([
'status'
=>
0
,
'message'
=>
'登陆后才能进行查看'
]);
$subId
=
$this
->
request
->
param
(
"id"
);
if
(
!
$subId
)
return
json
([
'status'
=>
0
,
'message'
=>
'缺少参数或参数获取错误'
]);
$subModel
=
new
SubAccountMoney
();
$res
=
$subModel
->
get_account_money_inf
(
$subId
);
if
(
!
$res
)
return
json
([
'status'
=>
0
,
'message'
=>
'操作失败'
]);
$res
[
'available_amount'
]
>=
0
?
$res
[
'available_amount'
]
:
0
;
$res
=
$subModel
->
ftoy
(
$res
);
$pos
=
Position
::
where
([
'sub_id'
=>
$subId
,
'buying'
=>
0
])
->
order
(
'id desc'
)
->
select
();
$res
[
'return_money'
]
=
0
;
$res
[
'market_value'
]
=
0
;
foreach
(
$pos
as
$k
=>
$v
)
{
$data
=
z_market
(
$v
[
'gupiao_code'
]);
if
(
$data
[
'Price'
]
==
''
)
continue
;
$res
[
'return_money'
]
+=
(
$data
[
'Price'
]
-
$v
[
'buy_average_price'
])
*
$v
[
'stock_count'
];
$res
[
'market_value'
]
+=
$data
[
'Price'
]
*
$v
[
'stock_count'
];
}
// 提盈额度计算 $all = $res['market_value'] + $res['avail']; $profit = bcsub(strval($all),bcadd(strval($res['borrow_money']),strval($res['deposit_money'])),2); if($profit > 0){ $res['available_amount'] = $profit < $res['available_amount'] ? $profit : $res['available_amount']; }else{ $res['available_amount'] = 0; } //$res['available_amount'] = $res['available_amount'] > $res['avail'] ? $res['avail'] : $res['available_amount']; $res['available_amount'] = bcadd(strval($res['available_amount']),'0',2); // 去掉卖出委托的市值 $weituo = 0; $weituo = Db::name('stock_trust')->where(['sub_id'=>$res['stock_subaccount_id'],'flag2'=>'卖出委托','status'=>'已委托'])->sum('amount'); $res['return_money'] = bcadd(strval($res['return_money']),'0', 2); //加法,在原值上加0为了保留小数点后两位 $res['market_value'] = bcsub(strval($res['market_value']),strval($weituo), 2);//减法 $res['total_money'] = bcadd(strval($res['market_value']),bcadd(strval($res['avail']),strval($res['freeze_amount']),2), 2);//加法 return json(['data'=>$res,'status' => 1, 'message' => '操作成功']); } /* * 持仓查询 */ public function position() { $token = $this->request->param("token"); $mid = isLogin($token); if(!$mid) return json(['status' => 0, 'message' => '未登录']); $subid = $this->request->param("id"); if(!$subid) return json(['status' => 0, 'message' => '参数不正确']); $submodel = new StockSubAccount(); $res = $submodel->get_account_by_id($subid); if (!$res) return json(['status' => 0, 'message' => '不存在的子账号']); if (empty($res['account_id'])) return json(['status' => 0, 'message' => '证券公司不存在']); $data = Position::where(['sub_id' => $subid,'buying' => 0])->where('stock_count','>',0)->order('id desc')->paginate(20, false, ['query' => request()->param()]); if (!$data || count($data) === 0) return ajaxmsg('没有数据',0,$data); foreach ($data as $k => $item){ //查询当天交易的数量 T+1 交易 $todayCount = Delivery::get_delivery_order($subid,$item["gupiao_code"]); $data[$k]['canbuy_count'] = $item['canbuy_count']; //查询股票最新行情 $Qdata = z_market($item["gupiao_code"],$item['market']); //查询子账户 $data[$k]['sub_account'] = $res['sub_account']; //提取当前价格 $data[$k]['now_price'] = $Qdata['Price']; //市值 = 当前价格*数量 $data[$k]['market_value'] = round((int)$Qdata['Price']*(int)$item['stock_count'],2); //参考成本价 $data[$k]['ck_price'] = Position::calculate($subid,$item["gupiao_code"],'price'); //买入均价 $data[$k]['buy_average_price'] = Position::calculate($subid,$item["gupiao_code"],'average'); //参考盈亏 //$data[$k]['ck_profit'] = $item['stock_count'] > 0 ? round(($Qdata['Price']-$data[$k]['buy_average_price'])*$item['stock_count'], 2) : 0; $data[$k]['ck_profit'] = $item['stock_count'] > 0 ? bcmul(strval((int)$Qdata["Price"]-(int)$data[$k]['buy_average_price']),strval($item['stock_count']),2) : 0;//参考浮动盈亏 //盈亏比例 //$data[$k]['profit_rate'] = $item['stock_count'] > 0 ? round(($data[$k]['ck_profit'] / ($data[$k]['buy_average_price'] * $item['stock_count'])) * 100, 2) : 0; //$data[$k]['profit_rate'] = $item['stock_count'] > 0 ? bcdiv(strval($data[$k]['ck_profit']),strval($data[$k]['buy_average_price']*$item['stock_count']*100),2) : 0;//盈亏比例 //当天可卖数量计算 //$data[$k]['canbuy_count'] = StockPosition::getCanbuyCount($subid,$item["gupiao_code"]); } return ajaxmsg('操作成功',1,$data); } /* * 证券买入 */ public function buy() { $token = $this->request->param("token"); $mid = isLogin($token); if(!$mid) return json(['status' => 0, 'message' => '登陆后才能进行查看']); if (!yan_time()) return json(['status' => 0, 'message' => '非交易时间']); if (!config('site_trade_buy')) return ajaxmsg('系统设置为禁买状态',0); $data = [ "sub_id" => input('id','', ['trim', FILTER_SANITIZE_NUMBER_INT]),//子账户ID, "code" => input('code' ,'', ['trim', FILTER_SANITIZE_NUMBER_INT]),//股票代码 "name" => input('name' ,''),//股票名称 "market" => input('market',''),//交易所代码 "count" => input('count', 0, ['trim', FILTER_SANITIZE_NUMBER_INT]),//购买数量 "price" => input('price', 0, ['trim']), 'model' => input('model', 1, ['trim', FILTER_SANITIZE_NUMBER_INT]),//1:是委托状态 ]; //验证数据 $result = $this->validate($data, 'Trade.trade'); if(!$result) return ajaxmsg($result,0); //检查其他买入条件是否符合 $Trust = new Trust; $trade_res = $Trust->executeData($data,'buy'); if(isset($trade_res['status'])&&!$trade_res['status']) return ajaxmsg($trade_res['message'],0); $trade_money = $trade_res['trade_money']; $moneyinfo = $trade_res['moneyinfo']; $price = $trade_res['price']; //计算佣金 commission_scale:佣金比例(单位:万分之几) 如:5 代表万分之五; min_commission:最低佣金(单位:元) $commission = commission($trade_money,$moneyinfo['commission_scale'],$moneyinfo['min_commission']); //计算过户费 $transfer = transfer($trade_money); //写入子账户资金变化表 Db::startTrans(); $effectMoney = $trade_money + $commission + $transfer; /*(子账户ID, 金额*100, 买卖方向[1:买入 2:卖出], 盈亏金额, 实盘相关, 股票代码)*/ $subaccount=new SubAccountMoney(); $ret = $subaccount->up_moneylog($data['sub_id'], $effectMoney, 3, 0, 0, $data['code']); if (!$ret){ Db::rollback(); return ajaxmsg('委托失败!',0); } $StockSubAccount = new StockSubAccount(); $subres = $StockSubAccount->get_account_by_id($data['sub_id']); $broker = $StockSubAccount->get_broker($subres['account_id']); if (!$subres) return ajaxmsg('子账号或证券公司不存在!',0); $Trust_no = mt_rand(101010, 999999) . substr(strval(time()), 1); //添加到委托表 $Trust_res = $Trust->add_m_trust($data, $data['count'],$data['price'],$data['sub_id'],$broker['lid'],$broker['user'],$broker['stockjobber'],$Trust_no,$broker, $data['model']); if(!$Trust_res){ Db::rollback(); return ajaxmsg('委托失败!',0); } //持仓数量查询 $position = Db::name('stock_position')->where(['sub_id' => $data['sub_id'],'gupiao_code' => $data['code'],'buying' => 0])->find(); $amount = empty($position) ? $data['count'] : $position['canbuy_count'] + $data['count']; //提交交易费用信息 $Delivery = new Delivery; $avail = ($moneyinfo['avail']) - $effectMoney; //print_r("佣金: ".$commission." 过户费: ".$transfer." 总计:".$effectMoney);Db::rollback();exit; $del_res = $Delivery->add_m_delivery_order($data, $data['count'],$data['price'],$data['sub_id'],$broker['lid'],$broker['user'],$broker['stockjobber'],$commission,$transfer,$Trust_no,$avail,$amount, $data['model']); //print_r($del_res);Db::rollback();exit; if(!$del_res){ Db::rollback(); return ajaxmsg('委托失败',0); }else{ Db::commit(); return ajaxmsg('买入委托已提交',1); } } public function sell() { $token = $this->request->param("token"); $mid = isLogin($token); if(!$mid) return json(['status' => 0, 'message' => '登陆后才能进行查看']); if (!yan_time()) return json(['status' => 0, 'message' => '非交易时间']); if (config('site_trade_sell') == 0) return ajaxmsg('系统设置为不允许卖出股票',0); if (time() <= strtotime(date("Y-m-d 09:30:15"))) { return ajaxmsg('平台设置9点30分15秒后可卖出',0); } $data = [ "sub_id" => input('id','', ['trim', FILTER_SANITIZE_NUMBER_INT]),//子账户ID, "code" => input('code' ,'', ['trim', FILTER_SANITIZE_NUMBER_INT]),//股票代码 "name" => input('name' ,''),//股票名称 "market" => input('market', ''),//交易所代码 "count" => input('count', 0, ['trim', FILTER_SANITIZE_NUMBER_INT]),//购买数量 "price" => input('price', 0, ['trim']), 'model' => input('model', 1, ['trim', FILTER_SANITIZE_NUMBER_INT]),//1:是委托状态 ]; //验证数据 $result = $this->validate($data, 'Trade.trade'); if(!$result) return ajaxmsg($result,0); $Trust = new Trust; $trade_res = $Trust->executeData($data,'sell'); //print_r($trade_res['message']);exit; if(isset($trade_res['status'])&&!$trade_res['status']) return ajaxmsg($trade_res['message'],0); $trade_money = $trade_res['trade_money']; $moneyinfo = $trade_res['moneyinfo']; $price = $trade_res['price']; //计算佣金 $commission = commission($trade_money,$moneyinfo['commission_scale'],$moneyinfo['min_commission']); //印花税 $stamps = stamps($trade_money); //计算过户费 $transfer = transfer($trade_money); //写入子账户资金变化表 Db::startTrans(); $effectMoney = $trade_money - $transfer - $stamps - $commission; $subaccount=new SubAccountMoney(); $ret = $subaccount->up_moneylog($data['sub_id'], $effectMoney, 4); if(!$ret){ Db::rollback(); return ajaxmsg('委托失败!',0); } $Trust_no = mt_rand(101010, 999999).substr(strval(time()),1); $StockSubAccount = new StockSubAccount(); $subres = $StockSubAccount->get_account_by_id($data['sub_id']); $broker = $StockSubAccount->get_broker($subres['account_id']); if (!$subres) return ajaxmsg('子账号或证券公司不存在!',0); $Trust_no = mt_rand(101010, 999999) . substr(strval(time()), 1); //添加到委托表 $Trust_res = $Trust->sell_m_trust($data, $data['count'],$data['price'],$data['sub_id'],$broker['lid'],$broker['user'],$broker['stockjobber'],$Trust_no,$broker, $data['model']); if(!$Trust_res){ Db::rollback(); return ajaxmsg('委托失败!',0); } //持仓数量查询 $position = Db::name('stock_position')->where(['sub_id' => $data['sub_id'],'gupiao_code' => $data['code'],'buying' => 0])->find(); $amount = $position['canbuy_count'] - $data['count']; //持仓减少 $pos_res = Db::name('stock_position')->where(['sub_id' => $data['sub_id'],'gupiao_code' => $data['code'],'buying' => 0])->dec('canbuy_count',intval($data['count']))->update(); $Delivery = new Delivery; $avail = $moneyinfo["avail"] + $effectMoney; $del_res = $Delivery->sell_m_delivery_order($data, $data['count'],$data['price'],$data['sub_id'],$broker['lid'],$broker['user'],$broker['stockjobber'],$commission,$transfer,$Trust_no,$avail,$amount, $data['model']); if(!$del_res || !$pos_res){ Db::rollback(); return ajaxmsg('委托失败',0); } Db::commit(); return ajaxmsg('卖出委托已提交',1); } /*委托记录*/ public function trust() { $token = $this->request->param("token"); $mid = isLogin($token); if(!$mid) return json(['status' => 0, 'message' => '登陆后才能进行查看']); $sub_id = input('id', '', ['trim', FILTER_SANITIZE_NUMBER_INT]); $startDate = input('start_date', ''); $endDate = input('end_date', ''); $page = input('page', 1, ['trim', FILTER_SANITIZE_NUMBER_INT]); $submodel = new StockSubAccount; $res = $submodel->get_account_by_id($sub_id); if (!$res['account_id']) return ajaxmsg('不存在的子账号',0); $trust = new Trust; //print_r($startDate);print_r($endDate);exit; $data = $trust->get_trust($sub_id,$startDate,$endDate); if(!$data) return ajaxmsg('没有数据',0); return ajaxmsg('操作成功',1,$data); } /* * 获取可撤单委托列表 */ public function cancel_trust() { $token = $this->request->param("token"); $mid = isLogin($token); if(!$mid) return json(['status' => 0, 'message' => '登陆后才能进行查看']); $sub_id = input('id', '', ['trim', FILTER_SANITIZE_NUMBER_INT]); $submodel = new StockSubAccount; $res = $submodel->get_account_by_id($sub_id); if (!$res['account_id']) return ajaxmsg('不存在的子账号',0); $trust = new Trust; $res = $trust->get_cancel_trust($sub_id); if(!$res) return ajaxmsg('没有数据',0); return ajaxmsg('操作成功',1,$res); } /* * 当天撤销委托 */ public function cancel() { $token = $this->request->param("token"); $mid = isLogin($token); if(!$mid) return json(['status' => 0, 'message' => '登陆后才能进行查看']); $sub_id = input('id', '', ['trim', FILTER_SANITIZE_NUMBER_INT]); $trust_no = input('trust_no', '', ['trim', FILTER_SANITIZE_NUMBER_INT]); $submodel = new StockSubAccount; $res = $submodel->get_account_by_id($sub_id); if (!$res['account_id']) return ajaxmsg('不存在的子账号',0); Db::startTrans(); $yes = false; $tempinfo = Db::name('stock_trust')->where(['trust_no' => $trust_no])->lock(true)->find(); if (!$tempinfo){ Db::rollback(); return ajaxmsg('没找到对应委托,撤单失败',0); } $trust['status'] = '已撤'; $trust['cancel_order_flag'] = '1'; $trust['cancel_order_count'] = $tempinfo['trust_count']; $trust_res = Db::name('stock_trust')->where(['trust_no' => $trust_no])->update($trust); $affect_money = Db::name('stock_delivery_order')->where(array('trust_no' => $trust_no))->value('liquidation_amount'); $subaccount=new SubAccountMoney(); if ($tempinfo['flag2'] == '买入委托'){ $subm_res = $subaccount->up_moneylog($sub_id, $affect_money, 8); $position = true; } if ($tempinfo['flag2'] == '卖出委托'){ $position = Db::name('stock_position')->where(['sub_id' => $sub_id,'gupiao_code' => $tempinfo['gupiao_code'],'buying' => 0])->find(); $position['canbuy_count'] = $position['canbuy_count'] + $tempinfo['trust_count']; $position = Db::name('stock_position')->where(['sub_id' => $sub_id,'gupiao_code' => $tempinfo['gupiao_code']])->update($position); $subm_res = $subaccount->up_moneylog($sub_id, $affect_money, 9); } $delivery = Db::name('stock_delivery_order')->where(array('trust_no' => $trust_no))->delete(); if($trust_res && $subm_res && $position && $delivery){ Db::commit(); return ajaxmsg('撤单成功',1); } Db::rollback(); return ajaxmsg('撤单失败',0); } /* * 成交记录 */ public function deal() { $token = $this->request->param("token"); $mid = isLogin($token); if(!$mid) return json(['status' => 0, 'message' => '登陆后才能进行查看']); $sub_id = input('id', '', ['trim', FILTER_SANITIZE_NUMBER_INT]); $startDate = input('start_date', ''); $endDate = input('end_date', ''); $page = input('page', 1, ['trim', FILTER_SANITIZE_NUMBER_INT]); $submodel = new StockSubAccount; $res = $submodel->get_account_by_id($sub_id); if (!$res['account_id']) return ajaxmsg('不存在的子账号',0); $deal_stack = new Deal_stock(); $data = $deal_stack->get_deal_stock($sub_id,$startDate,$endDate); if(!$data) return ajaxmsg('没有数据',0); return ajaxmsg('操作成功',1,$data); } /* * 交割记录 */ public function comp() { $token = $this->request->param("token"); $mid = isLogin($token); if(!$mid) return json(['status' => 0, 'message' => '登陆后才能进行查看']); $sub_id = input('id', '', ['trim', FILTER_SANITIZE_NUMBER_INT]); $startDate = input('start_date', ''); $endDate = input('end_date', ''); $page = input('page', 1, ['trim', FILTER_SANITIZE_NUMBER_INT]); $submodel = new StockSubAccount; $res = $submodel->get_account_by_id($sub_id); if (!$res['account_id']) return ajaxmsg('不存在的子账号',0); $deal_stack = new Delivery; $data = $deal_stack->get_delivery_order($sub_id,$startDate,$endDate); if(!$data) return ajaxmsg('没有数据',0); return ajaxmsg('操作成功',1,$data); } }
\ No newline at end of file
\ No newline at end of file
...
...
application/market/model/Deal_stock.php
View file @
34ed11aa
...
@@ -69,7 +69,7 @@ class Deal_stock extends Model{
...
@@ -69,7 +69,7 @@ class Deal_stock extends Model{
* 返回子账号成交
* 返回子账号成交
* $sub_id 子账号
* $sub_id 子账号
*/
*/
public
function
get_deal_stock
(
$sub_id
,
$beginday
,
$endday
){
public
function
get_deal_stock
(
$sub_id
,
$beginday
,
$endday
,
$offset
=
15
){
if
(
empty
(
$endday
)){
$endday
=
time
();}
else
{
$endday
=
strtotime
(
$endday
);}
if
(
empty
(
$endday
)){
$endday
=
time
();}
else
{
$endday
=
strtotime
(
$endday
);}
if
(
empty
(
$beginday
)){
$beginday
=
strtotime
(
date
(
"Y-m-d"
,
time
()));}
else
{
$beginday
=
strtotime
(
$beginday
);}
if
(
empty
(
$beginday
)){
$beginday
=
strtotime
(
date
(
"Y-m-d"
,
time
()));}
else
{
$beginday
=
strtotime
(
$beginday
);}
...
@@ -80,6 +80,7 @@ class Deal_stock extends Model{
...
@@ -80,6 +80,7 @@ class Deal_stock extends Model{
->
where
(
'deal_date'
,
'>='
,
$beginday
)
->
where
(
'deal_date'
,
'>='
,
$beginday
)
->
where
(
'deal_date'
,
'<='
,
$endday
)
->
where
(
'deal_date'
,
'<='
,
$endday
)
->
order
(
'id desc'
)
->
order
(
'id desc'
)
->
paginate
(
$offset
,
false
,
[
'query'
=>
request
()
->
param
()])
->
select
();
->
select
();
if
(
!
$res
||
count
(
$res
)
===
0
){
if
(
!
$res
||
count
(
$res
)
===
0
){
...
...
application/market/model/Delivery.php
View file @
34ed11aa
...
@@ -57,7 +57,7 @@ class Delivery extends Model{
...
@@ -57,7 +57,7 @@ class Delivery extends Model{
* 返回对应交割单
* 返回对应交割单
* $sub_id 子账号
* $sub_id 子账号
*/
*/
public
function
get_delivery_order
(
$sub_id
,
$beginday
=
""
,
$endday
=
""
){
public
function
get_delivery_order
(
$sub_id
,
$beginday
=
""
,
$endday
=
""
,
$offset
=
15
){
if
(
empty
(
$endday
)){
$endday
=
time
();}
else
{
$endday
=
strtotime
(
$endday
)
+
86400
;}
if
(
empty
(
$endday
)){
$endday
=
time
();}
else
{
$endday
=
strtotime
(
$endday
)
+
86400
;}
if
(
empty
(
$beginday
)){
$beginday
=
strtotime
(
date
(
"Y-m-d"
,
time
()));}
else
{
$beginday
=
strtotime
(
$beginday
);}
if
(
empty
(
$beginday
)){
$beginday
=
strtotime
(
date
(
"Y-m-d"
,
time
()));}
else
{
$beginday
=
strtotime
(
$beginday
);}
$res
=
Db
::
name
(
'stock_delivery_order'
)
$res
=
Db
::
name
(
'stock_delivery_order'
)
...
@@ -66,6 +66,7 @@ class Delivery extends Model{
...
@@ -66,6 +66,7 @@ class Delivery extends Model{
->
where
(
'deal_date'
,
'>='
,
$beginday
)
->
where
(
'deal_date'
,
'>='
,
$beginday
)
->
where
(
'deal_date'
,
'<='
,
$endday
)
->
where
(
'deal_date'
,
'<='
,
$endday
)
->
order
(
'id desc'
)
->
order
(
'id desc'
)
->
paginate
(
$offset
,
false
,
[
'query'
=>
request
()
->
param
()])
->
select
();
->
select
();
return
$res
;
return
$res
;
}
}
...
...
application/market/model/StockPosition.php
deleted
100644 → 0
View file @
f4afec72
<?php
namespace
app\market\model
;
use
think\Model
;
use
think\Db
;
use
app\market\model\StockDeliveryOrder
as
Delivery
;
//use util\RedisUtil;
/**
* 会员模型
*/
class
StockPosition
extends
Model
{
// 设置当前模型对应的完整数据表名称
protected
$name
=
'stock_position'
;
// 自动写入时间戳
protected
$autoWriteTimestamp
=
true
;
/*
* 存储持仓记录
* $data 持仓数据
* $sub_id 子账号
* $lid 安全模式id号
* $user 证券账户
* $soure 证券来源
*/
public
static
function
add_position_broker
(
$data
,
$lid
,
$user
,
$soure
){
$row
=
array
();
foreach
(
$data
as
$k
=>
$value
)
{
$row
[
$k
][
'lid'
]
=
$lid
;
$row
[
$k
][
'soruce'
]
=
$soure
;
$row
[
$k
][
'login_name'
]
=
$user
;
$row
[
$k
][
'gupiao_code'
]
=
$value
[
0
];
$row
[
$k
][
'gupiao_name'
]
=
$value
[
1
];
$row
[
$k
][
'count'
]
=
$value
[
2
];
$row
[
$k
][
'stock_count'
]
=
$value
[
3
];
$row
[
$k
][
'canbuy_count'
]
=
$value
[
4
];
$row
[
$k
][
'ck_price'
]
=
$value
[
5
];
$row
[
$k
][
'buy_average_price'
]
=
$value
[
6
];
$row
[
$k
][
'ck_profit_price'
]
=
$value
[
7
];
$row
[
$k
][
'now_price'
]
=
$value
[
8
];
$row
[
$k
][
'market_value'
]
=
$value
[
9
];
$row
[
$k
][
'ck_profit'
]
=
$value
[
10
];
$row
[
$k
][
'profit_rate'
]
=
$value
[
11
];
$row
[
$k
][
'buying'
]
=
$value
[
12
];
$row
[
$k
][
'selling'
]
=
$value
[
13
];
$row
[
$k
][
'gudong_code'
]
=
$value
[
14
];
$row
[
$k
][
'type'
]
=
$value
[
15
];
$row
[
$k
][
'jigou_type'
]
=
$value
[
15
];
$row
[
$k
][
'jiyisuo'
]
=
$value
[
16
]
==
0
?
"深交所"
:
"上交所"
;
$row
[
$k
][
'info'
]
=
$value
[
17
];
}
$result
=
Db
::
name
(
'stock_position_broker'
)
->
insertall
(
$row
,
true
);
return
$result
;
}
/*
* 返回子账号指定股票持仓数据-带最新市值
* $sub_id 子账号
*/
public
static
function
getPositionFind
(
$sub_id
=
''
,
$code
=
''
)
{
if
(
$sub_id
)
$map
[]
=
[
'sub_id'
,
'='
,
$sub_id
];
if
(
$code
)
$map
[]
=
[
'gupiao_code'
,
'='
,
$code
];
$map
[]
=
[
'buying'
,
'='
,
0
];
$res
=
Db
::
view
(
'stock_position p'
)
->
view
(
'stock_subaccount s'
,
'sub_account'
,
's.id=p.sub_id'
)
->
where
(
$map
)
->
order
(
'id desc'
)
->
select
()
->
toArray
();
foreach
(
$res
as
$k
=>
$v
){
//查询股票最新行情
$Qdata
=
RedisUtil
::
getQuotationData
(
$v
[
'gupiao_code'
],
toMarket
(
$v
[
'gupiao_code'
]));
$res
[
$k
][
'now_price'
]
=
$Qdata
[
"Price"
];
$res
[
$k
][
'code'
]
=
$Qdata
[
"code"
];
$res
[
$k
][
'name'
]
=
$Qdata
[
"name"
];
$res
[
$k
][
'market_value'
]
=
round
(
$Qdata
[
"Price"
]
*
$v
[
'stock_count'
],
2
);
//最新市值
$res
[
$k
][
'ck_profit'
]
=
$v
[
'stock_count'
]
>
0
?
bcmul
(
strval
(
$Qdata
[
"Price"
]
-
$v
[
'buy_average_price'
]),
strval
(
$v
[
'stock_count'
]),
2
)
:
0
;
//参考浮动盈亏
//$res[$k]['profit_rate'] = $v['stock_count'] > 0 ? bcdiv(strval($res[$k]['ck_profit']),strval($v['buy_average_price']*$v['stock_count']*100),2) : 0;//盈亏比例
}
return
$res
;
}
/*
* 返回子账号和股票代码对应的持仓
* $sub_id 子账号
* $code 股票代码
*/
public
static
function
getCodePosition
(
$sub_id
,
$code
){
$res
=
self
::
where
([
'sub_id'
=>
$sub_id
,
'gupiao_code'
=>
$code
,
'buying'
=>
0
])
->
find
();
if
(
empty
(
$res
))
return
false
;
$Qdata
=
RedisUtil
::
getQuotationData
(
$code
,
toMarket
(
$code
),
true
);
$res
[
'now_price'
]
=
$Qdata
[
"Price"
];
$res
[
'market_value'
]
=
$Qdata
[
"Price"
]
*
$res
[
'canbuy_count'
];
//最新市值
$res
[
'ck_profit'
]
=
$res
[
'stock_count'
]
>
0
?
bcmul
(
strval
(
$Qdata
[
"Price"
]
-
$res
[
'buy_average_price'
]),
strval
(
$res
[
'canbuy_count'
]),
2
)
:
0
;
//参考浮动盈亏
//$res['profit_rate'] = $res['stock_count'] > 0 && $res['ck_profit'] ? bcdiv(strval($res['ck_profit']),strval($res['buy_average_price']*$res['canbuy_count']*100),2) : 0;//盈亏比例
return
$res
;
}
/*
* 返回子账号和股票代码对应持仓的可卖数量
* $sub_id 子账号
* $code 股票代码
*/
public
static
function
getCanbuyCount
(
$sub_id
,
$code
){
$res
=
Db
::
name
(
'stock_position'
)
->
field
(
'canbuy_count,stock_count'
)
->
where
([
'sub_id'
=>
$sub_id
,
'gupiao_code'
=>
$code
,
'buying'
=>
0
])
->
find
();
//print_r($res);exit;
if
(
empty
(
$res
))
return
0
;
$res_d1
=
Db
::
name
(
'stock_delivery_order'
)
->
where
([
'sub_id'
=>
$sub_id
,
'gupiao_code'
=>
$code
,
'status'
=>
1
,
'business_name'
=>
'证券买入'
])
->
whereDay
(
'deal_date'
)
->
sum
(
'volume'
);
$res_d2
=
Db
::
name
(
'stock_delivery_order'
)
->
where
([
'sub_id'
=>
$sub_id
,
'gupiao_code'
=>
$code
,
'status'
=>
0
,
'business_name'
=>
'证券卖出'
])
->
whereDay
(
'deal_date'
)
->
sum
(
'volume'
);
//如果时T+0模式,就不计算当天的股票数量
if
(
TRUST_MODEL_TIME
==
0
)
$res_d1
=
$res_d2
=
0
;
$res
[
'canbuy_count'
]
=
$res
[
'stock_count'
]
-
$res_d1
-
$res_d2
;
//print_r($res);exit;
return
$res
[
'canbuy_count'
];
}
/*
* 添加卖出模拟持仓记录
* $data 持仓数据
* $sub_id 子账号
* $lid 安全模式id号
* $user 证券账户
* $soure 证券来源
* *************************************************************************************************************
*/
public
static
function
sell_m_position
(
$stockinfo
,
$count
,
$price
,
$sub_id
,
$model
,
$Trust_no
,
$commission
,
$Transfer
,
$stamp
){
$position_res
=
self
::
getCodePosition
(
$sub_id
,
$stockinfo
[
"code"
]);
if
(
empty
(
$position_res
))
return
false
;
if
(
!
$model
)
$model
=
2
;
$data
=
$position_res
;
$canbuy_count
=
$position_res
[
'canbuy_count'
]
-
$count
;
$stock_count
=
$position_res
[
'stock_count'
]
-
$count
;
$fee
=
$commission
+
$Transfer
+
$stamp
;
if
(
$canbuy_count
>
0
){
if
(
$model
==
1
){
$data
[
'canbuy_count'
]
=
$canbuy_count
;
}
if
(
$model
==
2
){
$new_price
=
round
((
$position_res
[
'canbuy_count'
]
*
$position_res
[
'buy_average_price'
]
-
$count
*
$price
-
$fee
)
/
$canbuy_count
,
3
);
$data
[
'count'
]
=
$stock_count
;
$data
[
'stock_count'
]
=
$stock_count
;
$data
[
'canbuy_count'
]
=
$canbuy_count
;
$data
[
'ck_price'
]
=
$new_price
;
//参考成本价
$data
[
'buy_average_price'
]
=
$new_price
;
//买入均价
$data
[
'ck_profit_price'
]
=
$new_price
;
//参考盈亏成本价
$data
[
'now_price'
]
=
$stockinfo
[
"Price"
];
//'当前价'
$data
[
'market_value'
]
=
$stockinfo
[
"Price"
]
*
$canbuy_count
;
//最新市值
$data
[
'ck_profit'
]
=
$canbuy_count
>
0
?
round
((
$stockinfo
[
"Price"
]
-
$new_price
)
*
$canbuy_count
,
2
)
:
0
;
//参考浮动盈亏
$data
[
'profit_rate'
]
=
$canbuy_count
>
0
?
round
(
$data
[
'ck_profit'
]
/
(
$new_price
*
$canbuy_count
)
*
100
,
2
)
:
0
;
//盈亏比例
}
$result
=
Db
::
name
(
'stock_position'
)
->
where
([
'sub_id'
=>
$sub_id
,
'gupiao_code'
=>
$stockinfo
[
"code"
]])
->
where
([
'buying'
=>
0
])
->
update
(
$data
);
Log
::
write
(
date
(
'y-m-d H:i:s'
,
time
())
.
"::子账户"
.
$sub_id
.
"委托卖出"
.
$count
.
"股"
.
$stockinfo
[
"code"
]
.
"股票"
);
}
elseif
(
$canbuy_count
==
0
){
if
(
$model
==
2
){
$info
=
Db
::
name
(
'stock_temp'
)
->
where
([
'sub_id'
=>
$sub_id
])
->
where
([
'gupiao_code'
=>
$stockinfo
[
"code"
]])
->
where
([
'deal_no'
=>
null
])
->
find
();
if
(
empty
(
$info
))
{
$result
=
Db
::
name
(
'stock_position'
)
->
where
([
'sub_id'
=>
$sub_id
,
'gupiao_code'
=>
$stockinfo
[
"code"
]])
->
where
([
'buying'
=>
0
])
->
delete
();
}
else
{
$new_price
=
round
((
$position_res
[
'stock_count'
]
*
$position_res
[
'buy_average_price'
]
-
$count
*
$price
-
$fee
)
/
$stock_count
,
3
);
$data
[
'count'
]
=
$stock_count
;
$data
[
'stock_count'
]
=
$stock_count
;
$data
[
'canbuy_count'
]
=
0
;
$data
[
'ck_price'
]
=
$new_price
;
//参考成本价
$data
[
'buy_average_price'
]
=
$new_price
;
//买入均价
$data
[
'ck_profit_price'
]
=
$new_price
;
//参考盈亏成本价
$data
[
'now_price'
]
=
$stockinfo
[
"Price"
];
//'当前价'
$data
[
'market_value'
]
=
$stockinfo
[
"Price"
]
*
$canbuy_count
;
//最新市值
$data
[
'ck_profit'
]
=
$canbuy_count
>
0
?
round
((
$stockinfo
[
"Price"
]
-
$new_price
)
*
$canbuy_count
,
2
)
:
0
;
//参考浮动盈亏
$data
[
'profit_rate'
]
=
$canbuy_count
>
0
?
round
(
$data
[
'ck_profit'
]
/
(
$new_price
*
$canbuy_count
)
*
100
,
2
)
:
0
;
//盈亏比例
$result
=
Db
::
name
(
'stock_position'
)
->
where
([
'sub_id'
=>
$sub_id
,
'gupiao_code'
=>
$stockinfo
[
"code"
]])
->
where
([
'buying'
=>
0
])
->
update
(
$data
);
}
}
elseif
(
$model
==
1
){
$data
[
'canbuy_count'
]
=
0
;
$result
=
Db
::
name
(
'stock_position'
)
->
where
([
'sub_id'
=>
$sub_id
,
'gupiao_code'
=>
$stockinfo
[
"code"
]])
->
where
([
'buying'
=>
0
])
->
update
(
$data
);
}
else
{
$result
=
false
;
}
}
else
{
$result
=
false
;
}
return
$result
;
}
/*
* 存储模拟持仓记录
* $data 持仓数据
* $sub_id 子账号
* $lid 安全模式id号
* $user 证券账户
* $soure 证券来源
*/
public
static
function
add_m_position
(
$code
,
$count
,
$sub_id
,
$lid
,
$user
,
$soure
,
$ck_price
,
$Trust_no
)
{
//if(!$count) return false;
//$canbuy = self::getCanbuyCount($sub_id,$code);
$stockinfo
=
RedisUtil
::
getQuotationData
(
$code
,
toMarket
(
$code
));
$data
=
array
();
$data
[
0
][
'sub_id'
]
=
$sub_id
;
$data
[
0
][
'lid'
]
=
$lid
;
$data
[
0
][
'soruce'
]
=
$soure
;
$data
[
0
][
'login_name'
]
=
$user
;
$data
[
0
][
'gupiao_code'
]
=
$stockinfo
[
"code"
];
$data
[
0
][
'gupiao_name'
]
=
$stockinfo
[
"name"
];
$data
[
0
][
'count'
]
=
$count
;
$data
[
0
][
'stock_count'
]
=
$count
;
$data
[
0
][
'canbuy_count'
]
=
$count
;
$data
[
0
][
'ck_price'
]
=
self
::
calculate
(
$sub_id
,
$code
,
'price'
);
//参考成本价
$data
[
0
][
'buy_average_price'
]
=
self
::
calculate
(
$sub_id
,
$code
,
'average'
);
//买入均价
$data
[
0
][
'ck_profit_price'
]
=
''
;
//参考盈亏成本价
$data
[
0
][
'now_price'
]
=
$stockinfo
[
"Price"
];
//'当前价'
$data
[
0
][
'market_value'
]
=
$stockinfo
[
"Price"
]
*
$count
;
//最新市值
$data
[
0
][
'ck_profit'
]
=
$count
>
0
?
bcmul
(
strval
(
$stockinfo
[
"Price"
]
-
$ck_price
),
strval
(
$count
),
2
)
:
0
;
//参考浮动盈亏
$data
[
0
][
'profit_rate'
]
=
$count
>
0
&&
$ck_price
>
0
?
bcdiv
(
strval
(
$data
[
0
][
'ck_profit'
]),
strval
(
$ck_price
*
$count
*
100
),
2
)
:
0
;
//盈亏比例
$data
[
0
][
'buying'
]
=
0
;
//买入成功
$data
[
0
][
'selling'
]
=
0
;
//1、在途卖出
$data
[
0
][
'gudong_code'
]
=
""
;
//股东代码 无法模拟暂时空
$data
[
0
][
'type'
]
=
0
;
//帐号类别
$data
[
0
][
'market'
]
=
toMarket
(
$code
);
//股票类别
$data
[
0
][
'jigou_type'
]
=
1
;
$data
[
0
][
'jiyisuo'
]
=
toMarket
(
$code
)
==
'SZ'
?
"深交所"
:
"上交所"
;
//交易所
$data
[
0
][
'info'
]
=
""
;
$position
=
self
::
getCodePosition
(
$sub_id
,
$code
);
/***********************************************************************************/
if
(
TRUST_MODEL_TIME
===
1
){
// 交易模式为T+1时,不能立即更新可卖数量
unset
(
$data
[
0
][
'canbuy_count'
]);
}
/***********************************************************************************/
if
(
!
empty
(
$position
)){
$result
=
self
::
where
([
'id'
=>
$position
[
'id'
]])
->
update
(
$data
[
0
]);
}
else
{
$result
=
self
::
strict
(
false
)
->
insert
(
$data
[
0
],
true
);
}
return
$result
;
}
public
static
function
calculate
(
$sub_id
,
$code
,
$variable
)
{
if
(
!
$sub_id
||
!
$code
||
!
$variable
)
return
0
;
switch
(
$variable
)
{
case
'price'
:
$order
=
Db
::
name
(
'stock_delivery_order'
)
->
where
([
'sub_id'
=>
$sub_id
,
'gupiao_code'
=>
$code
,
'status'
=>
1
,
'business_name'
=>
'证券买入'
]);
$amount
=
$order
->
sum
(
'liquidation_amount'
);
$volume
=
$order
->
sum
(
'volume'
);
$result
=
bcdiv
(
strval
(
$amount
),
strval
(
$volume
),
3
);
break
;
case
'average'
:
$order
=
Db
::
name
(
'stock_delivery_order'
)
->
where
([
'sub_id'
=>
$sub_id
,
'gupiao_code'
=>
$code
,
'status'
=>
1
,
'business_name'
=>
'证券买入'
]);
$amount
=
$order
->
sum
(
'residual_quantity'
);
$volume
=
$order
->
sum
(
'volume'
);
$result
=
bcdiv
(
strval
(
$amount
),
strval
(
$volume
),
3
);
break
;
default
:
$result
=
0
;
break
;
}
return
$result
;
}
}
\ No newline at end of file
application/market/model/Trust.php
View file @
34ed11aa
...
@@ -234,13 +234,15 @@ class Trust extends Model{
...
@@ -234,13 +234,15 @@ class Trust extends Model{
* 返回子账号委托
* 返回子账号委托
* $sub_id 子账号
* $sub_id 子账号
*/
*/
public
function
get_trust
(
$sub_id
,
$beginday
,
$endday
){
public
function
get_trust
(
$sub_id
,
$beginday
,
$endday
,
$offset
=
15
){
if
(
empty
(
$endday
)){
$endday
=
time
();}
else
{
$endday
=
strtotime
(
$endday
);}
if
(
empty
(
$endday
)){
$endday
=
time
();}
else
{
$endday
=
strtotime
(
$endday
);}
if
(
empty
(
$beginday
)){
$beginday
=
strtotime
(
date
(
"Y-m-d"
,
time
()));}
else
{
$beginday
=
strtotime
(
$beginday
);}
if
(
empty
(
$beginday
)){
$beginday
=
strtotime
(
date
(
"Y-m-d"
,
time
()));}
else
{
$beginday
=
strtotime
(
$beginday
);}
$res
=
Db
::
name
(
'stock_trust'
)
$res
=
Db
::
name
(
'stock_trust'
)
->
where
([
'sub_id'
=>
$sub_id
])
->
where
([
'sub_id'
=>
$sub_id
])
->
where
(
'trust_date'
,
'>='
,
$beginday
)
->
where
(
'trust_date'
,
'>='
,
$beginday
)
->
where
(
'trust_date'
,
'<='
,
$endday
)
->
where
(
'trust_date'
,
'<='
,
$endday
)
->
order
(
'id desc'
)
->
paginate
(
$offset
,
false
,
[
'query'
=>
request
()
->
param
()])
->
select
();
->
select
();
return
$res
;
return
$res
;
}
}
...
@@ -300,7 +302,7 @@ class Trust extends Model{
...
@@ -300,7 +302,7 @@ class Trust extends Model{
if
(
$res
)
return
[
'status'
=>
0
,
'message'
=>
'该股票禁止交易'
];
if
(
$res
)
return
[
'status'
=>
0
,
'message'
=>
'该股票禁止交易'
];
//判断购买数量书否正确
//判断购买数量书否正确
if
((
$data
[
'count'
]
%
100
)
!=
0
)
return
[
'status'
=>
0
,
'message'
=>
'交易数量必须是100的整数倍'
];
if
((
$data
[
'count'
]
%
100
)
!=
0
)
return
[
'status'
=>
0
,
'message'
=>
'交易数量必须是100的整数倍'
];
$bs_res
=
Db
::
name
(
'stock_borrow'
)
->
where
(
array
(
'stock_subaccount_id'
=>
$data
[
'subid'
]))
->
find
();
$bs_res
=
Db
::
name
(
'stock_borrow'
)
->
where
(
array
(
'stock_subaccount_id'
=>
$data
[
'sub
_
id'
]))
->
find
();
if
(
empty
(
$bs_res
))
{
if
(
empty
(
$bs_res
))
{
return
[
'status'
=>
0
,
'message'
=>
'没有对应的配资'
];
return
[
'status'
=>
0
,
'message'
=>
'没有对应的配资'
];
}
}
...
...
application/market/validate/Trade.php
View file @
34ed11aa
<?php
<?php
namespace
app\
apicom
\validate
;
namespace
app\
market
\validate
;
use
think\Validate
;
use
think\Validate
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment