123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- /**
- * Created by PhpStorm.
- * User: tandunzhao
- * Date: 2017/12/4
- * Time: 上午11:49
- */
- namespace App\Modules\Book\Services;
- use App\Modules\Book\Models\BookGifts;
- use App\Modules\Book\Models\BookGiftsSend;
- use App\Modules\BookGifts\Models\BookGiftsStatsByBook;
- use App\Modules\BookGifts\Models\BookGiftsStatsByGift;
- use App\Modules\User\Services\UserService;
- use DB;
- use Redis;
- class BookGiftsService
- {
- public static function getAllGifts() {
- $gifts = Redis::get('BookGifts:');
- if($gifts){
- $gifts = unserialize($gifts);
- }else{
- $gifts = BookGifts::getAllGifts();
- Redis::set('BookGifts:',serialize($gifts));
- Redis::expire('BookGifts:',86400);
- }
- return $gifts;
- }
- public static function getOneGift($id) {
- return BookGifts::find($id);
- }
- public static function sendGiftToBook($param) {
- DB::beginTransaction();
- try{
- $coin_paid = self::payCoin($param['cost'],$param['uid']);
- if($coin_paid) {
- $param['cost_reward'] =$coin_paid['decrease_reward'];
- $param['cost_recharge'] =$coin_paid['decrease_recharge'];
- }
- $BookGifts = BookGiftsSend::addGift($param);
- if(!$BookGifts || !$coin_paid) {
- DB::rollback();
- return false;
- };
- }catch (\Exception $e){
- DB::rollback();
- \Log::error('sendGiftForBook failed:'.$e->getMessage());
- return false;
- }
- DB::commit();
- return $BookGifts;
- }
- public static function getSendRecords($bid) {
- return BookGiftsSend::getSendRecords($bid);
- }
- public static function getSendRecordsV2($bid) {
- return BookGiftsSend::getSendRecordsV2($bid);
- }
- public static function getConsumeSendRecords($uid) {
- return BookGiftsSend::getConsumeSendRecords($uid);
- }
- protected static function payCoin($fee,$uid) {
- //DB::beginTransaction();
- $res = $result1 = $result2 = null;
- $user = UserService::getById($uid);
- if($user->balance < $fee){
- return false;
- }
- $decrease_recharge = 0;
- $decrease_reward =0;
- if ($user->charge_balance >= $fee) {
- $data['charge_balance'] = $fee;
- $charge_fee = $fee;
- $data['reward_balance'] = 0;
- $result1 = $user->decrement('balance', $fee);
- $result2 = $user->decrement('charge_balance', $fee);
- $decrease_recharge = $fee;
- } elseif ($user->charge_balance > 0) {
- $data['charge_balance'] = $user->charge_balance;
- $charge_fee = $user->charge_balance;
- $reword_fee = $data['reward_balance'] = $fee - $user->charge_balance;
- $result1 = $user->decrement('balance', $fee);
- $result2 = $user->decrement('charge_balance', $charge_fee);
- $result3 = $user->decrement('reward_balance', $reword_fee);
- $decrease_recharge = $charge_fee;
- $decrease_reward =$reword_fee;
- } else {
- $data['charge_balance'] = 0;
- $reword_fee = $data['reward_balance'] = $fee;
- $result1 = $user->decrement('balance', $fee);
- $result2 = $user->decrement('reward_balance', $reword_fee);
- $decrease_reward =$reword_fee;
- }
- if ($result2 && $result1) {
- //DB::commit();
- return compact('decrease_recharge','decrease_reward','fee');
- //return true;
- }
- //DB::rollback();
- return false;
- }
- public static function getGiftsSendStatisticByBook($start,$end){
- return BookGiftsSend::getGiftsSendStatisticByBook($start,$end);
- }
- public static function getGiftsSendStatisticByGift($start,$end){
- return BookGiftsSend::getGiftsSendStatisticByGift($start,$end);
- }
- public static function getGiftsSendDaillyStatsByBook($start='',$end='') {
- return BookGiftsStatsByBook::getStats($start,$end);
- }
- public static function getGiftsSendDaillyStatsByGift() {
- return BookGiftsStatsByGift::getStats();
- }
- }
|