love_php/app/Http/Controllers/Admin/RankController.php
2026-04-02 09:20:51 +08:00

503 lines
20 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Models\Appointment;
use App\Models\RankHistory;
use App\Models\Rank;
use App\Models\SingleService;
use App\Models\SubRank;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Support\Facades\DB;
class RankController extends Controller
{
public function __construct()
{
}
//会员列表
public function ranks(Request $request)
{
$ranks = Rank::with('subRanks')->select('id','name', 'explain', 'app_feature');
if($request->keyword){
$ranks = $ranks->where('name', 'like', '%'.$request->keyword.'%');
}
// if(!$request->nopage){
// $ranks = $ranks->paginate();
// }else{
$ranks = $ranks->get();
// }
foreach ($ranks as $rank){
if (!empty($rank)) {
$rank->explain = json_decode($rank->explain);
$rank->feature = json_decode($rank->app_feature);
foreach ($rank->subRanks as $subRank) {
if (request()->header('client-os') == "IOS") {
$subRank->discount_price = $subRank->ios_price;
$subRank->pic = $subRank->ios_pic;
}
}
unset($rank->app_feature);
}
}
return $this->success('ok', $ranks);
}
//会员套餐详情
public function rank(Request $request, $rank_id){
try {
$rank = Rank::with('subRanks')->where('id', $rank_id)->select('id','name', 'explain', 'app_feature','pay_type','can_coin_amount','can_cash_amount')->first();
if (!empty($rank)) {
$rank->explain = json_decode($rank->explain);
$rank->feature = json_decode($rank->app_feature);
unset($rank->app_feature);
}
return $this->success('ok', compact( 'rank'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('获取超级会员信息失败,请稍后再试');
}
}
//创建会员
public function createRank(Request $request){
$rank = New Rank();
$pay_type = $request->pay_type ? $request->pay_type : 'cash'; //支付方式 coin 或cash
$rank->pay_type = $pay_type;
if($request->name){
$rank->name = $request->name;
}
if(is_numeric($request->price)){
$rank->price = $request->price?:0.00;
}
if($request->type){
$rank->type = $request->type;
}
if ($pay_type == 'coin') {
if ($request->can_coin_amount < ($request->price * 10) && $request->can_coin_amount) {
$rank->can_coin_amount = $request->can_coin_amount;
$rank->can_cash_amount = $request->can_coin_amount / 10; //福币和现金比例 10:1;
}else{
return $this->failure('未输入可抵扣福币数量或可抵扣福币数量达到上限');
}
}else{
$rank->can_coin_amount = null;
$rank->can_cash_amount = null;
}
if($request->content){
$rank->content = $request->content;
}
if($request->explain){
$rank->explain = json_encode($request->explain);
}
if($request->feature){
$rank->feature = json_encode($request->feature);
}
if($request->app_feature){
$rank->app_feature = $request->app_feature;
}
if($request->official_feature){
$rank->official_feature = $request->official_feature;
}
if($request->num){
$rank->num = $request->num?:0;
}
if($request->is_show){
$rank->is_show = $request->is_show?:0;
}
$rank->save();
return $this->success('ok',$rank);
}
//修改会员
public function updateRank(Request $request, $rank_id){
$rank = Rank::find($rank_id);
if($request->name && $request->name != $rank->name){
$rank->name = $request->name;
}
if(is_numeric($request->price) && $request->price != $rank->price){
$rank->price = $request->price;
}
if($request->type && $request->type != $rank->type){
$rank->type = $request->type;
}
if($request->pay_type && $request->pay_type != $rank->pay_type){
$rank->pay_type = $request->pay_type;
}
if($request->content && $request->content != $rank->content){
$rank->content = $request->content;
}
if($request->explain && json_encode($request->explain) != $rank->explain){
$rank->explain = json_encode($request->explain);
}
// if($request->feature && json_encode($request->feature) != $rank->feature){
// $rank->feature = json_encode($request->feature);
// }
if($request->app_feature && json_encode($request->app_feature) != $rank->app_feature){
$rank->app_feature = json_encode($request->app_feature);
$rank->feature = $rank->app_feature;
}
if($request->official_feature && $request->official_feature != $rank->official_feature){
$rank->official_feature = $request->official_feature;
}
if($request->num && $request->num != $rank->num){
$rank->num = $request->num;
}
if($request->is_show && $request->is_show != $rank->is_show){
$rank->is_show = $request->is_show;
}
$rank->save();
return $this->success('ok',$rank);
}
public function updateRankV2(Request $request,$rank_id){
$rank = Rank::with('subRanks')->where('id',$rank_id)->first();
$price = [];
foreach ($rank->subRanks as $sub_rank) {
$price [] = $sub_rank->discount_price;
}
$min_price = min($price); //取子会员中的最小值
// dd($min_price);
$pay_type = $request->pay_type ? $request->pay_type : 'cash';
if ($pay_type == 'coin') {
if ($request->can_coin_amount < ($min_price * 10) && $request->can_coin_amount) {
$rank->can_coin_amount = $request->can_coin_amount;
$rank->can_cash_amount = $request->can_coin_amount / 10; //福币和现金比例 10:1;
}else{
return $this->failure('未输入可抵扣福币数量或可抵扣福币数量达到上限');
}
}else{
$rank->can_coin_amount = null;
$rank->can_cash_amount = null;
}
$rank->save();
return $this->success('ok',$rank);
}
//删除会员
public function delRank(Request $request, $rank_id){
try {
\DB::beginTransaction();
$hava_pay = RankHistory::where('rank_id', $rank_id)->count();
if($hava_pay){
return $this->failure('已有用户购买套餐,不能删除');
}
Rank::where('id', $rank_id)->delete();
SubRank::where('rank_id', $rank_id)->delete();
\DB::commit();
return $this->success('ok');
} catch (\Exception $e) {
\DB::rollBack();
$this->getError($e);
return $this->failure('删除失败');
}
}
//创建子会员
public function createSubRank(Request $request){
// switch ($request->name){
// case '月卡':
// $month = 1;
// break;
// case '季卡':
// $month = 3;
// break;
// case '年卡':
// $month = 12;
// break;
// default:
// $month = 0;
// }
$month = $request->month;
$price = $request->price?:0.00;
$sub_rank = New SubRank();
$sub_rank->rank_id = $request->rank_id ?: 0;
$sub_rank->name = $request->name;
$sub_rank->price = $request->price ?: 0.00;
$sub_rank->ios_price = $request->ios_price ?: 0.00;
$sub_rank->discount_price = $request->discount_price ?: 0.00;
$sub_rank->month_price = $month ? round($price/$month, 2) : 0.00;
$sub_rank->month = $month?:$request->month;
$sub_rank->pic = $request->pic;
$sub_rank->ios_pic = $request->ios_pic;
$sub_rank->is_show_mini = $request->is_show_mini ?1: 0;
$sub_rank->is_show_app = $request->is_show_app ?1: 0;
$sub_rank->is_show_web = $request->is_show_web ?1: 0;
$sub_rank->save();
return $this->success('ok');
}
//修改子会员
public function updateSubrank(Request $request, $sub_rank_id){
$sub_rank = SubRank::Find($sub_rank_id);
if($request->rank_id && $request->rand_id != $sub_rank->rank_id){
$sub_rank->rank_id = $request->rank_id;
}
if($request->name && $request->name != $sub_rank->name) {
$sub_rank->name = $request->name;
// switch ($request->name) {
// case '月卡':
// $month = 1;
// break;
// case '季卡':
// $month = 3;
// break;
// case '年卡':
// $month = 12;
// break;
// default:
// $month = 0;
// }
// $sub_rank->month = $month;
}
if(is_numeric($request->price) && $request->price != $sub_rank->price){
$sub_rank->price = $request->price;
}
if($request->ios_price && $request->ios_price != $sub_rank->ios_price){
$sub_rank->ios_price = $request->ios_price;
}
if($request->discount_price && $request->discount_price != $sub_rank->discount_price){
$sub_rank->discount_price = $request->discount_price;
}
if($request->month_price && $request->month_price != $sub_rank->month_price){
$sub_rank->month_price = $request->month_price;
}
if($request->month && $request->month != $sub_rank->monthe){
$sub_rank->month = $request->month;
}
if($request->pic && $request->pic != $sub_rank->pic){
$sub_rank->pic = $request->pic;
}
if($request->ios_pic && $request->ios_pic != $sub_rank->ios_pic){
$sub_rank->ios_pic = $request->ios_pic;
}
if(is_numeric($request->is_show_mini) && $request->is_show_mini != $sub_rank->is_show_mini){
$sub_rank->is_show_mini = $request->is_show_mini;
}
if(is_numeric($request->is_show_app) && $request->is_show_app != $sub_rank->is_show_app){
$sub_rank->is_show_app = $request->is_show_app;
}
if(is_numeric($request->is_show_web) && $request->is_show_web != $sub_rank->is_show_web){
$sub_rank->is_show_web = $request->is_show_web;
}
$sub_rank->save();
return $this->success('ok');
}
//删除会员
public function delSubRank(Request $request, $sub_rank_id){
SubRank::where('id', $sub_rank_id)->delete();
return $this->success('ok');
}
//服务套餐
public function singleServices(Request $request){
try {
$services = SingleService::with('rank:id,feature')->where('is_show',0)->orderBy('price', 'asc');
if($request->keyword) {
$services = $services->where('title', 'like', '%' . $request->keyword . '%');
}
if($request->nopage){
$services = $services->get();
}else{
$services = $services->paginate();
}
foreach ($services as $service) {
if ($service->discount_desc_type == 'json') {
$service->discount_desc = json_decode($service->discount_desc);
$service->feature = json_decode($service->feature);
unset($service->rank);
}else{
// $service->discount_desc = json_decode($service->discount_desc);
$service->feature = json_decode($service->feature);
$service->discount_desc = explode('****',$service->discount_desc);
unset($service->rank);
}
// $service->feature = json_decode($service->rank->feature);
}
return $this->success('ok', $services);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('获取会员套餐信息失败,请稍后再试');
}
}
//服务套餐
public function singleService(Request $request){
try {
$service = SingleService::with('rank:id,feature')->where('is_show',0)->orderBy('price', 'asc')->first();
if ($service->discount_desc_type == 'json') {
$service->discount_desc = json_decode($service->discount_desc);
$service->feature = json_decode($service->feature);
unset($service->rank);
}else{
// $service->discount_desc = json_decode($service->discount_desc);
$service->feature = json_decode($service->feature);
$service->discount_desc = explode('****',$service->discount_desc);
unset($service->rank);
}
// $service->feature = json_decode($service->rank->feature);
return $this->success('ok', $service);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('获取会员套餐信息失败,请稍后再试');
}
}
//创建单身套餐
public function createSingleService(Request $request){
if(empty($request->rank_id) || empty($request->type_id)){
return $this->failure('套餐或会员ID不能为空');
}
$type = Appointment::where('id', $request->type_id)->value('type');
$singleService = New SingleService();
$singleService->title = $request->title;
$singleService->desc = json_encode($request->desc);
$singleService->price = $request->price ? $request->price : 0;
$singleService->discount_price = $request->discount_price?$request->discount_price : 0;
$singleService->rank_id = $request->rank_id;
$singleService->rank_month = $request->rank_month;
$singleService->type_id = $request->type_id;
$singleService->type = $type;
$singleService->pay_type = $request->pay_type ? $request->pay_type : 'cash';
if ($request->pay_type == 'coin') {
if ($request->can_coin_amount < ($request->discount_price)*10 && $request->can_coin_amount) {
$singleService->can_coin_amount = $request->can_coin_amount;
$singleService->can_cash_amount = $request->can_coin_amount / 10; //福币和现金比例 10:1;
}else{
return $this->failure('未输入可抵扣福币数量或可抵扣福币数量达到上限');
}
}else{
$singleService->can_coin_amount = null;
$singleService->can_cash_amount = null;
}
$singleService->is_show = $request->is_show ?:0;
$singleService->pic = $request->pic;
$singleService->discount_name = $request->discount_name;
$singleService->discount_desc = $request->discount_desc;
$singleService->discount_desc_type = 'text';
$singleService->server_agreement = $request->server_agreement;
$singleService->is_show_mini = $request->is_show_mini ?1: 0;
$singleService->is_show_app = $request->is_show_app ?1: 0;
$singleService->is_show_web = $request->is_show_web ?1: 0;
$singleService->feature = json_encode($request->feature);
$singleService->save();
return $this->success('ok');
}
//修改单身套餐
public function updateSingleService(Request $request, $single_service_id){
$single_service = SingleService::Find($single_service_id);
if($request->title && $request->title != $single_service->title){
$single_service->title = $request->title;
}
if($request->desc && json_encode($request->desc) != $single_service->desc){
$single_service->desc = json_encode($request->desc);
}
if($request->price && $request->price != $single_service->price){
$single_service->price = $request->price;
}
if($request->discount_price && $request->discount_price != $single_service->discount_price){
$single_service->discount_price = $request->discount_price;
}
if($request->rank_id && $request->rank_id != $single_service->rank_id){
$single_service->rank_id = $request->rank_id;
}
if($request->rank_month && $request->rank_month != $single_service->rank_month){
$single_service->rank_month = $request->rank_month;
}
if($request->type_id && $request->type_id != $single_service->type_id){
$type = Appointment::where('id', $request->type_id)->value('type');
$single_service->type_id = $request->type_id;
$single_service->type = $type;
}
if($request->is_show && $request->is_show != $single_service->is_show){
$single_service->is_show = $request->is_show;
}
if($request->pay_type && $request->pay_type != $single_service->pay_type){
$single_service->pay_type = $request->pay_type;
}
if($request->pic && $request->pic != $single_service->pic){
$single_service->pic = $request->pic;
}
if($request->discount_name && $request->discount_name != $single_service->discount_name){
$single_service->discount_name = $request->discount_name;
}
if($request->discount_desc && $request->discount_desc != $single_service->discount_desc){
$single_service->discount_desc = $request->discount_desc;
$single_service->discount_desc_type = 'text';
}
if($request->server_agreement && $request->server_agreement != $single_service->server_agreement){
$single_service->server_agreement = $request->server_agreement;
}
if(is_numeric($request->is_show_mini) && $request->is_show_mini != $single_service->is_show_mini){
$single_service->is_show_mini = $request->is_show_mini;
}
if(is_numeric($request->is_show_app) && $request->is_show_app != $single_service->is_show_app){
$single_service->is_show_app = $request->is_show_app;
}
if(is_numeric($request->is_show_web) && $request->is_show_web != $single_service->is_show_web){
$single_service->is_show_web = $request->is_show_web;
}
if($request->feature && json_encode($request->feature) != $single_service->feature){
$single_service->feature = json_encode($request->feature);
}
if ($request->pay_type == 'coin') {
if ($request->can_coin_amount < ($request->discount_price)*10 && $request->can_coin_amount) {
$single_service->can_coin_amount = $request->can_coin_amount;
$single_service->can_cash_amount = $request->can_coin_amount / 10; //福币和现金比例 10:1;
}else{
return $this->failure('未输入可抵扣福币数量或可抵扣福币数量达到上限');
}
}else{
$single_service->can_coin_amount = null;
$single_service->can_cash_amount = null;
}
$single_service->save();
return $this->success('ok');
}
//删除单身套餐
public function delSingleService(Request $request, $single_service_id){
SingleService::where('id', $single_service_id)->delete();
return $this->success('ok');
}
//套餐列表
public function appointments(Request $request){
$appointments = Appointment::orderBy('id', 'desc');
if($request->keyword){
$appointments = $appointments->where('name', 'like', '%'.$request->keyword.'%');
}
if($request->nopage){
$appointments = $appointments->get();
}else{
$appointments = $appointments->paginate();
}
foreach ($appointments as $appointment){
$appointment->desc = json_decode($appointment->desc);
}
return $this->success('ok', $appointments);
}
}