UserPayController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace Modules\Finance\Http\Controllers;
  3. use App\Models\Ordertest;
  4. use App\Models\User;
  5. use App\Models\Userpay;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. class UserPayController extends Controller
  11. {
  12. public function get_crown(){
  13. $data=User::where('level',3)->get();
  14. foreach ($data as $k=>$v){
  15. $re[$k]['nickname']=$v->nickanme.'--'.$v->mobile;
  16. $re[$k]['id']=$v->id;
  17. }
  18. return $this->success_list($re);
  19. }
  20. /*
  21. * 充值
  22. * */
  23. public function add_money(Request $request){
  24. $input=$request->all();
  25. foreach ($input['mobile'] as $k=>$v){
  26. $user=User::where('mobile',$v)->first();
  27. if (!$user){
  28. return $this->error(40013,'用户'.$v.'不存在');
  29. }
  30. }
  31. DB::beginTransaction();
  32. try{
  33. foreach ($input['mobile'] as $k=>$v){
  34. $user=User::where('mobile',$v)->first();
  35. $user->money=$user->money+$input['money'][$k];
  36. if (!$user->save()){
  37. Log::error('充值失败'.$v);
  38. throw new \Exception('1');
  39. }
  40. $userpay=new Userpay();
  41. $userpay->pay_money=$input['money'][$k];
  42. $userpay->type=0;
  43. $userpay->op_name=Auth::user()->name;
  44. $userpay->user_id=$user->id;
  45. $userpay->pay_num=$this->pay_num($user->id);
  46. if (!$userpay->save()){
  47. throw new \Exception('3');
  48. }
  49. DB::commit();
  50. return $this->success([],'充值完成');
  51. }
  52. }catch (\Exception $exception){
  53. DB::rollBack();
  54. return $this->error(40013,'充值失败,出错用户手机号为'.$v);
  55. }
  56. }
  57. /*
  58. * */
  59. public function pay_num($id){
  60. $num=date('Ymd',time()).'100000'.str_pad($id,6,0,STR_PAD_LEFT).rand(11111,99999);
  61. $count=UserPay::where('pay_num',$num)->count();
  62. if ($count>0){
  63. $num=$this->pay_num($id);
  64. return $num;
  65. }
  66. return $num;
  67. }
  68. /*
  69. * 搜索
  70. * */
  71. public function search(Request $request){
  72. // if ($request->start==null || $request->end==null){
  73. // $where_time=null;
  74. // }else{
  75. //// $where_time=
  76. // }
  77. $user=User::where('mobile',$request->mobile)->first();
  78. if ($user){
  79. return $this->error(40013,'查无此人');
  80. }
  81. $userpay=Userpay::where('user_id',$user->id)->orderByDesc('created_at')->paginate(15);
  82. return $this->success_list($userpay->items(),'',$userpay->total());
  83. }
  84. /*
  85. * 代理账户金额
  86. * */
  87. public function user_account(Request $request){
  88. $user=User::where('level',3)->paginate(15);
  89. return $this->success_list($user->items(),'',$user->total());
  90. }
  91. /*
  92. * 详情
  93. * */
  94. public function user_pay_log(Request $request){
  95. $userpay=Userpay::where('user_id',$request->id)->orderByDesc('created_at')->paginate(15);
  96. return $this->success_list($userpay->items(),'',$userpay->total());
  97. }
  98. /** 作废**/
  99. public function UserPayCancel(Request $request){
  100. $id=$request->id;
  101. $ids=[$id];
  102. $userPayLog=Userpay::find($id);
  103. $user_id=$userPayLog->user_id;
  104. $re=Userpay::where('user_id',$user_id)->where('id','<',$id)->orderByDesc('id')->limit(1)->first();
  105. $user_money=$re->user_money;
  106. $user=User::where('id',$user_id)->first();
  107. $pay_chongzhi=Userpay::where('user_id',$user_id)->where('id','>',$id)->whereIn('type',[0,2,3])->first();
  108. if ($pay_chongzhi){
  109. return $this->error(50241,'该记录后面存在充值(扣款、退回)记录,请联系开发人员');
  110. // Log::error('该记录后面存在充值(扣款、退回)记录,请联系开发人员');
  111. // exit();
  112. }
  113. DB::beginTransaction();
  114. try{
  115. $orderPayLog=Userpay::where('user_id',$user_id)->where('id','>',$id)->where('type',1)->get();
  116. foreach ($orderPayLog as $k=>$v){
  117. $order=Ordertest::where('order_num',$v->order_num)->first();
  118. $pay_money=$v->pay_money;
  119. if ($order->wait_back_money!=0){
  120. return $this->error(50214,'订单编号为:'.$v->order_num.'有待退款');
  121. // Log::error('订单编号为:'.$v->order_num.'有待退款');
  122. // exit();
  123. }
  124. $order->pay_money-=$pay_money;
  125. if ($order->pay_money>0){
  126. $order->pay_status=1;
  127. }elseif ($order->pay_money==0){
  128. $order->pay_status=0;
  129. }else{
  130. return $this->error(50214,'订单编号为:'.$v->order_num.'金额出错');
  131. // Log::error('订单编号为:'.$v->order_num.'金额出错');
  132. // exit();
  133. }
  134. $order->no_pay_money=$order->money-$order->pay_money;
  135. if (!$order->save()){
  136. throw new \Exception(1);
  137. }
  138. $order->save();
  139. array_push($ids,$v->id);
  140. }
  141. $user->money=$user_money;
  142. if (!$user->save()){
  143. throw new \Exception(1);
  144. }
  145. Userpay::whereIn('id',$ids)->delete();
  146. DB::commit();
  147. return $this->success([]);
  148. }catch (\Exception $exception){
  149. DB::rollBack();
  150. return $this->error(500214,$exception->getMessage());
  151. }
  152. }
  153. }