<?php
namespace App\Entity;
use App\Extension\Annotation\SetDeleteInfoOnRemove;
use App\Extension\Entity\TimestampableAndBlameableEntity;
use App\Extension\Entity\PatientInfo;
use Doctrine\ORM\Mapping as ORM;
use phpDocumentor\Reflection\Types\Integer;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use App\Entity\User;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation as Serializer;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @SetDeleteInfoOnRemove()
* @ORM\Entity
* @ORM\Table(name="patient")
* @ApiResource(
* itemOperations={
* "get",
* "put"={
* "normalization_context"={"groups"={"put"}}
* }
* },
* collectionOperations={
* "get"={
* "normalization_context"={"groups"={"list"}}
* },
* "post"={
* "normalization_context"={"groups"={"post"}}
* }
* }
* )
* @ApiFilter(SearchFilter::class, properties={"caregiver": "exact", "patientStatus": "exact"})
* @Vich\Uploadable
*/
class Patient implements \Serializable
{
use TimestampableAndBlameableEntity;
use PatientInfo;
CONST STATUS_NORMAL = "正常";
CONST STATUS_LOST = "走失";
CONST STATUS_PASS_AWAY = "已離世";
CONST STATUS_QUIT = "已退出服務";
/**
* @var integer
* @Groups("list")
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="patients")
*/
private $caregiver;
/**
* @var string
*
* @ORM\Column(name="firstname", type="string", nullable=true)
*/
private $firstname;
/**
* @var string
*
* @ORM\Column(name="lastname", type="string", nullable=true)
*/
private $lastname;
/**
* @var string
*
* @ORM\Column(name="birth_year", type="string", nullable=true)
*/
private $birthYear;
/**
* @var string
* @Groups("list")
* @ORM\Column(name="gender", type="string", nullable=true)
*/
private $gender;
/**
* @var string
*
* @ORM\Column(name="language", type="string", nullable=true)
*/
private $language = "";
/**
* @var string
*
* @ORM\Column(name="language_other", type="string", nullable=true)
*/
private $languageOther = "";
/**
* @var string
* @Groups("list")
* @ORM\Column(name="height", type="string", nullable=true)
*/
private $height = "";
/**
* @var string
* @Groups("list")
* @ORM\Column(name="weight", type="string", nullable=true)
*/
private $weight = "";
/**
* @ORM\OneToMany(targetEntity="PatientIbeacon", mappedBy="patient", cascade={"persist", "remove"})
*/
private $ibeacons = [];
/**
* @ORM\OneToMany(targetEntity="PatientLost", mappedBy="patient", cascade={"persist", "remove"})
*/
private $losts;
/**
* @ORM\Column(name="record_status", type="boolean")
* @Groups("list")
* @var bool
*/
private $enabled = true;
/**
* @var string
* @Groups("list")
* @ORM\Column(name="patient_status", type="string", nullable=true)
*/
private $patientStatus = self::STATUS_NORMAL;
/**
* @var null|array
* @Groups("list")
* @ORM\Column(name="last_api_location", type="json", nullable=true)
*/
private $lastAPILocation = [];
/**
* @var null|array
* @Groups("list")
* @ORM\Column(name="last_api_seen_time", type="json", nullable=true)
*/
private $lastAPISeenTime = [];
/**
* @var null|string
* @Groups("list")
* @ORM\Column(name="quit_reason", type="string", nullable=true)
*/
private $quitReason = "";
/**
* @ORM\Column(name="quit_ts", type="datetime", nullable=true)
* @Groups("list")
* @var null|\DateTimeInterface
*/
private $quitedAt;
/**
* @ORM\Version
* @ORM\Column(type="integer")
*/
private $version;
public function getId()
{
return $this->id;
}
public function getVersion()
{
return $this->version;
}
/**
* @return mixed
*/
public function getCaregiver()
{
return $this->caregiver;
}
/**
* @param mixed $caregiver
*/
public function setCaregiver($caregiver)
{
$this->caregiver = $caregiver;
}
/**
* @Groups("list")
* @return string
*/
public function getFirstName()
{
return $this->firstname;
}
/**
* @param string $firstname
*/
public function setFirstName(string $firstname)
{
$this->firstname = $firstname;
}
/**
* @Groups("list")
* @return string
*/
public function getLastName()
{
return $this->lastname;
}
/**
* @param string $lastname
*/
public function setLastName(string $lastname)
{
$this->lastname = $lastname;
}
/**
* @Groups("list")
* @return string
*/
public function getName()
{
return $this->lastname . $this->firstname;
}
/**
* @return string
*/
public function getBirthYear()
{
return $this->birthYear;
}
/**
* @param string $birthYear
*/
public function setBirthYear(string $birthYear)
{
$this->birthYear = $birthYear;
}
/**
* @return string
*/
public function getGender()
{
return $this->gender;
}
/**
* @param string $gender
*/
public function setGender(string $gender)
{
$this->gender = $gender;
}
/**
* @return string
*/
public function getLanguage()
{
return $this->language;
}
/**
* @param null|string $language
*/
public function setLanguage($language)
{
$this->language = $language;
}
/**
* @return string
*/
public function getLanguageOther()
{
return $this->languageOther;
}
/**
* @param null|string $languageOther
*/
public function setLanguageOther($languageOther)
{
$this->languageOther = $languageOther;
}
/**
* @return string
*/
public function getHeight()
{
return $this->height;
}
/**
* @param null|string $height
*/
public function setHeight($height)
{
$this->height = $height;
}
/**
* @return string
*/
public function getWeight()
{
return $this->weight;
}
/**
* @param null|string $weight
*/
public function setWeight($weight)
{
$this->weight = $weight;
}
/**
* @return bool
*/
public function isEnabled(): bool
{
return $this->enabled;
}
/**
* @param bool $enabled
*/
public function setEnabled(bool $enabled): void
{
if ($this->isPassAway()) {
$this->enabled = false;
} else {
$this->enabled = $enabled;
}
}
/**
* @Groups("list")
* @return mixed
*/
public function getPatientIbeacons()
{
return $this->getActiveIbeacons();
}
/**
* @Groups("list")
* @return mixed
*/
public function getPatientLosts()
{
return $this->losts;
}
/**
* @return string
*/
public function getPatientStatus()
{
if ($this->isLost()) {
return self::STATUS_LOST;
}
return $this->patientStatus;
}
/**
* @param string $patientStatus
*/
public function setPatientStatus(string $patientStatus)
{
$this->patientStatus = $patientStatus;
if ($patientStatus == self::STATUS_QUIT) {
$this->quitService();
$this->setQuitedAt(new \DateTime('now'));
}
}
/**
* @param null|array $lastAPILocation
*/
public function setLastAPILocation($lastAPILocation)
{
$this->lastAPILocation = $lastAPILocation;
}
/**
* @param null|array $lastAPISeenTime
*/
public function setLastAPISeenTime($lastAPISeenTime)
{
$this->lastAPISeenTime = $lastAPISeenTime;
}
/**
* @return null|array
*/
public function getLastAPILocation()
{
$lastAPILocations = [];
if (count($this->getActiveIbeacons())) {
foreach ($this->getActiveIbeacons() as $ibeacon) {
if ($ibeacon) {
$lastAPILocations[$ibeacon->getName()] = $ibeacon->getLastAPILocation();
}
}
}
return $lastAPILocations;
}
/**
* @return null|array
*/
public function getLastAPISeenTime()
{
$lastAPISeenTimes = [];
foreach ($this->getActiveIbeacons() as $ibeacon) {
if ($ibeacon) {
$lastAPISeenTimes[$ibeacon->getName()] = $ibeacon->getLastAPISeenTime();
}
}
return $lastAPISeenTimes;
}
/**
* @return null|string
*/
public function getQuitReason()
{
return $this->quitReason;
}
/**
* @param null|string $quitReason
*/
public function setQuitReason($quitReason)
{
$this->quitReason = $quitReason;
}
/**
* @return null|\DateTimeInterface
*/
public function getQuitedAt()
{
return $this->quitedAt;
}
/**
* @param null|\DateTimeInterface $quitedAt
*/
public function setQuitedAt(\DateTimeInterface $quitedAt)
{
$this->quitedAt = $quitedAt;
}
public function isNormal(): bool
{
if ($this->isLost()) {
return false;
}
return $this->patientStatus == Patient::STATUS_NORMAL;
}
public function isLost() : bool
{
if ($this->losts) {
foreach ($this->losts as $lost) {
if (!$lost->isFound()) {
return true;
}
}
}
return false;
}
public function isPassAway(): bool
{
return $this->patientStatus == Patient::STATUS_PASS_AWAY;
}
public function quitService()
{
$this->enabled = false;
$this->patientStatus = self::STATUS_QUIT;
foreach ($this->ibeacons as $ibeacon) {
$ibeacon->setEnabled(false);
}
}
public function getActiveIbeacons()
{
$activeIbeacons = [];
if (count($this->ibeacons)) {
foreach ($this->ibeacons as $ibeacon) {
if ($ibeacon && $ibeacon->isEnabled()) {
$activeIbeacons[] = $ibeacon;
}
}
}
return $activeIbeacons;
}
public function ibeaconsForExport()
{
return $this->ibeacons;
}
public function exportContent()
{
// $content = "<table border='1'><tr>";
$content = $this->getCaregiver()->exportContent();
$content .= "<td align='left'>" . $this->getName() . "</td>";
$content .= "<td align='left'>" . $this->getPatientStatus() . "</td>";
$content .= "<td align='left'>" . $this->getBirthYear() . "</td>";
$content .= "<td align='left'>" . $this->getGender() . "</td>";
$content .= "<td align='left'>" . $this->getLanguage() . ($this->getLanguageOther() ? ": " . $this->getLanguageOther() : "") . "</td>";
$content .= "<td align='left'>" . $this->getHeight() . "</td>";
$content .= "<td align='left'>" . $this->getWeight() . "</td>";
$content .= "<td align='left'>" . ($this->checkDummy($this->getBody()) ? "" : $this->getBody()) . "</td>";
$content .= "<td align='left'>" . ($this->checkDummy($this->getHair()) ? "" : $this->getHair()) . "</td>";
$content .= "<td align='left'>" . ($this->checkDummy($this->getSkin()) ? "" : $this->getSkin()) . "</td>";
$content .= "<td align='left'>" . ($this->checkDummy($this->getFace()) ? "" : $this->getFace()) . "</td>";
$content .= "<td align='left'>" . $this->getOther() . "</td>";
$content .= "<td align='left'>" . ($this->isIdentity() ? "是" : "否") . "</td>";
$content .= "<td align='left'>" . $this->getIdentityValue() . "</td>";
$content .= "<td align='left'>" . $this->getMovement() . "</td>";
$content .= "<td align='left'>" . ($this->isTool() ? "是" : "否") . "</td>";
$content .= "<td align='left'>" . $this->getToolMain() . "</td>";
$content .= "<td align='left'>" . $this->getToolSub() . "</td>";
$content .= "<td align='left'>" . ($this->checkDummy($this->getLocation()) ? "" : $this->getLocation()) . ($this->getLocationOther() ? ": " . $this->getLocationOther() : "") . "</td>";
$content .= "<td align='left'>" . $this->getCharacteristic() . "</td>";
$content .= "<td align='left'>" . $this->getCommunication() . "</td>";
$content .= "<td align='left'>" . ($this->checkDummy($this->getHealth()) ? "" : $this->getHealth()) . "</td>";
$content .= "<td align='left'>" . $this->getImageName() . "</td>";
$content .= "<td align='left'>" . $this->getCreatedAt()->format('Y-m-d H:i') . "</td>";
$content .= "<td align='left'>" . $this->getUpdatedAt()->format('Y-m-d H:i') . "</td>";
// $content .= "</tr></table>";
return $content;
}
/**
* Persists the minimum amount of properties required to refresh the user
* in the next request and ensure correct AbstractToken::hasUserChanged()
* comparison.
*
* @return string|null
*/
public function serialize()
{
return serialize([
'id' => $this->id,
'firstname' => $this->firstname,
'lastname' => $this->lastname,
]);
}
public function unserialize($data)
{
$data = unserialize($data);
$this->id = $data['id'];
$this->firstname = $data['firstname'];
$this->lastname = $data['lastname'];
}
public function __toString()
{
return $this->getName();
}
}