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'] ]; } }