ExtensionUserProfitLog.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\Redis;
  6. class ExtensionUserProfitLog
  7. {
  8. public function add($UserID, $Type, $InvitationUserID, $Level)
  9. {
  10. // 获取深度 -- 邀请用户
  11. $NoteCount = $this->addLog($UserID, $Type, $InvitationUserID, $Level);
  12. // 查询用户产生总得金额--匹配档位
  13. $verify = $this->verify($UserID, $Type);
  14. $toGold = $verify['TotalScore'] ?? 0; // 待审额度
  15. $totalScore = $verify['allScore'] ?? 0; // 审核通过 + 待审
  16. $Status = $verify['Status'] ?? 0;
  17. // 查询配置
  18. $config = DB::connection('write')->table('agent.dbo.extension_config')
  19. ->where('quota_min', '<=', $totalScore)
  20. ->where('quota_max', '>=', $totalScore)
  21. ->where('type', $Type)
  22. ->first();
  23. $addScore = 0;
  24. if ($config) {
  25. if ($Type == 1) {
  26. if ($Level == 1 && $NoteCount >= $config->level_quota_number) {
  27. $addScore = mt_rand($config->level_quota_min, $config->level_quota_max);
  28. } elseif ($Level == 2 && $NoteCount >= $config->two_level_quota_number) {
  29. $addScore = mt_rand($config->two_level_quota_min, $config->two_level_quota_max);
  30. }
  31. }
  32. if ($Type == 2) { // 对局 =》 获取局数
  33. $redis = Redis::connection('test');
  34. $NoteCount = $redis->get($InvitationUserID);
  35. if ($Level == 1 && $NoteCount % $config->level_quota_number == 0) {
  36. $addScore = mt_rand($config->level_quota_min, $config->level_quota_max);
  37. } elseif ($Level == 2 && $NoteCount % $config->two_level_quota_number == 0) {
  38. $addScore = mt_rand($config->two_level_quota_min, $config->two_level_quota_max);
  39. }
  40. }
  41. Log::channel('invitation')->info($UserID . ' 第一步的随机值:' . $addScore);
  42. if ($addScore > 0) {
  43. // 读取上限配置
  44. $upperLimit = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  45. ->where('StatusName', 'UpperLimit')
  46. ->first()->StatusValue ?? 0;
  47. Log::channel('invitation')->info($UserID . ' 读取上限配置:' . $upperLimit);
  48. // 查询是否审核通过,没有审核通过走默认配置
  49. if ($Status == 0) {
  50. Log::channel('invitation')->info($UserID . ' 值 ' . ($toGold + $addScore));
  51. // 默认配置1 达到90%-96%这个“达成额度”区间以后,每次生效增长额度按照3%*真实生效额度来增加
  52. $upperLimitMin = $upperLimit * 0.9;
  53. $upperLimitMax = $upperLimit * 0.96;
  54. // if ($toGold >= $upperLimitMin && $toGold <= $upperLimitMax) {
  55. // $addScore = $addScore * 0.03;
  56. // Log::channel('invitation')->info($UserID . ' 默认配置1 ' . $addScore);
  57. // }
  58. // 默认配置2 达到96%-99%这个“达成额度”区间以后,每次生效增长额度按照1%*真实生效额度来增加.
  59. $upperLimitMin = $upperLimit * 0.96;
  60. $upperLimitMax = $upperLimit * 0.99;
  61. // if ($toGold > $upperLimitMin && $toGold <= $upperLimitMax) {
  62. // $addScore = $toGold + ($addScore * 0.01);
  63. // Log::channel('invitation')->info($UserID . ' 默认配置1 ' . $addScore);
  64. // }
  65. // 默认配置3 达到99%-99.99%这个“达成额度”区间以后,增长为0。反正要防止用户永远也达不到100%的额度。
  66. $upperLimitMin = $upperLimit * 0.99;
  67. $upperLimitMax = $upperLimit * 0.9999;
  68. // if ($toGold > $upperLimitMin && $toGold <= $upperLimitMax) {
  69. // $addScore = 0;
  70. // Log::channel('invitation')->info($UserID . ' 默认配置1 ' . $addScore);
  71. // }
  72. // 极端判断
  73. // if ($toGold + $addScore > $upperLimit * 0.99 && $toGold < $upperLimit * 0.9) {
  74. // $addScore = ($upperLimit * 0.9) - $toGold;
  75. // } elseif ($toGold + $addScore > $upperLimit * 0.99 && $toGold < $upperLimit * 0.96) {
  76. // $addScore = ($upperLimit * 0.96) - $toGold;
  77. // } elseif ($toGold + $addScore > $upperLimit * 0.99 && $toGold < $upperLimit * 0.99) {
  78. // $addScore = ($upperLimit * 0.99) - $toGold;
  79. // } elseif ($toGold + $addScore > $upperLimit) {
  80. // $addScore = 0;
  81. // }
  82. // 自动审核开关
  83. $autoVerify = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  84. ->where('StatusName', 'AutoVerify')
  85. ->value('StatusValue');
  86. if ($autoVerify == 0) {
  87. // 极端判断
  88. if ($toGold + $addScore > $upperLimit * 0.99 && $toGold < $upperLimit * 0.99) {
  89. $addScore = ($upperLimit * 0.99) - $toGold;
  90. } elseif ($toGold + $addScore > $upperLimit) {
  91. $addScore = 0;
  92. }
  93. Log::channel('invitation')->info($UserID . ' 默认配置4 ' . $addScore);
  94. }
  95. } else {
  96. // if ($addScore > $upperLimit) {
  97. // $addScore = $upperLimit * 0.9;
  98. // }
  99. }
  100. Log::channel('invitation')->info($UserID . ' 要修改的分数 ' . $addScore);
  101. // 修改分数
  102. if ($Type == 1) {
  103. DB::connection('write')->table('agent.dbo.extension_user_profit_log')
  104. ->where('UserID', $UserID)
  105. ->where('Status', 0)
  106. ->where('Type', $Type)
  107. ->where('Level', $Level)
  108. ->update(['toGold' => DB::raw("toGold + $addScore"), 'Status' => 1]);
  109. } else {
  110. DB::connection('write')->table('agent.dbo.extension_user_profit_log')
  111. ->where('InvitationUserIDs', $InvitationUserID)
  112. ->where('Status', 0)
  113. ->where('Type', $Type)
  114. ->where('Level', $Level)
  115. ->update(['toGold' => DB::raw("toGold + $addScore"), 'Status' => 1]);
  116. }
  117. DB::connection('write')->table('agent.dbo.extension_verify')
  118. ->where('UserID', $UserID)
  119. ->where('Type', $Type)
  120. ->update(['TotalScore' => DB::raw("TotalScore + $addScore")]);
  121. // 生成订单
  122. if ($toGold + $addScore >= $upperLimit) {
  123. $data = [
  124. 'UserID' => $UserID,
  125. 'Score' => $upperLimit,
  126. 'CreateAt' => date('Y-m-d H:i:s'),
  127. 'Type' => $Type,
  128. 'OverflowScore' => abs($upperLimit - ($toGold + $addScore))
  129. ];
  130. $orderID = DB::connection('write')->table('agent.dbo.extension_user_profit_order')->insertGetId($data);
  131. DB::connection('write')->table('agent.dbo.extension_user_profit_log')
  132. ->where('UserID', $UserID)
  133. ->where('Type', $Type)
  134. ->where('OrderID', 0)
  135. ->update(['OrderID' => $orderID]);
  136. // 修改记录
  137. DB::connection('write')->table('agent.dbo.extension_verify_log')
  138. ->where('UserID', $UserID)
  139. ->where('OrderID', 0)
  140. ->where('Type', $Type)
  141. ->update(['UpdateTime' => date('Y-m-d H:i:s'), 'OrderID' => $orderID, 'FinalScore' => $upperLimit]);
  142. // 改变完成审核金额度
  143. DB::connection('write')->table('agent.dbo.extension_verify')
  144. ->where('UserID', $UserID)
  145. ->where('Type', $Type)
  146. ->update(['FinalScore' => DB::raw("FinalScore + $upperLimit"), 'TotalScore' => DB::raw("TotalScore - $upperLimit"), 'Status' => 0]);
  147. Log::channel('invitation')->info($UserID . ' 生成订单 ' . \GuzzleHttp\json_encode($data));
  148. }
  149. }
  150. }
  151. }
  152. public function addLog($UserID, $Type, $InvitationUserID, $Level)
  153. {
  154. $NoteCount = 1;
  155. // 查询有没有添加过
  156. if ($Type == 1) {
  157. $first = DB::connection('write')->table('agent.dbo.extension_user_profit_log')
  158. ->where('UserID', $UserID)
  159. ->where('Status', 0)
  160. ->where('Type', $Type)
  161. ->where('Level', $Level)
  162. ->first();
  163. if ($first) {
  164. $data['NoteCount'] = $first->NoteCount + 1;
  165. $data['InvitationUserIDs'] = $first->InvitationUserIDs . ',' . $InvitationUserID;
  166. $data['UpdateAt'] = date('Y-m-d H:i:s');
  167. $NoteCount = $first->NoteCount + 1;
  168. } else {
  169. $data['NoteCount'] = $NoteCount;
  170. $data['InvitationUserIDs'] = $InvitationUserID;
  171. $data['UpdateAt'] = date('Y-m-d H:i:s');
  172. $data['CreateAt'] = date('Y-m-d H:i:s');
  173. $data['Type'] = $Type;
  174. }
  175. $where = [
  176. 'UserID' => $UserID,
  177. 'Status' => 0,
  178. 'Type' => $Type,
  179. 'Level' => $Level,
  180. ];
  181. DB::connection('write')->table('agent.dbo.extension_user_profit_log')->updateOrInsert($where, $data);
  182. } else {
  183. $first = DB::connection('write')->table('agent.dbo.extension_user_profit_log')
  184. ->where('InvitationUserIDs', $InvitationUserID)
  185. ->where('UserID', $UserID)
  186. ->where('Status', 0)
  187. ->where('Type', $Type)
  188. ->where('Level', $Level)
  189. ->first();
  190. if (!$first) {
  191. $data['NoteCount'] = $NoteCount;
  192. $data['InvitationUserIDs'] = $InvitationUserID;
  193. $data['UpdateAt'] = date('Y-m-d H:i:s');
  194. $data['CreateAt'] = date('Y-m-d H:i:s');
  195. $data['Type'] = $Type;
  196. $data['UserID'] = $UserID;
  197. $data['Level'] = $Level;
  198. DB::connection('write')->table('agent.dbo.extension_user_profit_log')->Insert($data);
  199. }
  200. }
  201. return $NoteCount;
  202. }
  203. // 审核表信息
  204. protected function verify($UserID, $Type)
  205. {
  206. $exists = DB::connection('write')->table('agent.dbo.extension_verify')
  207. ->where('UserID', $UserID)
  208. ->where('Type', $Type)
  209. ->selectRaw('IsNull(FinalScore+TotalScore,0) allScore,Status,TotalScore')
  210. ->first();
  211. $allScore = $TotalScore = 0;
  212. $Status = $exists->Status ?? 0; # 审核状态
  213. if ($exists) {
  214. $allScore = $exists->allScore;
  215. $TotalScore = $exists->TotalScore;
  216. } else {
  217. // 添加审核记录
  218. DB::connection('write')->table('agent.dbo.extension_verify')
  219. ->insert([
  220. 'UserID' => $UserID,
  221. 'FinalScore' => 0,
  222. 'TotalScore' => $allScore,
  223. 'Type' => $Type,
  224. 'Status' => 0,
  225. ]);
  226. }
  227. return compact('allScore', 'TotalScore', 'Status');
  228. }
  229. }