123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- <?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'];
- if($item->type == 'COIN' || $item->type == 'FIRST_COIN'){
- $item->charge_coin = (int)($item->price*100);
- }else{
- $item->charge_coin = $item->type_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']
- ];
- }
- public function optionSequence(){
- return [
- ['name'=>'选项一','value'=>1],
- ['name'=>'选项二','value'=>2],
- ['name'=>'选项三','value'=>3],
- ['name'=>'选项四','value'=>4],
- ['name'=>'选项五','value'=>5],
- ['name'=>'选项六','value'=>6]
- ];
- }
- }
|