GetVoiceModel.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Models\Gnight;
  3. use App\Models\Model;
  4. /**
  5. * App\Models\Gnight\GetVoiceModel
  6. *
  7. * @property int $id 主键,用户
  8. * @property \Illuminate\Support\Carbon $created_at 创建时间
  9. * @property \Illuminate\Support\Carbon|null $updated_at 更新时间
  10. * @property int $uid 用户
  11. * @property int $get_uid 获取的用户
  12. * @property int $lock 解锁进度
  13. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel newModelQuery()
  14. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel newQuery()
  15. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel query()
  16. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel whereCreatedAt($value)
  17. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel whereGetUid($value)
  18. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel whereId($value)
  19. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel whereLock($value)
  20. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel whereUid($value)
  21. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel whereUpdatedAt($value)
  22. * @mixin \Eloquent
  23. */
  24. class GetVoiceModel extends Model
  25. {
  26. protected $table = 'kdgx_gnight_get_voice';
  27. protected $dateFormat = 'U';
  28. protected $fillable = ['uid', 'get_uid', 'lock'];
  29. /**
  30. * 获取某人获得的语音
  31. * @param int $uid
  32. * @return \Illuminate\Support\Collection
  33. */
  34. public function voicesByUser(int $uid)
  35. {
  36. $voinces = $this->where('uid', $uid)->get();
  37. $users = UserModel::whereIn('uid', $voinces->pluck('get_uid')->toArray())->get([
  38. 'uid',
  39. 'headimgurl',
  40. 'nickname',
  41. 'sex',
  42. 'like',
  43. 'voice',
  44. 'voice_state',
  45. 'contact_type',
  46. 'contact'
  47. ]);
  48. foreach ($users as &$user) {
  49. $voince = $voinces->where('get_uid', $user->uid)->pop()->toArray();
  50. $user->get_voice_id = $voince['id'];
  51. $user->lock = $voince['lock'] ?? 0;
  52. if ($user->lock < 3) {
  53. unset($user->contact);
  54. }
  55. }
  56. return $users;
  57. }
  58. /**
  59. * 给某人推荐一条语音
  60. * @param int $uid
  61. * @return int 语音用户id
  62. * @throws \ApiException
  63. */
  64. public function pop(int $uid)
  65. {
  66. $getVoices = GetVoiceModel::where('uid', $uid)->get();
  67. $voices = $getVoices->pluck('get_uid')->toArray();
  68. array_push($voices, $uid);
  69. $user = UserModel::findOrFail($uid);
  70. if ($user->like == 0) {
  71. $likes = [0, 1, 2];
  72. } else {
  73. $likes = [$user->like];
  74. }
  75. $count = UserModel::whereNotIn('uid', $voices)->where('uid', '!=', $uid)->whereIn(
  76. 'sex',
  77. $likes
  78. )->where('voice_state', 1)->count();
  79. $rand = rand(0, $count - 1);
  80. $voice = UserModel::whereNotIn('uid', $voices)->where('uid', '!=', $uid)->whereIn(
  81. 'sex',
  82. $likes
  83. )->where('voice_state', 1)->skip($rand)->first();
  84. if (collect($voice)->isEmpty()) {
  85. throw new \ApiException("语音库空空如也", 401);
  86. }
  87. return $voice->uid;
  88. }
  89. }