ContractServer.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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',$contract->{'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. }
  112. DB::commit();
  113. }
  114. }
  115. }
  116. /**
  117. * 系统分账功能
  118. * @param Contract $contract
  119. * @param $pIds
  120. */
  121. private function platformDistribution(Contract $contract,$pIds){
  122. $platformMoney=$contract->{'platform_money'};
  123. if(empty($platformMoney)){
  124. $this->addContractLog($contract,'平台清分金额为0,停止平台清分');
  125. }else{
  126. //验证平台分红
  127. $receiveLevelId=CommonServer::creatServer()->getConfigValue('receive_level_id');
  128. //用户分红金额
  129. if(empty($receiveLevelId)){
  130. $this->addContractLog($contract,'未设置平台分红等级,跳出分红');
  131. }else{
  132. $pMember=Member::whereIn('id',$pIds)->where('level_id','>=',$receiveLevelId)->orderBy('id','desc')->select(['id','address','is_auto'])->first();
  133. if($pMember){
  134. $receiveUserMax=CommonServer::creatServer()->getConfigValue('receive_user_max');
  135. $teamContractNum=$this->getTeamContractNum($pMember->{'id'});
  136. if($teamContractNum>=$receiveUserMax){
  137. //最大比例
  138. $receiveProportion=CommonServer::creatServer()->getConfigValue('receive_max');
  139. $this->addContractLog($contract,$teamContractNum.'团队数量满足条件,执行最大分红比例');
  140. }else{
  141. //最小比例
  142. $receiveProportion=CommonServer::creatServer()->getConfigValue('receive_mix');
  143. $this->addContractLog($contract,$teamContractNum.'团队数量满不足条件,执行最小分红比例');
  144. }
  145. if(empty($receiveProportion)){
  146. $this->addContractLog($contract,$teamContractNum.'平台分红比例不存在,跳出分红');
  147. }else{
  148. $receiveMoney=round($platformMoney*$receiveProportion/100,6);
  149. if($receiveMoney<=0){
  150. $this->addContractLog($contract,$receiveProportion.'分红比例金额小于0,跳出分红');
  151. }else{
  152. //计算推荐分红数量及平台收益数量
  153. if($pMember->{'is_auto'}==1){
  154. $this->addContractLog($contract,'后台升级用户,跳出分红');
  155. }else{
  156. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$receiveMoney,1,$pMember->{'id'},$pMember->{'address'},$receiveProportion,1,'平台用户分红');
  157. $platformMoney-=$receiveMoney;
  158. }
  159. }
  160. }
  161. }else{
  162. $this->addContractLog($contract,$receiveLevelId.'及以上的父级不存在,跳出分红');
  163. }
  164. }
  165. $openSys=CommonServer::creatServer()->getConfigValue('open_sys');
  166. if($openSys){
  167. $receiveAddress=CommonServer::creatServer()->getConfigValue('receive_address');
  168. if(empty($receiveAddress)){
  169. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$platformMoney,1,0,'',$contract->{'platform_money'},0,'平台分红至系统账户');
  170. }else{
  171. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$platformMoney,1,0,$receiveAddress,$contract->{'platform_money'},1,'平台分红至系统账户');
  172. }
  173. }else{
  174. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$platformMoney,1,0,'',$contract->{'platform_money'},0,'平台关闭转账至系统账户,不进行划拨分账');
  175. }
  176. }
  177. }
  178. /**
  179. * 直推及间推
  180. * @param Contract $contract
  181. * @param MemberClan $memberTeam
  182. */
  183. private function directProportion(Contract $contract,MemberClan $memberTeam){
  184. $directMember=Member::where('id',$memberTeam->{'one_m_id'})->select(['id','address','is_boss'])->first();
  185. if(empty($directMember)){
  186. $this->addContractLog($contract,'直推人信息异常,停止清分');
  187. }else{
  188. if($directMember->{'is_boss'}){
  189. $this->addContractLog($contract,'直推人是boss,直与间都划分为直推人');
  190. $directProportion=($contract->{'direct_proportion'}+$contract->{'indirect_proportion'});
  191. $directMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$directProportion/100,6);
  192. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$directMoney,2,$directMember->{'id'},$directMember->{'address'},$directProportion,1,'直推BOSS分红');
  193. }else{
  194. $directMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'direct_proportion'}/100,6);
  195. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$directMoney,2,$directMember->{'id'},$directMember->{'address'},$contract->{'direct_proportion'},1,'直推分红');
  196. $indirectMember=Member::where('id',$memberTeam->{'two_m_id'})->select(['id','address','is_boss'])->first();
  197. $indirectMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'indirect_proportion'}/100,6);
  198. if(empty($indirectMember)){
  199. $this->addContractLog($contract,'间推人信息不存在,停止清分');
  200. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$indirectMoney,3,$directMember->{'id'},$directMember->{'address'},$contract->{'indirect_proportion'},0,'间推推分红,间推人不存在,未划分');
  201. }else{
  202. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$indirectMoney,3,$directMember->{'id'},$directMember->{'address'},$contract->{'indirect_proportion'},1,'间推推分红');
  203. }
  204. }
  205. }
  206. }
  207. /**
  208. * 团队清分
  209. * @param Contract $contract
  210. * @param $pIds
  211. */
  212. private function teamProportion(Contract $contract,$pIds){
  213. $levels=Level::select(['direct_num','id','lateral_num'])->get()->toArray();
  214. $levelArr=[];
  215. foreach ($levels as $level){
  216. $levelArr[$level['id']]=$level;
  217. }
  218. $totalMoney=$contract->{'money'}-$contract->{'platform_money'};
  219. $teamTotalMoney=round($totalMoney*$contract->{'team_proportion'}/100,6);
  220. $startId=1;
  221. $isLateral=false;
  222. //已经使用过的比例
  223. $useProportion=0;
  224. //已经使用金额
  225. $userMoney=0;
  226. //使用金额
  227. $grantMoney=0;
  228. foreach ($pIds as $pId){
  229. $pMember=Member::where('id',$pId)->select(['id','address','level_id'])->first();
  230. if(empty($pMember)){
  231. $this->addContractLog($contract,$pId.',无当前父及信息,停止清分');
  232. }else{
  233. $levelData=empty($levelArr[$pMember->{'level_id'}])?0:$levelArr[$pMember->{'level_id'}];
  234. if(empty($levelData)){
  235. $this->addContractLog($contract,$pMember->{'level_id'}.',无当前等级配置信息,停止清分');
  236. }else{
  237. if($pMember->{'level_id'}>$startId || ($pMember->{'level_id'}==$startId &&!$isLateral)){
  238. //等级大于等于当前等级
  239. $isLateral=true;
  240. //等级比例
  241. $levelProportion=$levelData['direct_num'];
  242. //等级比例-减去已经使用比例,获取到可用比列
  243. $grantProportion=$levelProportion-$useProportion;
  244. //累计已经使用的比例
  245. $useProportion+=$grantProportion;
  246. $grantMoney=round($grantProportion*$totalMoney/100,6);
  247. if($userMoney+$grantMoney>$teamTotalMoney){
  248. $grantMoney=$teamTotalMoney-$userMoney;
  249. if($grantMoney<=0){
  250. $this->addContractLog($contract,$pMember->{'level_id'}.'层级,资金已不足,停止团队奖金清分');
  251. }
  252. }
  253. $userMoney+=$grantMoney;
  254. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$grantMoney,4,$pMember->{'id'},$pMember->{'address'},$grantProportion,1,$pMember->{'level_id'}.'级别团队分红');
  255. }elseif($pMember->{'level_id'}==$startId && $isLateral){
  256. //平级奖金发放
  257. $isLateral=false;
  258. $startId=$pMember->{'level_id'}+1;
  259. $lateralMoney=round($grantMoney*$levelData['lateral_num']/100,6);
  260. if($lateralMoney>0){
  261. if($userMoney+$lateralMoney>$teamTotalMoney){
  262. $lateralMoney=$teamTotalMoney-$userMoney;
  263. if($lateralMoney<=0){
  264. $this->addContractLog($contract,$pMember->{'level_id'}.'层级,资金已不足,停止团队平级奖金清分');
  265. }
  266. }
  267. $userMoney+=$lateralMoney;
  268. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$lateralMoney,4,$pMember->{'id'},$pMember->{'address'},$levelData['lateral_num'],1,$pMember->{'level_id'}.'级别团队平级分红');
  269. }else{
  270. $this->addContractLog($contract,$pMember->{'level_id'}.'层级,平级奖金小于等于0,停止团队平级奖金清分');
  271. }
  272. if($startId>count($levels)){
  273. //清分完成,跳出循环
  274. break;
  275. }
  276. }
  277. }
  278. }
  279. }
  280. }
  281. /**
  282. * 公排分账
  283. * @param Contract $contract
  284. */
  285. private function commonProportion(Contract $contract){
  286. $commonMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'common_proportion'}/100,6);
  287. $openSys=CommonServer::creatServer()->getConfigValue('open_sys');
  288. if($openSys){
  289. $commonAddress=CommonServer::creatServer()->getConfigValue('common_address');
  290. if(empty($commonAddress)){
  291. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$commonMoney,5,0,'',$contract->{'common_proportion'},0,'公排分账,无收款地址信息。系统关闭');
  292. }else{
  293. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$commonMoney,5,0,$commonAddress,$contract->{'common_proportion'},1,'公排分账');
  294. }
  295. }else{
  296. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$commonMoney,5,0,'',$contract->{'common_proportion'},0,'平台关闭转账至系统账户,不进行划拨分账');
  297. }
  298. //公排触发清算队列
  299. }
  300. /**
  301. * 沉淀账户收款地址
  302. * @param Contract $contract
  303. */
  304. private function sedimentProportion(Contract $contract){
  305. $sedimentMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'sediment_proportion'}/100,6);
  306. $openSys=CommonServer::creatServer()->getConfigValue('open_sys');
  307. if($openSys){
  308. $sedimentAddress=CommonServer::creatServer()->getConfigValue('sediment_address');
  309. if(empty($sedimentAddress)){
  310. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$sedimentMoney,6,0,'',$contract->{'sediment_proportion'},0,'沉淀分账,无收款地址信息。系统关闭');
  311. }else{
  312. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$sedimentMoney,6,0,$sedimentAddress,$contract->{'sediment_proportion'},1,'沉淀分账');
  313. }
  314. }else{
  315. $this->addDistribution($contract->{'id'},$contract->{'m_id'},0,$sedimentMoney,6,0,'',$contract->{'sediment_proportion'},0,'平台关闭转账至系统账户,不进行划拨分账');
  316. }
  317. }
  318. /**
  319. * 代币比例
  320. * @param Contract $contract
  321. */
  322. private function agencyProportion(Contract $contract){
  323. $agencyMoney=round(($contract->{'money'}-$contract->{'platform_money'})*$contract->{'agency_proportion'}/100,6);
  324. $exchangeMoney=CommonServer::creatServer()->getConfigValue('exchange_money');
  325. $dbMoney=$agencyMoney*$exchangeMoney;
  326. $member=Member::where('id',$contract->{'m_id'})->select(['id','address','is_boss'])->first();
  327. $this->addDistribution($contract->{'id'},$contract->{'m_id'},$dbMoney,0,7,$member->{'id'},$member->{'address'},$contract->{'agency_proportion'},1,'代币分账');
  328. MoneyDetailServer::creatServer()->write(1,1,$dbMoney,1,$member->{'id'},'报单代币清分',$contract->{'id'});
  329. }
  330. /**
  331. * 添加清分记录
  332. * @param $contractId
  333. * @param $mId
  334. * @param $nbnMoney
  335. * @param $dbMoney
  336. * @param $type
  337. * @param $toMid
  338. * @param $toAddress
  339. * @param $proportion
  340. * @param $status
  341. * @param string $remark
  342. */
  343. function addDistribution($contractId,$mId,$dbMoney,$nbnMoney,$type,$toMid,$toAddress,$proportion,$status,$remark='奖金发放'){
  344. ContractDistribution::create([
  345. 'contract_id' => $contractId,
  346. 'broadcast_id' => 0,
  347. 'm_id' => $mId,
  348. 'db_money' => $dbMoney,
  349. 'nbn_money' => $nbnMoney,
  350. 'type' => $type,
  351. 'to_m_id' => $toMid,
  352. 'to_address' => $toAddress,
  353. 'status' => $status,
  354. 'proportion' => $proportion,
  355. 'remark' => $remark
  356. ]);
  357. }
  358. /**
  359. * 获取团队合约数量
  360. * @param $mId
  361. * @return int
  362. */
  363. function getTeamContractNum($mId){
  364. $contractNum=0;
  365. $teamIds=MemberClan::where('id',$mId)->value('m_ids');
  366. $teamIds=array_filter(explode(',',$teamIds));
  367. $teamIds[]=$mId;
  368. if($teamIds){
  369. $contractNum=Contract::whereIn('m_id',$teamIds)->where('status','2')->count();
  370. }
  371. return $contractNum;
  372. }
  373. /**
  374. * 合约日志记录
  375. * @param Contract $contract
  376. * @param $msg
  377. * @param array $data
  378. */
  379. function addContractLog(Contract $contract,$msg,$data=[]){
  380. ContractLog::create([
  381. 'contract_id'=> $contract->{'id'},
  382. 'm_id'=> $contract->{'m_id'},
  383. 'msg'=> $msg,
  384. 'contract_data'=> json_encode($contract),
  385. 'data'=> json_encode($data),
  386. ]);
  387. }
  388. }