UserScoreLog.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace app\admin\model;
  3. use think\model;
  4. use think\Exception;
  5. /**
  6. * UserScoreLog 模型
  7. * @controllerUrl 'userScoreLog'
  8. */
  9. class UserScoreLog extends model
  10. {
  11. protected $autoWriteTimestamp = 'int';
  12. protected $createTime = 'createtime';
  13. protected $updateTime = false;
  14. public static function onBeforeInsert($model)
  15. {
  16. $user = User::where('id', $model->user_id)->find();
  17. if (!$user) {
  18. throw new Exception("The user can't find it");
  19. }
  20. if (!$model->memo) {
  21. throw new Exception("Change note cannot be blank");
  22. }
  23. $model->before = $user->score;
  24. $model->after = $user->score + $model->score;
  25. }
  26. public static function onAfterInsert($model)
  27. {
  28. $user = User::where('id', $model->user_id)->find();
  29. if (!$user) {
  30. $model->delete();
  31. throw new Exception("The user can't find it");
  32. }
  33. $user->score += $model->score;
  34. $user->save();
  35. }
  36. public static function onBeforeDelete()
  37. {
  38. return false;
  39. }
  40. public function user()
  41. {
  42. return $this->belongsTo(User::class, 'user_id');
  43. }
  44. }