ContractServer.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. $directMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'direct_proportion'}/100,6);
  214. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$directMoney,2,$directMember->{'id'},$directMember->{'address'},$contract->{'direct_proportion'},1,'直推分红');
  215. $indirectMember=Member::where('id',$memberTeam->{'two_m_id'})->select(['id','address','is_boss'])->first();
  216. $indirectMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'indirect_proportion'}/100,6);
  217. if(empty($indirectMember)){
  218. $this->addContractLog($contract,'间推人信息不存在,停止清分');
  219. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$indirectMoney,3,$directMember->{'id'},$directMember->{'address'},$contract->{'indirect_proportion'},0,'间推推分红,间推人不存在,未划分');
  220. }else{
  221. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$indirectMoney,3,$directMember->{'id'},$directMember->{'address'},$contract->{'indirect_proportion'},1,'间推推分红');
  222. }
  223. }
  224. }
  225. }
  226. /**
  227. * 团队清分
  228. * @param Contract $contract
  229. * @param $pIds
  230. */
  231. private function teamProportion(Contract $contract,$pIds){
  232. $levels=Level::select(['direct_num','id','lateral_num'])->get()->toArray();
  233. $levelArr=[];
  234. foreach ($levels as $level){
  235. $levelArr[$level['id']]=$level;
  236. }
  237. $totalMoney=$contract->{'money'}-$contract->{'platform_money'};
  238. $teamTotalMoney=round($totalMoney*$contract->{'team_proportion'}/100,6);
  239. $startId=1;
  240. $isLateral=false;
  241. //已经使用过的比例
  242. $useProportion=0;
  243. //已经使用金额
  244. $userMoney=0;
  245. //使用金额
  246. $grantMoney=0;
  247. foreach ($pIds as $pId){
  248. $pMember=Member::where('id',$pId)->select(['id','address','level_id'])->first();
  249. if(empty($pMember)){
  250. $this->addContractLog($contract,$pId.',无当前父及信息,停止清分');
  251. }else{
  252. $levelData=empty($levelArr[$pMember->{'level_id'}])?0:$levelArr[$pMember->{'level_id'}];
  253. if(empty($levelData)){
  254. $this->addContractLog($contract,$pMember->{'id'}.'ID,'.$pMember->{'level_id'}.',无当前等级配置信息,停止清分');
  255. }else{
  256. if($pMember->{'level_id'}>$startId || ($pMember->{'level_id'}==$startId &&!$isLateral)){
  257. //等级大于等于当前等级
  258. $isLateral=true;
  259. //等级比例
  260. $levelProportion=$levelData['direct_num'];
  261. //等级比例-减去已经使用比例,获取到可用比列
  262. $grantProportion=$levelProportion-$useProportion;
  263. //累计已经使用的比例
  264. $useProportion+=$grantProportion;
  265. $grantMoney=round($grantProportion*$totalMoney/100,6);
  266. if($userMoney+$grantMoney>$teamTotalMoney){
  267. $grantMoney=$teamTotalMoney-$userMoney;
  268. if($grantMoney<=0){
  269. $this->addContractLog($contract,$pMember->{'level_id'}.'层级,资金已不足,停止团队奖金清分');
  270. }
  271. }
  272. $userMoney+=$grantMoney;
  273. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$grantMoney,4,$pMember->{'id'},$pMember->{'address'},$grantProportion,1,$pMember->{'level_id'}.'级别团队分红');
  274. }elseif($pMember->{'level_id'}==$startId && $isLateral){
  275. //平级奖金发放
  276. $isLateral=false;
  277. $startId=$pMember->{'level_id'}+1;
  278. $lateralMoney=round($grantMoney*$levelData['lateral_num']/100,6);
  279. if($lateralMoney>0){
  280. if($userMoney+$lateralMoney>$teamTotalMoney){
  281. $lateralMoney=$teamTotalMoney-$userMoney;
  282. if($lateralMoney<=0){
  283. $this->addContractLog($contract,$pMember->{'level_id'}.'层级,资金已不足,停止团队平级奖金清分');
  284. }
  285. }
  286. $userMoney+=$lateralMoney;
  287. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$lateralMoney,4,$pMember->{'id'},$pMember->{'address'},$levelData['lateral_num'],1,$pMember->{'level_id'}.'级别团队平级分红');
  288. }else{
  289. $this->addContractLog($contract,$pMember->{'level_id'}.'层级,平级奖金小于等于0,停止团队平级奖金清分');
  290. }
  291. if($startId>count($levels)){
  292. //清分完成,跳出循环
  293. break;
  294. }
  295. }
  296. }
  297. }
  298. }
  299. }
  300. /**
  301. * 公排分账
  302. * @param Contract $contract
  303. */
  304. private function commonProportion(Contract $contract){
  305. $commonMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'common_proportion'}/100,6);
  306. $openSys=CommonServer::creatServer()->getConfigValue('open_sys');
  307. if($openSys && $commonMoney>0){
  308. $commonAddress=CommonServer::creatServer()->getConfigValue('common_address');
  309. if(empty($commonAddress)){
  310. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$commonMoney,5,0,'',$contract->{'common_proportion'},0,'公排分账,无收款地址信息。系统关闭');
  311. }else{
  312. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$commonMoney,5,0,$commonAddress,$contract->{'common_proportion'},1,'公排分账');
  313. }
  314. }else{
  315. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$commonMoney,5,0,'',$contract->{'common_proportion'},0,'平台关闭转账至系统账户,不进行划拨分账');
  316. }
  317. //公排触发清算队列
  318. JobServer::creatServer()->contractCommonJob();
  319. }
  320. /**
  321. * 沉淀账户收款地址
  322. * @param Contract $contract
  323. */
  324. private function sedimentProportion(Contract $contract){
  325. $sedimentMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'sediment_proportion'}/100,6);
  326. $openSys=CommonServer::creatServer()->getConfigValue('open_sys');
  327. if($openSys && $sedimentMoney>0){
  328. $sedimentAddress=CommonServer::creatServer()->getConfigValue('sediment_address');
  329. if(empty($sedimentAddress)){
  330. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$sedimentMoney,6,0,'',$contract->{'sediment_proportion'},0,'沉淀分账,无收款地址信息。系统关闭');
  331. }else{
  332. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$sedimentMoney,6,0,$sedimentAddress,$contract->{'sediment_proportion'},1,'沉淀分账');
  333. }
  334. }else{
  335. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$sedimentMoney,6,0,'',$contract->{'sediment_proportion'},0,'平台关闭转账至系统账户,不进行划拨分账');
  336. }
  337. }
  338. /**
  339. * 代币比例
  340. * @param Contract $contract
  341. */
  342. private function agencyProportion(Contract $contract){
  343. $agencyMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'agency_proportion'}/100,6);
  344. $exchangeMoney=CommonServer::creatServer()->getConfigValue('exchange_money');
  345. $dbMoney=$agencyMoney*$exchangeMoney;
  346. if($exchangeMoney >0){
  347. $member=Member::where('id',$contract->{'m_id'})->select(['id','address','is_boss'])->first();
  348. $this->addDistribution($contract->{'id'},$contract->{'m_id'},$dbMoney,0,7,$member->{'id'},$member->{'address'},$contract->{'agency_proportion'},1,'代币分账');
  349. MoneyDetailServer::creatServer()->write(1,1,$dbMoney,1,$member->{'id'},'报单代币清分',$contract->{'id'});
  350. }else{
  351. $sedimentAddress=CommonServer::creatServer()->getConfigValue('sediment_address');
  352. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$agencyMoney,7,0,$sedimentAddress,$contract->{'agency_proportion'},1,'代币价格为0,转入沉淀分账');
  353. $this->addContractLog($contract,'代币价格为0,不分发');
  354. }
  355. }
  356. /**
  357. * 添加清分记录
  358. * @param $contractId
  359. * @param $mId
  360. * @param $nbnMoney
  361. * @param $dbMoney
  362. * @param $type
  363. * @param $toMid
  364. * @param $toAddress
  365. * @param $proportion
  366. * @param $status
  367. * @param string $remark
  368. */
  369. function addDistribution($contractId,$mId,$dbMoney,$nbnMoney,$type,$toMid,$toAddress,$proportion,$status,$remark='奖金发放'){
  370. $serviceMoney=0;
  371. if($toMid>0 && $nbnMoney>0){
  372. $serviceMoney=CommonServer::creatServer()->getConfigValue('service_money')?:0;
  373. }
  374. ContractDistribution::create([
  375. 'contract_id' => $contractId,
  376. 'broadcast_id' => 0,
  377. 'm_id' => $mId,
  378. 'db_money' => $dbMoney,
  379. 'nbn_money' => $nbnMoney,
  380. 'type' => $type,
  381. 'to_m_id' => $toMid,
  382. 'to_address' => $toAddress,
  383. 'status' => $status,
  384. 'proportion' => $proportion,
  385. 'service_money' => $serviceMoney,
  386. 'remark' => $remark
  387. ]);
  388. }
  389. /**
  390. * 获取团队合约数量
  391. * @param $mId
  392. * @return int
  393. */
  394. function getTeamContractNum($mId){
  395. $contractNum=0;
  396. $teamIds=MemberClan::where('id',$mId)->value('m_ids');
  397. $teamIds=array_filter(explode(',',$teamIds));
  398. $teamIds[]=$mId;
  399. if($teamIds){
  400. $contractNum=Contract::whereIn('m_id',$teamIds)->whereIn('status',[2,4])->count();
  401. }
  402. return $contractNum;
  403. }
  404. /**
  405. * 公排出局检测
  406. * @throws \Exception
  407. */
  408. function verifyContractCommon(){
  409. $commonNum=CommonServer::creatServer()->getConfigValue('common_num');
  410. $contractIds=Contract::where('status',2)->where('is_out',0)->orderBy('id','asc')->limit($commonNum)->pluck('id')->toArray();
  411. if(count($contractIds)>=$commonNum){
  412. //选择第一个单子排队出公排
  413. $contract=Contract::where('status',2)->orderBy('id','asc')->first();
  414. DB::beginTransaction();
  415. Contract::whereIn('id',$contractIds)->update(['is_out'=>1]);
  416. $commonBnb=CommonServer::creatServer()->getConfigValue('common_bnb');
  417. $commonDb=CommonServer::creatServer()->getConfigValue('common_db');
  418. $exchangeMoney=CommonServer::creatServer()->getConfigValue('exchange_money');
  419. $member=Member::where('id',$contract->{'m_id'})->select(['id','address','is_boss'])->first();
  420. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$commonBnb,9,$member->{'id'},$member->{'address'},$commonBnb,1,'bsn公排出局');
  421. $dbMoney=1*$exchangeMoney*$commonDb;
  422. $this->addDistribution($contract->{'id'},$contract->{'m_id'},$dbMoney,0,9,$member->{'id'},$member->{'address'},$commonBnb,1,'代币公排出局');
  423. MoneyDetailServer::creatServer()->write(1,2,$dbMoney,1,$member->{'id'},'公排池代币出局',$contract->{'id'});
  424. $contract->update(['status' => 4]);
  425. DB::commit();
  426. }
  427. }
  428. /**
  429. * 合约日志记录
  430. * @param Contract $contract
  431. * @param $msg
  432. * @param array $data
  433. */
  434. function addContractLog(Contract $contract,$msg,$data=[]){
  435. ContractLog::create([
  436. 'contract_id'=> $contract->{'id'},
  437. 'm_id'=> $contract->{'m_id'},
  438. 'msg'=> $msg,
  439. 'contract_data'=> json_encode($contract),
  440. 'data'=> json_encode($data),
  441. ]);
  442. }
  443. }