Vaccine.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Models;
  3. class Vaccine extends BaseModel
  4. {
  5. const FREETYPE = 1, NOFREETYPE =2;
  6. protected $appends = ['use_count','appoint_count'];
  7. protected static $_type = [
  8. self::FREETYPE => 'Ⅰ类',
  9. self::NOFREETYPE => 'Ⅱ类',
  10. ];
  11. public static function getType(){
  12. return self::$_type;
  13. }
  14. //导入格式
  15. protected $fillable = ['type','price',"name","remark","supplier",'stock','states'];
  16. public function organization()
  17. {
  18. return $this->belongsToMany(Organization::class,'org_id');
  19. }
  20. protected function getUseCountAttribute()
  21. {
  22. $id = $this->attributes['id'];
  23. if(empty($id)) return 0;
  24. return OrderVaccine::where(['vaccine_id'=>$id])->whereHas('orders',function($query){
  25. $query->where('order_status',4);
  26. })->count();
  27. }
  28. protected function getAppointCountAttribute()
  29. {
  30. $id = $this->attributes['id'];
  31. if(empty($id)) return 0;
  32. return OrderVaccine::where(['vaccine_id'=>$id])->whereHas('orders',function($query){
  33. $query->where('order_status',3);
  34. })->count();
  35. }
  36. public function organizationvaccines(){
  37. return $this->hasMany(OrganizationVaccines::class,'vaccine_id');
  38. }
  39. public function vaccineAppoint()
  40. {
  41. return $this->hasMany(VaccineAppoint::class);
  42. }
  43. }