Files
boursecu_server/app/Http/Controllers/SuperController.php
T

167 lines
5.5 KiB
PHP
Raw Normal View History

2026-01-24 14:03:11 +00:00
<?php
namespace App\Http\Controllers;
use App\DB\Te\TableEngine;
use App\Models\Cnt;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
class SuperController extends Controller
{
public function __construct($model,$with,$select = ['*'],$name_model = null, $query = null, $search = [])
{
$this->model = $model;
$this->with = $with;
$this->select = $select;
$this->name_model = $name_model;
$this->query = $query;
$this->search = $search;
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\jsonResponse
*/
public function store(Request $request)
{
$data = $request->all();
if (isset($data['fname']) && isset($data['lname'])){
$data['name'] = $data['fname'] . ' ' . $data['lname'];
}
if (isset($data['user_type']) && !is_int($data['user_type'])){
$cnt = Cnt::set('user_type',$data['user_type']);
$data['user_type'] = $cnt->id;
}
if (!is_null($this->name_model)){
if ($this->name_model == 'user' | $this->name_model == 'customer'){
if (!isset($data['password'])){
$data['password'] = trim($data['mobile']);
}
if ($this->name_model == 'customer'){
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$charactersLength = strlen($characters);
do {
$uniqueId = '';
for ($i = 0; $i < 4; $i++) { // ایجاد یک کد چهار رقمی
$index = random_int(0, $charactersLength - 1);
$uniqueId .= $characters[$index];
}
// بررسی اینکه آیا کد ایجاد شده یونیک است
$existingCustomer = $this->model::where('referral_id', $uniqueId)->first();
} while ($existingCustomer); // تکرار تا زمانی که کد یونیک ایجاد شود
$data['referral_id'] = $uniqueId; // ذخیره کد یونیک در فیلد referral_id
$message = "{$data['name']} عزیز، اکانت شما در پلتفرم {$this->Config('name_fa')} ایجاد شد.\nنام کاربری: {$data['mobile']}\nورود به نرم افزار: {$this->Config('app_url')}";
$this->Send($data['mobile'], $message);
}
}
}
$user = $this->model::create($data);
return $this->resultCreate($user->id);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\jsonResponse
*/
public function show($id)
{
$user = $this->model::with($this->with)
->select($this->select)
->findOrFail($id);
return response()->json($user);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\jsonResponse
*/
public function update(Request $request, $id)
{
$data= $request->all();
if (isset($data['mobile']) && !is_null($data['mobile'])){
$check_mobile = $this->model::where('mobile',$data['mobile'])
->where('id','<>',$id)
->first();
if (!is_null($check_mobile)){
throw new UnprocessableEntityHttpException('این شماره همراه توسط شخص دیگری در حال استفاده است');
}
}
if (isset($data['fname']) && isset($data['lname'])){
$data['name'] = $data['fname'] . ' ' . $data['lname'];
}
if (isset($data['user_type']) && !is_int($data['user_type'])){
$cnt = Cnt::set('user_type',$data['user_type']);
$data['user_type'] = $cnt->id;
}
$this->model::findOrFail($id)
->update($data);
return response()->json(true);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\jsonResponse
*/
public function destroy($id)
{
$this->model::findOrFail($id)->delete();
return response()->json(true);
}
/**
* @param $id
* @return \Illuminate\Http\JsonResponse
*/
public function active($id){
$query = $this->model::findOrFail($id);
$query->update([
'active'=>!$query->active
]);
return response()->json(true);
}
public function table(Request $request)
{
$data = $request->all();
if (isset($data["select"]) && is_null($data["select"])){
$data["select"] = [];
}
if (is_null($this->query)){
$query_table = $this->model::with($this->with);
//*******کداضافه شده
// ->where('active', '<=', now()) // فیلتر محصولات فعال
// ->get();
//*********
}else{
$query_table = $this->query;
// $query_table = $query_table->where('active', '<=', now());
}
TableEngine::create($query_table, $data)
->initSearch($this->search)
->select($this->select)
->export($query_table);
return $query_table;
}
}