UserGiftService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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'])->increment('num',$gift['num']);
  27. if(!$result){
  28. $add_data[] = ['gift_id'=>$gift['gift_id'],
  29. 'uid'=>$uid,'num'=>$gift['num'],
  30. 'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  31. }
  32. }
  33. if($add_data){
  34. UserGift::insert($add_data);
  35. }
  36. }
  37. public static function getByUidAndGiftId($uid,$gift_id){
  38. return UserGift::where('uid',$uid)->where('gift_id',$gift_id)->select('id','num','gift_id','uid')->first();
  39. }
  40. public static function getGifts($uid){
  41. $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
  42. LEFT JOIN user_gifts b on a.id = b.gift_id and b.uid = %s
  43. LEFT JOIN users c on c.id = %s
  44. where a.status = 1 ORDER by cost desc';
  45. return DB::select(sprintf($sql,$uid,$uid));
  46. }
  47. public static function sendGiftToBook($uid,$gift_id,$bid,$gift){
  48. $user_gift = self::getByUidAndGiftId($uid,$gift_id);
  49. $send_info =[
  50. 'uid'=>$uid,
  51. 'name_desc'=>$gift->name_desc,
  52. 'icon'=>$gift->icon,
  53. 'cost'=>$gift->cost,
  54. 'bid'=>$bid,
  55. 'gift_id'=>$gift_id,
  56. 'cost_recharge'=>0,
  57. 'cost_reward'=>0
  58. ];
  59. if($user_gift && $user_gift->num >0){
  60. //$send_info['cost'] = 0;
  61. $user_gift->num -=1;
  62. $user_gift->save();
  63. return BookGiftsSend::addGift($send_info);
  64. }else{
  65. $result = self::payCoin($gift->cost,$uid);
  66. if(!$result) return false;
  67. $send_info['cost_recharge'] = $result['decrease_recharge'];
  68. $send_info['cost_reward'] = $result['decrease_reward'];
  69. return BookGiftsSend::addGift($send_info);
  70. }
  71. }
  72. protected static function payCoin($fee,$uid) {
  73. $user = UserService::getById($uid);
  74. if($user->balance < $fee){
  75. return false;
  76. }
  77. $decrease_recharge = 0;
  78. $decrease_reward =0;
  79. DB::beginTransaction();
  80. if ($user->charge_balance >= $fee) {
  81. $user->balance -= $fee;
  82. $user->charge_balance -= $fee;
  83. $decrease_recharge = $fee;
  84. } elseif ($user->charge_balance > 0) {
  85. $charge_fee = $user->charge_balance;
  86. $reword_fee = $fee - $user->charge_balance;
  87. $user->balance -= $fee;
  88. $user->charge_balance -= $charge_fee;
  89. $user->reward_balance -= $reword_fee;
  90. $decrease_recharge = $charge_fee;
  91. $decrease_reward =$reword_fee;
  92. } else {
  93. $data['charge_balance'] = 0;
  94. $user->balance -= $fee;
  95. $user->reward_balance -= $fee;
  96. $decrease_reward =$fee;
  97. }
  98. try{
  99. $user->save();
  100. DB::commit();
  101. return compact('decrease_recharge','decrease_reward','fee');
  102. }catch (\Exception $e){
  103. DB::rollback();
  104. }
  105. return false;
  106. }
  107. }