118 lines
3.2 KiB
PHP
118 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Facades\CommonService;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Band extends BaseModel
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
|
|
public $fillable = ['user_id', 'name', 'mac', 'status'];
|
|
|
|
const SHOW_STATUS = 1;
|
|
const HIDDEN_STATUS = 0;
|
|
|
|
const DEFAULT_STEP_NUM = 10000;
|
|
const DEFAULT_KCAL = 300;
|
|
const DEFAULT_DISTANCE = 5;
|
|
const DEFAULT_SLEEP = 480;
|
|
|
|
public const SLEEP_TYPE = "sleep";//睡眠
|
|
const HRREST_TYPE = "hrrest"; //心率
|
|
const TEMP_TYPE = "temp"; //体温
|
|
|
|
public static function bindUser($mac,$name, $user)
|
|
{
|
|
$data = ['user_id'=>$user->id, 'name'=>$name, 'mac'=>$mac, 'status'=>self::SHOW_STATUS];
|
|
$band = self::create($data);
|
|
return $band;
|
|
}
|
|
|
|
public static function unbindUser($mac, $user)
|
|
{
|
|
self::owner($user->id)->mac($mac)->delete();
|
|
|
|
}
|
|
|
|
public static function connectUser($mac, $user)
|
|
{
|
|
self::owner($user->id)->update(['status'=>self::HIDDEN_STATUS]);
|
|
self::owner($user->id)->mac($mac)->update(['status'=>self::SHOW_STATUS]);
|
|
}
|
|
|
|
public static function breakUser($mac, $user)
|
|
{
|
|
self::owner($user->id)->mac($mac)->update(['status'=>self::HIDDEN_STATUS]);
|
|
}
|
|
|
|
public static function bindStatus($mac, $user=null)
|
|
{
|
|
$is_bind = $is_self = false;
|
|
$mobile = '';
|
|
$band = Band::mac($mac)->first();
|
|
if ($band) {
|
|
$is_bind = true;
|
|
if ($user && $band->user_id == $user->id) $is_self = true;
|
|
$mobile = User::where("id", $band->user_id)->value("mobile");
|
|
$mobile = CommonService::hidePhone($mobile);
|
|
}
|
|
return [$is_bind, $is_self, $mobile];
|
|
}
|
|
|
|
public static function currentBand($user)
|
|
{
|
|
$band = self::owner($user->id)->status(self::SHOW_STATUS)->first();
|
|
return $band;
|
|
}
|
|
|
|
|
|
public function scopeMac($query, $mac)
|
|
{
|
|
return $query->where("mac", $mac);
|
|
}
|
|
|
|
public function scopeOwner($query, $user_id)
|
|
{
|
|
return $query->where("user_id", $user_id);
|
|
}
|
|
|
|
public function scopeStatus($query, $status)
|
|
{
|
|
return $query->where("status", $status);
|
|
}
|
|
|
|
public function setGoals($data=["step_num"=>self::DEFAULT_STEP_NUM, 'kcal'=>self::DEFAULT_KCAL, 'sleep'=>self::DEFAULT_SLEEP, 'distance'=>self::DEFAULT_DISTANCE])
|
|
{
|
|
$data = array_merge($data, ['user_id'=>$this->user_id, 'band_id'=>$this->id]);
|
|
$goals = $this->goals;
|
|
if ($goals) {
|
|
$goals->update($data);
|
|
}else {
|
|
BandGoal::create($data);
|
|
}
|
|
$start_time = date("Y-m-d");
|
|
$end_time = date("Y-m-d", strtotime("+1 day"));
|
|
//增加记录 当天有记录则修改
|
|
$log = BandGoalLog::owner($this->user_id)->band($this->id)->times($start_time, $end_time)->first();
|
|
if ($log) {
|
|
$log->update($data);
|
|
}else {
|
|
BandGoalLog::create($data);
|
|
}
|
|
}
|
|
|
|
public function goals()
|
|
{
|
|
return $this->hasOne(BandGoal::class, 'band_id', 'id');
|
|
}
|
|
|
|
public function goalsLogs()
|
|
{
|
|
return $this->hasMany(BandGoalLog::class, 'band_id', 'id');
|
|
}
|
|
}
|