Forráskód Böngészése

Merge branch 'gift'

zz 6 éve
szülő
commit
1b9830644f

+ 36 - 0
app/Http/Controllers/Wap/Book/BooksGiftController.php

@@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
 use App\Http\Controllers\Wap\BaseController;
 use App\Http\Controllers\Wap\BaseController;
 use App\Http\Controllers\Wap\Book\Transformers\BookGiftsTransformer;
 use App\Http\Controllers\Wap\Book\Transformers\BookGiftsTransformer;
 use App\Modules\Book\Services\BookGiftsService;
 use App\Modules\Book\Services\BookGiftsService;
+use App\Modules\User\Services\UserGiftService;
 use App\Modules\User\Services\UserService;
 use App\Modules\User\Services\UserService;
 use Illuminate\Http\Request;
 use Illuminate\Http\Request;
 use Hashids;
 use Hashids;
@@ -23,6 +24,16 @@ class BooksGiftController extends BaseController
        return response()->success($res);
        return response()->success($res);
    }
    }
 
 
+    public function getAllGiftsV2() {
+        $gifts = UserGiftService::getGifts($this->uid);
+        $balance  = 0;
+        foreach ($gifts as $gift){
+            $balance = $gift->balance;break;
+        }
+        $res = ['gift'=>$gifts,'balance'=>$balance];
+        return response()->success($res);
+    }
+
     /**
     /**
      * 用户打赏图书
      * 用户打赏图书
      * @param Request $request
      * @param Request $request
@@ -56,6 +67,31 @@ class BooksGiftController extends BaseController
        return response()->success($added);
        return response()->success($added);
    }
    }
 
 
+    public function sendGiftsV2(Request $request) {
+        if(!$this->checkUid()){
+            return response()->error('WAP_NOT_LOGIN');
+        }
+        $uid = $this->uid;
+        $bid = $request->input('bid','');
+        if(empty($bid)) {
+            return response()->error('PARAM_EMPTY');
+        }
+        $bid = Hashids::decode($bid)[0];
+        $gift_id = $request->input('gift_id','');
+        if(empty($gift_id)){
+            return response()->error('PARAM_EMPTY');
+        }
+        $gift_info = BookGiftsService::getOneGift($gift_id);
+        if(!$gift_info){
+            return response()->error('PARAM_ERROR');
+        }
+        $result = UserGiftService::sendGiftToBook($uid,$gift_id,$bid,$gift_info);
+        if(!$result){
+            return response()->error('WAP_SEND_GIFT_FAILED');
+        }
+        return response()->success();
+    }
+
     /**
     /**
      * 获取送礼记录
      * 获取送礼记录
      * @param Request $request
      * @param Request $request

+ 126 - 5
app/Http/Controllers/Wap/Pay/OrdersController.php

@@ -9,6 +9,7 @@ use App\Modules\Subscribe\Services\OrderParamService;
 use App\Modules\Subscribe\Services\SubstituteOrderService;
 use App\Modules\Subscribe\Services\SubstituteOrderService;
 use App\Modules\User\Services\ReadRecordService;
 use App\Modules\User\Services\ReadRecordService;
 use App\Modules\User\Services\UserDivisionCpcPropertyService;
 use App\Modules\User\Services\UserDivisionCpcPropertyService;
+use App\Modules\User\Services\UserGiftService;
 use Illuminate\Routing\Controller;
 use Illuminate\Routing\Controller;
 use App\Libs\Pay\WechatPay;
 use App\Libs\Pay\WechatPay;
 use Illuminate\Http\Request;
 use Illuminate\Http\Request;
@@ -876,7 +877,7 @@ class OrdersController extends Controller
                 ];
                 ];
                 UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
                 UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
                 //用户属性
                 //用户属性
-                $this->userProperty($uid);
+                $this->userProperty($uid,$price,$product->type);
                 return true;
                 return true;
             } catch (\Exception $e) {
             } catch (\Exception $e) {
                 DB::rollback();
                 DB::rollback();
@@ -1022,7 +1023,7 @@ class OrdersController extends Controller
                     $this->successPayPushMsg($uid, $product, $order->id);
                     $this->successPayPushMsg($uid, $product, $order->id);
                     $this->orderStatistical($order);
                     $this->orderStatistical($order);
                     DB::commit();
                     DB::commit();
-                    $this->userProperty($uid);
+                    $this->userProperty($uid,$price,$product->type);
                     $this->huaweiActivity($order->activity_id,$uid,$product_id);
                     $this->huaweiActivity($order->activity_id,$uid,$product_id);
                     //redis 删除未支付的uid
                     //redis 删除未支付的uid
                     try {
                     try {
@@ -1133,7 +1134,7 @@ class OrdersController extends Controller
                 $this->successPayPushMsg($uid, $product, $order->id);
                 $this->successPayPushMsg($uid, $product, $order->id);
                 $this->orderStatistical($order);
                 $this->orderStatistical($order);
                 DB::commit();
                 DB::commit();
-                $this->userProperty($uid);
+                $this->userProperty($uid,$price,$product->type);
                 $this->huaweiActivity($order->activity_id,$uid,$product_id);
                 $this->huaweiActivity($order->activity_id,$uid,$product_id);
                 //redis 删除未支付的uid
                 //redis 删除未支付的uid
                 $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id' . $distribution_channel_id;
                 $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id' . $distribution_channel_id;
@@ -1270,7 +1271,7 @@ class OrdersController extends Controller
                     ];
                     ];
                     UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
                     UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
 
 
-                    $this->userProperty($uid);
+                    $this->userProperty($uid,$price,$product->type);
                     $this->huaweiActivity($order->activity_id,$uid,$product_id);
                     $this->huaweiActivity($order->activity_id,$uid,$product_id);
 
 
                 }
                 }
@@ -1710,11 +1711,13 @@ class OrdersController extends Controller
         return 0;
         return 0;
     }
     }
 
 
-    private function userProperty($uid){
+    private function userProperty($uid,$price,$charge_type){
         try{
         try{
             $userproperty = UserDivisionCpcPropertyService::getUserSubscribeAndChargeInfoByUid($uid);
             $userproperty = UserDivisionCpcPropertyService::getUserSubscribeAndChargeInfoByUid($uid);
             if(!$userproperty) return ;
             if(!$userproperty) return ;
             UserDivisionCpcPropertyService::createorUpdate($userproperty);
             UserDivisionCpcPropertyService::createorUpdate($userproperty);
+            //chargeGiveGift
+            $this->chargeGiveGift($uid,$userproperty,$price,$charge_type);
         }catch (\Exception $e){}
         }catch (\Exception $e){}
 
 
     }
     }
@@ -1789,4 +1792,122 @@ class OrdersController extends Controller
             Log::info($e);
             Log::info($e);
         }
         }
     }
     }
+
+    private function chargeGiveGift($uid,$property,$price,$charge_type){
+        if(!$property) return ;
+        if($property == 'none' || $property == 'low'){
+            $gift = [];
+            if($price == 2){
+                $gift = [['gift_id'=>6,'num'=>1]];
+            }
+            if($price == 18){
+                $gift = [['gift_id'=>6,'num'=>1],['gift_id'=>7,'num'=>1]];
+            }
+            if($price == 30){
+                $gift = [
+                    ['gift_id'=>6,'num'=>1],
+                    ['gift_id'=>7,'num'=>1],
+                    ['gift_id'=>1,'num'=>1]
+                ];
+            }
+            if($price == 50){
+                $gift = [
+                    ['gift_id'=>6,'num'=>1],
+                    ['gift_id'=>7,'num'=>1],
+                    ['gift_id'=>1,'num'=>1],
+                    ['gift_id'=>3,'num'=>1]
+                ];
+            }
+            if($price == 100){
+                $gift = [
+                    ['gift_id'=>7,'num'=>1],
+                    ['gift_id'=>1,'num'=>1],
+                    ['gift_id'=>3,'num'=>1],
+                    ['gift_id'=>2,'num'=>1]
+                ];
+            }
+            if($price == 200){
+                $gift = [
+                    ['gift_id'=>7,'num'=>1],
+                    ['gift_id'=>1,'num'=>1],
+                    ['gift_id'=>3,'num'=>1],
+                    ['gift_id'=>2,'num'=>1],
+                    ['gift_id'=>8,'num'=>1]
+                ];
+            }
+            if($charge_type == 'YEAR_ORDER'){
+                $gift = [
+                    ['gift_id'=>1,'num'=>1],
+                    ['gift_id'=>2,'num'=>1],
+                    ['gift_id'=>3,'num'=>1],
+                    ['gift_id'=>6,'num'=>1],
+                    ['gift_id'=>7,'num'=>1],
+                    ['gift_id'=>8,'num'=>1]
+                ];
+            }
+            if($gift){
+                UserGiftService::addGift($uid,$gift);
+            }
+            return ;
+        }
+
+        if($property == 'high' || $property == 'medium'){
+            $gift = [];
+            if($price == 18){
+                $gift = [
+                    ['gift_id'=>6,'num'=>1],
+                    ['gift_id'=>1,'num'=>1],
+                    ['gift_id'=>7,'num'=>1]
+                ];
+            }
+            if($price == 30){
+                $gift = [
+                    ['gift_id'=>6,'num'=>1],
+                    ['gift_id'=>7,'num'=>1],
+                    ['gift_id'=>1,'num'=>2]
+                ];
+            }
+            if($price == 50){
+                $gift = [
+                    ['gift_id'=>6,'num'=>2],
+                    ['gift_id'=>7,'num'=>2],
+                    ['gift_id'=>1,'num'=>2],
+                    ['gift_id'=>3,'num'=>1]
+                ];
+            }
+            if($price == 100){
+                $gift = [
+                    ['gift_id'=>7,'num'=>3],
+                    ['gift_id'=>1,'num'=>2],
+                    ['gift_id'=>3,'num'=>1],
+                    ['gift_id'=>2,'num'=>1]
+                ];
+            }
+            if($price == 200){
+                $gift = [
+                    ['gift_id'=>7,'num'=>1],
+                    ['gift_id'=>1,'num'=>1],
+                    ['gift_id'=>3,'num'=>1],
+                    ['gift_id'=>2,'num'=>1],
+                    ['gift_id'=>8,'num'=>1]
+                ];
+            }
+            if($charge_type == 'YEAR_ORDER'){
+                $gift = [
+                    ['gift_id'=>1,'num'=>2],
+                    ['gift_id'=>2,'num'=>2],
+                    ['gift_id'=>3,'num'=>2],
+                    ['gift_id'=>6,'num'=>2],
+                    ['gift_id'=>7,'num'=>2],
+                    ['gift_id'=>8,'num'=>2]
+                ];
+            }
+            if($gift){
+                UserGiftService::addGift($uid,$gift);
+            }
+            return ;
+        }
+
+        return ;
+    }
 }
 }

+ 2 - 2
app/Http/Routes/Wap/WapRoutes.php

@@ -181,8 +181,8 @@ Route::group(['domain'=>env('WAP_DOMAIN'),'namespace'=>'App\Http\Controllers\Wap
         Route::get('chapter/getComment','Book\ChapterController@getChapterComment');
         Route::get('chapter/getComment','Book\ChapterController@getChapterComment');
 
 
         //书籍送礼
         //书籍送礼
-        Route::get('gift/getGiftsList','Book\BooksGiftController@getAllGifts');
-        Route::post('gift/sendGifts','Book\BooksGiftController@sendGifts');
+        Route::get('gift/getGiftsList','Book\BooksGiftController@getAllGiftsV2');
+        Route::post('gift/sendGifts','Book\BooksGiftController@sendGiftsV2');
         Route::get('gift/getUserSendGiftsRecord','Book\BooksGiftController@getUserSendGiftsRecordV2');
         Route::get('gift/getUserSendGiftsRecord','Book\BooksGiftController@getUserSendGiftsRecordV2');
         Route::get('gift/getUserGiftsConsumeRecord','Book\BooksGiftController@getSendGiftsForConsumeRecord');
         Route::get('gift/getUserGiftsConsumeRecord','Book\BooksGiftController@getSendGiftsForConsumeRecord');
         //分享签名
         //分享签名

+ 12 - 0
app/Modules/User/Models/UserGift.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace App\Modules\User\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class UserGift extends Model
+{
+    protected $table = 'user_gifts';
+    protected $fillable = ['uid','gift_id','num'];
+
+}

+ 115 - 0
app/Modules/User/Services/UserGiftService.php

@@ -0,0 +1,115 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: z-yang
+ * Date: 2019/4/2
+ * Time: 14:55
+ */
+
+namespace App\Modules\User\Services;
+
+use App\Modules\Book\Models\BookGiftsSend;
+use App\Modules\User\Models\UserGift;
+use DB;
+
+class UserGiftService
+{
+
+    /**
+     * 给用户添加礼物  $gift=[['gift_id'=>1,'num'=>1]]
+     * @param $uid
+     * @param $gift
+     */
+    public static function addGift($uid,$gifts){
+        if(empty($gifts)){
+            return false;
+        }
+        $add_data = [];
+        foreach ($gifts as $gift){
+            if(!isset($gift['num']) || $gift['num']<=0) continue;
+            $result = UserGift::where('uid',$uid)->where('gift_id',$gift['gift_id'])->incrment('num',$gift['num']);
+            if(!$result){
+                $add_data[] = ['gift_id'=>$gift['gift_id'],'uid'=>$uid,'num'=>$gift['num']];
+            }
+        }
+        if($add_data){
+            UserGift::insert($add_data);
+        }
+    }
+
+
+    public static function getByUidAndGiftId($uid,$gift_id){
+        return UserGift::where('uid',$uid)->where('gift_id',$gift_id)->select('id','num','gift_id','uid')->first();
+    }
+
+
+
+    public static function getGifts($uid){
+        $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 
+LEFT JOIN user_gifts b on a.id = b.gift_id and b.uid = %s
+LEFT JOIN users c on  c.id = %s
+where a.status = 1 ORDER  by cost desc';
+        return DB::select(sprintf($sql,$uid,$uid));
+    }
+
+    public static function sendGiftToBook($uid,$gift_id,$bid,$gift){
+        $user_gift = self::getByUidAndGiftId($uid,$gift_id);
+        $send_info =[
+            'uid'=>$uid,
+                'name_desc'=>$gift->name_desc,
+                'icon'=>$gift->icon,
+                'cost'=>$gift->cost,
+                'bid'=>$bid,
+                'gift_id'=>$gift_id,
+                'cost_recharge'=>0,
+                'cost_reward'=>0
+            ];
+        if($user_gift && $user_gift->num >0){
+            $user_gift->num -=1;
+            $user_gift->save();
+            return  BookGiftsSend::addGift($send_info);
+        }else{
+            $result = self::payCoin($gift->cost,$uid);
+            if(!$result) return false;
+            $send_info['cost_recharge'] = $result['decrease_recharge'];
+            $send_info['cost_reward'] = $result['decrease_reward'];
+            return BookGiftsSend::addGift($send_info);
+        }
+    }
+
+    protected static function payCoin($fee,$uid) {
+        $user = UserService::getById($uid);
+        if($user->balance < $fee){
+            return false;
+        }
+        $decrease_recharge = 0;
+        $decrease_reward =0;
+        DB::beginTransaction();
+        if ($user->charge_balance >= $fee) {
+            $user->balance -= $fee;
+            $user->charge_balance -= $fee;
+            $decrease_recharge = $fee;
+        } elseif ($user->charge_balance > 0) {
+            $charge_fee = $user->charge_balance;
+            $reword_fee  = $fee - $user->charge_balance;
+            $user->balance -= $fee;
+            $user->charge_balance -= $charge_fee;
+            $user->reward_balance -= $reword_fee;
+            $decrease_recharge = $charge_fee;
+            $decrease_reward =$reword_fee;
+        } else {
+            $data['charge_balance'] = 0;
+            $user->balance -= $fee;
+            $user->reward_balance -= $fee;
+            $decrease_reward =$fee;
+        }
+        try{
+            $user->save();
+            DB::commit();
+            return compact('decrease_recharge','decrease_reward','fee');
+        }catch (\Exception $e){
+            DB::rollback();
+        }
+        return false;
+    }
+}