src/Entity/Patient.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Extension\Annotation\SetDeleteInfoOnRemove;
  4. use App\Extension\Entity\TimestampableAndBlameableEntity;
  5. use App\Extension\Entity\PatientInfo;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use phpDocumentor\Reflection\Types\Integer;
  8. use Ramsey\Uuid\Uuid;
  9. use Symfony\Component\Security\Core\User\AdvancedUserInterface;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. use ApiPlatform\Core\Annotation\ApiResource;
  12. use ApiPlatform\Core\Annotation\ApiFilter;
  13. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  14. use App\Entity\User;
  15. use Symfony\Component\HttpFoundation\File\File;
  16. use Symfony\Component\Serializer\Annotation as Serializer;
  17. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  18. use Symfony\Component\Serializer\Annotation\Groups;
  19. /**
  20.  * @SetDeleteInfoOnRemove()
  21.  * @ORM\Entity
  22.  * @ORM\Table(name="patient")
  23.  * @ApiResource(
  24.  *      itemOperations={
  25.  *          "get",
  26.  *          "put"={
  27.  *             "normalization_context"={"groups"={"put"}}
  28.  *         }
  29.  *     },
  30.  *     collectionOperations={
  31.  *          "get"={
  32.  *             "normalization_context"={"groups"={"list"}}
  33.  *         },
  34.  *         "post"={
  35.  *             "normalization_context"={"groups"={"post"}}
  36.  *         }
  37.  *     }
  38.  * )
  39.  * @ApiFilter(SearchFilter::class, properties={"caregiver": "exact", "patientStatus": "exact"})
  40.  * @Vich\Uploadable
  41.  */
  42. class Patient implements \Serializable
  43. {
  44.     use TimestampableAndBlameableEntity;
  45.     use PatientInfo;
  46.     CONST STATUS_NORMAL "正常";
  47.     CONST STATUS_LOST "走失";
  48.     CONST STATUS_PASS_AWAY "已離世";
  49.     CONST STATUS_QUIT "已退出服務";
  50.     /**
  51.      * @var integer
  52.      * @Groups("list")
  53.      * @ORM\Column(name="id", type="integer", nullable=false)
  54.      * @ORM\Id
  55.      * @ORM\GeneratedValue(strategy="AUTO")
  56.      */
  57.     private $id;
  58.     /**
  59.      * @ORM\ManyToOne(targetEntity="User", inversedBy="patients")
  60.      */
  61.     private $caregiver;
  62.     /**
  63.     * @var string
  64.     *
  65.     * @ORM\Column(name="firstname", type="string", nullable=true)
  66.     */
  67.     private $firstname;
  68.     /**
  69.      * @var string
  70.      *
  71.      * @ORM\Column(name="lastname", type="string", nullable=true)
  72.      */
  73.     private $lastname;
  74.     /**
  75.     * @var string
  76.     *
  77.     * @ORM\Column(name="birth_year", type="string", nullable=true)
  78.     */
  79.     private $birthYear;
  80.     /**
  81.     * @var string
  82.     * @Groups("list")
  83.     * @ORM\Column(name="gender", type="string", nullable=true)
  84.     */
  85.     private $gender;
  86.     /**
  87.      * @var string
  88.      *
  89.      * @ORM\Column(name="language", type="string", nullable=true)
  90.      */
  91.     private $language "";
  92.     /**
  93.      * @var string
  94.      *
  95.      * @ORM\Column(name="language_other", type="string", nullable=true)
  96.      */
  97.     private $languageOther "";
  98.     /**
  99.      * @var string
  100.      * @Groups("list")
  101.      * @ORM\Column(name="height", type="string", nullable=true)
  102.      */
  103.     private $height "";
  104.     /**
  105.      * @var string
  106.      * @Groups("list")
  107.      * @ORM\Column(name="weight", type="string", nullable=true)
  108.      */
  109.     private $weight "";
  110.     /**
  111.      * @ORM\OneToMany(targetEntity="PatientIbeacon", mappedBy="patient", cascade={"persist", "remove"})
  112.      */
  113.     private $ibeacons = [];
  114.     /**
  115.      * @ORM\OneToMany(targetEntity="PatientLost", mappedBy="patient", cascade={"persist", "remove"})
  116.      */
  117.     private $losts;
  118.     /**
  119.      * @ORM\Column(name="record_status", type="boolean")
  120.      * @Groups("list")
  121.      * @var bool
  122.      */
  123.     private $enabled true;
  124.     /**
  125.      * @var string
  126.      * @Groups("list")
  127.      * @ORM\Column(name="patient_status", type="string", nullable=true)
  128.      */
  129.     private $patientStatus self::STATUS_NORMAL;
  130.     /**
  131.      * @var null|array
  132.      * @Groups("list")
  133.      * @ORM\Column(name="last_api_location", type="json", nullable=true)
  134.      */
  135.     private $lastAPILocation = [];
  136.     /**
  137.      * @var null|array
  138.      * @Groups("list")
  139.      * @ORM\Column(name="last_api_seen_time", type="json", nullable=true)
  140.      */
  141.     private $lastAPISeenTime = [];
  142.     /**
  143.      * @var null|string
  144.      * @Groups("list")
  145.      * @ORM\Column(name="quit_reason", type="string", nullable=true)
  146.      */
  147.     private $quitReason "";
  148.     /**
  149.      * @ORM\Column(name="quit_ts", type="datetime", nullable=true)
  150.      * @Groups("list")
  151.      * @var null|\DateTimeInterface
  152.      */
  153.     private $quitedAt;
  154.     /**
  155.      * @ORM\Version
  156.      * @ORM\Column(type="integer")
  157.      */
  158.     private $version;
  159.     public function getId()
  160.     {
  161.         return $this->id;
  162.     }
  163.     public function getVersion()
  164.     {
  165.         return $this->version;
  166.     }
  167.     /**
  168.      * @return mixed
  169.      */
  170.     public function getCaregiver()
  171.     {
  172.         return $this->caregiver;
  173.     }
  174.     /**
  175.      * @param mixed $caregiver
  176.      */
  177.     public function setCaregiver($caregiver)
  178.     {
  179.         $this->caregiver $caregiver;
  180.     }
  181.     /**
  182.      * @Groups("list")
  183.      * @return string
  184.      */
  185.     public function getFirstName()
  186.     {
  187.         return $this->firstname;
  188.     }
  189.     /**
  190.      * @param string $firstname
  191.      */
  192.     public function setFirstName(string $firstname)
  193.     {
  194.         $this->firstname $firstname;
  195.     }
  196.     /**
  197.      * @Groups("list")
  198.      * @return string
  199.      */
  200.     public function getLastName()
  201.     {
  202.         return $this->lastname;
  203.     }
  204.     /**
  205.      * @param string $lastname
  206.      */
  207.     public function setLastName(string $lastname)
  208.     {
  209.         $this->lastname $lastname;
  210.     }
  211.     /**
  212.      * @Groups("list")
  213.      * @return string
  214.      */
  215.     public function getName()
  216.     {
  217.         return $this->lastname $this->firstname;
  218.     }
  219.     /**
  220.      * @return string
  221.      */
  222.     public function getBirthYear()
  223.     {
  224.         return $this->birthYear;
  225.     }
  226.     /**
  227.      * @param string $birthYear
  228.      */
  229.     public function setBirthYear(string $birthYear)
  230.     {
  231.         $this->birthYear $birthYear;
  232.     }
  233.     /**
  234.      * @return string
  235.      */
  236.     public function getGender()
  237.     {
  238.         return $this->gender;
  239.     }
  240.     /**
  241.      * @param string $gender
  242.      */
  243.     public function setGender(string $gender)
  244.     {
  245.         $this->gender $gender;
  246.     }
  247.     /**
  248.      * @return string
  249.      */
  250.     public function getLanguage()
  251.     {
  252.         return $this->language;
  253.     }
  254.     /**
  255.      * @param null|string $language
  256.      */
  257.     public function setLanguage($language)
  258.     {
  259.         $this->language $language;
  260.     }
  261.     /**
  262.      * @return string
  263.      */
  264.     public function getLanguageOther()
  265.     {
  266.         return $this->languageOther;
  267.     }
  268.     /**
  269.      * @param null|string $languageOther
  270.      */
  271.     public function setLanguageOther($languageOther)
  272.     {
  273.         $this->languageOther $languageOther;
  274.     }
  275.     /**
  276.      * @return string
  277.      */
  278.     public function getHeight()
  279.     {
  280.         return $this->height;
  281.     }
  282.     /**
  283.      * @param null|string $height
  284.      */
  285.     public function setHeight($height)
  286.     {
  287.         $this->height $height;
  288.     }
  289.     /**
  290.      * @return string
  291.      */
  292.     public function getWeight()
  293.     {
  294.         return $this->weight;
  295.     }
  296.     /**
  297.      * @param null|string $weight
  298.      */
  299.     public function setWeight($weight)
  300.     {
  301.         $this->weight $weight;
  302.     }
  303.     /**
  304.      * @return bool
  305.      */
  306.     public function isEnabled(): bool
  307.     {
  308.         return $this->enabled;
  309.     }
  310.     /**
  311.      * @param bool $enabled
  312.      */
  313.     public function setEnabled(bool $enabled): void
  314.     {
  315.         if ($this->isPassAway()) {
  316.             $this->enabled false;
  317.         } else {
  318.             $this->enabled $enabled;
  319.         }
  320.     }
  321.     /**
  322.      * @Groups("list")
  323.      * @return mixed
  324.      */
  325.     public function getPatientIbeacons()
  326.     {
  327.         return $this->getActiveIbeacons();
  328.     }
  329.     /**
  330.      * @Groups("list")
  331.      * @return mixed
  332.      */
  333.     public function getPatientLosts()
  334.     {
  335.         return $this->losts;
  336.     }
  337.     /**
  338.      * @return string
  339.      */
  340.     public function getPatientStatus()
  341.     {
  342.         if ($this->isLost()) {
  343.             return self::STATUS_LOST;
  344.         }
  345.         return $this->patientStatus;
  346.     }
  347.     /**
  348.      * @param string $patientStatus
  349.      */
  350.     public function setPatientStatus(string $patientStatus)
  351.     {
  352.         $this->patientStatus $patientStatus;
  353.         if  ($patientStatus == self::STATUS_QUIT) {
  354.             $this->quitService();
  355.             $this->setQuitedAt(new \DateTime('now'));
  356.         }
  357.     }
  358.     /**
  359.      * @param null|array $lastAPILocation
  360.      */
  361.     public function setLastAPILocation($lastAPILocation)
  362.     {
  363.         $this->lastAPILocation $lastAPILocation;
  364.     }
  365.     /**
  366.      * @param null|array $lastAPISeenTime
  367.      */
  368.     public function setLastAPISeenTime($lastAPISeenTime)
  369.     {
  370.         $this->lastAPISeenTime $lastAPISeenTime;
  371.     }
  372.     /**
  373.      * @return null|array
  374.      */
  375.     public function getLastAPILocation()
  376.     {
  377.         $lastAPILocations = [];
  378.         if (count($this->getActiveIbeacons())) {
  379.             foreach ($this->getActiveIbeacons() as $ibeacon) {
  380.                 if ($ibeacon) {
  381.                     $lastAPILocations[$ibeacon->getName()] = $ibeacon->getLastAPILocation();
  382.                 }
  383.             }
  384.         }
  385.         return $lastAPILocations;
  386.     }
  387.     /**
  388.      * @return null|array
  389.      */
  390.     public function getLastAPISeenTime()
  391.     {
  392.         $lastAPISeenTimes = [];
  393.         foreach ($this->getActiveIbeacons() as $ibeacon) {
  394.             if ($ibeacon) {
  395.                 $lastAPISeenTimes[$ibeacon->getName()] = $ibeacon->getLastAPISeenTime();
  396.             }
  397.         }
  398.         return $lastAPISeenTimes;
  399.     }
  400.     /**
  401.      * @return null|string
  402.      */
  403.     public function getQuitReason()
  404.     {
  405.         return $this->quitReason;
  406.     }
  407.     /**
  408.      * @param null|string $quitReason
  409.      */
  410.     public function setQuitReason($quitReason)
  411.     {
  412.         $this->quitReason $quitReason;
  413.     }
  414.     /**
  415.      * @return null|\DateTimeInterface
  416.      */
  417.     public function getQuitedAt()
  418.     {
  419.         return $this->quitedAt;
  420.     }
  421.     /**
  422.      * @param null|\DateTimeInterface $quitedAt
  423.      */
  424.     public function setQuitedAt(\DateTimeInterface $quitedAt)
  425.     {
  426.         $this->quitedAt $quitedAt;
  427.     }
  428.     public function isNormal(): bool
  429.     {
  430.         if ($this->isLost()) {
  431.             return false;
  432.         }
  433.         return $this->patientStatus == Patient::STATUS_NORMAL;
  434.     }
  435.     public function isLost() : bool
  436.     {
  437.         if ($this->losts) {
  438.             foreach ($this->losts as $lost) {
  439.                 if (!$lost->isFound()) {
  440.                     return true;
  441.                 }
  442.             }
  443.         }
  444.         return false;
  445.     }
  446.     public function isPassAway(): bool
  447.     {
  448.         return $this->patientStatus == Patient::STATUS_PASS_AWAY;
  449.     }
  450.     public function quitService()
  451.     {
  452.         $this->enabled false;
  453.         $this->patientStatus self::STATUS_QUIT;
  454.         foreach ($this->ibeacons as $ibeacon) {
  455.             $ibeacon->setEnabled(false);
  456.         }
  457.     }
  458.     public function getActiveIbeacons()
  459.     {
  460.         $activeIbeacons = [];
  461.         if (count($this->ibeacons)) {
  462.             foreach ($this->ibeacons as $ibeacon) {
  463.                 if ($ibeacon && $ibeacon->isEnabled()) {
  464.                     $activeIbeacons[] = $ibeacon;
  465.                 }
  466.             }
  467.         }
  468.         return $activeIbeacons;
  469.     }
  470.     public function ibeaconsForExport()
  471.     {
  472.         return $this->ibeacons;
  473.     }
  474.     public function exportContent()
  475.     {
  476. //        $content = "<table border='1'><tr>";
  477.         $content $this->getCaregiver()->exportContent();
  478.         $content .= "<td align='left'>" $this->getName() . "</td>";
  479.         $content .= "<td align='left'>" $this->getPatientStatus() . "</td>";
  480.         $content .= "<td align='left'>" $this->getBirthYear() . "</td>";
  481.         $content .= "<td align='left'>" $this->getGender() . "</td>";
  482.         $content .= "<td align='left'>" $this->getLanguage() . ($this->getLanguageOther() ? ": " $this->getLanguageOther() : "") . "</td>";
  483.         $content .= "<td align='left'>" $this->getHeight() . "</td>";
  484.         $content .= "<td align='left'>" $this->getWeight() . "</td>";
  485.         $content .= "<td align='left'>" . ($this->checkDummy($this->getBody()) ? "" $this->getBody()) . "</td>";
  486.         $content .= "<td align='left'>" . ($this->checkDummy($this->getHair()) ? "" $this->getHair()) . "</td>";
  487.         $content .= "<td align='left'>" . ($this->checkDummy($this->getSkin()) ? "" $this->getSkin()) . "</td>";
  488.         $content .= "<td align='left'>" . ($this->checkDummy($this->getFace()) ? "" $this->getFace()) . "</td>";
  489.         $content .= "<td align='left'>" $this->getOther() . "</td>";
  490.         $content .= "<td align='left'>" . ($this->isIdentity() ? "是" "否") . "</td>";
  491.         $content .= "<td align='left'>" $this->getIdentityValue() . "</td>";
  492.         $content .= "<td align='left'>" $this->getMovement() . "</td>";
  493.         $content .= "<td align='left'>" . ($this->isTool() ? "是" "否") . "</td>";
  494.         $content .= "<td align='left'>" $this->getToolMain() . "</td>";
  495.         $content .= "<td align='left'>" $this->getToolSub() . "</td>";
  496.         $content .= "<td align='left'>" . ($this->checkDummy($this->getLocation()) ? "" $this->getLocation()) . ($this->getLocationOther() ? ": " $this->getLocationOther() : "") . "</td>";
  497.         $content .= "<td align='left'>" $this->getCharacteristic() . "</td>";
  498.         $content .= "<td align='left'>" $this->getCommunication() . "</td>";
  499.         $content .= "<td align='left'>" . ($this->checkDummy($this->getHealth()) ? "" $this->getHealth()) . "</td>";
  500.         $content .= "<td align='left'>" $this->getImageName() . "</td>";
  501.         $content .= "<td align='left'>" $this->getCreatedAt()->format('Y-m-d H:i') . "</td>";
  502.         $content .= "<td align='left'>" $this->getUpdatedAt()->format('Y-m-d H:i') . "</td>";
  503. //        $content .= "</tr></table>";
  504.         return $content;
  505.     }
  506.     /**
  507.      * Persists the minimum amount of properties required to refresh the user
  508.      * in the next request and ensure correct AbstractToken::hasUserChanged()
  509.      * comparison.
  510.      *
  511.      * @return string|null
  512.      */
  513.     public function serialize()
  514.     {
  515.         return serialize([
  516.             'id' => $this->id,
  517.             'firstname' => $this->firstname,
  518.             'lastname' => $this->lastname,
  519.         ]);
  520.     }
  521.     public function unserialize($data)
  522.     {
  523.         $data unserialize($data);
  524.         $this->id $data['id'];
  525.         $this->firstname $data['firstname'];
  526.         $this->lastname $data['lastname'];
  527.     }
  528.     public function __toString()
  529.     {
  530.         return $this->getName();
  531.     }
  532. }