ContractServer.php 19 KB

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