ContractServer.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Servers;
  3. use App\Jobs\BusinessFileJob;
  4. use App\Jobs\MsgFileJob;
  5. use App\Models\BlockItems;
  6. use App\Models\Contract;
  7. use App\Models\ContractDistribution;
  8. use App\Models\ContractLog;
  9. use App\Models\ErrorRecord;
  10. use App\Models\Invest;
  11. use App\Models\Member;
  12. use App\Servers\Icon\TronRPC;
  13. /**
  14. * Redis数据缓存类
  15. */
  16. class ContractServer
  17. {
  18. static private $server = '';
  19. private $distributionArr=[
  20. 1=>'直推奖金',
  21. 2=>'累计均富池',
  22. ];
  23. private function __construct()
  24. {
  25. }
  26. /**
  27. * 创建对象
  28. * @return ContractServer
  29. */
  30. static function creatServer()
  31. {
  32. if (empty(self::$server)) {
  33. self::$server = new ContractServer();
  34. }
  35. return self::$server;
  36. }
  37. /**
  38. * 报单信息检测
  39. * @param BlockItems $blockItem
  40. */
  41. function broadcastDetection(BlockItems $blockItem){
  42. //检测报单信息
  43. $contract=Contract::where('hash',$blockItem->{'hash'})->first();
  44. if($contract){
  45. if($contract->{'status'}!=1){
  46. ErrorRecord::create([
  47. 'm_id' => 0,
  48. 'msg' => '合约状态从异常',
  49. 'data' => json_encode($contract)
  50. ]);
  51. $this->addContractLog($contract,'合约状态异常,程序终止清分,记录扫快信息',$blockItem);
  52. }else{
  53. $ret = TronRPC::CreationTron()->getTransaction($contract->{'hash'});
  54. if (!empty($ret['ret'][0]['contractRet']) && $ret['ret'][0]['contractRet'] == 'SUCCESS') {
  55. //检测交易信息
  56. if($contract->{'type'}==1){
  57. //初始合约
  58. //生成双轨关系
  59. $member=Member::where('id',$contract->{'m_id'})->select(['id','recom_id'])->first();
  60. JobServer::creatServer()->creatBothJob($member->{'id'},$member->{'recom_id'});
  61. //报单拆分
  62. $this->oneSettle($contract);
  63. }else{
  64. //复投合约
  65. $this->twoSettle($contract);
  66. }
  67. } else {
  68. ErrorRecord::create([
  69. 'm_id' => $contract->{'m_id'},
  70. 'msg' => '交易失败',
  71. 'data' => json_encode($contract)
  72. ]);
  73. }
  74. }
  75. }else{
  76. ErrorRecord::create([
  77. 'm_id' => 0,
  78. 'msg' => '合约信息不存在,扫快信息:',
  79. 'data' => json_encode($blockItem)
  80. ]);
  81. }
  82. }
  83. /**
  84. * 首单清分
  85. * @param Contract $contract
  86. */
  87. function oneSettle(Contract $contract){
  88. if($contract->{'direct_proportion'}>0){
  89. //直推金额分化
  90. $directId=Member::where('id',$contract->{'m_id'})->value('recom_id');
  91. if($directId){
  92. $this->addDistribution($contract,$directId,1,$contract->{'direct_proportion'},0);
  93. }else{
  94. $this->addContractLog($contract,'直推人信息异常,跳出首单直推奖');
  95. }
  96. }
  97. if($contract->{'grant_proportion'}>0){
  98. //均富池清分
  99. $distribution= $this->addDistribution($contract,0,2,$contract->{'grant_proportion'},1);
  100. if($distribution){
  101. GrantPondServer::creatServer()->grandPond($distribution);
  102. }
  103. }
  104. }
  105. /**
  106. * 复投清分
  107. * @param Contract $contract
  108. */
  109. function twoSettle(Contract $contract){
  110. }
  111. /**
  112. * 添加清分信息
  113. * @param Contract $contract
  114. * @param $toMid
  115. * @param $type
  116. * @param $money
  117. * @param int $isTransition
  118. * @return ContractDistribution|\Illuminate\Database\Eloquent\Model
  119. */
  120. function addDistribution(Contract $contract,$toMid,$type,$money,$isTransition=1){
  121. $lkMoney=CommonServer::creatServer()->getConfigValue('lk_money');
  122. $lkNum=$lkMoney*$money;
  123. return ContractDistribution::create([
  124. 'contract_id'=>$contract->{'id'},
  125. 'm_id'=>$contract->{'m_id'},
  126. 'broadcast_id'=>0,
  127. 'is_transition'=>$isTransition,
  128. 'usdt_num'=>$money,
  129. 'lk_num'=>$lkNum,
  130. 'lk_money'=>$lkMoney,
  131. 'type'=>$type,
  132. 'to_m_id'=>$toMid,
  133. ]);
  134. }
  135. /**
  136. * 合约日志记录
  137. * @param Contract $contract
  138. * @param $msg
  139. * @param array $data
  140. */
  141. function addContractLog(Contract $contract,$msg,$data=[]){
  142. ContractLog::create([
  143. 'contract_id'=> $contract->{'id'},
  144. 'm_id'=> $contract->{'m_id'},
  145. 'msg'=> $msg,
  146. 'contract_data'=> json_encode($contract),
  147. 'data'=> json_encode($data),
  148. ]);
  149. }
  150. }