Alltools.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. $config_site = DB::table('mt_config')->whereIn('name',['shouyi_b2a_rate','shouyi_c2a_rate'])->pluck('value','name');
  33. //收益者本人
  34. $userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','agent_id','gender'])->where('id',$user_id)->first();
  35. //先确认自己的等级
  36. if($userinfo->group_id == 1){
  37. //充值者上级
  38. $intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','agent_id','gender'])->where('id',$userinfo->intro_uid)->first();
  39. if(empty($intro_userinfo)){return true;}
  40. //上级是二级邀请B1,贡献给A
  41. if($intro_userinfo->group_id == 2){
  42. //充值者上上级,也就是A
  43. $intro_intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','agent_id','gender'])->where('id',$intro_userinfo->agent_id)->first();
  44. if(empty($intro_intro_userinfo)){return true;}
  45. if($intro_intro_userinfo->group_id == 3){
  46. $shouyi_c2a_rate = $config_site['shouyi_c2a_rate'];
  47. $jewelA = bcdiv(bcmul($gold,$shouyi_c2a_rate),100);
  48. $result = $this->lockChangeAccountRemain($intro_intro_userinfo->id,'agentjewel',$jewelA,52, $userinfo->username.$remark,$table,$table_id);
  49. if($result['status']===false)
  50. {
  51. //Db::rollback();
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57. //上级是A
  58. if($intro_userinfo->group_id == 3){
  59. $shouyi_b2a_rate = $config_site['shouyi_b2a_rate'];//b2a 与 b02a相等
  60. $jewelA = bcdiv(bcmul($gold,$shouyi_b2a_rate),100);
  61. $result = $this->lockChangeAccountRemain($intro_userinfo->id,'agentjewel',$jewelA,52, $userinfo->username.$remark,$table,$table_id);
  62. if($result['status']===false)
  63. {
  64. //Db::rollback();
  65. return false;
  66. }
  67. return true;//结束了
  68. }
  69. }
  70. if($userinfo->group_id == 2){
  71. //充值者上级
  72. $intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','agent_id','gender'])->where('id',$userinfo->intro_uid)->first();
  73. if(empty($intro_userinfo)){return true;}
  74. //上级是一级邀请A,本人为B普、B1(二级邀请人),贡献给A
  75. /*if($intro_userinfo->group_id == 3){//不是返给一级代理,而是给上级,因为可能和上级解绑放到自己名下了 */
  76. $shouyi_b2a_rate = $config_site['shouyi_b2a_rate'];//b2a 与 b02a相等
  77. $jewelA = bcdiv(bcmul($gold,$shouyi_b2a_rate),100);
  78. $result = $this->lockChangeAccountRemain($intro_userinfo->id,'agentjewel',$jewelA,52, $userinfo->username.$remark,$table,$table_id);
  79. if($result['status']===false)
  80. {
  81. //Db::rollback();
  82. return false;
  83. }
  84. return true;//结束了
  85. /*}*/
  86. }
  87. //没有推荐人,自己等级最高
  88. if($userinfo->group_id == 3 && !empty($userinfo->intro_uid)){
  89. //充值者上级
  90. $intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','agent_id','gender'])->where('id',$userinfo->intro_uid)->first();
  91. if(empty($intro_userinfo)){return true;}
  92. $shouyi_b2a_rate = $config_site['shouyi_b2a_rate'];
  93. $jewelA = bcdiv(bcmul($gold,$shouyi_b2a_rate),100);
  94. $result = $this->lockChangeAccountRemain($intro_userinfo->id,'agentjewel',$jewelA,52, $userinfo->username.$remark,$table,$table_id);
  95. if($result['status']===false)
  96. {
  97. //Db::rollback();
  98. return false;
  99. }
  100. return true;//结束了
  101. }
  102. //////////////////
  103. return true;
  104. }
  105. //充值后奖金
  106. public function recharge($user_id,$gold,$table,$payorder_id,$remark = '',$times = 0){
  107. //充值者本人
  108. $userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','agent_id','gender'])->where('id',$user_id)->first();
  109. //配置
  110. $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');
  111. //备注
  112. $remark = !empty($remark) ? $remark : $userinfo->username.'充值';
  113. //个人直推邀请充值
  114. if(!empty($userinfo->invite_uid) && $userinfo->gender == 1 && empty($userinfo->intro_uid)){
  115. $recharge_invite_rate = $config_site['invite_recharge_rate'];
  116. $jewel = bcdiv(bcmul($gold,$recharge_invite_rate,1),100,1);
  117. $result = $this->lockChangeAccountRemain($userinfo->invite_uid,'jewel',$jewel,41, $remark,'pay_order',$payorder_id);
  118. if($result['status']===false)
  119. {
  120. //Db::rollback();
  121. return false;
  122. }
  123. //D2C,C2A
  124. $invite_info = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','agent_id','gender'])->where('id',$userinfo->invite_uid)->first();
  125. if(!empty($invite_info->intro_uid) && $times == 0){
  126. $times = 1;//可以执行的次数
  127. $recharge_rs = $this->recharge($userinfo->invite_uid,$jewel,'pay_order',$payorder_id,$invite_info->username.'邀请'.$remark,$times);
  128. return $recharge_rs;
  129. }
  130. }
  131. //没有推荐人,女性充值不返
  132. if(empty($userinfo->intro_uid) || ($userinfo->gender == 0 && $times == 0)){
  133. return true;
  134. }
  135. //精确小数点
  136. bcscale(2);
  137. //自己等级最高
  138. if($userinfo->group_id == 3 && !empty($userinfo->intro_uid)){
  139. //充值者上级
  140. $intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','agent_id','gender'])->where('id',$userinfo->intro_uid)->first();
  141. if(empty($intro_userinfo)){return true;}
  142. $recharge_b2a_rate = $config_site['recharge_b2a_rate'];
  143. $jewelA = bcdiv(bcmul($gold,$recharge_b2a_rate),100);
  144. $result = $this->lockChangeAccountRemain($intro_userinfo->id,'agentjewel',$jewelA,51, $remark,'pay_order',$payorder_id);
  145. if($result['status']===false)
  146. {
  147. //Db::rollback();
  148. return false;
  149. }
  150. return true;//结束了
  151. }
  152. if($userinfo->group_id == 2){
  153. //充值者上级
  154. $intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','agent_id','gender'])->where('id',$userinfo->intro_uid)->first();
  155. if(empty($intro_userinfo)){return true;}
  156. //上级是一级邀请A,本人为B普、B1(二级邀请人),贡献给A
  157. /*if($intro_userinfo->group_id == 3){*/
  158. $recharge_b2a_rate = $config_site['recharge_b2a_rate'];//b2a 与 b02a相等
  159. $jewelA = bcdiv(bcmul($gold,$recharge_b2a_rate),100);
  160. $result = $this->lockChangeAccountRemain($intro_userinfo->id,'agentjewel',$jewelA,51, $remark,'pay_order',$payorder_id);
  161. if($result['status']===false)
  162. {
  163. //Db::rollback();
  164. return false;
  165. }
  166. return true;//结束了
  167. /*}*/
  168. }
  169. if($userinfo->group_id == 1){
  170. //充值者上级
  171. $intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','agent_id','gender'])->where('id',$userinfo->intro_uid)->first();
  172. if(empty($intro_userinfo)){return true;}
  173. //上级是二级邀请B1,本人C1,贡献给B的上级A
  174. if($intro_userinfo->group_id == 2){
  175. //充值者上上级,也就是A
  176. $intro_intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','agent_id','gender'])->where('id',$intro_userinfo->agent_id)->first();
  177. if(empty($intro_intro_userinfo)){return true;}
  178. if($intro_intro_userinfo->group_id == 3){
  179. $recharge_c2a_rate = $config_site['recharge_c2a_rate'];
  180. $jewelA = bcdiv(bcmul($gold,$recharge_c2a_rate),100);
  181. $result = $this->lockChangeAccountRemain($intro_intro_userinfo->id,'agentjewel',$jewelA,51, $remark,'pay_order',$payorder_id);
  182. if($result['status']===false)
  183. {
  184. //Db::rollback();
  185. return false;
  186. }
  187. }
  188. return true;
  189. }
  190. //上级是A1
  191. if($intro_userinfo->group_id == 3){
  192. $recharge_c2a_rate = $config_site['recharge_c2a_rate'];//b2a 与 b02a相等
  193. $jewelA = bcdiv(bcmul($gold,$recharge_c2a_rate),100);
  194. $result = $this->lockChangeAccountRemain($intro_userinfo->id,'agentjewel',$jewelA,51, $remark,'pay_order',$payorder_id);
  195. if($result['status']===false)
  196. {
  197. //Db::rollback();
  198. return false;
  199. }
  200. return true;//结束了
  201. }
  202. //上级是普通用户,那自己肯定也是普通,则上级可能是D1、C1,找到上上级,如果也是普通C1,停止。如果上上级是B1,则一直追到A1
  203. if(1 == 2 && $intro_userinfo->group_id == 1){
  204. //先给上级普通用户返
  205. $recharge_d2c_rate = $config_site['recharge_d2c_rate'];//d2c 或 e2d
  206. $jewelC = bcdiv(bcmul($gold,$recharge_d2c_rate),100);
  207. $result = $this->lockChangeAccountRemain($intro_userinfo->id,'agentjewel',$jewelC,51, $userinfo->username.'充值','pay_order',$payorder_id);
  208. if($result['status']===false)
  209. {
  210. //Db::rollback();
  211. return false;
  212. }
  213. //充值者上上级
  214. $intro_intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','agent_id','gender'])->where('id',$intro_userinfo->intro_uid)->first();
  215. if(empty($intro_intro_userinfo)){return true;}
  216. //上上级也是普通C1,说明自己在E1,上级是D1,停止
  217. if($intro_intro_userinfo->group_id == 1){
  218. return true;
  219. }
  220. //上上级是B1,上级是C1,自己是D1,
  221. if($intro_intro_userinfo->group_id == 2){
  222. //充值者上上级,也就是A
  223. $intro_intro_intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','agent_id','gender'])->where('id',$intro_intro_userinfo->intro_uid)->first();
  224. if(empty($intro_intro_intro_userinfo)){return true;}
  225. if($intro_intro_intro_userinfo->group_id == 3){
  226. $recharge_c2a_rate = $config_site['recharge_c2a_rate'];
  227. $jewelA = bcdiv(bcmul($jewelC,$recharge_c2a_rate),100);
  228. $result = $this->lockChangeAccountRemain($intro_intro_intro_userinfo->id,'agentjewel',$jewelA,51, $userinfo->username.'充值','pay_order',$payorder_id);
  229. if($result['status']===false)
  230. {
  231. //Db::rollback();
  232. return false;
  233. }
  234. }
  235. }
  236. return true;
  237. }
  238. }
  239. return true;
  240. }
  241. /**
  242. *
  243. * @param floatval $number 金额(正数进账,负数出账)
  244. * @param $accountType 货币类型,money,score
  245. * @param $logtype 日志的类型
  246. * @param $remark 备注
  247. * @param $user_id 用户id
  248. * @param $table 来源表
  249. * @param $data_id 表id
  250. * @param $isAdmin 是否是管理员处理
  251. * @return array
  252. * @return array[status]
  253. * @return array[msg]
  254. * @return array[log_table]
  255. * @return array[log_id]
  256. */
  257. public function lockChangeAccountRemain($user_id,$accountType='gold',$number,$logtype='',$remark='',$table='',$table_id=0,$isAdmin=false)
  258. {
  259. //初始化
  260. $result = array(
  261. 'status'=>false,
  262. 'msg'=>'',
  263. 'log_table' => '',
  264. 'log_id' => '',
  265. );
  266. //获取小数点
  267. $point = 1;
  268. if($accountType == 'agentjewel'){
  269. $point = 2;
  270. }
  271. bcscale($point);
  272. //检测0
  273. $number = floatval( $number );
  274. if( $number == 0 )
  275. {
  276. $result['status'] = true;
  277. $result['msg'] = '交易金额:0';
  278. return $result;
  279. }
  280. if(0 === bccomp($number, 0)){
  281. $result['status'] = true;
  282. $result['msg'] = '交易金额:0';
  283. return $result;
  284. }
  285. //查询余额
  286. $wallet = DB::table('mt_user_wallet')->where('user_id',$user_id)->lockForUpdate()->first();
  287. if(empty($wallet))
  288. {
  289. $result['msg'] = '不存在的用户';
  290. return $result;
  291. }
  292. //扣除金币单独方法
  293. //gold的增加,jewel的增、减
  294. //之前的余额
  295. $wallet_before = $wallet->$accountType;
  296. //正常方法
  297. if(bccomp(bcadd($wallet_before, $number), 0) === -1)
  298. {
  299. $result['msg'] = '余额不足!';
  300. return $result;
  301. }
  302. //钱币操作
  303. $data = array();
  304. $data['user_id'] = $user_id;
  305. $data['log_type'] = $logtype;
  306. $data['money_type'] = $accountType;
  307. $data['before'] = $wallet_before;
  308. $data['change_value'] = $number;
  309. $data['remain'] = bcadd($wallet_before, $number);
  310. $data['table'] = $table;
  311. $data['table_id'] = $table_id;
  312. $data['remark'] = $remark;
  313. $data['createtime'] = time();
  314. $data['updatetime'] = time();
  315. $log_table = 'mt_user_gold_log';
  316. if($accountType == 'agentjewel'){
  317. $log_table = 'mt_user_agentjewel_log';
  318. }
  319. $rs2_id = DB::table($log_table)->insertGetId($data);
  320. //修改金额
  321. $rs1 = DB::table('mt_user_wallet')->where('user_id',$user_id)->update([$accountType => $data['remain']]);
  322. //操作数据库失败
  323. if($rs1 === false || $rs2_id === false){
  324. $result['msg'] = '更新财务记录失败!';
  325. return $result;
  326. }
  327. //默认成功
  328. $result['status'] = true;
  329. $result['msg'] = '账户余额已更新!';
  330. $result['log_table'] = $log_table;
  331. $result['log_id'] = $rs2_id;
  332. return $result;
  333. }
  334. }