UserMoneyLog.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\admin\model;
  3. use think\model;
  4. use think\Exception;
  5. /**
  6. * UserMoneyLog 模型
  7. * @controllerUrl 'userMoneyLog'
  8. */
  9. class UserMoneyLog 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->money;
  24. $model->after = $user->money + $model->money;
  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->money += $model->money;
  34. $user->save();
  35. }
  36. public static function onBeforeDelete()
  37. {
  38. return false;
  39. }
  40. public function getMoneyAttr($value)
  41. {
  42. return bcdiv($value, 100, 2);
  43. }
  44. public function setMoneyAttr($value)
  45. {
  46. return bcmul($value, 100, 2);
  47. }
  48. public function getBeforeAttr($value)
  49. {
  50. return bcdiv($value, 100, 2);
  51. }
  52. public function setBeforeAttr($value)
  53. {
  54. return bcmul($value, 100, 2);
  55. }
  56. public function getAfterAttr($value)
  57. {
  58. return bcdiv($value, 100, 2);
  59. }
  60. public function setAfterAttr($value)
  61. {
  62. return bcmul($value, 100, 2);
  63. }
  64. public function user()
  65. {
  66. return $this->belongsTo(User::class, 'user_id');
  67. }
  68. }