ContractServer.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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\Level;
  12. use App\Models\Member;
  13. use App\Models\MemberBoth;
  14. use App\Models\MemberClan;
  15. use App\Servers\Icon\BanRPC;
  16. use App\Servers\Icon\TronRPC;
  17. use Illuminate\Support\Facades\DB;
  18. /**
  19. * Redis数据缓存类
  20. */
  21. class ContractServer
  22. {
  23. static private $server = '';
  24. private $distributionArr=[
  25. 1=>'平台奖金',
  26. 2=>'直推奖金',
  27. 3=>'间推奖金',
  28. 4=>'团队奖金',
  29. 5=>'公排池',
  30. 6=>'沉淀奖',
  31. 7=>'代币奖',
  32. 8=>'公排出局',
  33. ];
  34. /**
  35. * @return string[]
  36. */
  37. public function getDistributionArr(): array
  38. {
  39. return $this->distributionArr;
  40. }
  41. /**
  42. * 获取清分类型
  43. * @param $type
  44. * @return string
  45. */
  46. public function getDistributionArrStr($type)
  47. {
  48. return empty($this->distributionArr[$type])?'无':$this->distributionArr[$type];
  49. }
  50. private function __construct()
  51. {
  52. }
  53. /**
  54. * 创建对象
  55. * @return ContractServer
  56. */
  57. static function creatServer()
  58. {
  59. if (empty(self::$server)) {
  60. self::$server = new ContractServer();
  61. }
  62. return self::$server;
  63. }
  64. /**
  65. * 报单信息检测
  66. */
  67. function broadcastDetection(){
  68. //检测报单信息
  69. $contractList=Contract::where('status',1)->limit(50)->get();
  70. $sysAddress=CommonServer::creatServer()->getConfigValue('sys_address');
  71. $sysAddress=strtolower($sysAddress);
  72. foreach ($contractList as $contract){
  73. $contractNum=Contract::where('hash',$contract->{'hash'})->count();
  74. if($contractNum>0){
  75. $broadcastData=BanRPC::creatServer()->getTransactionReceipt($contract->{'hash'});
  76. $memberAddress=Member::where('id',$contract->{'m_id'})->value('address');
  77. $memberAddress=strtolower($memberAddress);
  78. if (empty($broadcastData)) {
  79. //交易正在广播中,下次继续查询
  80. continue;
  81. } else {
  82. //交易已完成 返回数据格式数据 ,将数据json保存
  83. $updateInfo['broadcast_data'] = json_encode($broadcastData);
  84. if ($broadcastData['status'] == '0x1') {
  85. if($sysAddress!=strtolower($broadcastData['to'])){
  86. $updateInfo['status'] = 3;
  87. $updateInfo['error_msg'] = '收款地址不匹配';
  88. }elseif ($memberAddress!=strtolower($broadcastData['from'])){
  89. $updateInfo['status'] = 3;
  90. $updateInfo['error_msg'] = '转出地址不匹配';
  91. }else{
  92. $updateInfo['status'] = 2;
  93. }
  94. } else {
  95. $updateInfo['status'] = 3;
  96. $updateInfo['error_msg'] = '广播失败';
  97. }
  98. }
  99. }else{
  100. $updateInfo['status'] = 3;
  101. $updateInfo['error_msg'] = '当前哈希被重复使用';
  102. }
  103. //检测是否有更新细腻
  104. if($updateInfo){
  105. DB::beginTransaction();
  106. $ret= $contract->update($updateInfo);
  107. if(empty($ret)){
  108. CommonServer::creatServer()->addErrorRecord('保单信息更新失败',$contract,0);
  109. DB::rollBack();
  110. }
  111. if($updateInfo['status']==2){
  112. //报单成功,就进行清分
  113. $memberTeam=MemberClan::where('m_id',$contract->{'m_id'})->select(['id','m_id','p_ids','one_m_id','two_m_id'])->first();
  114. $pIds=array_reverse(array_filter(explode(',',$memberTeam->{'p_ids'})));
  115. //平台清分
  116. $this->platformDistribution($contract,$pIds);
  117. //直推及间推发放
  118. $this->directProportion($contract,$memberTeam);
  119. //团队清分
  120. $this->teamProportion($contract,$pIds);
  121. //公排分账
  122. $this->commonProportion($contract);
  123. //沉淀账户分账
  124. $this->sedimentProportion($contract);
  125. //代币清分
  126. $this->agencyProportion($contract);
  127. //触发会员升级
  128. JobServer::creatServer()->verificationLevelJob($pIds);
  129. }
  130. DB::commit();
  131. }
  132. }
  133. return $contractList->count();
  134. }
  135. /**
  136. * 系统分账功能
  137. * @param Contract $contract
  138. * @param $pIds
  139. */
  140. private function platformDistribution(Contract $contract,$pIds){
  141. $platformMoney=$contract->{'platform_money'};
  142. if(empty($platformMoney)){
  143. $this->addContractLog($contract,'平台清分金额为0,停止平台清分');
  144. }else{
  145. //验证平台分红
  146. $receiveLevelId=CommonServer::creatServer()->getConfigValue('receive_level_id');
  147. //用户分红金额
  148. if(empty($receiveLevelId)){
  149. $this->addContractLog($contract,'未设置平台分红等级,跳出分红');
  150. }else{
  151. $pMember=Member::whereIn('id',$pIds)->where('level_id','>=',$receiveLevelId)->orderBy('id','desc')->select(['id','address','is_auto'])->first();
  152. if($pMember){
  153. $receiveUserMax=CommonServer::creatServer()->getConfigValue('receive_user_max');
  154. $teamContractNum=$this->getTeamContractNum($pMember->{'id'});
  155. if($teamContractNum>=$receiveUserMax){
  156. //最大比例
  157. $receiveProportion=CommonServer::creatServer()->getConfigValue('receive_max');
  158. $this->addContractLog($contract,$teamContractNum.'团队数量满足条件,执行最大分红比例');
  159. }else{
  160. //最小比例
  161. $receiveProportion=CommonServer::creatServer()->getConfigValue('receive_mix');
  162. $this->addContractLog($contract,$teamContractNum.'团队数量满不足条件,执行最小分红比例');
  163. }
  164. if(empty($receiveProportion)){
  165. $this->addContractLog($contract,$teamContractNum.'平台分红比例不存在,跳出分红');
  166. }else{
  167. $receiveMoney=round($platformMoney*$receiveProportion/100,6);
  168. if($receiveMoney<=0){
  169. $this->addContractLog($contract,$receiveProportion.'分红比例金额小于0,跳出分红');
  170. }else{
  171. //计算推荐分红数量及平台收益数量
  172. if($pMember->{'is_auto'}==0){
  173. $this->addContractLog($contract,'后台升级用户,跳出分红');
  174. }else{
  175. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$receiveMoney,1,$pMember->{'id'},$pMember->{'address'},$receiveProportion,1,'平台用户分红');
  176. $platformMoney-=$receiveMoney;
  177. }
  178. }
  179. }
  180. }else{
  181. $this->addContractLog($contract,$receiveLevelId.'及以上的父级不存在,跳出分红');
  182. }
  183. }
  184. $openSys=CommonServer::creatServer()->getConfigValue('open_sys');
  185. if($openSys){
  186. $receiveAddress=CommonServer::creatServer()->getConfigValue('receive_address');
  187. if(empty($receiveAddress)){
  188. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$platformMoney,1,0,'',$contract->{'platform_money'},0,'平台分红至系统账户');
  189. }else{
  190. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$platformMoney,1,0,$receiveAddress,$contract->{'platform_money'},1,'平台分红至系统账户');
  191. }
  192. }else{
  193. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$platformMoney,1,0,'',$contract->{'platform_money'},0,'平台关闭转账至系统账户,不进行划拨分账');
  194. }
  195. }
  196. }
  197. /**
  198. * 直推及间推
  199. * @param Contract $contract
  200. * @param MemberClan $memberTeam
  201. */
  202. private function directProportion(Contract $contract,MemberClan $memberTeam){
  203. $directMember=Member::where('id',$memberTeam->{'one_m_id'})->select(['id','address','is_boss'])->first();
  204. if(empty($directMember)){
  205. $this->addContractLog($contract,'直推人信息异常,停止清分');
  206. }else{
  207. if($directMember->{'is_boss'}){
  208. $this->addContractLog($contract,'直推人是boss,直与间都划分为直推人');
  209. $directProportion=($contract->{'direct_proportion'}+$contract->{'indirect_proportion'});
  210. $directMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$directProportion/100,6);
  211. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$directMoney,2,$directMember->{'id'},$directMember->{'address'},$directProportion,1,'直推BOSS分红');
  212. }else{
  213. $directNum=Contract::where('m_id',$directMember->{'id'})->whereIn('status',[2,4])->count();
  214. if($directNum>0){
  215. $directMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'direct_proportion'}/100,6);
  216. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$directMoney,2,$directMember->{'id'},$directMember->{'address'},$contract->{'direct_proportion'},1,'直推分红');
  217. }else{
  218. $this->addContractLog($contract,'直推人未报单停止清分');
  219. }
  220. $indirectMember=Member::where('id',$memberTeam->{'two_m_id'})->select(['id','address','is_boss'])->first();
  221. $indirectMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'indirect_proportion'}/100,6);
  222. if(empty($indirectMember)){
  223. $this->addContractLog($contract,'间推人信息不存在,停止清分');
  224. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$indirectMoney,3,0,'',$contract->{'indirect_proportion'},0,'间推推分红,间推人不存在,未划分');
  225. }else{
  226. $indirectNum=Contract::where('m_id',$indirectMember->{'id'})->whereIn('status',[2,4])->count();
  227. if($indirectNum>0){
  228. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$indirectMoney,3,$indirectMember->{'id'},$indirectMember->{'address'},$contract->{'indirect_proportion'},1,'间推推分红');
  229. }else{
  230. $this->addContractLog($contract,'间推人未报单停止清分');
  231. }
  232. // $indirectNum=Member::where('recom_id',$indirectMember->{'id'})->count();
  233. // if($indirectNum>2){
  234. //
  235. // }else{
  236. // $this->addContractLog($contract,'间推人推广人数低于两人,停止清分');
  237. // $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$indirectMoney,3,$directMember->{'id'},$directMember->{'address'},$contract->{'indirect_proportion'},0,'间推人推广人数低于两人,停止清分');
  238. // }
  239. }
  240. }
  241. }
  242. }
  243. /**
  244. * 团队清分
  245. * @param Contract $contract
  246. * @param $pIds
  247. */
  248. function teamProportion(Contract $contract,$pIds){
  249. $levels=Level::select(['direct_num','id','lateral_num'])->get()->toArray();
  250. $levelArr=[];
  251. foreach ($levels as $level){
  252. $levelArr[$level['id']]=$level;
  253. }
  254. $totalMoney=$contract->{'money'}-$contract->{'platform_money'};
  255. $teamTotalMoney=round($totalMoney*$contract->{'team_proportion'}/100,6);
  256. $startId=1;
  257. $isLateral=false;
  258. //已经使用过的比例
  259. $useProportion=0;
  260. //已经使用金额
  261. $userMoney=0;
  262. //使用金额
  263. $grantMoney=0;
  264. foreach ($pIds as $pId){
  265. $pMember=Member::where('id',$pId)->select(['id','address','level_id'])->first();
  266. if(empty($pMember)){
  267. $this->addContractLog($contract,$pId.',无当前父及信息,停止清分');
  268. }else{
  269. $levelData=empty($levelArr[$pMember->{'level_id'}])?0:$levelArr[$pMember->{'level_id'}];
  270. if(empty($levelData)){
  271. $this->addContractLog($contract,$pMember->{'id'}.'ID,'.$pMember->{'level_id'}.',无当前等级配置信息,停止清分');
  272. }else{
  273. if($pMember->{'level_id'}>$startId || ($pMember->{'level_id'}==$startId && !$isLateral)){
  274. //等级大于等于当前等级
  275. $isLateral=true;
  276. $startId=$pMember->{'level_id'};
  277. //等级比例
  278. $levelProportion=$levelData['direct_num'];
  279. //等级比例-减去已经使用比例,获取到可用比列
  280. $grantProportion=$levelProportion-$useProportion;
  281. //累计已经使用的比例
  282. $useProportion+=$grantProportion;
  283. $grantMoney=round($grantProportion*$totalMoney/100,6);
  284. if($userMoney+$grantMoney>$teamTotalMoney){
  285. $grantMoney=$teamTotalMoney-$userMoney;
  286. if($grantMoney<=0){
  287. $this->addContractLog($contract,$pMember->{'level_id'}.'层级,资金已不足,停止团队奖金清分');
  288. }
  289. }
  290. $userMoney+=$grantMoney;
  291. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$grantMoney,4,$pMember->{'id'},$pMember->{'address'},$grantProportion,1,$pMember->{'level_id'}.'级别团队分红');
  292. }elseif($pMember->{'level_id'}==$startId && $isLateral){
  293. //平级奖金发放
  294. $isLateral=false;
  295. $startId=$pMember->{'level_id'}+1;
  296. $lateralMoney=round($grantMoney*$levelData['lateral_num']/100,6);
  297. if($lateralMoney>0){
  298. if($userMoney+$lateralMoney>$teamTotalMoney){
  299. $lateralMoney=$teamTotalMoney-$userMoney;
  300. if($lateralMoney<=0){
  301. $this->addContractLog($contract,$pMember->{'level_id'}.'层级,资金已不足,停止团队平级奖金清分');
  302. }
  303. }
  304. $userMoney+=$lateralMoney;
  305. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$lateralMoney,4,$pMember->{'id'},$pMember->{'address'},$levelData['lateral_num'],1,$pMember->{'level_id'}.'级别团队平级分红');
  306. }else{
  307. $this->addContractLog($contract,$pMember->{'level_id'}.'层级,平级奖金小于等于0,停止团队平级奖金清分');
  308. }
  309. if($startId>count($levels)){
  310. //清分完成,跳出循环
  311. break;
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. /**
  319. * 公排分账
  320. * @param Contract $contract
  321. */
  322. private function commonProportion(Contract $contract){
  323. $commonMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'common_proportion'}/100,6);
  324. $openSys=CommonServer::creatServer()->getConfigValue('open_sys');
  325. if($openSys && $commonMoney>0){
  326. $commonAddress=CommonServer::creatServer()->getConfigValue('common_address');
  327. if(empty($commonAddress)){
  328. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$commonMoney,5,0,'',$contract->{'common_proportion'},0,'公排分账,无收款地址信息。系统关闭');
  329. }else{
  330. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$commonMoney,5,0,$commonAddress,$contract->{'common_proportion'},1,'公排分账');
  331. }
  332. }else{
  333. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$commonMoney,5,0,'',$contract->{'common_proportion'},0,'平台关闭转账至系统账户,不进行划拨分账');
  334. }
  335. //公排触发清算队列
  336. JobServer::creatServer()->contractCommonJob();
  337. }
  338. /**
  339. * 沉淀账户收款地址
  340. * @param Contract $contract
  341. */
  342. private function sedimentProportion(Contract $contract){
  343. $sedimentMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'sediment_proportion'}/100,6);
  344. $openSys=CommonServer::creatServer()->getConfigValue('open_sys');
  345. if($openSys && $sedimentMoney>0){
  346. $sedimentAddress=CommonServer::creatServer()->getConfigValue('sediment_address');
  347. if(empty($sedimentAddress)){
  348. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$sedimentMoney,6,0,'',$contract->{'sediment_proportion'},0,'沉淀分账,无收款地址信息。系统关闭');
  349. }else{
  350. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$sedimentMoney,6,0,$sedimentAddress,$contract->{'sediment_proportion'},1,'沉淀分账');
  351. }
  352. }else{
  353. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$sedimentMoney,6,0,'',$contract->{'sediment_proportion'},0,'平台关闭转账至系统账户,不进行划拨分账');
  354. }
  355. }
  356. /**
  357. * 代币比例
  358. * @param Contract $contract
  359. */
  360. private function agencyProportion(Contract $contract){
  361. $agencyMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'agency_proportion'}/100,6);
  362. $exchangeMoney=CommonServer::creatServer()->getConfigValue('exchange_money');
  363. $dbMoney=$agencyMoney*$exchangeMoney;
  364. if($exchangeMoney >0){
  365. $member=Member::where('id',$contract->{'m_id'})->select(['id','address','is_boss'])->first();
  366. $this->addDistribution($contract->{'id'},$contract->{'m_id'},$dbMoney,0,7,$member->{'id'},$member->{'address'},$contract->{'agency_proportion'},1,'代币分账');
  367. MoneyDetailServer::creatServer()->write(1,1,$dbMoney,1,$member->{'id'},'报单代币清分',$contract->{'id'});
  368. }else{
  369. $sedimentAddress=CommonServer::creatServer()->getConfigValue('agency_address');
  370. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$agencyMoney,7,0,$sedimentAddress,$contract->{'agency_proportion'},1,'代币价格为0,转入沉淀分账');
  371. $this->addContractLog($contract,'代币价格为0,不分发');
  372. }
  373. }
  374. /**
  375. * 添加清分记录
  376. * @param $contractId
  377. * @param $mId
  378. * @param $nbnMoney
  379. * @param $dbMoney
  380. * @param $type
  381. * @param $toMid
  382. * @param $toAddress
  383. * @param $proportion
  384. * @param $status
  385. * @param string $remark
  386. */
  387. function addDistribution($contractId,$mId,$dbMoney,$nbnMoney,$type,$toMid,$toAddress,$proportion,$status,$remark='奖金发放'){
  388. $serviceMoney=0;
  389. if($toMid>0 && $nbnMoney>0){
  390. $serviceMoney=CommonServer::creatServer()->getConfigValue('service_money')?:0;
  391. }
  392. ContractDistribution::create([
  393. 'contract_id' => $contractId,
  394. 'broadcast_id' => 0,
  395. 'm_id' => $mId,
  396. 'db_money' => $dbMoney,
  397. 'nbn_money' => $nbnMoney,
  398. 'type' => $type,
  399. 'to_m_id' => $toMid,
  400. 'to_address' => $toAddress,
  401. 'status' => $status,
  402. 'proportion' => $proportion,
  403. 'service_money' => $serviceMoney,
  404. 'remark' => $remark
  405. ]);
  406. }
  407. /**
  408. * 获取团队合约数量
  409. * @param $mId
  410. * @return int
  411. */
  412. function getTeamContractNum($mId){
  413. $contractNum=0;
  414. $teamIds=MemberClan::where('id',$mId)->value('m_ids');
  415. $teamIds=array_filter(explode(',',$teamIds));
  416. $teamIds[]=$mId;
  417. if($teamIds){
  418. $contractNum=Contract::whereIn('m_id',$teamIds)->whereIn('status',[2,4])->count();
  419. }
  420. return $contractNum;
  421. }
  422. /**
  423. * 公排出局检测
  424. * @throws \Exception
  425. */
  426. function verifyContractCommon(){
  427. $commonNum=CommonServer::creatServer()->getConfigValue('common_num');
  428. $contractIds=Contract::where('status',2)->where('is_out',0)->orderBy('id','asc')->limit($commonNum)->pluck('id')->toArray();
  429. if(count($contractIds)>=$commonNum){
  430. //选择第一个单子排队出公排
  431. $contract=Contract::where('status',2)->orderBy('id','asc')->first();
  432. DB::beginTransaction();
  433. Contract::whereIn('id',$contractIds)->update(['is_out'=>1]);
  434. $commonBnb=CommonServer::creatServer()->getConfigValue('common_bnb');
  435. $commonDb=CommonServer::creatServer()->getConfigValue('common_db');
  436. $exchangeMoney=CommonServer::creatServer()->getConfigValue('exchange_money');
  437. $member=Member::where('id',$contract->{'m_id'})->select(['id','address','is_boss'])->first();
  438. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$commonBnb,9,$member->{'id'},$member->{'address'},$commonBnb,1,'bsn公排出局');
  439. $dbMoney=1*$exchangeMoney*$commonDb;
  440. $this->addDistribution($contract->{'id'},$contract->{'m_id'},$dbMoney,0,9,$member->{'id'},$member->{'address'},$commonBnb,1,'代币公排出局');
  441. MoneyDetailServer::creatServer()->write(1,2,$dbMoney,1,$member->{'id'},'公排池代币出局',$contract->{'id'});
  442. $contract->update(['status' => 4]);
  443. DB::commit();
  444. }
  445. }
  446. /**
  447. * 合约日志记录
  448. * @param Contract $contract
  449. * @param $msg
  450. * @param array $data
  451. */
  452. function addContractLog(Contract $contract,$msg,$data=[]){
  453. ContractLog::create([
  454. 'contract_id'=> $contract->{'id'},
  455. 'm_id'=> $contract->{'m_id'},
  456. 'msg'=> $msg,
  457. 'contract_data'=> json_encode($contract),
  458. 'data'=> json_encode($data),
  459. ]);
  460. }
  461. }