Files
li 1b24994e74 feat: 后端全量更新 - 含所有本次需求
- Contract.php: 返回合约账户余额(balance_contract)
- My.php: 地址管理增加BTC/ETH
- AppContract.php: 一键平仓(closeall)
- AppProxy.php: 代理专属注册链接 + 分级权限(L1/L2)
- site.php: 手续费减半(0.018→0.009)
- agent_permission_setup.sql: 代理权限SQL
- crypto_news_crawler.py: 新闻自动采集脚本
2026-03-30 20:16:32 +08:00

2359 lines
100 KiB
PHP
Executable File

<?php
namespace app\api\controller;
use app\common\controller\Api;
use fast\Random;
use think\Db;
use think\Config;
use function fast\e;
/**
* 现货
*/
class Task extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
//平仓处理(秒合约)
public function pc_deal()
{
$redis = getRedis();
$dealorder = json_decode($redis->get('deal_order'),true);
$info = '';
$order = Db::name('order');
if(!$dealorder){
$dealorder = [];
}else{
$order->whereNotIn('id',$dealorder);
}
$info .= "deal_order:".json_encode($dealorder);
$orderdata = $order
->where('open_time','<=',time())
// ->where('id','>','38886')
->order('id','asc')->limit(1)->select();
$orders = array_column($orderdata,'id');
$info .= "********noworder:".json_encode($orders);
$sforder = [];
if($orders) {
//设定待处理的order
$neworder = array_merge_recursive($dealorder, $orders);
$redis->set('deal_order',json_encode($neworder),120);
$info .= "******allorder:".json_encode($neworder);
foreach ($orderdata as $key=>$value)
{
$tablename = 'hb_1s_'.str_replace('usdt','',$value['symbol']);
$pctime = $value['createtime'] + $value['trade_cycle'];
$current_price = Db::name($tablename)
->where('ts','<=',$pctime)
->order('ts','desc')
->find();
$ctrldata = Db::name('hb_ctrl')
->where('ts',$pctime)->find();
if($ctrldata){
$pcprice = $ctrldata['price'];
}else{
$pcprice = $current_price['price'];
}
if(!$pcprice){
$sforder[] = $value['id'];
continue;
}
//设置盈亏生效
if($value['set_loss'] == 1){
//强行盈利
if($value['rise_or_fall'] == 'rise' && $value['current_price']>$pcprice){
$cha_price = $value['current_price']-$pcprice;
$pcprice = $value['current_price']+($cha_price/20);
}
if($value['rise_or_fall'] == 'fall' && $value['current_price']<$pcprice){
$cha_price = $pcprice-$value['current_price'];
$pcprice = $value['current_price']-($cha_price/20);
}
}elseif($value['set_loss'] == 2){
//强行亏损
if($value['rise_or_fall'] == 'rise' && $value['current_price']<$pcprice){
$cha_price = $pcprice - $value['current_price'];
$pcprice = $value['current_price']-($cha_price/20);
}
if($value['rise_or_fall'] == 'fall' && $value['current_price']>$pcprice){
$cha_price = $value['current_price']-$pcprice;
$pcprice = $value['current_price']+($cha_price/20);
}
}
$hisorder = $value;
unset($hisorder['id']);
$hisorder['close_price'] = $pcprice; //平仓价格
$hisorder['open_time'] = $pctime; //结算时间
if(($value['rise_or_fall'] == 'rise' && $value['current_price']<$pcprice) ||
($value['rise_or_fall'] == 'fall' && $value['current_price']>$pcprice))
{
$hisorder['trade_result'] = 'win'; //交易结果:win=盈,loss=亏,equals=平
$hisorder['trade_result_num'] = ($value['expect_percent']*$hisorder['trade_num']); //盈亏数量
$description = '周期持仓获利';
$cart = '1';
}else if($value['current_price'] == $pcprice)
{
$hisorder['trade_result'] = 'equals'; //交易结果:win=盈,loss=亏,equals=平
$hisorder['trade_result_num'] = 0; //盈亏数量
$description = '周期持仓平仓';
$cart = '1';
}else{
$hisorder['trade_result'] = 'loss'; //交易结果:win=盈,loss=亏,equals=平
$hisorder['trade_result_num'] = 0-($value['expect_percent']*$hisorder['trade_num']); //盈亏数量
$description = '周期持仓亏损';
$cart = '2';
}
$currency = Db::name('app_currency_user')
->field('num,id')
->where('user_id',$value['user_id'])
->where('curr_id',1)->find();
$lostnum = $currency['num'] + $hisorder['trade_num'] + $hisorder['trade_result_num'];
//收支记录
$detailed = [
'user_id' => $value['user_id'],
'curr_id' => 1,
'price' => abs($hisorder['trade_result_num']),
'cart' => $cart,
'type' => '3',
'serial_no' => $value['serial_no'],
'order_result' => 'result',
'description' => $description,
'createtime' => time(),
'notice' => '周期持仓',
'before_num' => $currency['num']+ $hisorder['trade_num'],
'after_num' => $lostnum,
];
// //计算返利三代
// $nowuser = Db::name('user')
// ->where('id',$value['user_id'])
// ->field('id,pid,grade,path,mobile')->find();
// //上一代
// $puser1 = Db::name('user')
// ->where('id',$nowuser['pid'])
// ->field('id,pid,grade')->find();
// $detailed1 = $detailed2 = $detailed3 = [];
// if($puser1 && $puser1['grade']>1 && $puser1['grade']<5){
// $grade = Db::name('user_grade')
// ->where('id',$puser1['grade'])->find();
// $gradejson = json_decode($grade['rejson'],true);
// $flprice = sprintf("%.4f",$value['trade_num'] * $gradejson[1]);
// $currency1 = Db::name('app_currency_user')
// ->field('num,id')
// ->where('user_id',$puser1['id'])
// ->where('curr_id',1)->find();
// $changenum1 = $currency1['num']+ $flprice;
// $detailed1 = [
// 'user_id' => $puser1['id'],
// 'curr_id' => 1,
// 'price' => $flprice,
// 'cart' => '1',
// 'type' => '4',
// 'serial_no' => $value['serial_no'],
// 'order_result' => 'result',
// 'description' => '交易佣金',
// 'createtime' => time(),
// 'notice' => '佣金入账:'.$nowuser['mobile']." 购买".$value['trade_num']." 产生佣金".$flprice,
// 'before_num' => $currency1['num'],
// 'after_num' => $changenum1,
// ];
// }
// //上二代
// if($puser1) {
// $puser2 = Db::name('user')
// ->where('id', $puser1['pid'])
// ->field('id,pid,grade')->find();
// if ($puser2['grade'] > 1 && $puser2['grade'] < 5) {
// $grade = Db::name('user_grade')
// ->where('id',$puser2['grade'])->find();
// $gradejson = json_decode($grade['rejson'],true);
// $flprice = sprintf("%.4f",$value['trade_num'] * $gradejson[2]);
// $currency2 = Db::name('app_currency_user')
// ->field('num,id')
// ->where('user_id',$puser2['id'])
// ->where('curr_id',1)->find();
// $changenum2 = $currency2['num']+ $flprice;
// $detailed2 = [
// 'user_id' => $puser2['id'],
// 'curr_id' => 1,
// 'price' => $flprice,
// 'cart' => '1',
// 'type' => '4',
// 'serial_no' => $value['serial_no'],
// 'order_result' => 'result',
// 'description' => '交易佣金',
// 'createtime' => time(),
// 'notice' => '佣金入账:'.$nowuser['mobile']." 购买".$value['trade_num']." 产生佣金".$flprice,
// 'before_num' => $currency2['num'],
// 'after_num' => $changenum2,
// ];
// }
// //上三代
// if ($puser2) {
// $puser3 = Db::name('user')
// ->where('id', $puser2['pid'])
// ->field('id,pid,grade')->find();
// if ($puser3['grade'] > 1 && $puser3['grade'] < 5) {
// $grade = Db::name('user_grade')
// ->where('id',$puser3['grade'])->find();
// $gradejson = json_decode($grade['rejson'],true);
// $flprice = sprintf("%.4f",$value['trade_num'] * $gradejson[3]);
// $currency3 = Db::name('app_currency_user')
// ->field('num,id')
// ->where('user_id',$puser3['id'])
// ->where('curr_id',1)->find();
// $changenum3 = $currency3['num']+ $flprice;
// $detailed3 = [
// 'user_id' => $puser3['id'],
// 'curr_id' => 1,
// 'price' => $flprice,
// 'cart' => '1',
// 'type' => '4',
// 'serial_no' => $value['serial_no'],
// 'order_result' => 'result',
// 'description' => '交易佣金',
// 'createtime' => time(),
// 'notice' => '佣金入账:'.$nowuser['mobile']." 购买".$value['trade_num']." 产生佣金".$flprice,
// 'before_num' => $currency3['num'],
// 'after_num' => $changenum3,
// ];
// }
// }
// }
Db::startTrans();
try {
$ret = Db::name('app_currency_user')
->where('id',$currency['id'])->update(['num'=>$lostnum,'updatetime'=>time()]);
if(!$ret)
{
Db::rollback();
continue;
}
$ret1 = Db::name('his_order')->insertGetId($hisorder);
if(!$ret1)
{
Db::rollback();
continue;
}
$ret2 = Db::name('app_detailed')->insert($detailed);
if(!$ret2)
{
Db::rollback();
continue;
}
$ret3 = Db::name('order')
->where('id',$value['id'])
->delete();
if(!$ret3)
{
Db::rollback();
continue;
}
//返利
// if($detailed1 && $changenum1 > 0)
// {
// Db::name('app_currency_user')
// ->where('id',$puser1['id'])->update(['num'=>$changenum1,'updatetime'=>time()]);
// Db::name('app_detailed')->insert($detailed1);
// }
// if($detailed2 && $changenum2 > 0)
// {
// Db::name('app_currency_user')
// ->where('id',$puser2['id'])->update(['num'=>$changenum2,'updatetime'=>time()]);
// Db::name('app_detailed')->insert($detailed2);
// }
// if($detailed3 && $changenum3 > 0)
// {
// Db::name('app_currency_user')
// ->where('id',$puser3['id'])->update(['num'=>$changenum3,'updatetime'=>time()]);
// Db::name('app_detailed')->insert($detailed3);
// }
Db::commit();
$sforder[] = $value['id'];
} catch (Exception $e) {
Db::rollback();
}
}
$info .= "*******sforder:".json_encode($sforder);
if(!empty($sforder))
{
$noworders = json_decode($redis->get('deal_order'),true);
foreach ($sforder as $key=>$value)
{
$keys = array_search($value, $noworders);
if($keys) unset($noworders[$keys]);
}
sort($noworders);
$redis->set('deal_order',json_encode($neworder),120);
$info .= "*******lastorder:".json_encode($neworder);
}
}
var_dump($info);
}
//清理1s数据
public function del_hbsj()
{
//清理时间
$time = time() - 86400;
$ret = Db::name('hb_1s_bch')->where('ts','<',$time)->delete();
$ret = Db::name('hb_1s_btc')->where('ts','<',$time)->delete();
$ret = Db::name('hb_1s_eth')->where('ts','<',$time)->delete();
$ret = Db::name('hb_1s_ltc')->where('ts','<',$time)->delete();
$ret = Db::name('hb_1s_eos')->where('ts','<',$time)->delete();
echo "del success";
}
public function get_allb()
{
$user = Db::name('app_currency_user')
->where('tron_address','<>','')
->where('num','>',0)
->select();
foreach ($user as $key=>$value){
$data = [
'command' => "getBalance",
'code' => "tronapi2020.1",
'address' => $value['tron_address'],
'contract' => 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t' //usdt
];
$usdt = json_decode(http_curl(Config::get('site.tron_http'), 'post', $data), true);
$usdt = $usdt['data'];
if($usdt > 0) {
Db::name('app_currency_user')->where('user_id', $value['user_id'])
->update(['trxusdt' => $usdt, 'listentimes' => time()]);
echo "<pre>";
echo $value['tron_address'].'-----'. base64_decode($value['tron_private_key']).'-----'.$usdt;
}
}
}
//止盈止损平仓
public function contract_deal()
{
$redis = getRedis();
echo "合约止盈止损---";
$running = $redis->get('contract_deal');
if($running) {
echo' is running';
$redis->set('contract_deal', false);
exit;
}
$contract = Db::name('app_contract')
->where('status','1')
// ->where('user_id','3736')
->select();
// echo '<pre>';var_dump($contract);exit;
foreach ($contract as $key=>$value)
{
if($value['zy_price'] <= 0 && $value['zs_price']<=0) continue;
$coin = Db::name('app_rate')
->where('symbol',$value['symbol'])
->find();
//调整价格
$tradejson = json_decode($coin['tradectrl_json'],true);
$jd = $coin['jd'];
$hour = date('H');
$minute = date('i');
$prices = $num = 0;
if($tradejson) {
foreach ($tradejson as $keys => $values) {
$time1 = explode('-', $keys);
$time2 = explode(':', $time1[0]);
$time3 = explode(':', $time1[1]);
$pricearr = explode('-', $values);
if ($hour >= $time2[0] && $hour <= $time3[0]) {
if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue;
$prices = (float)$this->randomFloat($pricearr[0], $pricearr[1], $jd);
break;
}
}
if ($prices > 0) $coin['close'] = $prices;
}
$user = Db::name('user')->where('id',$value['user_id'])->find();
//个人调整价格
$tradejsons = json_decode($user['tradectrl_json'],true);
if($tradejsons) {
$pricess = 0;
foreach ($tradejsons as $keys => $values) {
if($keys == $coin['symbol']) {
$jsonarr = explode('|',$values);
if(!isset($jsonarr[0]) || !isset($jsonarr[1])) continue;
$time1 = explode('-',$jsonarr[0]);
$time2 = explode(':', $time1[0]);
$time3 = explode(':', $time1[1]);
$pricearr = explode('-', $jsonarr[1]);
if(!isset($pricearr[0]) || !isset($pricearr[1])) continue;
if ($hour >= $time2[0] && $hour <= $time3[0]) {
if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue;
$pricess = (float)$this->randomFloat($pricearr[0], $pricearr[1], $jd);
break;
}
}
}
if ($pricess > 0) $coin['close'] = $pricess;
}
$release = Db::name("app_curr_release")->where("symbol",$coin['symbol'])->find();
if($release){
$trademulte_json = json_decode($release['trademulte_json'],true);
if($trademulte_json && !$prices){
foreach ($trademulte_json as $kk => $vv) {
$time1 = explode('-', $kk);
$time2 = explode(':', $time1[0]);
$time3 = explode(':', $time1[1]);
if ($hour >= $time2[0] && $hour <= $time3[0]) {
if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue;
$datass = json_decode(http_curl('https://api.huobi.pro/market/history/kline?period=1min&size=1&symbol='.$release['symbol'],'get'),true);
if(!$datass || !isset($datass['data']) || empty($datass['data']))
{
$coin['close'] = $coin['close'];
break;
}
$coin['close'] = $datass['data'][0]['close'] * $vv;
break;
}
}
}
}
// if(($coin['close'] > $value['zy_price'] && $coin['close']>$value['price'] && $value['zy_price'] > 0) ||
// ($coin['close'] < $value['zs_price'] && $coin['close']<$value['price'] && $value['zs_price'] > 0)) {
// var_dump($value['type'],$coin['close'],$value['zy_price'],$value['zs_price']);
$flag = false;
$newprices = 0;
if($value['type'] == 'more' && $coin['close'] > $value['zy_price'] && $value['zy_price']>0){
$flag = true;
$newprices = $value['zy_price'];
}else if($value['type'] == 'more' && $coin['close'] < $value['zs_price'] && $value['zs_price'] > 0){
$flag = true;
$newprices = $value['zs_price'];
}
if($value['type'] == 'free' && $coin['close'] < $value['zy_price'] && $value['zy_price']>0){
$flag = true;
$newprices = $value['zy_price'];
}else if($value['type'] == 'free' && $coin['close'] > $value['zs_price'] && $value['zs_price']>0){
$flag = true;
$newprices = $value['zs_price'];
}
if(!$flag) continue;
$coin['close'] = $newprices;
// if($coin['close'] > $value['zy_price'] && $value['zy_price'] > 0)
// {
// $coin['close'] = $value['zy_price'];
// }else if($coin['close'] < $value['zs_price'] && $value['zs_price'] > 0)
// {
// $coin['close'] = $value['zs_price'];
// }
$diffnum = $coin['close'] - $value['price'];
if ($value['type'] == 'more') {
//计算收益
// $income = $diffnum / 100 * $value['num'] * $value['multiple'] * Config::get('site.shizhi');
$income = $diffnum * $value['num'] * Config::get('site.shizhi');
} else {
if ($diffnum < 0) {
// $income = abs($diffnum) / 100 * $value['num'] * $value['multiple'] * Config::get('site.shizhi');
$income = abs($diffnum) * $value['num'] * Config::get('site.shizhi');
} else {
// $income = 0 - $diffnum / 100 * $value['num'] * $value['multiple'] * Config::get('site.shizhi');
$income = 0 - $diffnum * $value['num'] * Config::get('site.shizhi');
}
}
$benjin = $value['promise_fee'];
$income = sprintf('%.6f', $income);
$flags = false;
//爆仓
if($income<0 && abs($income) > $benjin)
{
$flags = true;
$income = 0 - $benjin;
}
$currency = Db::name('app_currency_user')
->field('num,id')
->where('user_id',$value['user_id'])
->where('curr_id',1)->find();
$detailed = [
'user_id' => $value['user_id'],
'curr_id' => 1,
'price' => abs($income),
'cart' => '2',
'type' => '6',
'serial_no' => time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '合约交易平仓',
'createtime' => time(),
'notice' => '合约交易平仓',
'before_num' => $currency['num'],
'after_num' => $currency['num'] + $income,
];
if($income >0 ){
$detailed['cart'] = '1';
$detailed['description'] = '合约交易平仓盈利';
}else{
$detailed['cart'] = '2';
$detailed['description'] = '合约交易平仓亏损';
}
$detaileds[] = $detailed;
if(!$flags) {
$detaileds[] = [
'user_id' => $value['user_id'],
'curr_id' => 1,
'price' => $value['promise_fee'],
'cart' => '1',
'type' => '6',
'serial_no' => time() . Random::build('numeric', 4),
'order_result' => 'result',
'description' => '合约交易保证金',
'createtime' => time(),
'notice' => '合约交易保证金',
'before_num' => $detailed['after_num'],
'after_num' => $detailed['after_num'] + $value['promise_fee'],
];
$lostnum = sprintf("%.6f",($currency['num'] + $income + $value['promise_fee']));
}
Db::startTrans();
try {
if(!$flags) {
$ret = Db::name('app_currency_user')
->where('id', $currency['id'])->update(['num' => $lostnum, 'updatetime' => time()]);
if (!$ret) {
Db::rollback();
continue;
}
}
$ret1 = Db::name('app_contract')->where('id',$value['id'])
->update(['status'=>'2','pc_price'=>$coin['close'],'pctime'=>time(),'income'=>$income]);
if(!$ret1)
{
Db::rollback();
continue;
}
$ret2 = Db::name('app_detailed')->insertAll($detaileds);
if(!$ret2)
{
Db::rollback();
continue;
}
Db::commit();
echo '合约-'.$value['id'].'止盈止损平仓----';
} catch (Exception $e) {
Db::rollback();
}
}
// }
$redis->set('contract_deal', false);
}
//爆仓处理
public function baocang()
{
$redis = getRedis();
echo "合约爆仓---";
$running = $redis->get('baocang');
if($running) {
echo' is running';
$redis->set('baocang', false);
exit;
}
$contract = Db::name('app_contract')
// ->where('user_id',3727)
->where('status','1')->select();
foreach ($contract as $key=>$value)
{
$coin = Db::name('app_rate')
->where('symbol',$value['symbol'])
->find();
//调整价格
$tradejson = json_decode($coin['tradectrl_json'],true);
$jd = $coin['jd'];
$hour = date('H');
$minute = date('i');
$prices = $num = 0;
if($tradejson) {
foreach ($tradejson as $keys => $values) {
$time1 = explode('-', $keys);
$time2 = explode(':', $time1[0]);
$time3 = explode(':', $time1[1]);
$pricearr = explode('-', $values);
if ($hour >= $time2[0] && $hour <= $time3[0]) {
if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue;
$prices = (float)$this->randomFloat($pricearr[0], $pricearr[1], $jd);
break;
}
}
if ($prices > 0) $coin['close'] = $prices;
}
$user = Db::name('user')->where('id',$value['user_id'])->find();
//个人调整价格
$tradejsons = json_decode($user['tradectrl_json'],true);
if($tradejsons) {
$pricess = 0;
foreach ($tradejsons as $keys => $values) {
if($keys == $coin['symbol']) {
$jsonarr = explode('|',$values);
if(!isset($jsonarr[0]) || !isset($jsonarr[1])) continue;
$time1 = explode('-',$jsonarr[0]);
$time2 = explode(':', $time1[0]);
$time3 = explode(':', $time1[1]);
$pricearr = explode('-', $jsonarr[1]);
if(!isset($pricearr[0]) || !isset($pricearr[1])) continue;
if ($hour >= $time2[0] && $hour <= $time3[0]) {
if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue;
$pricess = (float)$this->randomFloat($pricearr[0], $pricearr[1], $jd);
break;
}
}
}
if ($pricess > 0) $coin['close'] = $pricess;
}
$release = Db::name("app_curr_release")->where("symbol",$coin['symbol'])->find();
if($release){
$trademulte_json = json_decode($release['trademulte_json'],true);
if($trademulte_json && !$prices){
foreach ($trademulte_json as $kk => $vv) {
$time1 = explode('-', $kk);
$time2 = explode(':', $time1[0]);
$time3 = explode(':', $time1[1]);
if ($hour >= $time2[0] && $hour <= $time3[0]) {
if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue;
$datass = json_decode(http_curl('https://api.huobi.pro/market/history/kline?period=1min&size=1&symbol='.$release['symbol'],'get'),true);
if(!$datass || !isset($datass['data']) || empty($datass['data']))
{
$coin['close'] = $coin['close'];
break;
}
$coin['close'] = $datass['data'][0]['close'] * $vv;
break;
}
}
}
}
$diffnum = $coin['close'] - $value['price'];
if($value['type'] == 'more')
{
//计算收益
// $income = $diffnum / 100 * $value['num'] * $value['multiple'] * Config::get('site.shizhi');
$income = $diffnum * $value['num'] * Config::get('site.shizhi');
}else{
if($diffnum < 0) {
// $income = abs($diffnum) / 100 * $value['num'] * $value['multiple'] * Config::get('site.shizhi');
$income = abs($diffnum) * $value['num'] * Config::get('site.shizhi');
}else{
// $income = 0 - $diffnum / 100 * $value['num'] * $value['multiple'] * Config::get('site.shizhi');
$income = 0 - $diffnum * $value['num'] * Config::get('site.shizhi');
}
}
$benjin = $value['promise_fee'];
$income = sprintf('%.6f',$income);
$flags = false;
//爆仓
if($income<0 && abs($income) > $benjin)
{
$currency = Db::name('app_currency_user')
->field('num,id')
->where('user_id',$value['user_id'])
->where('curr_id',1)->find();
$flags = true;
$income = 0 - $benjin;
$detailed = [
'user_id' => $value['user_id'],
'curr_id' => 1,
'price' => abs($income),
'cart' => '2',
'type' => '6',
'serial_no' => time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '合约交易平仓',
'createtime' => time(),
'notice' => '合约交易平仓',
'before_num' => $currency['num'],
'after_num' => $currency['num'],
];
if($income >0 ){
$detailed['cart'] = '1';
$detailed['description'] = '合约交易平仓盈利';
}else{
$detailed['cart'] = '2';
$detailed['description'] = '合约交易平仓亏损';
}
$ret1 = Db::name('app_contract')->where('id',$value['id'])
->update(['status'=>'2','pc_price'=>$coin['close'],'pctime'=>time(),'income'=>$income]);
$ret2 = Db::name('app_detailed')->insert($detailed);
echo '合约-'.$value['id'].'-爆仓------';
}
}
$redis->set('baocang', false);
// echo "<pre>";var_dump($contract);
}
//限价合约入场
public function limit_comein()
{
echo "合约";
$redis = getRedis();
$running = $redis->get('contractdeal');
if($running) {
echo' is running';
$redis->set('contractdeal', false);
exit;
}
$contract = Db::name('app_contract')
->where('status','0')->order('id','asc')->select();
foreach ($contract as $key=>$value)
{
$coin = Db::name('app_rate')
->where('id',$value['coin_id'])->find();
//调整价格
$tradejson = json_decode($coin['tradectrl_json'],true);
$jd = $coin['jd'];
$hour = date('H');
$minute = date('i');
$prices = $coin['close'];
$num = 0;
if($tradejson) {
foreach ($tradejson as $keys => $values) {
$time1 = explode('-', $keys);
$time2 = explode(':', $time1[0]);
$time3 = explode(':', $time1[1]);
$pricearr = explode('-', $values);
if ($hour >= $time2[0] && $hour <= $time3[0]) {
if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue;
$prices = (float)$this->randomFloat($pricearr[0], $pricearr[1], $jd);
break;
}
}
if ($prices > 0) $coin['close'] = $prices;
}
$user = Db::name('user')->where('id',$value['user_id'])->find();
//个人调整价格
$tradejsons = json_decode($user['tradectrl_json'],true);
if($tradejsons) {
$pricess = 0;
foreach ($tradejsons as $keys => $values) {
if($keys == $coin['symbol']) {
$jsonarr = explode('|',$values);
if(!isset($jsonarr[0]) || !isset($jsonarr[1])) continue;
$time1 = explode('-',$jsonarr[0]);
$time2 = explode(':', $time1[0]);
$time3 = explode(':', $time1[1]);
$pricearr = explode('-', $jsonarr[1]);
if(!isset($pricearr[0]) || !isset($pricearr[1])) continue;
if ($hour >= $time2[0] && $hour <= $time3[0]) {
if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue;
$pricess = (float)$this->randomFloat($pricearr[0], $pricearr[1], $jd);
break;
}
}
}
if ($pricess > 0) $coin['close'] = $pricess;
$prices = $coin['close'];
}
if($prices <= 0) continue;
$flag = false;
if($value['type'] == 'more' && $coin['close'] <= $value['price']){
$flag = true;
}else if($value['type'] == 'free' && $coin['close'] >= $value['price'])
{
$flag = true;
}
if($flag)
{
$update = [
'status' => '1',
// 'price' => $prices,
'updatetime' => time()
];
$ret = Db::name('app_contract')->where('id',$value['id'])
->update($update);
echo "限价合约:".$value['id'].'入场';
if($ret)
break;
}
}
$redis->set('contractdeal', false);
}
//币币撮合交易
public function coin_deal()
{
echo 'coin trade ---';
$order = Db::name('app_coin_trade')
->whereIn('status','0,1')
->where('cart','market')
->order('id','desc')->find();
$rates = Db::name('app_rate')
->select();
$rate_arr = array_column($rates,'close','id');
if(empty($order))
{
$orders = Db::name('app_coin_trade a')
->field('a.*')
->join('app_rate b','a.coin_id=b.id','left')
->whereIn('a.status','0,1')
->where('a.type','buy')
->where('a.cart','limit')
->select();
$order = [];
foreach ($orders as $key => $value)
{
if(isset($rate_arr[$value['coin_id']]) && $value['price'] >= $rate_arr[$value['coin_id']]){
$order = $value;
break;
}
}
if(empty($order))
{
$orders = Db::name('app_coin_trade a')
->field('a.*')
->join('app_rate b','a.coin_id=b.id','left')
->whereIn('a.status','0,1')
->where('a.type','sell')
->where('a.cart','limit')
->select();
$order = [];
foreach ($orders as $key => $value)
{
if(isset($rate_arr[$value['coin_id']]) && $value['price'] <= $rate_arr[$value['coin_id']]){
$order = $value;
break;
}
}
}
}
// echo "<pre>";
// var_dump($order);exit;
if(empty($order)){
echo "暂无订单";exit;
}
$redis = getRedis();
$running = $redis->get('cointrades');
if($running) {
echo' is running';
$redis->set('cointrades', false);
exit;
}
$redis->set('cointrades', time(), 60);
$coin = Db::name('app_rate')
->where('id',$order['coin_id'])
->find();
$currency = Db::name('app_currency')
->where('suffix',$coin['symbol'])->find();
$prices = $coin['close'];
$user = Db::name('user')->where('id',$order['user_id'])->find();
if($order['cart'] == 'limit' && $order['type'] == 'buy' && $order['price'] < $prices)
{
echo $order['id'].'-'.'价格未达限价-'.$order['price'].'-当前价格-'.$prices;
$redis->set('cointrades', false);
exit;
}
$trade_order = [];
$ordersn = 'C' . time() . Random::build('numeric', 4);
//质押手续费折扣
$count_zhiya = Db::name("app_zhiya_user")->where("user_id", $order['user_id'])->where("status",1)->sum("num");
$zhiya_free_rebate = Config::get("site.zhiya_free_rebate");
$zhekou = 1;
foreach ($zhiya_free_rebate as $key => $value) {
if($count_zhiya > $key){
$zhekou = $value;
}
}
//手续费
$market_bb_free = Config::get("site.market_bb_free")*$zhekou/100;
//现价买 市价买(高->低) 现价卖 市价卖(低->高)
if($order['type'] == 'buy') {
$tradenum = sprintf('%.6f', $order['amount'] / $prices);
$amount = $order['amount'];
$fee = sprintf('%.8f', $tradenum * $market_bb_free);
$fee_type = 1;
////是否开启APT手续费支付
if($user['apy_pay'] == 1){
//开启apt手续费支付
$apt_free = Config::get("site.apt_free");
$apy_curr = Db::name("app_currency")->where("id",2)->find();
$res_curr = Db::name("app_currency")->where("suffix",$coin['symbol'])->find();
$apy_num = sprintf("%.8f",$fee*$res_curr['exchange']/$apy_curr['exchange']*$apt_free);
$user_apy = Db::name('app_currency_user')->where('curr_id',2)->where('user_id',$order['user_id'])->find();
if($user_apy['num'] >= $apy_num){
//达到APT手续费支付条件
$fee = $apy_num;
$fee_type = 2;
$detailed[] = [
'user_id' => $order['user_id'],
'curr_id' => $user_apy['curr_id'],
'price' => $apy_num,
'cart' => '2',
'type' => '7',
'serial_no' => 'C'.time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '币币APT手续费',
'createtime' => time(),
'notice' => '币币APT手续费',
'before_num' => $user_apy['num'],
'after_num' => $user_apy['num'] - $apy_num,
];
}
}
$trade_order[] = [
'user_id' => $order['user_id'],
'coin_id' => $order['coin_id'],
'trade_id' => $order['id'],
'num' => $tradenum,
'price' => $prices,
'amount' => $order['amount'],
'fee' => $fee,
'createtime' => time(),
'type' => 'buy',
'order_sn' => $ordersn,
'fee_type' => $fee_type,
];
}else{
$tradenum = $order['num'];
$amount = sprintf('%.6f',$tradenum * $prices);
$fee = sprintf('%.8f',$amount*$market_bb_free);
$fee_type = 1;//
////是否开启APT手续费支付
if($user['apy_pay'] == 1){
//开启apt手续费支付
$apt_free = Config::get("site.apt_free");
$apy_curr = Db::name("app_currency")->where("id",2)->find();
$res_curr = Db::name("app_currency")->where("suffix",$coin['symbol'])->find();
$apy_num = sprintf("%.6f",$fee*$res_curr['exchange']/$apy_curr['exchange']*$apt_free);
$user_apy = Db::name('app_currency_user')->where('curr_id',2)->where('user_id',$order['user_id'])->find();
if($user_apy['num'] >= $apy_num){
//达到APT手续费支付条件
$fee = $apy_num;
$fee_type = 2;
$detailed[] = [
'user_id' => $order['user_id'],
'curr_id' => $user_apy['curr_id'],
'price' => $apy_num,
'cart' => '2',
'type' => '7',
'serial_no' => 'C'.time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '币币APT手续费',
'createtime' => time(),
'notice' => '币币APT手续费',
'before_num' => $user_apy['num'],
'after_num' => $user_apy['num'] - $apy_num,
];
}
}
$trade_order[] = [
'user_id' => $order['user_id'],
'coin_id' => $order['coin_id'],
'trade_id' => $order['id'],
'num' => $tradenum,
'price' => $prices,
'amount' => $amount,
'fee' => $fee,
'createtime' => time(),
'type' => 'sell',
'order_sn' => $ordersn,
'fee_type' => $fee_type,
];
}
Db::startTrans();
try {
Db::name('app_coin_trade_order')->insertAll($trade_order);
$update1['status'] = '2';
$update1['finishtime'] = time();
if($order['cart'] == 'market'){
$update1['price'] = $prices;
}
$update1['num'] = $tradenum;
$update1['amount'] = $amount;
$update1['updatetime'] = time();
Db::name('app_coin_trade')->where('id', $order['id'])->update($update1);
if($order['type'] == 'buy') {
$currency1 = Db::name('app_currency_user')->where('user_id', $order['user_id'])->where('curr_id', $currency['id'])->find();
if($fee_type == 2){
//使用APT来支付手续费
$upnum = $currency1['num'] + $tradenum;
Db::name('app_currency_user')->where('id', $user_apy['id'])->update(['num' => $user_apy['num']-$apy_num]);
$last_price = $tradenum;
}else{
$upnum = $currency1['num'] + $tradenum - $trade_order[0]['fee'];
$last_price = $tradenum - $trade_order[0]['fee'];
}
Db::name('app_currency_user')->where('id', $currency1['id'])->update(['num' => $upnum, 'updatetime' => time()]);
$detailed[] = [
'user_id' => $order['user_id'],
'curr_id' => $currency['id'],
'price' => $last_price,
'cart' => '1',
'type' => '7',
'serial_no' => 'C'.time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '币币交易买入',
'createtime' => time(),
'notice' => '币币交易买入',
'before_num' => $currency1['num'],
'after_num' => $upnum,
];
Db::name('app_detailed')->insertAll($detailed);
}else{
$currency = Db::name('app_currency_user')->where('user_id', $order['user_id'])->where('curr_id', 1)->find();
if($fee_type == 2){
//使用APT来支付手续费
$upnum = $currency['num'] + $amount;
Db::name('app_currency_user')->where('id', $user_apy['id'])->update(['num' => $user_apy['num']-$apy_num]);
$last_price = $amount;
}else{
$upnum = $currency['num'] + $amount - $trade_order[0]['fee'];
$last_price = $amount - $trade_order[0]['fee'];
}
Db::name('app_currency_user')->where('id', $currency['id'])->update(['num' => $upnum, 'updatetime' => time()]);
$detailed[] = [
'user_id' => $order['user_id'],
'curr_id' => 1,
'price' => $last_price,
'cart' => '1',
'type' => '7',
'serial_no' => 'C'.time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '币币交易买入',
'createtime' => time(),
'notice' => '币币交易买入',
'before_num' => $currency['num'],
'after_num' => $upnum,
];
Db::name('app_detailed')->insertAll($detailed);
}
Db::commit();
$redis->set('cointrades', false);
echo '交易单:'.$order['id'].'已成交-';
} catch (Exception $e) {
Db::rollback();
}
$redis->set('cointrades', false);
}
//币币撮合交易-所有市价
public function coin_deal_marketall()
{
echo 'coin trade ---';
$order = Db::name('app_coin_trade')
->whereIn('status','0,1')
->where('cart','market')
->order('id','asc')
->select();
if(empty($order))
{
echo "暂无订单";exit;
}
$redis = getRedis();
$running = $redis->get('cointrades_marketall');
if($running) {
echo' is running';
$redis->set('cointrades_marketall', false);
exit;
}
$redis->set('cointrades_marketall', time(), 60);
foreach ($order as $kkk => $vvvvv) {
$coin = Db::name('app_rate')->where('id',$vvvvv['coin_id'])->find();
$currency = Db::name('app_currency')
->where('suffix',$coin['symbol'])->find();
$prices = $coin['close'];
//价格调整
if(strtolower($coin['name'])."usdt" == $coin['symbol']){
$trademulte_json = json_decode($coin['trademulte_json'],true);
$hour = date('H');
$minute = date('i');
if($trademulte_json){
foreach ($trademulte_json as $kk => $vv) {
$time1 = explode('-', $kk);
$time2 = explode(':', $time1[0]);
$time3 = explode(':', $time1[1]);
if ($hour >= $time2[0] && $hour <= $time3[0]) {
if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue;
$prices = $prices * $vv;
break;
}
}
}
}
$trade_order = [];
$ordersn = 'C' . time() . Random::build('numeric', 4);
//质押手续费折扣
$zhekou = 1;
//手续费
$market_bb_free = Config::get("site.market_bb_free")*$zhekou/100;
//现价买 市价买(高->低) 现价卖 市价卖(低->高)
if($vvvvv['type'] == 'buy') {
$tradenum = sprintf('%.6f', $vvvvv['amount'] / $prices);
$amount = $vvvvv['amount'];
$fee = sprintf('%.8f', $tradenum * $market_bb_free);
$fee_type = 1;
$trade_order[] = [
'user_id' => $vvvvv['user_id'],
'coin_id' => $vvvvv['coin_id'],
'trade_id' => $vvvvv['id'],
'num' => $tradenum,
'price' => $prices,
'amount' => $vvvvv['amount'],
'fee' => $fee,
'createtime' => time(),
'type' => 'buy',
'order_sn' => $ordersn,
'fee_type' => $fee_type,
];
}else{
$tradenum = $vvvvv['num'];
$amount = sprintf('%.6f',$tradenum * $prices);
$fee = sprintf('%.8f',$amount*$market_bb_free);
$fee_type = 1;
$trade_order[] = [
'user_id' => $vvvvv['user_id'],
'coin_id' => $vvvvv['coin_id'],
'trade_id' => $vvvvv['id'],
'num' => $tradenum,
'price' => $prices,
'amount' => $amount,
'fee' => $fee,
'createtime' => time(),
'type' => 'sell',
'order_sn' => $ordersn,
'fee_type' => $fee_type,
];
}
Db::startTrans();
try {
$detailed = [];
Db::name('app_coin_trade_order')->insertAll($trade_order);
$update1['status'] = '2';
$update1['finishtime'] = time();
if($vvvvv['cart'] == 'market'){
$update1['price'] = $prices;
}
$update1['num'] = $tradenum;
$update1['amount'] = $amount;
$update1['updatetime'] = time();
Db::name('app_coin_trade')->where('id', $vvvvv['id'])->update($update1);
if($vvvvv['type'] == 'buy') {
$currency1 = Db::name('app_currency_user')->where('user_id', $vvvvv['user_id'])->where('curr_id', $currency['id'])->find();
$upnum = $currency1['num'] + $tradenum - $trade_order[0]['fee'];
$last_price = $tradenum - $trade_order[0]['fee'];
Db::name('app_currency_user')->where('id', $currency1['id'])->update(['num' => $upnum, 'updatetime' => time()]);
$detailed[] = [
'user_id' => $vvvvv['user_id'],
'curr_id' => $currency['id'],
'price' => $last_price,
'cart' => '1',
'type' => '7',
'serial_no' => 'C'.time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '币币交易买入',
'createtime' => time(),
'notice' => '币币交易买入',
'before_num' => $currency1['num'],
'after_num' => $upnum,
];
Db::name('app_detailed')->insertAll($detailed);
}else{
$currency = Db::name('app_currency_user')->where('user_id', $vvvvv['user_id'])->where('curr_id', 1)->find();
$upnum = $currency['num'] + $amount - $trade_order[0]['fee'];
$last_price = $amount - $trade_order[0]['fee'];
Db::name('app_currency_user')->where('id', $currency['id'])->update(['num' => $upnum, 'updatetime' => time()]);
$detailed[] = [
'user_id' => $vvvvv['user_id'],
'curr_id' => 1,
'price' => $last_price,
'cart' => '1',
'type' => '7',
'serial_no' => 'C'.time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '币币交易买入',
'createtime' => time(),
'notice' => '币币交易买入',
'before_num' => $currency['num'],
'after_num' => $upnum,
];
Db::name('app_detailed')->insertAll($detailed);
}
Db::commit();
$redis->set('cointrades_marketall', false);
echo '交易单:'.$vvvvv['id'].'已成交-';
} catch (Exception $e) {
Db::rollback();
}
}
$redis->set('cointrades_marketall', false);
}
//币币撮合交易 - 限价所有
public function coin_deal_limitall()
{
echo 'coin trade ---';
$order = Db::name('app_coin_trade')
->whereIn('status','0,1')
->where('cart','limit')
->order('id','desc')
->select();
if(empty($order)){
echo "暂无订单";exit;
}
$redis = getRedis();
$running = $redis->get('cointrades_limitall');
if($running) {
echo' is running';
$redis->set('cointrades_limitall', false);
exit;
}
$redis->set('cointrades_limitall', time(), 60);
foreach ($order as $kkk => $vvvvv) {
$coin = Db::name('app_rate')->where('id',$vvvvv['coin_id'])->find();
$currency = Db::name('app_currency')
->where('suffix',$coin['symbol'])->find();
$prices = $coin['close'];
//价格调整
if(strtolower($coin['name'])."usdt" == $coin['symbol']){
$trademulte_json = json_decode($coin['trademulte_json'],true);
$hour = date('H');
$minute = date('i');
if($trademulte_json){
foreach ($trademulte_json as $kk => $vv) {
$time1 = explode('-', $kk);
$time2 = explode(':', $time1[0]);
$time3 = explode(':', $time1[1]);
if ($hour >= $time2[0] && $hour <= $time3[0]) {
if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue;
$prices = $prices * $vv;
break;
}
}
}
}
if($vvvvv['cart'] == 'limit' && $vvvvv['type'] == 'buy' && $vvvvv['price'] < $prices)
{
echo $vvvvv['id'].'-'.'价格未达限价-'.$vvvvv['price'].'-当前价格-'.$prices;
$redis->set('cointrades_limitall', false);
continue;
}
if($vvvvv['cart'] == 'limit' && $vvvvv['type'] == 'sell' && $vvvvv['price'] > $prices)
{
echo $vvvvv['id'].'-'.'价格未达限价-'.$vvvvv['price'].'-当前价格-'.$prices;
$redis->set('cointrades_limitall', false);
continue;
}
$trade_order = [];
$ordersn = 'C' . time() . Random::build('numeric', 4);
//质押手续费折扣
$zhekou = 1;
//手续费
$market_bb_free = Config::get("site.market_bb_free")*$zhekou/100;
//现价买 市价买(高->低) 现价卖 市价卖(低->高)
if($vvvvv['type'] == 'buy') {
$tradenum = sprintf('%.6f', $vvvvv['amount'] / $prices);
$amount = $vvvvv['amount'];
$fee = sprintf('%.8f', $tradenum * $market_bb_free);
$fee_type = 1;
$trade_order[] = [
'user_id' => $vvvvv['user_id'],
'coin_id' => $vvvvv['coin_id'],
'trade_id' => $vvvvv['id'],
'num' => $tradenum,
'price' => $prices,
'amount' => $vvvvv['amount'],
'fee' => $fee,
'createtime' => time(),
'type' => 'buy',
'order_sn' => $ordersn,
'fee_type' => $fee_type,
];
}else{
$tradenum = $vvvvv['num'];
$amount = sprintf('%.6f',$tradenum * $prices);
$fee = sprintf('%.8f',$amount*$market_bb_free);
$fee_type = 1;//
$trade_order[] = [
'user_id' => $vvvvv['user_id'],
'coin_id' => $vvvvv['coin_id'],
'trade_id' => $vvvvv['id'],
'num' => $tradenum,
'price' => $prices,
'amount' => $amount,
'fee' => $fee,
'createtime' => time(),
'type' => 'sell',
'order_sn' => $ordersn,
'fee_type' => $fee_type,
];
}
$detailed = [];
Db::startTrans();
try {
Db::name('app_coin_trade_order')->insertAll($trade_order);
$update1['status'] = '2';
$update1['finishtime'] = time();
if($vvvvv['cart'] == 'market'){
$update1['price'] = $prices;
}
$update1['num'] = $tradenum;
$update1['amount'] = $amount;
$update1['updatetime'] = time();
Db::name('app_coin_trade')->where('id', $vvvvv['id'])->update($update1);
if($vvvvv['type'] == 'buy') {
$currency1 = Db::name('app_currency_user')->where('user_id', $vvvvv['user_id'])->where('curr_id', $currency['id'])->find();
$upnum = $currency1['num'] + $tradenum - $trade_order[0]['fee'];
$last_price = $tradenum - $trade_order[0]['fee'];
Db::name('app_currency_user')->where('id', $currency1['id'])->update(['num' => $upnum, 'updatetime' => time()]);
$detailed[] = [
'user_id' => $vvvvv['user_id'],
'curr_id' => $currency['id'],
'price' => $last_price,
'cart' => '1',
'type' => '7',
'serial_no' => 'C'.time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '币币交易买入',
'createtime' => time(),
'notice' => '币币交易买入',
'before_num' => $currency1['num'],
'after_num' => $upnum,
];
Db::name('app_detailed')->insertAll($detailed);
}else{
$currency = Db::name('app_currency_user')->where('user_id', $vvvvv['user_id'])->where('curr_id', 1)->find();
$upnum = $currency['num'] + $amount - $trade_order[0]['fee'];
$last_price = $amount - $trade_order[0]['fee'];
Db::name('app_currency_user')->where('id', $currency['id'])->update(['num' => $upnum, 'updatetime' => time()]);
$detailed[] = [
'user_id' => $vvvvv['user_id'],
'curr_id' => 1,
'price' => $last_price,
'cart' => '1',
'type' => '7',
'serial_no' => 'C'.time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '币币交易买入',
'createtime' => time(),
'notice' => '币币交易买入',
'before_num' => $currency['num'],
'after_num' => $upnum,
];
Db::name('app_detailed')->insertAll($detailed);
}
Db::commit();
$redis->set('cointrades_limitall', false);
echo '交易单:'.$vvvvv['id'].'已成交-';
} catch (Exception $e) {
Db::rollback();
}
}
$redis->set('cointrades_limitall', false);
}
//撮合成交单
public function cuohe_ztc()
{
$coin = Db::name('app_rate')
->where('id',19)->find();
$tradejson = json_decode($coin['tradectrl_json'],true);
$hour = date('H');
$minute = date('i');
$price = $num = 0;
foreach ($tradejson as $key=>$value){
$time1 = explode('-',$key);
$time2 = explode(':',$time1[0]);
$time3 = explode(':',$time1[1]);
$pricearr = explode('-',$value);
if($hour >= $time2[0] && $hour <= $time3[0]){
if(($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue;
$price = $this->randomFloat($pricearr[0],$pricearr[1],6);
$num = $this->randomFloat(1,10,6);
}
}
if($price == 0 || $num == 0)
{
echo 'price null';exit;
}
$data = [
'symbol' => 'ztcusdt',
'ts' => time(),
'price' => $price,
'amount' => $num,
'source' =>0
];
$ret = Db::name('ztc_1s')->insert($data);
echo "update ztc";
}
//4位小数的随机数
public function randomFloat($min = 0, $max = 10 , $localnum = 4)
{
if($localnum == 4) {
if ($max - $min <= 0.0002) {
return 0;
}
$rand = mt_rand() / mt_getrandmax() * ($max - $min-0.0001);
$num = $min + $rand;
$number = sprintf("%.4f", $num);
if($number == $min){
$number += 0.0001;
}
}else if($localnum == 5){
if ($max - $min <= 0.00002) {
return 0;
}
$rand = mt_rand() / mt_getrandmax() * ($max - $min-0.00001);
$num = $min + $rand;
$number = sprintf("%.5f", $num);
if($number == $min){
$number += 0.00001;
}
}else if($localnum == 6){
if ($max - $min <= 0.000002) {
return 0;
}
$rand = mt_rand() / mt_getrandmax() * ($max - $min-0.000001);
$num = $min + $rand;
$number = sprintf("%.6f", $num);
if($number == $min){
$number += 0.000001;
}
}else if($localnum == 2){
if ($max - $min <= 0.02) {
return 0;
}
$rand = mt_rand() / mt_getrandmax() * ($max - $min-0.01);
$num = $min + $rand;
$number = sprintf("%.2f", $num);
if($number == $min){
$number += 0.01;
}
}else if($localnum == 3){
if ($max - $min <= 0.001) {
return 0;
}
$rand = mt_rand() / mt_getrandmax() * ($max - $min-0.001);
$num = $min + $rand;
$number = sprintf("%.3f", $num);
if($number == $min){
$number += 0.001;
}
}
return $number;
}
//更新ztcK线数据
public function update_ztc_kline()
{
echo "start ".date('Y-m-d H:i:s');
$alltrade = Db::name('ztc_1s')
->order('id','desc')
->limit(100)
->select();
$last_names = array_column($alltrade,'id');
array_multisort($last_names,SORT_ASC,$alltrade);
$nowtime = strtotime(date('H:i'));
//更新分钟
$mindata = Db::name('ztc_k_1min')
->order('ts','desc')->find();
$start1min = $mindata['ts'];
$end1min = $mindata['ts'] + 60;
$mints = $mindata['ts'];
$allmints[$mints] = $mindata;
//更新5分钟
$min5data = Db::name('ztc_k_5min')
->order('ts','desc')->find();
$start5min = $min5data['ts'];
$end5min = $min5data['ts'] + 300;
$min5ts = $min5data['ts'];
$allmin5ts[$min5ts] = $min5data;
//更新15分钟
$min15data = Db::name('ztc_k_15min')
->order('ts','desc')->find();
$start15min = $min15data['ts'];
$end15min = $min15data['ts'] + 900;
$min15ts = $min15data['ts'];
$allmin15ts[$min15ts] = $min15data;
//更新30分钟
$min30data = Db::name('ztc_k_30min')
->order('ts','desc')->find();
$start30min = $min30data['ts'];
$end30min = $min30data['ts'] + 1800;
$min30ts = $min30data['ts'];
$allmin30ts[$min30ts] = $min30data;
//更新1小时
$min1hdata = Db::name('ztc_k_1h')
->order('ts','desc')->find();
$start1hmin = $min1hdata['ts'];
$end1hmin = $min1hdata['ts'] + 3600;
$min1hts = $min1hdata['ts'];
$allmin1hts[$min1hts] = $min1hdata;
//更新24小时
$min24hdata = Db::name('ztc_k_24h')
->order('ts','desc')->find();
$start24hmin = $min24hdata['ts'];
$end24hmin = $min24hdata['ts'] + 86400;
$min24hts = $min24hdata['ts'];
$allmin24hts[$min24hts] = $min24hdata;
//更新1周
$min1wdata = Db::name('ztc_k_1week')
->order('ts','desc')->find();
$start1wmin = $min1wdata['ts'];
$end1wmin = $min1wdata['ts'] + 604800;
$min1wts = $min1wdata['ts'];
$allmin1wts[$min1wts] = $min1wdata;
//
// //更新1月
$min1mdata = Db::name('ztc_k_1month')
->order('ts','desc')->find();
$start1mmin = $min1mdata['ts'];
$end1mmin = $min1mdata['ts'] + 2592000;
$min1mts = $min1mdata['ts'];
$allmin1mts[$min1mts] = $min1mdata;
//更新当前这分钟
foreach($alltrade as $key=>$value)
{
//分钟
if($value['ts'] >= $start1min && $value['ts'] < $end1min)
{
$update1min = $allmints[$mints];
$close = $value['price'];
$update1min['close'] = $close;
if($close < $allmints[$mints]['low']) $update1min['low'] = $close;
if($close > $allmints[$mints]['high']) $update1min['high'] = $close;
$update1min['amount'] = $allmints[$mints]['amount'] + $value['amount'];
$update1min['vol'] = $allmints[$mints]['vol'] + $value['amount'];
$update1min['count'] = $allmints[$mints]['count'] + 1;
$allmints[$mints] = $update1min;
}else if($value['ts'] >= $end1min)
{
for ($i = 0;$i<100;$i++)
{
$newts = $mints + 60;
$start1min = $newts;
$end1min = $newts + 60;
$mints = $newts;
$allmints[$mints] = [
'symbol' => 'ztcusdt',
'ts' => $mints,
'amount' => $value['amount'],
'open' => $value['price'],
'close' => $value['price'],
'high' => $value['price'],
'low' => $value['price'],
'vol' => $value['amount'],
'count' => 0,
];
if($value['ts'] < $end1min) break;
}
}
//更新五分钟
if($value['ts'] >= $start5min && $value['ts'] < $end5min)
{
$update1min = $allmin5ts[$min5ts];
$close = $value['price'];
$update1min['close'] = $close;
if($close < $allmin5ts[$min5ts]['low']) $update1min['low'] = $close;
if($close > $allmin5ts[$min5ts]['high']) $update1min['high'] = $close;
$update1min['amount'] = $allmin5ts[$min5ts]['amount'] + $value['amount'];
$update1min['vol'] = $allmin5ts[$min5ts]['vol'] + $value['amount'];
$update1min['count'] = $allmin5ts[$min5ts]['count'] + 1;
$allmin5ts[$min5ts] = $update1min;
}else if($value['ts'] >= $end5min)
{
for ($i = 0;$i<100;$i++)
{
$newts = $min5ts + 300;
$start5min = $newts;
$end5min = $newts + 300;
$min5ts = $newts;
$allmin5ts[$min5ts] = [
'symbol' => 'ztcusdt',
'ts' => $min5ts,
'amount' => $value['amount'],
'open' => $value['price'],
'close' => $value['price'],
'high' => $value['price'],
'low' => $value['price'],
'vol' => $value['amount'],
'count' => 0,
];
if($value['ts'] < $end5min) break;
}
}
//更新1五分钟
if($value['ts'] >= $start15min && $value['ts'] < $end15min)
{
$update1min = $allmin15ts[$min15ts];
$close = $value['price'];
$update1min['close'] = $close;
if($close < $allmin15ts[$min15ts]['low']) $update1min['low'] = $close;
if($close > $allmin15ts[$min15ts]['high']) $update1min['high'] = $close;
$update1min['amount'] = $allmin15ts[$min15ts]['amount'] + $value['amount'];
$update1min['vol'] = $allmin15ts[$min15ts]['vol'] + $value['amount'];
$update1min['count'] = $allmin15ts[$min15ts]['count'] + 1;
$allmin15ts[$min15ts] = $update1min;
}else if($value['ts'] >= $end15min)
{
for ($i = 0;$i<100;$i++)
{
$newts = $min15ts + 300;
$start15min = $newts;
$end15min = $newts + 300;
$min15ts = $newts;
$allmin15ts[$min15ts] = [
'symbol' => 'ztcusdt',
'ts' => $min15ts,
'amount' => $value['amount'],
'open' => $value['price'],
'close' => $value['price'],
'high' => $value['price'],
'low' => $value['price'],
'vol' => $value['amount'],
'count' => 0,
];
if($value['ts'] < $end15min) break;
}
}
//更新30分钟
if($value['ts'] >= $start30min && $value['ts'] < $end30min)
{
$update1min = $allmin30ts[$min30ts];
$close = $value['price'];
$update1min['close'] = $close;
if($close < $allmin30ts[$min30ts]['low']) $update1min['low'] = $close;
if($close > $allmin30ts[$min30ts]['high']) $update1min['high'] = $close;
$update1min['amount'] = $allmin30ts[$min30ts]['amount'] + $value['amount'];
$update1min['vol'] = $allmin30ts[$min30ts]['vol'] + $value['amount'];
$update1min['count'] = $allmin30ts[$min30ts]['count'] + 1;
$allmin30ts[$min30ts] = $update1min;
}else if($value['ts'] >= $end30min)
{
for ($i = 0;$i<100;$i++)
{
$newts = $min30ts + 1800;
$start30min = $newts;
$end30min = $newts + 1800;
$min30ts = $newts;
$allmin30ts[$min30ts] = [
'symbol' => 'ztcusdt',
'ts' => $min30ts,
'amount' => $value['amount'],
'open' => $value['price'],
'close' => $value['price'],
'high' => $value['price'],
'low' => $value['price'],
'vol' => $value['amount'],
'count' => 0,
];
if($value['ts'] < $end30min) break;
}
}
//更新1小时
if($value['ts'] >= $start1hmin && $value['ts'] < $end1hmin)
{
$update1min = $allmin1hts[$min1hts];
$close = $value['price'];
$update1min['close'] = $close;
if($close < $allmin1hts[$min1hts]['low']) $update1min['low'] = $close;
if($close > $allmin1hts[$min1hts]['high']) $update1min['high'] = $close;
$update1min['amount'] = $allmin1hts[$min1hts]['amount'] + $value['amount'];
$update1min['vol'] = $allmin1hts[$min1hts]['vol'] + $value['amount'];
$update1min['count'] = $allmin1hts[$min1hts]['count'] + 1;
$allmin1hts[$min1hts] = $update1min;
}else if($value['ts'] >= $end1hmin)
{
for ($i = 0;$i<100;$i++)
{
$newts = $min1hts + 3600;
$start1hmin = $newts;
$end1hmin = $newts + 3600;
$min1hts = $newts;
$allmin1hts[$min1hts] = [
'symbol' => 'ztcusdt',
'ts' => $min1hts,
'amount' => $value['amount'],
'open' => $value['price'],
'close' => $value['price'],
'high' => $value['price'],
'low' => $value['price'],
'vol' => $value['amount'],
'count' => 0,
];
if($value['ts'] < $end1hmin) break;
}
}
//更新24小时
if($value['ts'] >= $start24hmin && $value['ts'] < $end24hmin)
{
$update1min = $allmin24hts[$min24hts];
$close = $value['price'];
$update1min['close'] = $close;
if($close < $allmin24hts[$min24hts]['low']) $update1min['low'] = $close;
if($close > $allmin24hts[$min24hts]['high']) $update1min['high'] = $close;
$update1min['amount'] = $allmin24hts[$min24hts]['amount'] + $value['amount'];
$update1min['vol'] = $allmin24hts[$min24hts]['vol'] + $value['amount'];
$update1min['count'] = $allmin24hts[$min24hts]['count'] + 1;
$allmin24hts[$min24hts] = $update1min;
}else if($value['ts'] >= $end24hmin)
{
for ($i = 0;$i<100;$i++)
{
$newts = $min24hts + 86400;
$start24hmin = $newts;
$end24hmin = $newts + 86400;
$min24hts = $newts;
$allmin24hts[$min24hts] = [
'symbol' => 'ztcusdt',
'ts' => $min24hts,
'amount' => $value['amount'],
'open' => $value['price'],
'close' => $value['price'],
'high' => $value['price'],
'low' => $value['price'],
'vol' => $value['amount'],
'count' => 0,
];
if($value['ts'] < $end24hmin) break;
}
}
//更新1周
if($value['ts'] >= $start1wmin && $value['ts'] < $end1wmin)
{
$update1min = $allmin1wts[$min1wts];
$close = $value['price'];
$update1min['close'] = $close;
if($close < $allmin1wts[$min1wts]['low']) $update1min['low'] = $close;
if($close > $allmin1wts[$min1wts]['high']) $update1min['high'] = $close;
$update1min['amount'] = $allmin1wts[$min1wts]['amount'] + $value['amount'];
$update1min['vol'] = $allmin1wts[$min1wts]['vol'] + $value['amount'];
$update1min['count'] = $allmin1wts[$min1wts]['count'] + 1;
$allmin1wts[$min1wts] = $update1min;
}else if($value['ts'] >= $end1wmin)
{
for ($i = 0;$i<100;$i++)
{
$newts = $min1wts + 604800;
$start1wmin = $newts;
$end1wmin = $newts + 604800;
$min1wts = $newts;
$allmin1wts[$min1wts] = [
'symbol' => 'ztcusdt',
'ts' => $min1wts,
'amount' => $value['amount'],
'open' => $value['price'],
'close' => $value['price'],
'high' => $value['price'],
'low' => $value['price'],
'vol' => $value['amount'],
'count' => 0,
];
if($value['ts'] < $end1wmin) break;
}
}
//更新1月
if($value['ts'] >= $start1mmin && $value['ts'] < $end1mmin)
{
$update1min = $allmin1mts[$min1mts];
$close = $value['price'];
$update1min['close'] = $close;
if($close < $allmin1mts[$min1mts]['low']) $update1min['low'] = $close;
if($close > $allmin1mts[$min1mts]['high']) $update1min['high'] = $close;
$update1min['amount'] = $allmin1mts[$min1mts]['amount'] + $value['amount'];
$update1min['vol'] = $allmin1mts[$min1mts]['vol'] + $value['amount'];
$update1min['count'] = $allmin1mts[$min1mts]['count'] + 1;
$allmin1mts[$min1mts] = $update1min;
}else if($value['ts'] >= $end1mmin)
{
for ($i = 0;$i<100;$i++)
{
$newts = $min1mts + 2592000;
$start1mmin = $newts;
$end1mmin = $newts + 2592000;
$min1mts = $newts;
$allmin1mts[$min1mts] = [
'symbol' => 'ztcusdt',
'ts' => $min1mts,
'amount' => $value['amount'],
'open' => $value['price'],
'close' => $value['price'],
'high' => $value['price'],
'low' => $value['price'],
'vol' => $value['amount'],
'count' => 0,
];
if($value['ts'] < $end1mmin) break;
}
}
}
//1分钟
foreach ($allmints as $key=>$value)
{
if($key == $mindata['ts']) //更新
{
Db::name('ztc_k_1min')->where('id',$mindata['id'])
->update($value);
}else{
Db::name('ztc_k_1min')
->insert($value);
}
}
//5分钟
foreach ($allmin5ts as $key=>$value)
{
if($key == $min5data['ts']) //更新
{
Db::name('ztc_k_5min')->where('id',$min5data['id'])
->update($value);
}else{
Db::name('ztc_k_5min')
->insert($value);
}
}
//15分钟
foreach ($allmin15ts as $key=>$value)
{
if($key == $min15data['ts']) //更新
{
Db::name('ztc_k_15min')->where('id',$min15data['id'])
->update($value);
}else{
Db::name('ztc_k_15min')
->insert($value);
}
}
//30分钟
foreach ($allmin30ts as $key=>$value)
{
if($key == $min30data['ts']) //更新
{
Db::name('ztc_k_30min')->where('id',$min30data['id'])
->update($value);
}else{
Db::name('ztc_k_30min')
->insert($value);
}
}
//1小时
foreach ($allmin1hts as $key=>$value)
{
if($key == $min1hdata['ts']) //更新
{
Db::name('ztc_k_1h')->where('id',$min1hdata['id'])
->update($value);
}else{
Db::name('ztc_k_1h')
->insert($value);
}
}
//1天
foreach ($allmin24hts as $key=>$value)
{
if($key == $min24hdata['ts']) //更新
{
Db::name('ztc_k_24h')->where('id',$min24hdata['id'])
->update($value);
}else{
Db::name('ztc_k_24h')
->insert($value);
}
}
//1周
foreach ($allmin1wts as $key=>$value)
{
if($key == $min1wdata['ts']) //更新
{
Db::name('ztc_k_1week')->where('id',$min1wdata['id'])
->update($value);
}else{
Db::name('ztc_k_1week')
->insert($value);
}
}
//1月
foreach ($allmin1mts as $key=>$value)
{
if($key == $min1mdata['ts']) //更新
{
Db::name('ztc_k_1month')->where('id',$min1mdata['id'])
->update($value);
}else{
Db::name('ztc_k_1month')
->insert($value);
}
}
$updatearr = [
'high' => $allmin24hts[$min24hdata['ts']]['high'],
'low' => $allmin24hts[$min24hdata['ts']]['low'],
'open' => $allmin24hts[$min24hdata['ts']]['open'],
'close' => $allmin24hts[$min24hdata['ts']]['close'],
'usdt' => $allmin24hts[$min24hdata['ts']]['close'],
'updatetime' => time(),
];
$updatearr['increase'] = sprintf("%.4f",
($updatearr['close']-$updatearr['open'])/$updatearr['close']);
Db::name('app_rate')->where('id',19)->update($updatearr);
echo "update complete ".date('Y-m-d H:i:s');
echo "<pre>";exit;
}
//更新火币ztc数据k线
public function sync_ztc()
{
$request = [
// 'code' => "shethdata2020.1",
'period' => '1mon',
'size' => 100,
'symbol' => "firousdt",
// 'command' => 'kline',
];
$data = json_decode(http_curl('https://api.huobi.pro/market/history/kline?period='.
$request['period'].'&size='.$request['size'].'&symbol='.$request['symbol'],'get',$request),true);
// echo "<pre>";var_dump($data);exit;
$insertarr = [];
foreach ($data['data'] as $key=>$value)
{
$insertarr[] = [
'symbol' => 'ztcusdt',
'ts' => $value['id'],
'amount' => $value['amount'],
'open' => $value['open'],
'close' => $value['close'],
'high' => $value['high'],
'low' => $value['low'],
'vol' => $value['vol'],
'count' => $value['count'],
];
}
// $ret = Db::name('ztc_k_1month')->insertAll($insertarr);
}
/**
* 更新团队等级
*/
public function level_update()
{
$t = time();
$user = Db::name("user a")->field("a.id,b.level,a.path,a.level_call,a.last_level_time")
->join("app_level b","a.level_id=b.id","left")
->where("level_call",0)
->order("id desc")
->limit(50)
->select();
if(empty($user)){
echo "empty";
Db::name("user")->where("level_call",1)->update(['level_call'=>0]);
exit;
}
//查询所有质押
$all_zhiya = Db::name("app_zhiya_user")->where("status",1)->select();
$id_arr = [];
foreach ($user as $key => $value) {
$id_arr[] = $value['id'];
$user_update = array(
"level_call" => 1,
);
//下一个等级
$next_level = Db::name("app_level")->where("level",">",$value['level'])->order("level asc")->find();
//达标升级
$team_user = Db::name("user")->field("id")->where("path","like",$value['path']."%")->where("id","neq",$value['id'])->select();
$user_arr = [];
foreach ($team_user as $ke => $val) {
$user_arr[] = $val['id'];
}
$team_zhiya = 0;
foreach ($all_zhiya as $ke => $val) {
if(in_array($val['user_id'], $user_arr)){
$team_zhiya += $val['num'];
}
}
if($team_zhiya >= $next_level['team_num']){
$user_update['level_id'] = $next_level['id'];
$user_update['last_level_time'] = time();
}else{
//1个月强制更新一次
if($value['last_level_time'] < time()-(30*86400)){
$leves = Db::name("app_level")->where("team_num","<=",$team_zhiya)->order("level desc")->find();
$user_update['level_id'] = $leves['id'];
$user_update['last_level_time'] = time();
}
}
Db::name("user")->where("id",$value['id'])->update($user_update);
}
echo "执行时长:".(time()-$t);
}
/**
* 锁仓收益计算
*/
public function zhiya_js()
{
$zhiya = Db::name("app_zhiya_user a")->field("a.*,b.yield")
->join("app_zhiya_set b","a.zhiya_id=b.id","left")
->where("a.status",1)
->order("a.last_time asc")
->limit(20)
->select();
if(empty($zhiya)){
echo "empty";exit;
}
foreach ($zhiya as $key => $value) {
if($value['end_time'] < time()){
$detail = [];
//完结
$update = array(
"status" => 0,
);
Db::startTrans();
try {
Db::name("app_zhiya_user")->where("id",$value['id'])->update($update);
//收益发放
$user_curr = Db::name("app_currency_user")->where("user_id",$value['user_id'])->where("curr_id",2)->find();
$detail[] = array(
"user_id" => $user_curr['user_id'],
"curr_id" => $user_curr['curr_id'],
"price" => $value['num'],
"cart" => 1,
"type" => 9,
"description" => "锁仓释放",
"createtime" => time(),
"before_num" => $user_curr['num'],
"after_num" => $user_curr['num']+$value['num'],
);
$detail[] = array(
"user_id" => $user_curr['user_id'],
"curr_id" => $user_curr['curr_id'],
"price" => $value['profit_num'],
"cart" => 1,
"type" => 9,
"description" => "锁仓收益",
"createtime" => time(),
"before_num" => $user_curr['num']+$value['num'],
"after_num" => $user_curr['num']+$value['num']+$value['profit_num'],
);
Db::name("app_currency_user")->where("id",$user_curr['id'])->update(['num'=>$user_curr['num']+$value['num']+$value['profit_num']]);
//团队收益
$profit_num = $value['profit_num'];
$users = Db::name('user')->where('id',$user_curr['user_id'])->find();
$usersarr = array_filter(array_reverse( explode('|',$users['path'])));
$level = 0;
$zhiya_push_profit = Config::get("site.zhiya_push_profit");//直推
$level_arr = Db::name("app_level")->field("id,name,level,team_prize")->where("team_prize",">",0)->select();
$team_prize = 0;
foreach ($usersarr as $ks => $vals)
{
if(!is_numeric($vals) || empty($vals)) continue;
if($vals == 1) continue;
if($vals == $users['id']) continue;
$usera = Db::name('user')->where('id',$vals)->find();
$currencys = Db::name('app_currency_user')
->where('user_id',$usera['id'])
->where('curr_id','2')->find();
//先算直推
if($ks == 2){
$push_price = sprintf("%.6f",$profit_num*$zhiya_push_profit);
$detail[] = array(
'user_id' => $usera['id'],
'curr_id' => '2',
'price' => $push_price,
'cart' => '1',
'type' => '4',
'description' => '锁仓直推收益',
'createtime' => time(),
'before_num' => $currencys['num'],
'after_num' => $currencys['num']+$push_price,
);
Db::name("app_currency_user")->where("id",$currencys['id'])->update(['num'=>$currencys['num']+$push_price]);
continue;
}
if($usera['level_id'] == 1 || $usera['level_id']<=$level){
continue;
}
// 团队将
foreach ($level_arr as $kk => $vv) {
if($vv['id'] == $usera['level_id']){
$team_price = sprintf("%.6f",$profit_num*($vv['team_prize']-$team_prize)/100);
$detail[] = array(
'user_id' => $usera['id'],
'curr_id' => '2',
'price' => $team_price,
'cart' => '1',
'type' => '10',
'description' => '锁仓团队收益',
'createtime' => time(),
'before_num' => $currencys['num'],
'after_num' => $currencys['num']+$team_price,
);
Db::name("app_currency_user")->where("id",$currencys['id'])->update(['num'=>$currencys['num']+$team_price]);
$level = $usera['level_id'];
$team_prize = $vv['team_prize'];
}
}
}
Db::name("app_detailed")->insertAll($detail);
echo 2;
Db::commit();
} catch (Exception $e) {
Db::rollback();
continue;
}
}else{
if($value['last_time'] > time()-86400){
continue;
}
$profit_num = sprintf("%.6f",$value['num']*$value['yield']/100);
$update = array(
"profit_num" => $value['profit_num']+$profit_num,
"last_time" => $value['last_time']+86400,
);
Db::name("app_zhiya_user")->where("id",$value['id'])->update($update);
$zhiya_detail = array(
"user_id" => $value['user_id'],
"zhiya_id" => $value['id'],
"num" => $profit_num,
"addtime" => time(),
);
Db::name("app_zhiya_detail")->insert($zhiya_detail);
echo 1;
}
}
}
/**
* C2C订单自动取消
*/
public function clear_order()
{
$today = strtotime(date("Y-m-d", time()));
$order = Db::name("app_market_user")->where("status",1)->order("add_time asc")->limit(5)->select();
if(empty($order)){
echo "empty";exit;
}
$jiedian_times = Config::get("site.market_time");
$times = time() - $jiedian_times*60;
foreach ($order as $key => $value) {
if($value['add_time'] > $times){
continue;
}
if($value['type'] == 1){
$user_id = $value['user_id'];//买家id
$sell_id = $value['relid'];
}else{
$user_id = $value['relid'];//买家id
$sell_id = $value['user_id'];
}
//查询累计取消次数
$cleal_num = Config::get("site.cleal_num");
$user_cleal_num = Db::name("app_market_cancel_log")->where("user_id",$user_id)->where("createtime",">", $today)->count();
if($user_cleal_num+1 >= $cleal_num){
Db::name("app_market")->where("user_id",$user_id)->where("id",$value['order_id'])->update(["status"=>4]);
}else{
Db::name("app_market")->where("user_id",$user_id)->where("id",$value['order_id'])->update(["status"=>1]);
}
Db::name("app_market_user")->where("id",$value['id'])->update(['status'=>5,"cancel_time"=> time()]);
$cleal_data = array(
"user_id" => $user_id,
"type" => 1,
"description" => "未接单自动取消",
"createtime" => time(),
);
Db::name("app_market_cancel_log")->insert($cleal_data);
$market = Db::name("app_market")->where("id",$value['order_id'])->find();
//退款
if($value['type'] == 2){
$user_curr = Db::name("app_currency_user")->where("user_id",$sell_id)->where("curr_id",$market['curr_id'])->find();
$count_num = $value['num']+$value['free_num'];
$detail_data = array(
"user_id" => $sell_id,
"curr_id" => $market['curr_id'],
"price" => $count_num,
"cart" => 1,
"type" => 2,
"description" => "取消订单",
"createtime" => time(),
"before_num" => $user_curr['num_fb'],
"after_num" => $user_curr['num_fb']+$count_num,
);
Db::name("app_detailed_fb")->insert($detail_data);
Db::name("app_currency_user")->where("id",$user_curr['id'])->update(['num_fb' => $user_curr['num_fb']+$count_num]);
}else{
Db::name("app_market")->where("user_id",$sell_id)->where("id",$value['order_id'])->update(["status"=>1]);
}
echo 1;
}
echo "success";
}
/**
* 天使投资释放
*/
public function angel_sf()
{
$angel = Db::name("app_angel_user a")->field("a.*,b.interest,b.freed_day,b.freed_bl,b.curr_id")
->join("app_angel_set b","a.angel_id=b.id","left")
->where("a.lasttime","<=", time()-86400)
->order("a.lasttime asc")
->limit(20)
->select();
if(empty($angel)){
echo "empty";exit;
}
foreach ($angel as $key => $value) {
if($value['lasttime'] > time()-86400){
continue;
}
$profit_num = sprintf("%.6f",$value['real_num']*$value['interest']/100);
if($value['lasttime'] == 0){
$lasttime = time();
}else{
$lasttime = $value['lasttime']+86400;
}
$update = array(
"days" => $value['days']+1,
"count_lixi" => $value['count_lixi']+$profit_num,
"wait_lixi" => $value['wait_lixi']+$profit_num,
"lasttime" => $lasttime,
);
$detail_data = [];
$angel_detail = [];
Db::startTrans();
try {
if($update['days'] >= $value['freed_day']){
//达到释放周期
$user_curr = Db::name("app_currency_user")->where("user_id",$value['user_id'])->where("curr_id",$value['curr_id'])->find();
$detail_data[] = array(
"user_id" => $value['user_id'],
"curr_id" => $value['curr_id'],
"price" => $update['wait_lixi'],
"cart" => 1,
"type" => 11,
"description" => "日息释放",
"createtime" => time(),
"before_num" => $user_curr['num'],
"after_num" => $user_curr['num']+$update['wait_lixi'],
);
$bj_num = sprintf("%.6f",$value['real_num']*0.2);
$detail_data[] = array(
"user_id" => $value['user_id'],
"curr_id" => $value['curr_id'],
"price" => $bj_num,
"cart" => 1,
"type" => 11,
"description" => "本金释放",
"createtime" => time(),
"before_num" => $user_curr['num']+$update['wait_lixi'],
"after_num" => $user_curr['num']+$update['wait_lixi']+$bj_num,
);
Db::name("app_currency_user")->where("id",$user_curr['id'])->update(['num'=>$user_curr['num']+$update['wait_lixi']+$bj_num]);
Db::name("app_detailed")->insertAll($detail_data);
$update['days'] = 0;
$update['wait_lixi'] = 0;
$update['real_num'] = $value['real_num']-$bj_num;
$angel_detail[] = array(
"num" => $bj_num,
"type" => 2,
"addtime" => time(),
"pro_id" => $value['id'],
);
echo 2;
}else{
echo 1;
}
//每日产生释放记录
$angel_detail[] = array(
"num" => $profit_num,
"type" => 1,
"addtime" => time(),
"pro_id" => $value['id'],
);
Db::name("app_angel_detail")->insertAll($angel_detail);
Db::name("app_angel_user")->where("id",$value['id'])->update($update);
Db::commit();
} catch (Exception $e) {
Db::rollback();
continue;
}
}
echo "success";
}
}