Alltools.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace App\Common\Library;
  3. use Illuminate\Support\Facades\DB;
  4. class Alltools {
  5. //四大收益,视频,音频,私聊,礼物
  6. public function shouyi($user_id,$gold,$table,$table_id){
  7. //精确小数点
  8. bcscale(2);
  9. //枚举
  10. $table_enum = [
  11. 'user_match_audio_log' => [
  12. 'prefix'=>'audio',
  13. 'remark' =>'语音',
  14. ],
  15. 'user_match_typing_log' => [
  16. 'prefix'=>'typing',
  17. 'remark' =>'私聊',
  18. ],
  19. 'user_match_video_log' => [
  20. 'prefix'=>'video',
  21. 'remark' =>'视频',
  22. ],
  23. 'gift_user_typing' => [
  24. 'prefix'=>'gift',
  25. 'remark' =>'收礼',
  26. ],
  27. ];
  28. //变量前缀,钱包备注
  29. $prefix = $table_enum[$table]['prefix'];
  30. $remark = $table_enum[$table]['remark'];
  31. //收益者本人
  32. $userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','gender'])->where('id',$user_id)->first();
  33. //没有推荐人,自己等级最高
  34. if(empty($userinfo->intro_uid) || $userinfo->group_id == 3){
  35. return true;
  36. }
  37. //充值者上级
  38. $intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','gender'])->where('id',$userinfo->intro_uid)->first();
  39. if(empty($intro_userinfo)){return true;}
  40. //配置
  41. $config_site = DB::table('mt_config')->whereIn('name',['shouyi_b2a_rate','shouyi_c2a_rate'])->pluck('value','name');
  42. //上级是一级邀请A,本人为B普、B1(二级邀请人),贡献给A
  43. if($intro_userinfo->group_id == 3){
  44. $shouyi_b2a_rate = $config_site['shouyi_b2a_rate'];//b2a 与 b02a相等
  45. $jewelA = bcdiv(bcmul($gold,$shouyi_b2a_rate),100);
  46. $result = $this->lockChangeAccountRemain($intro_userinfo->id,'agentjewel',$jewelA,52, $userinfo->username.$remark,$table,$table_id);
  47. if($result['status']===false)
  48. {
  49. //Db::rollback();
  50. return false;
  51. }
  52. return true;//结束了
  53. }
  54. //上级是二级邀请B1,本人C1,贡献给B和A(表面上说B的钱在A那里,但是有记录,有总额,不能提现即可)
  55. if($intro_userinfo->group_id == 2 && $intro_userinfo->group_id > $userinfo->group_id){
  56. //充值者上上级,也就是A
  57. $intro_intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','gender'])->where('id',$intro_userinfo->intro_uid)->first();
  58. if(empty($intro_intro_userinfo)){return true;}
  59. if($intro_intro_userinfo->group_id == 3){
  60. $shouyi_c2a_rate = $config_site['shouyi_c2a_rate'];
  61. $jewelA = bcdiv(bcmul($gold,$shouyi_c2a_rate),100);
  62. $result = $this->lockChangeAccountRemain($intro_intro_userinfo->id,'agentjewel',$jewelA,52, $userinfo->username.$remark,$table,$table_id);
  63. if($result['status']===false)
  64. {
  65. //Db::rollback();
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71. return true;
  72. }
  73. //充值后奖金
  74. public function recharge($user_id,$gold,$table,$payorder_id){
  75. //充值者本人
  76. $userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','gender'])->where('id',$user_id)->first();
  77. //配置
  78. $config_site = DB::table('mt_config')->whereIn('name',['invite_recharge_rate','recharge_b2a_rate','recharge_c2a_rate','recharge_d2c_rate','recharge_c2a_rate'])->pluck('value','name');
  79. //个人直推邀请充值
  80. if(!empty($userinfo->invite_uid) && $userinfo->gender == 1 && empty($userinfo->intro_uid)){
  81. $recharge_invite_rate = $config_site['invite_recharge_rate'];
  82. $jewel = bcdiv(bcmul($gold,$recharge_invite_rate,1),100,1);
  83. $result = $this->lockChangeAccountRemain($userinfo->invite_uid,'jewel',$jewel,41, $userinfo->username.'充值','pay_order',$payorder_id);
  84. if($result['status']===false)
  85. {
  86. //Db::rollback();
  87. return false;
  88. }
  89. }
  90. //没有推荐人,自己等级最高,女性充值不返
  91. if(empty($userinfo->intro_uid) || $userinfo->group_id == 3 || $userinfo->gender == 0){
  92. return true;
  93. }
  94. //充值者上级
  95. $intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','gender'])->where('id',$userinfo->intro_uid)->first();
  96. if(empty($intro_userinfo)){return true;}
  97. //精确小数点
  98. bcscale(2);
  99. //上级是一级邀请A,本人为B普、B1(二级邀请人),贡献给A
  100. if($intro_userinfo->group_id == 3){
  101. $recharge_b2a_rate = $config_site['recharge_b2a_rate'];//b2a 与 b02a相等
  102. $jewelA = bcdiv(bcmul($gold,$recharge_b2a_rate),100);
  103. $result = $this->lockChangeAccountRemain($intro_userinfo->id,'agentjewel',$jewelA,51, $userinfo->username.'充值','pay_order',$payorder_id);
  104. if($result['status']===false)
  105. {
  106. //Db::rollback();
  107. return false;
  108. }
  109. return true;//结束了
  110. }
  111. //上级是二级邀请B1,本人C1,贡献给B和A(表面上说B的钱在A那里,但是有记录,有总额,不能提现即可)
  112. if($intro_userinfo->group_id == 2 && $intro_userinfo->group_id > $userinfo->group_id){
  113. //充值者上上级,也就是A
  114. $intro_intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','gender'])->where('id',$intro_userinfo->intro_uid)->first();
  115. if(empty($intro_intro_userinfo)){return true;}
  116. if($intro_intro_userinfo->group_id == 3){
  117. $recharge_c2a_rate = $config_site['recharge_c2a_rate'];
  118. $jewelA = bcdiv(bcmul($gold,$recharge_c2a_rate),100);
  119. $result = $this->lockChangeAccountRemain($intro_intro_userinfo->id,'agentjewel',$jewelA,51, $userinfo->username.'充值','pay_order',$payorder_id);
  120. if($result['status']===false)
  121. {
  122. //Db::rollback();
  123. return false;
  124. }
  125. }
  126. return true;
  127. }
  128. //上级是普通用户,那自己肯定也是普通,则上级可能是D1、C1,找到上上级,如果也是普通C1,停止。如果上上级是B1,则一直追到A1
  129. //上级是普通,自己可能是B1,这种不考虑,因为上级只能用普通网,自己相当于独立于超推网里的新分支,是后台新升级来的
  130. if($intro_userinfo->group_id == 1 && $userinfo->group_id == 1){
  131. //先给上级普通用户返
  132. $recharge_d2c_rate = $config_site['recharge_d2c_rate'];//d2c 或 e2d
  133. $jewelC = bcdiv(bcmul($gold,$recharge_d2c_rate),100);
  134. $result = $this->lockChangeAccountRemain($intro_userinfo->id,'agentjewel',$jewelC,51, $userinfo->username.'充值','pay_order',$payorder_id);
  135. if($result['status']===false)
  136. {
  137. //Db::rollback();
  138. return false;
  139. }
  140. //充值者上上级
  141. $intro_intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','gender'])->where('id',$intro_userinfo->intro_uid)->first();
  142. if(empty($intro_intro_userinfo)){return true;}
  143. //上上级也是普通C1,说明自己在E1,上级是D1,停止
  144. if($intro_intro_userinfo->group_id == 1){
  145. return true;
  146. }
  147. //上上级是B1,上级是C1,自己是D1,
  148. if($intro_intro_userinfo->group_id == 2){
  149. //充值者上上级,也就是A
  150. $intro_intro_intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','gender'])->where('id',$intro_intro_userinfo->intro_uid)->first();
  151. if(empty($intro_intro_intro_userinfo)){return true;}
  152. if($intro_intro_intro_userinfo->group_id == 3){
  153. $recharge_c2a_rate = $config_site['recharge_c2a_rate'];
  154. $jewelA = bcdiv(bcmul($jewelC,$recharge_c2a_rate),100);
  155. $result = $this->lockChangeAccountRemain($intro_intro_intro_userinfo->id,'agentjewel',$jewelA,51, $userinfo->username.'充值','pay_order',$payorder_id);
  156. if($result['status']===false)
  157. {
  158. //Db::rollback();
  159. return false;
  160. }
  161. }
  162. }
  163. return true;
  164. }
  165. return true;
  166. }
  167. /**
  168. *
  169. * @param floatval $number 金额(正数进账,负数出账)
  170. * @param $accountType 货币类型,money,score
  171. * @param $logtype 日志的类型
  172. * @param $remark 备注
  173. * @param $user_id 用户id
  174. * @param $table 来源表
  175. * @param $data_id 表id
  176. * @param $isAdmin 是否是管理员处理
  177. * @return array
  178. * @return array[status]
  179. * @return array[msg]
  180. * @return array[log_table]
  181. * @return array[log_id]
  182. */
  183. public function lockChangeAccountRemain($user_id,$accountType='gold',$number,$logtype='',$remark='',$table='',$table_id=0,$isAdmin=false)
  184. {
  185. //初始化
  186. $result = array(
  187. 'status'=>false,
  188. 'msg'=>'',
  189. 'log_table' => '',
  190. 'log_id' => '',
  191. );
  192. //获取小数点
  193. $point = 1;
  194. if($accountType == 'agentjewel'){
  195. $point = 2;
  196. }
  197. bcscale($point);
  198. //检测0
  199. $number = floatval( $number );
  200. if( $number == 0 )
  201. {
  202. $result['status'] = true;
  203. $result['msg'] = '交易金额:0';
  204. return $result;
  205. }
  206. if(0 === bccomp($number, 0)){
  207. $result['status'] = true;
  208. $result['msg'] = '交易金额:0';
  209. return $result;
  210. }
  211. //查询余额
  212. $wallet = DB::table('mt_user_wallet')->where('user_id',$user_id)->lockForUpdate()->first();
  213. if(empty($wallet))
  214. {
  215. $result['msg'] = '不存在的用户';
  216. return $result;
  217. }
  218. //扣除金币单独方法
  219. //gold的增加,jewel的增、减
  220. //之前的余额
  221. $wallet_before = $wallet->$accountType;
  222. //正常方法
  223. if(bccomp(bcadd($wallet_before, $number), 0) === -1)
  224. {
  225. $result['msg'] = '余额不足!';
  226. return $result;
  227. }
  228. //钱币操作
  229. $data = array();
  230. $data['user_id'] = $user_id;
  231. $data['log_type'] = $logtype;
  232. $data['money_type'] = $accountType;
  233. $data['before'] = $wallet_before;
  234. $data['change_value'] = $number;
  235. $data['remain'] = bcadd($wallet_before, $number);
  236. $data['table'] = $table;
  237. $data['table_id'] = $table_id;
  238. $data['remark'] = $remark;
  239. $data['createtime'] = time();
  240. $data['updatetime'] = time();
  241. $log_table = 'mt_user_gold_log';
  242. if($accountType == 'agentjewel'){
  243. $log_table = 'mt_user_agentjewel_log';
  244. }
  245. $rs2_id = DB::table($log_table)->insertGetId($data);
  246. //修改金额
  247. $rs1 = DB::table('mt_user_wallet')->where('user_id',$user_id)->update([$accountType => $data['remain']]);
  248. //操作数据库失败
  249. if($rs1 === false || $rs2_id === false){
  250. $result['msg'] = '更新财务记录失败!';
  251. return $result;
  252. }
  253. //默认成功
  254. $result['status'] = true;
  255. $result['msg'] = '账户余额已更新!';
  256. $result['log_table'] = $log_table;
  257. $result['log_id'] = $rs2_id;
  258. return $result;
  259. }
  260. }