340 lines
14 KiB
PHP
340 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Response;
|
|
use Schema;
|
|
use Hash;
|
|
use App\Libraries\ListHelper;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Contracts\StationResourceInterface;
|
|
use App\Helpers\HttpStatusCode;
|
|
use App\Helpers\CurrentUserHelper;
|
|
use App\Libraries\UuidHelper;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Station;
|
|
use App\CodeCity;
|
|
use App\CodeProvince;
|
|
|
|
class StationService implements StationResourceInterface
|
|
{
|
|
|
|
public $station;
|
|
|
|
public $station_id;
|
|
|
|
public function getByField($field,$value)
|
|
{
|
|
$this->station = Station::where($field,$value)
|
|
->where('is_active',1)
|
|
->orderBy('description','asc');
|
|
return $this->station->get();
|
|
}
|
|
|
|
|
|
public function getDetailsWhereIn($field,$value,$relationship = null)
|
|
{
|
|
if($relationship)
|
|
{
|
|
$details = Station::with($relationship)
|
|
->whereIn($field,$value)->get();
|
|
}
|
|
else
|
|
{
|
|
$details = Station::whereIn($field,$value)->get();
|
|
}
|
|
|
|
return $details->toArray();
|
|
}
|
|
|
|
public function radius($meters,$longitude,$latitude)
|
|
{
|
|
$details = Station::select(DB::raw('station_id, station_uuid, description, address, longitude, latitude, (
|
|
6371 *
|
|
acos(
|
|
cos( radians( '.$latitude.' ) ) *
|
|
cos( radians( `latitude` ) ) *
|
|
cos(
|
|
radians( `longitude` ) - radians( '.$longitude.' )
|
|
) +
|
|
sin( radians('.$latitude.') ) *
|
|
sin( radians(`latitude`) )
|
|
)
|
|
) `distance`'))
|
|
->having('distance', '<=', $meters * .001)
|
|
->orderBy('distance','asc')
|
|
->get();
|
|
|
|
return $details;
|
|
}
|
|
|
|
public function nearby($longitude,$latitude)
|
|
{
|
|
$member_details = CurrentUserHelper::get_currentMember();
|
|
$details = Station::select(DB::raw('station_id as sid, station_uuid, description, address, longitude, latitude, (
|
|
6371 *
|
|
acos(
|
|
cos( radians( '.$latitude.' ) ) *
|
|
cos( radians( `latitude` ) ) *
|
|
cos(
|
|
radians( `longitude` ) - radians( '.$longitude.' )
|
|
) +
|
|
sin( radians('.$latitude.') ) *
|
|
sin( radians(`latitude`) )
|
|
)
|
|
) `distance`,
|
|
(
|
|
SELECT AVG(rate)
|
|
FROM ratings
|
|
WHERE station_id = sid
|
|
) as average ,
|
|
(
|
|
SELECT COUNT(lcard_id)
|
|
FROM station_favorite
|
|
WHERE lcard_id = '.$member_details->lcard_id.'
|
|
AND station_id = sid
|
|
) as favorite'))
|
|
->where('is_viewable',1)
|
|
->where('city_id','<>','0') // Unioil Tester
|
|
->where('code','<>','1000') // Unioil Tester
|
|
->orderBy('distance','asc');
|
|
return $details->get();
|
|
}
|
|
|
|
public function favorites()
|
|
{
|
|
$member_details = CurrentUserHelper::get_currentMember();
|
|
$details = Station::select(DB::raw('station_id as sid, station_uuid, description, address, longitude, latitude,
|
|
(
|
|
SELECT AVG(rate)
|
|
FROM ratings
|
|
WHERE station_id = sid
|
|
) as average ,
|
|
(
|
|
SELECT COUNT(lcard_id)
|
|
FROM station_favorite
|
|
WHERE lcard_id = '.$member_details->lcard_id.'
|
|
AND station_id = sid
|
|
) as favorite'))
|
|
->having('favorite','>',0);
|
|
|
|
return $details->get();
|
|
}
|
|
|
|
public function search_by_keyword($keyword)
|
|
{
|
|
$details = CodeCity::select(DB::raw('city_id as cid, city_uuid,code_city.name as city_name,
|
|
(
|
|
SELECT COUNT(station_id)
|
|
FROM station
|
|
WHERE city_id = cid
|
|
) as station_count'))
|
|
->leftJoin('code_province', 'code_province.province_id', '=','code_city.province_id')
|
|
->where('code_city.name', 'LIKE', $keyword.'%')
|
|
->orWhere('code_province.name', 'LIKE', $keyword.'%')
|
|
->orderBy('city_name','asc');
|
|
if(count($details->get())){
|
|
if($details->get()[0]->station_count != 0)
|
|
return $details->get();
|
|
}
|
|
else
|
|
return [];
|
|
}
|
|
|
|
public function search_by_province($province_uuid)
|
|
{
|
|
$details = CodeCity::select(DB::raw('city_id as cid, city_uuid,code_city.name as city_name,
|
|
(
|
|
SELECT COUNT(station_id)
|
|
FROM station
|
|
WHERE city_id = cid
|
|
) as station_count'))
|
|
->leftJoin('code_province', 'code_province.province_id', '=','code_city.province_id')
|
|
->where('province_uuid', $province_uuid)
|
|
->orderBy('city_name','asc');
|
|
return $details->get();
|
|
}
|
|
|
|
public function search_all()
|
|
{
|
|
$details = CodeCity::select(DB::raw('city_id as cid, city_uuid,code_city.name as name,
|
|
(
|
|
SELECT COUNT(station_id)
|
|
FROM station
|
|
WHERE city_id = cid
|
|
) as count'))
|
|
->where('code_city.is_active',1)
|
|
->orderBy('name','asc')
|
|
->get();
|
|
$return = [];
|
|
foreach ($details as $key => $value){
|
|
if($value->count != 0){
|
|
$return[] = $value;
|
|
}
|
|
}
|
|
|
|
return $return;
|
|
}
|
|
|
|
public function search_by_city($city_uuid)
|
|
{
|
|
$member_details = CurrentUserHelper::get_currentMember();
|
|
$details = Station::select(DB::raw('station_id as sid, station_uuid, description, address, longitude, latitude,
|
|
(
|
|
SELECT AVG(rate)
|
|
FROM ratings
|
|
WHERE station_id = sid
|
|
AND ratings.is_active = 1
|
|
) as average ,
|
|
(
|
|
SELECT COUNT(lcard_id)
|
|
FROM station_favorite
|
|
WHERE lcard_id = '.$member_details->lcard_id.'
|
|
AND station_id = sid
|
|
) as favorite'))
|
|
->leftJoin('code_city', 'station.city_id', '=','code_city.city_id')
|
|
->where('city_uuid', $city_uuid)
|
|
->where('is_viewable',1)
|
|
->where('station.code','<>','1000') // Unioil Tester
|
|
->orderBy('description','asc');
|
|
return $details->get();
|
|
}
|
|
|
|
public function search_by_uuid($uuid)
|
|
{
|
|
$member_details = CurrentUserHelper::get_currentMember();
|
|
$details = Station::select(DB::raw('station_id as sid, station_uuid, code, description, address, longitude, latitude, contact_number,
|
|
(
|
|
SELECT AVG(rate)
|
|
FROM ratings
|
|
WHERE station_id = sid
|
|
AND ratings.is_active = 1
|
|
) as average ,
|
|
(
|
|
SELECT COUNT(lcard_id)
|
|
FROM station_favorite
|
|
WHERE lcard_id = '.$member_details->lcard_id.'
|
|
AND station_id = sid
|
|
) as favorite'))
|
|
->where('station_uuid', $uuid);
|
|
return $details->get();
|
|
}
|
|
|
|
public function search_by_code($station_code)
|
|
{
|
|
$details = Station::where('code',$station_code)
|
|
->where('is_active',1)
|
|
->get();
|
|
|
|
return $details;
|
|
}
|
|
|
|
public function update_content($value)
|
|
{
|
|
$toUpdate = [];
|
|
|
|
if($value->station_name != '') $toUpdate['description'] = $value->station_name;
|
|
if($value->longitude != '') $toUpdate['longitude'] = $value->longitude;
|
|
if($value->latitude != '') $toUpdate['latitude'] = $value->latitude;
|
|
if($value->station_location != '') $toUpdate['address'] = $value->station_location;
|
|
if($value->contact_number != '') $toUpdate['contact_number'] = $this->removeSpecial($value->contact_number);
|
|
|
|
if($value->city_code != '')
|
|
{
|
|
$city = CodeCity::where('code',$value->city_code)->first();
|
|
if($city) $toUpdate['city_id'] = $city->city_id;
|
|
}
|
|
|
|
|
|
Station::where('code',$value->station_code)
|
|
->where('is_active',1)
|
|
->update($toUpdate);
|
|
}
|
|
|
|
public function store($value)
|
|
{
|
|
$currentUser = CurrentUserHelper::get_currentAdmin();
|
|
$this->station = new Station;
|
|
|
|
$uuid = new UuidHelper;
|
|
$this->station->station_uuid = $uuid->generate_uuid1();
|
|
|
|
$this->station->code = $value->code;
|
|
$this->station->description = $value->description;
|
|
$this->station->longitude = $value->longitude;
|
|
$this->station->latitude = $value->latitude;
|
|
$this->station->address = $value->address;
|
|
$this->station->contact_number = $value->contact_number;
|
|
$this->station->is_viewable = 1;
|
|
$this->station->created_by = $currentUser->admin_id;
|
|
$this->station->created_at = date('Y-m-d H:i:s');
|
|
|
|
|
|
if($value->Branch->code != '')
|
|
{
|
|
$city = CodeCity::where('code',$value->Branch->code)->first();
|
|
if($city) $this->station->city_id = $city->city_id;
|
|
}
|
|
|
|
|
|
if ($this->station->save())
|
|
{
|
|
return $this->station->station_id;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
private function removeSpecial($value){
|
|
return str_replace('(', '', str_replace(')', '', str_replace(' ', '', str_replace('-', '', $value))));
|
|
}
|
|
///RNRPH
|
|
public function update_stations($value)
|
|
{
|
|
$toUpdate = [];
|
|
|
|
if($value->description != '') $toUpdate['description'] = $value->description;
|
|
if($value->longitude != '') $toUpdate['longitude'] = $value->longitude;
|
|
if($value->latitude != '') $toUpdate['latitude'] = $value->latitude;
|
|
if($value->address != '') $toUpdate['address'] = $value->address;
|
|
if($value->contact_number != '') $toUpdate['contact_number'] = $this->removeSpecial($value->contact_number);
|
|
|
|
if($value->code != '')
|
|
{
|
|
$city = CodeCity::where('code',$value->Branch->code)->first();
|
|
if($city) $toUpdate['city_id'] = $city->city_id;
|
|
}
|
|
|
|
$toUpdate['is_viewable'] = 1;
|
|
|
|
Station::where('code',$value->code)
|
|
->where('is_active',1)
|
|
->update($toUpdate);
|
|
}
|
|
|
|
public function get_active_stations()
|
|
{
|
|
$stations = Station::where('is_active',1)
|
|
->get();
|
|
return $stations;
|
|
}
|
|
|
|
public function disbale_stations($ids){
|
|
$toUpdate = [];
|
|
$toUpdate['is_viewable'] = 0;
|
|
Station::whereIn('station_id', $ids)
|
|
->update($toUpdate);
|
|
}
|
|
|
|
public function get_station_by_city_uuid($city_id){
|
|
$stations = Station::where('city_id',$city_id)
|
|
->get();
|
|
return $stations;
|
|
}
|
|
}
|