Activity.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Models;
  3. use Encore\Admin\Auth\Database\Administrator;
  4. use Encore\Admin\Facades\Admin;
  5. use Exception;
  6. use Illuminate\Database\Eloquent\Model;
  7. class Activity extends Model
  8. {
  9. public static $mode = 'api';
  10. //
  11. public static function boot ()
  12. {
  13. parent::boot();
  14. if(self::$mode == 'admin'){
  15. static::creating(function ($model){
  16. $model->creator = Admin::user()->id;
  17. });
  18. }
  19. if(self::$mode == 'admin'){
  20. static::updating(function ($model){
  21. $model->updator = Admin::user()->id;
  22. });
  23. }
  24. }
  25. public function setExtAttribute($ext)
  26. {
  27. if (is_array($ext)) {
  28. $this->attributes['ext'] = json_encode($ext);
  29. }
  30. }
  31. public function getExtAttribute($ext)
  32. {
  33. return json_decode($ext, true);
  34. }
  35. /**
  36. * 创建活动
  37. *
  38. *
  39. *
  40. */
  41. public static function store($data, $id = 0)
  42. {
  43. // var_dump($data['ext']);exit;
  44. self::$mode = 'api';
  45. if($id) {
  46. $model = self::find($id);
  47. if(!$model) {
  48. throw new Exception('该信息不存在');
  49. }
  50. }else {
  51. $model = new self();
  52. }
  53. $model->organiz_type = $data['organiz_type'];
  54. $model->activity_range_id = $data['activity_range_id'];
  55. $model->organiz_name = $data['organiz_name'];
  56. $model->title = $data['title'];
  57. $model->member_num = $data['member_num'];
  58. $model->content = $data['content'];
  59. $model->active_time = $data['activity_time']??'';
  60. $model->belong = $data['belong'];
  61. $model->member = $data['member'];
  62. $model->ext = $data['ext'];
  63. $res = $model->save();
  64. if($res) {
  65. return ["id" => $model->id];
  66. } else {
  67. return [];
  68. }
  69. }
  70. public function creater()
  71. {
  72. return $this->belongsTo(Administrator::class, 'creator');
  73. }
  74. public function updater()
  75. {
  76. return $this->belongsTo(Administrator::class, 'updator');
  77. }
  78. public function range()
  79. {
  80. return $this->belongsTo(ActivityRange::class, 'activity_range_id');
  81. }
  82. public function workstation()
  83. {
  84. return $this->belongsTo(ActivityWorkStation::class, 'belong','work_station_id');
  85. }
  86. }