AudioJob.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Redis;
  10. use Illuminate\Support\Facades\Config;
  11. use Illuminate\Support\Facades\Log;
  12. class GiftJob implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. protected $msgid;
  16. /**
  17. * Create a new job instance.
  18. *
  19. * @return void
  20. */
  21. public function __construct($msgid)
  22. {
  23. //
  24. $this->msgid = $msgid;
  25. Log::info(date('Y-m-d H:i:d').':'.$msgid);
  26. }
  27. /**
  28. * Execute the job.
  29. *
  30. * @return void
  31. */
  32. public function handle()
  33. {
  34. $result = $this->goaaa();
  35. /*if($result === false){
  36. //防止失败
  37. $key = 'kgelavarel_giftjob_'.$this->msgid;
  38. $limit_rs = $this->apiLimit(2,3600000,$key);//一小时最多另执行2次
  39. if($limit_rs === true){
  40. //echo 123;
  41. dispatch((new GiftJob($this->msgid))->delay(60));//加入队列
  42. }else{
  43. //echo 345;
  44. }
  45. }*/
  46. }
  47. private function apiLimit($apiLimit = 1, $apiLimitTime = 1000, $key = '')
  48. {
  49. $app_name = Config::get('app.name');// laravel name
  50. $key = "{$app_name}:{$key}";
  51. $redis = Redis::connection();
  52. //
  53. //指定键值新增+1 并获取
  54. $count = $redis->incr($key);
  55. if ($count > $apiLimit) {
  56. return false;
  57. }
  58. //设置过期时间
  59. if ($count == 1) {
  60. $redis->pExpire($key, $apiLimitTime);
  61. }
  62. return true;
  63. }
  64. //主要调用
  65. public function goaaa(){
  66. //
  67. $data = DB::table('mt_user_match_audio_log')->where('id',$this->msgid)->first();
  68. DB::beginTransaction();
  69. $result = $this->shouyi($data->to_user_id,$data->money,'user_match_audio_log',$this->msgid);
  70. if($result !== true){
  71. DB::rollBack();
  72. return false;
  73. }
  74. DB::commit();
  75. return true;
  76. }
  77. //四大收益,视频,音频,私聊,礼物
  78. public function shouyi($user_id,$gold,$table,$table_id){
  79. //精确小数点
  80. bcscale(2);
  81. //枚举
  82. $table_enum = [
  83. 'user_match_audio_log' => [
  84. 'prefix'=>'audio',
  85. 'remark' =>'语音',
  86. ],
  87. 'user_match_typing_log' => [
  88. 'prefix'=>'typing',
  89. 'remark' =>'私聊',
  90. ],
  91. 'user_match_video_log' => [
  92. 'prefix'=>'video',
  93. 'remark' =>'视频',
  94. ],
  95. 'gift_user_typing' => [
  96. 'prefix'=>'gift',
  97. 'remark' =>'收礼',
  98. ],
  99. ];
  100. //变量前缀,钱包备注
  101. $prefix = $table_enum[$table]['prefix'];
  102. $remark = $table_enum[$table]['remark'];
  103. //收益者本人
  104. $userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','invite_uid','gender'])->where('id',$user_id)->first();
  105. //没有推荐人,自己等级最高
  106. if(empty($userinfo->intro_uid) || $userinfo->group_id == 3){
  107. return true;
  108. }
  109. //充值者上级
  110. $intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','gender'])->where('id',$userinfo->intro_uid)->first();
  111. if(empty($intro_userinfo)){return true;}
  112. //配置
  113. $config_site = DB::table('mt_config')->whereIn('name',['shouyi_b2a_rate','shouyi_c2a_rate'])->pluck('name','value');
  114. //上级是一级邀请A,本人为B普、B1(二级邀请人),贡献给A
  115. if($intro_userinfo->group_id == 3){
  116. $shouyi_b2a_rate = $config_site['shouyi_b2a_rate'];//b2a 与 b02a相等
  117. $jewelA = bcdiv(bcmul($gold,$shouyi_b2a_rate),100);
  118. $result = $this->lockChangeAccountRemain($intro_userinfo->id,'agentjewel',$jewelA,52, $userinfo->username.$remark,$table,$table_id);
  119. if($result['status']===false)
  120. {
  121. //Db::rollback();
  122. return false;
  123. }
  124. return true;//结束了
  125. }
  126. //上级是二级邀请B1,本人C1,贡献给B和A(表面上说B的钱在A那里,但是有记录,有总额,不能提现即可)
  127. if($intro_userinfo->group_id == 2 && $intro_userinfo->group_id > $userinfo->group_id){
  128. //充值者上上级,也就是A
  129. $intro_intro_userinfo = DB::table('mt_user')->select(['id','username','group_id','intro_uid','gender'])->where('id',$intro_userinfo->intro_uid)->first();
  130. if(empty($intro_intro_userinfo)){return true;}
  131. if($intro_intro_userinfo->group_id == 3){
  132. $shouyi_c2a_rate = $config_site['shouyi_c2a_rate'];
  133. $jewelA = bcdiv(bcmul($gold,$shouyi_c2a_rate),100);
  134. $result = $this->lockChangeAccountRemain($intro_intro_userinfo->id,'agentjewel',$jewelA,52, $userinfo->username.$remark,$table,$table_id);
  135. if($result['status']===false)
  136. {
  137. //Db::rollback();
  138. return false;
  139. }
  140. }
  141. return true;
  142. }
  143. return true;
  144. }
  145. /**
  146. *
  147. * @param floatval $number 金额(正数进账,负数出账)
  148. * @param $accountType 货币类型,money,score
  149. * @param $logtype 日志的类型
  150. * @param $remark 备注
  151. * @param $user_id 用户id
  152. * @param $table 来源表
  153. * @param $data_id 表id
  154. * @param $isAdmin 是否是管理员处理
  155. * @return array
  156. * @return array[status]
  157. * @return array[msg]
  158. * @return array[log_table]
  159. * @return array[log_id]
  160. */
  161. public function lockChangeAccountRemain($user_id,$accountType='gold',$number,$logtype='',$remark='',$table='',$table_id=0,$isAdmin=false)
  162. {
  163. //初始化
  164. $result = array(
  165. 'status'=>false,
  166. 'msg'=>'',
  167. 'log_table' => '',
  168. 'log_id' => '',
  169. );
  170. //获取小数点
  171. $point = 1;
  172. if($accountType == 'agentjewel'){
  173. $point = 2;
  174. }
  175. bcscale($point);
  176. //检测0
  177. $number = floatval( $number );
  178. if( $number == 0 )
  179. {
  180. $result['status'] = true;
  181. $result['msg'] = '交易金额:0';
  182. return $result;
  183. }
  184. if(0 === bccomp($number, 0)){
  185. $result['status'] = true;
  186. $result['msg'] = '交易金额:0';
  187. return $result;
  188. }
  189. //查询余额
  190. $wallet = DB::table('mt_user_wallet')->where('user_id',$user_id)->lockForUpdate()->first();
  191. if(empty($wallet))
  192. {
  193. $result['msg'] = '不存在的用户';
  194. return $result;
  195. }
  196. //扣除金币单独方法
  197. //gold的增加,jewel的增、减
  198. //之前的余额
  199. $wallet_before = $wallet->$accountType;
  200. //正常方法
  201. if(bccomp(bcadd($wallet_before, $number), 0) === -1)
  202. {
  203. $result['msg'] = '余额不足!';
  204. return $result;
  205. }
  206. //钱币操作
  207. $data = array();
  208. $data['user_id'] = $user_id;
  209. $data['log_type'] = $logtype;
  210. $data['money_type'] = $accountType;
  211. $data['before'] = $wallet_before;
  212. $data['change_value'] = $number;
  213. $data['remain'] = bcadd($wallet_before, $number);
  214. $data['table'] = $table;
  215. $data['table_id'] = $table_id;
  216. $data['remark'] = $remark;
  217. $data['createtime'] = time();
  218. $data['updatetime'] = time();
  219. $log_table = 'mt_user_gold_log';
  220. if($accountType == 'agentjewel'){
  221. $log_table = 'mt_user_agentjewel_log';
  222. }
  223. $rs2_id = DB::table($log_table)->insertGetId($data);
  224. //修改金额
  225. $rs1 = DB::table('mt_user_wallet')->where('user_id',$user_id)->update([$accountType => $data['remain']]);
  226. //操作数据库失败
  227. if($rs1 === false || $rs2_id === false){
  228. $result['msg'] = '更新财务记录失败!';
  229. return $result;
  230. }
  231. //默认成功
  232. $result['status'] = true;
  233. $result['msg'] = '账户余额已更新!';
  234. $result['log_table'] = $log_table;
  235. $result['log_id'] = $rs2_id;
  236. return $result;
  237. }
  238. }