BookGiftsService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/12/4
  6. * Time: 上午11:49
  7. */
  8. namespace App\Modules\Book\Services;
  9. use App\Modules\Book\Models\BookGifts;
  10. use App\Modules\Book\Models\BookGiftsSend;
  11. use App\Modules\BookGifts\Models\BookGiftsStatsByBook;
  12. use App\Modules\BookGifts\Models\BookGiftsStatsByGift;
  13. use App\Modules\User\Services\UserService;
  14. use DB;
  15. use Redis;
  16. class BookGiftsService
  17. {
  18. public static function getAllGifts() {
  19. $gifts = Redis::get('BookGifts:');
  20. if($gifts){
  21. $gifts = unserialize($gifts);
  22. }else{
  23. $gifts = BookGifts::getAllGifts();
  24. Redis::set('BookGifts:',serialize($gifts));
  25. Redis::expire('BookGifts:',86400);
  26. }
  27. return $gifts;
  28. }
  29. public static function getOneGift($id) {
  30. return BookGifts::find($id);
  31. }
  32. public static function sendGiftToBook($param) {
  33. DB::beginTransaction();
  34. try{
  35. $coin_paid = self::payCoin($param['cost'],$param['uid']);
  36. if($coin_paid) {
  37. $param['cost_reward'] =$coin_paid['decrease_reward'];
  38. $param['cost_recharge'] =$coin_paid['decrease_recharge'];
  39. }
  40. $BookGifts = BookGiftsSend::addGift($param);
  41. if(!$BookGifts || !$coin_paid) {
  42. DB::rollback();
  43. return false;
  44. };
  45. }catch (\Exception $e){
  46. DB::rollback();
  47. \Log::error('sendGiftForBook failed:'.$e->getMessage());
  48. return false;
  49. }
  50. DB::commit();
  51. return $BookGifts;
  52. }
  53. public static function getSendRecords($bid) {
  54. return BookGiftsSend::getSendRecords($bid);
  55. }
  56. public static function getSendRecordsV2($bid) {
  57. return BookGiftsSend::getSendRecordsV2($bid);
  58. }
  59. public static function getConsumeSendRecords($uid) {
  60. return BookGiftsSend::getConsumeSendRecords($uid);
  61. }
  62. protected static function payCoin($fee,$uid) {
  63. //DB::beginTransaction();
  64. $res = $result1 = $result2 = null;
  65. $user = UserService::getById($uid);
  66. if($user->balance < $fee){
  67. return false;
  68. }
  69. $decrease_recharge = 0;
  70. $decrease_reward =0;
  71. if ($user->charge_balance >= $fee) {
  72. $data['charge_balance'] = $fee;
  73. $charge_fee = $fee;
  74. $data['reward_balance'] = 0;
  75. $result1 = $user->decrement('balance', $fee);
  76. $result2 = $user->decrement('charge_balance', $fee);
  77. $decrease_recharge = $fee;
  78. } elseif ($user->charge_balance > 0) {
  79. $data['charge_balance'] = $user->charge_balance;
  80. $charge_fee = $user->charge_balance;
  81. $reword_fee = $data['reward_balance'] = $fee - $user->charge_balance;
  82. $result1 = $user->decrement('balance', $fee);
  83. $result2 = $user->decrement('charge_balance', $charge_fee);
  84. $result3 = $user->decrement('reward_balance', $reword_fee);
  85. $decrease_recharge = $charge_fee;
  86. $decrease_reward =$reword_fee;
  87. } else {
  88. $data['charge_balance'] = 0;
  89. $reword_fee = $data['reward_balance'] = $fee;
  90. $result1 = $user->decrement('balance', $fee);
  91. $result2 = $user->decrement('reward_balance', $reword_fee);
  92. $decrease_reward =$reword_fee;
  93. }
  94. if ($result2 && $result1) {
  95. //DB::commit();
  96. return compact('decrease_recharge','decrease_reward','fee');
  97. //return true;
  98. }
  99. //DB::rollback();
  100. return false;
  101. }
  102. public static function getGiftsSendStatisticByBook($start,$end){
  103. return BookGiftsSend::getGiftsSendStatisticByBook($start,$end);
  104. }
  105. public static function getGiftsSendStatisticByGift($start,$end){
  106. return BookGiftsSend::getGiftsSendStatisticByGift($start,$end);
  107. }
  108. public static function getGiftsSendDaillyStatsByBook($start='',$end='') {
  109. return BookGiftsStatsByBook::getStats($start,$end);
  110. }
  111. public static function getGiftsSendDaillyStatsByGift() {
  112. return BookGiftsStatsByGift::getStats();
  113. }
  114. }