UserGiftService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2019/4/2
  6. * Time: 14:55
  7. */
  8. namespace App\Modules\User\Services;
  9. use App\Modules\Book\Models\BookGiftsSend;
  10. use App\Modules\User\Models\UserGift;
  11. use DB;
  12. class UserGiftService
  13. {
  14. /**
  15. * 给用户添加礼物 $gift=[['gift_id'=>1,'num'=>1]]
  16. * @param $uid
  17. * @param $gift
  18. */
  19. public static function addGift($uid,$gifts){
  20. if(empty($gifts)){
  21. return false;
  22. }
  23. $add_data = [];
  24. foreach ($gifts as $gift){
  25. if(!isset($gift['num']) || $gift['num']<=0) continue;
  26. $result = UserGift::where('uid',$uid)->where('gift_id',$gift['gift_id'])->incrment('num',$gift['num']);
  27. if(!$result){
  28. $add_data[] = ['gift_id'=>$gift['gift_id'],'uid'=>$uid,'num'=>$gift['num']];
  29. }
  30. }
  31. if($add_data){
  32. UserGift::insert($add_data);
  33. }
  34. }
  35. public static function getByUidAndGiftId($uid,$gift_id){
  36. return UserGift::where('uid',$uid)->where('gift_id',$gift_id)->select('id','num','gift_id','uid')->first();
  37. }
  38. public static function getGifts($uid){
  39. $sql = 'SELECT a.id,a.name ,a.name_desc,a.icon,a.cost,IFNULL(b.num,0) as num,c.balance from book_gifts a
  40. LEFT JOIN user_gifts b on a.id = b.gift_id and b.uid = %s
  41. LEFT JOIN users c on c.id = %s
  42. where a.status = 1 ORDER by cost desc';
  43. return DB::select(sprintf($sql,$uid,$uid));
  44. }
  45. public static function sendGiftToBook($uid,$gift_id,$bid,$gift){
  46. $user_gift = self::getByUidAndGiftId($uid,$gift_id);
  47. $send_info =[
  48. 'uid'=>$uid,
  49. 'name_desc'=>$gift->name_desc,
  50. 'icon'=>$gift->icon,
  51. 'cost'=>$gift->cost,
  52. 'bid'=>$bid,
  53. 'gift_id'=>$gift_id,
  54. 'cost_recharge'=>0,
  55. 'cost_reward'=>0
  56. ];
  57. if($user_gift && $user_gift->num >0){
  58. $user_gift->num -=1;
  59. $user_gift->save();
  60. return BookGiftsSend::addGift($send_info);
  61. }else{
  62. $result = self::payCoin($gift->cost,$uid);
  63. if(!$result) return false;
  64. $send_info['cost_recharge'] = $result['decrease_recharge'];
  65. $send_info['cost_reward'] = $result['decrease_reward'];
  66. return BookGiftsSend::addGift($send_info);
  67. }
  68. }
  69. protected static function payCoin($fee,$uid) {
  70. $user = UserService::getById($uid);
  71. if($user->balance < $fee){
  72. return false;
  73. }
  74. $decrease_recharge = 0;
  75. $decrease_reward =0;
  76. DB::beginTransaction();
  77. if ($user->charge_balance >= $fee) {
  78. $user->balance -= $fee;
  79. $user->charge_balance -= $fee;
  80. $decrease_recharge = $fee;
  81. } elseif ($user->charge_balance > 0) {
  82. $charge_fee = $user->charge_balance;
  83. $reword_fee = $fee - $user->charge_balance;
  84. $user->balance -= $fee;
  85. $user->charge_balance -= $charge_fee;
  86. $user->reward_balance -= $reword_fee;
  87. $decrease_recharge = $charge_fee;
  88. $decrease_reward =$reword_fee;
  89. } else {
  90. $data['charge_balance'] = 0;
  91. $user->balance -= $fee;
  92. $user->reward_balance -= $fee;
  93. $decrease_reward =$fee;
  94. }
  95. try{
  96. $user->save();
  97. DB::commit();
  98. return compact('decrease_recharge','decrease_reward','fee');
  99. }catch (\Exception $e){
  100. DB::rollback();
  101. }
  102. return false;
  103. }
  104. }