UserGroup.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 会员组模型
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\model\user;
  15. use think\Model;
  16. class UserGroup extends Model
  17. {
  18. protected $autoWriteTimestamp = true;
  19. public static function onBeforeInsert($row)
  20. {
  21. $maxPoint = self::max('point');
  22. if ($row->point <= $maxPoint) {
  23. throw new \Exception("积分必须大于当前最大积分({$maxPoint})");
  24. }
  25. }
  26. public static function onBeforeUpdate($row)
  27. {
  28. $prevPoint = self::where('id', '<', $row->id)
  29. ->order('point', 'desc')
  30. ->value('point');
  31. if ($prevPoint && $row->point <= $prevPoint) {
  32. throw new \Exception("积分必须大于上一个等级积分({$prevPoint})");
  33. }
  34. $nextPoint = self::where('id', '>', $row->id)
  35. ->order('point', 'asc')
  36. ->value('point');
  37. if ($nextPoint && $row->point >= $nextPoint) {
  38. throw new \Exception("积分必须小于下一个等级积分({$nextPoint})");
  39. }
  40. }
  41. public static function onbeforeDelete($row)
  42. {
  43. if ($row['issystem']) {
  44. throw new \Exception('系统用户组[' . $row['name'] . ']不能删除!');
  45. }
  46. }
  47. public function user()
  48. {
  49. return $this->belongsTo('User', 'id', 'group_id');
  50. }
  51. }