UserStateModel.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Models\User;
  3. use App\Exceptions\AlertException;
  4. use App\Models\Model;
  5. use Illuminate\Database\Eloquent\ModelNotFoundException;
  6. /**
  7. * App\Models\User\UserStateModel
  8. *
  9. * @property int $id
  10. * @property \Illuminate\Support\Carbon|null $created_at
  11. * @property \Illuminate\Support\Carbon|null $updated_at
  12. * @property int $uid 用户
  13. * @property string $key key
  14. * @property string $value value
  15. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel newModelQuery()
  16. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel newQuery()
  17. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel query()
  18. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel whereCreatedAt($value)
  19. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel whereId($value)
  20. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel whereKey($value)
  21. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel whereUid($value)
  22. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel whereUpdatedAt($value)
  23. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel whereValue($value)
  24. * @mixin \Eloquent
  25. */
  26. class UserStateModel extends Model
  27. {
  28. protected $table = "kdgx_fpdx_user_states";
  29. protected $fillable = ['uid', 'key', 'value'];
  30. public static $keys = array(
  31. 'pay_open_supvip' => 0,
  32. 'popularity_sign_end_at' => 0,
  33. 'algorithm' => null
  34. );
  35. /**
  36. * 获取
  37. * @param int $uid
  38. * @param string $key
  39. * @return mixed
  40. * @throws AlertException
  41. */
  42. public static function get(int $uid, string $key)
  43. {
  44. if (!in_array($key, self::$keys)) {
  45. throw new AlertException("错误的配置");
  46. }
  47. try {
  48. $model = self::where([['uid', $uid], ['key', $key]])->firstOrFail();
  49. return $model->value;
  50. } catch (ModelNotFoundException $exception) {
  51. return self::$keys[$key];
  52. }
  53. }
  54. /**
  55. * 设置
  56. * @param int $uid
  57. * @param string $key
  58. * @param string $value
  59. * @return bool
  60. * @throws AlertException
  61. */
  62. public static function set(int $uid, string $key, $value)
  63. {
  64. if (!in_array($key, self::$keys)) {
  65. throw new AlertException("错误的配置");
  66. }
  67. self::updateOrCreate(['uid' => $uid, 'key' => $key], [
  68. 'uid' => $uid,
  69. 'key' => $key,
  70. 'value' => (string)$value
  71. ]);
  72. return true;
  73. }
  74. /**
  75. * 增加
  76. * @param int $uid
  77. * @param string $key
  78. * @param string $value
  79. * @param bool $now 是否以现在作为基准
  80. * @return bool
  81. * @throws AlertException
  82. */
  83. public static function inc(int $uid, string $key, $value, bool $now = false)
  84. {
  85. if (!in_array($key, self::$keys)) {
  86. throw new AlertException("错误的配置");
  87. }
  88. $state = self::where([['uid', $uid], ['key', $key]])->first();
  89. if (collect($state)->isEmpty()) {
  90. if ($now) {
  91. $v = time() + $value;
  92. } else {
  93. $v = self::$keys[$key] + $value;
  94. }
  95. self::create(['uid' => $uid, 'key' => $key, 'value' => $v]);
  96. } else {
  97. $v = intval($state->value) + intval($value);
  98. if ($now && intval($state->value) < time()) {
  99. $v = time() + $value;
  100. }
  101. $state->value = $v;
  102. $state->save();
  103. }
  104. return true;
  105. }
  106. }