Browse Source

pay template

zhaoyang 1 year ago
parent
commit
5f4efc7647

+ 313 - 0
modules/Channel/Http/Controllers/PayTemplateController.php

@@ -0,0 +1,313 @@
+<?php
+
+namespace Modules\Channel\Http\Controllers;
+
+use Carbon\Carbon;
+use Illuminate\Http\Request;
+use Catch\Base\CatchController;
+use Modules\Common\Errors\Errors;
+use Modules\Channel\Models\PayProduct;
+use Modules\Channel\Models\PayTemplate;
+use Modules\Channel\Models\PayTemplateItem;
+use Modules\Channel\Exceptions\ChannelBusinessException;
+use Modules\Channel\Services\User\UserService;
+
+class PayTemplateController extends CatchController
+{
+    public function __construct(protected readonly PayTemplate $payTemplate,protected readonly PayTemplateItem $payTemplateItem,protected readonly PayProduct $payProduct)
+    {
+        
+    }
+
+
+    private $sequence_map = [
+        '','位置一','位置二','位置三','位置四','位置五','位置六','位置七','位置八'
+    ];
+
+    /**
+     * 充值模板列表
+     *
+     * @param Request $request
+     * @return void
+     */
+    public function index(Request $request)
+    {
+        $uid = $this->getLoginUser()->id;
+        if(UserService::userHasRole($uid,'administrator')){
+            $uid = 0;
+        }
+        $name = $request->get('name');
+        $where = [[
+            'uid','=',$uid
+        ]];
+        if($name){
+            $where[] = ['name','like','%'.$name.'%']; 
+        }
+        return $this->payTemplate->where($where)->paginate(20);
+    }
+
+
+    /**
+     * 添加模板
+     *
+     * @param Request $request
+     * @return void
+     */
+    public function store(Request $request)
+    {
+        $uid = $this->getLoginUser()->id;
+        $name = $request->post('name');
+        $status = $request->post('status');
+        $options = $request->post('options');
+        if(empty($name) || empty($options)){
+            ChannelBusinessException::throwError(Errors::PARAM_EMPTY);
+        }
+        if(UserService::userHasRole($uid,'administrator')){
+            $uid = 0;
+        }
+
+        $exists = $this->payTemplate->where('uid',$uid)->where('name',$name)->count();
+        if($exists){
+            ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_EXISTS_ERROR);
+        }
+        $option_list = json_decode($options,1);
+        $data = [];
+        $default_optioin = 0;
+        $option_cache = [];
+        $type_list = collect($this->optionTypeList())->pluck('value');
+        foreach($option_list as $option){
+            if($option['type'] == 'COIN' || $option['type'] == 'FIRST_COIN'){
+                if($option['given'] > 3*$option['price']*100){
+                    ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_GIVEN_TOO_MUCH);
+                    break;
+                }
+            }
+
+            if(!$type_list->contains($option['type'])){
+                ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_OPTIONS_ERROR);
+            }
+
+            if(in_array($option['price'],$option_cache)){
+                ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_CONFLICT);
+            }
+            $option_cache[] = $option['price'];
+
+            if(!$default_optioin && $option['is_default']){
+                $default_optioin = $option['price'];
+            }
+        }
+
+        $default_optioin = $default_optioin>0?$default_optioin:$option_list[0]['price'];
+
+        $pay_template_info = $this->payTemplate->create(['name'=>$name,'uid'=>$uid,'status'=>$status]);
+
+        foreach($option_list as $option){
+            $type = $option['type'];
+            if($option['type'] == 'COIN' || $option['type'] == 'FIRST_COIN'){
+                $type = 'COIN';
+            }
+            $product_info = $this->getPayProduct($option['price'], $type,$option['given']);
+            $data[] = [
+                'pay_template_id'=>$pay_template_info->id,'pay_product_id'=>$product_info->id,
+                'is_first_pay'=>$option['type'] == 'FIRST_COIN' ?1:0,'is_default'=>$option['price'] == $default_optioin ?1:0,
+                'status'=>1,'sequence'=>$option['sequence'],'created_at'=>Carbon::now(),'updated_at'=>Carbon::now()  
+            ];
+        }
+
+        $this->payTemplateItem->insert($data);
+        return [];
+    }
+
+    /**
+     * 模板详情
+     *
+     * @param int $id
+     * @return void
+     */
+    public function show($id)
+    {
+        $uid = $this->getLoginUser()->id;
+        if(UserService::userHasRole($uid,'administrator')){
+            $uid = 0;
+        }
+        $exists = $this->payTemplate->where('uid',$uid)->where('id',$id)->first();
+        if(!$exists){
+            ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_NOT_EXISTS_ERROR);
+        }
+
+        $pay_template_item = $this->payTemplateItem->join('pay_products','pay_products.id','=','pay_template_items.pay_product_id')
+        ->where('pay_template_id',$id)
+        ->select('pay_products.price','pay_products.type','pay_products.given','pay_template_id','pay_product_id','is_first_pay','is_default','sequence')
+        ->where('pay_template_items.status',1)
+        ->orderBy('pay_template_items.sequence')
+        ->get();
+        $type_list = collect($this->optionTypeList());
+        foreach($pay_template_item as $item){
+            
+            if($item->type == 'COIN' && $item->is_first_pay == 1){
+                $item->type == 'FIRST_COIN';
+            }
+            $item->type_name = $type_list->where('value',$item->type)->first()['name'];
+            $item->sequence_text = $this->sequence_map[$item->sequence];
+            $item->default_text = $item->is_default? '默认项':"非默认项";
+        }
+
+        return [
+            'template_info'=>$exists,
+            'template_item_list'=>$pay_template_item
+        ];
+    }
+
+    /**
+     * 更新模板
+     *
+     * @param [type] $id
+     * @param Request $request
+     * @return void
+     */
+    public function update($id, Request $request)
+    {
+        $name = $request->post('name');
+        $status = $request->post('status',-1);
+        $options = $request->post('options');
+        $uid = $this->getLoginUser()->id;
+        if(UserService::userHasRole($uid,'administrator')){
+            $uid = 0;
+        }
+        $exists = $this->payTemplate->where('uid',$uid)->where('id',$id)->first();
+        if(!$exists){
+            ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_NOT_EXISTS_ERROR);
+        }
+        $template_info_update = false;
+        if($name){
+            $template_info_update = true;
+            $exists->name = $name;
+        }
+        if($status != -1 && in_array($status,[1,0])){
+            $template_info_update = true;
+            $exists->status = $status;
+        }
+
+        if($status == 1){
+            $this->payTemplate->where('uid',$uid)->update(['status'=>0]);
+        }
+        if($template_info_update){
+            $exists->save();
+        }
+
+        if($options){
+            $option_list = json_decode($options,1);
+            $default_optioin = 0;
+            $option_cache = [];
+            $type_list = collect($this->optionTypeList())->pluck('value');
+            foreach($option_list as $option){
+                if($option['type'] == 'COIN' || $option['type'] == 'FIRST_COIN'){
+                    if($option['given'] > 3*$option['price']*100){
+                        ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_GIVEN_TOO_MUCH);
+                        break;
+                    }
+                }
+
+                if(!$type_list->contains($option['type'])){
+                    ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_OPTIONS_ERROR);
+                }
+
+                if(in_array($option['price'],$option_cache)){
+                    ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_CONFLICT);
+                }
+
+                $option_cache[] = $option['price'];
+
+                if(!$default_optioin && $option['is_default']){
+                    $default_optioin = $option['price'];
+                }
+
+            }
+            $this->payTemplateItem->where('pay_template_id',$id)->update(['status'=>0]);
+            $default_optioin = $default_optioin>0?$default_optioin:$option_list[0]['price'];
+            foreach($option_list as $option){
+                $type = $option['type'];
+                if($option['type'] == 'COIN' || $option['type'] == 'FIRST_COIN'){
+                    $type = 'COIN';
+                }
+                $product_info = $this->getPayProduct($option['price'], $type,$option['given']);
+            
+                $pay_template_item = $this->payTemplateItem->where('pay_template_id',$id)->where('pay_product_id',$product_info->id)->first();
+                if($pay_template_item){
+                    $pay_template_item->status = 1;
+                    $pay_template_item->sequence = $option['sequence'];
+                    $pay_template_item->is_first_pay = $option['type'] == 'FIRST_COIN' ?1:0;
+                    $pay_template_item->is_default = $option['price'] == $default_optioin ?1:0;
+                    $pay_template_item->save();
+                }else{
+                    $data = [
+                        'pay_template_id'=>$id,'pay_product_id'=>$product_info->id,
+                        'is_first_pay'=>$option['type'] == 'FIRST_COIN' ?1:0,'is_default'=>$option['price'] == $default_optioin ?1:0,
+                        'status'=>1,'sequence'=>$option['sequence'],'created_at'=>Carbon::now(),'updated_at'=>Carbon::now()  
+                    ];
+                    $this->payTemplateItem->insert($data);
+                }
+            }
+        }
+
+        return [];
+    }
+
+
+    /**
+     * 更新模板状态
+     *
+     * @param int $id
+     * @param Request $request
+     * @return void
+     */
+    public function updateStatus($id,Request $request){
+        $uid = $this->getLoginUser()->id;
+        if(UserService::userHasRole($uid,'administrator')){
+            $uid = 0;
+        }
+
+        $exists = $this->payTemplate->where('uid',$uid)->where('id',$id)->first();
+        $status = $request->post('status');
+        if(!in_array($status,[1,0])){
+            ChannelBusinessException::throwError(Errors::PARAM_EMPTY);
+        }
+        if(!$exists){
+            ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_NOT_EXISTS_ERROR);
+        }
+        if($status == 1){
+            $this->payTemplate->where('uid',$uid)->update(['status'=>0]);
+        }
+
+        $exists->status = $status;
+        $exists->save();
+        return [];
+    }
+
+
+
+
+    private function getPayProduct($price,$type,$given){
+        $where = [
+            ['price','=',$price],
+            ['type','=',$type],
+            ['given','=',$given],
+        ];
+        $product_info = $this->payProduct->where($where)->first();
+        if($product_info){
+            return $product_info ;
+        }
+        return $this->payProduct->create(compact('price','type','given'));
+    }
+
+
+    public function optionTypeList(){
+        return [
+            ['name'=>'普通充值','value'=>'COIN'],
+            ['name'=>'首充','value'=>'FIRST_COIN'],
+            ['name'=>'包月','value'=>'MONTH'],
+            ['name'=>'包季','value'=>'QUARTER'],
+            ['name'=>'包年','value'=>'YEAR']
+        ];
+    }
+}

+ 14 - 0
modules/Channel/Models/PayProduct.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace Modules\Channel\Models;
+
+
+class PayProduct extends BaseModel
+{
+    protected $table = 'pay_products';
+
+    protected $fillable = [
+        'id', 'price', 'type', 'given', 'created_at', 'updated_at', 
+    ];
+
+}

+ 16 - 0
modules/Channel/Models/PayTemplate.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace Modules\Channel\Models;
+
+use Catch\Base\CatchModel as Model;
+
+
+class PayTemplate extends BaseModel
+{
+    protected $table = 'pay_templates';
+
+    protected $fillable = [
+        'id', 'name', 'uid','status', 'created_at', 'updated_at', 
+    ];
+
+}

+ 16 - 0
modules/Channel/Models/PayTemplateItem.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace Modules\Channel\Models;
+
+use Catch\Base\CatchModel as Model;
+
+
+class PayTemplateItem extends BaseModel
+{
+    protected $table = 'pay_template_items';
+
+    protected $fillable = [
+        'id', 'pay_template_id', 'pay_product_id', 'is_first_pay', 'is_default', 'status', 'sequence', 'created_at', 'updated_at', 
+    ];
+
+}

+ 13 - 0
modules/Channel/routes/route.php

@@ -2,6 +2,7 @@
 
 
 use Illuminate\Support\Facades\Route;
 use Illuminate\Support\Facades\Route;
 use Modules\Channel\Http\Controllers\AdvertiserController;
 use Modules\Channel\Http\Controllers\AdvertiserController;
+use Modules\Channel\Http\Controllers\PayTemplateController;
 use Modules\Channel\Http\Controllers\UserMiniprogramController;
 use Modules\Channel\Http\Controllers\UserMiniprogramController;
 use Modules\System\Http\Controllers\NoticesController;
 use Modules\System\Http\Controllers\NoticesController;
 use Modules\System\Http\Controllers\NoticeTypesController;
 use Modules\System\Http\Controllers\NoticeTypesController;
@@ -15,5 +16,17 @@ Route::prefix('channel')->group(function () {
 
 
     Route::get('miniprogram/list', [UserMiniprogramController::class, 'index']);
     Route::get('miniprogram/list', [UserMiniprogramController::class, 'index']);
 
 
+
+    //支付模板
+    Route::prefix('paytemplate')->group(function(){
+        Route::get('optionTypeList',[PayTemplateController::class,'optionTypeList']);
+        Route::get('list',[PayTemplateController::class,'index']);
+        Route::get('show/{id}',[PayTemplateController::class,'show']);
+        Route::post('store',[PayTemplateController::class,'store']);
+        Route::post('update/{id}',[PayTemplateController::class,'update']);
+        Route::post('updateStatus/{id}',[PayTemplateController::class,'updateStatus']);//->withoutMiddleware(config('catch.route.middlewares'));;
+    });
+
+
 });
 });
 
 

+ 5 - 0
modules/Common/Errors/Errors.php

@@ -11,4 +11,9 @@ class Errors
     public const  NO_OPERATE_PERMISSION= [500003, '用户无操作权限'];
     public const  NO_OPERATE_PERMISSION= [500003, '用户无操作权限'];
     public const  VIDEO_NOT_EXISTS= [500004, '视频不存在'];
     public const  VIDEO_NOT_EXISTS= [500004, '视频不存在'];
     public const  MINIPROGRAM_STATUS_ERROR= [500005, '小程序未启用'];
     public const  MINIPROGRAM_STATUS_ERROR= [500005, '小程序未启用'];
+    public const  PAY_TEMPLATE_EXISTS_ERROR= [500006, '充值模板已经存在'];
+    public const  PAY_TEMPLATE_GIVEN_TOO_MUCH= [500007, '赠送不能超过金额3倍'];
+    public const  PAY_TEMPLATE_NOT_EXISTS_ERROR= [500008, '充值模板不存在'];
+    public const  PAY_TEMPLATE_CONFLICT= [500008, '该档位已存在'];
+    public const  PAY_TEMPLATE_OPTIONS_ERROR= [500009, '类型不存在'];
 }
 }

+ 12 - 13
tests/Feature/MiniprogramTest.php

@@ -21,18 +21,17 @@ class MiniprogramTest extends TestCase
     }
     }
 
 
 
 
-    public function test_store(): void
+    public function  ttest_store(): void
     {
     {
-        $response = $this->postJson('/api/manage/miniprogram/update/13',[
-            'name'=>'好看看短剧',
-            'company'=>'三个三集团',
-            'play_name'=>'三个十千短剧',
-            'type'=>1,
-            'appid'=>'wxts5415641fgtjc43twearhygesrthgysedhgyed',
-            'appsecret'=>'2f659djhsjsfa256b5523ta1shqa3d313',
-            'status'=>1,
-            'remark'=>''
+        
+        $response = $this->postJson('/api/channel/paytemplate/update/5',[
+            'name'=>'AAAA模板11',
+            'status'=>'0',
+            'options'=>'[{"price":9,"type":"FIRST_COIN","given":1000,"sequence":1,"is_default":0},{"price":30,"type":"COIN","given":0,"sequence":2,"is_default":1},{"price":50,"type":"COIN","given":4000,"sequence":3,"is_default":1},{"price":190,"type":"MONTH","given":0,"sequence":4,"is_default":1},{"price":290,"type":"YEAR","given":0,"sequence":5,"is_default":0}]'
         ]);
         ]);
+
+        //$response = $this->postJson('/api/channel/paytemplate');
+
         echo $response->getContent();
         echo $response->getContent();
 
 
         $response->dd();
         $response->dd();
@@ -50,11 +49,11 @@ class MiniprogramTest extends TestCase
     }
     }
 
 
 
 
-    public function ttest_index(): void
+    public function test_index(): void
     {
     {
         $name = '亿';
         $name = '亿';
-        $response = $this->getJson('/api/manage/miniprogram/index?name='.$name);
-
+        $response = $this->getJson('/api/channel/paytemplate/show/5');
+        echo $response->getContent();
         $response->dd();
         $response->dd();
     }
     }
 }
 }