FlowerService.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Services\Pay;
  3. use App\Exceptions\AlertException;
  4. use App\Models\FlowerLogModel;
  5. use App\Models\User\UserModel;
  6. use App\Models\User\UserSysTagModel;
  7. use App\Services\Service;
  8. use Illuminate\Support\Facades\DB;
  9. class FlowerService extends Service
  10. {
  11. /**
  12. * 用户小花明细
  13. * @param int $uid
  14. * @param array $pages
  15. * @return array
  16. */
  17. public function logs(int $uid, array $pages)
  18. {
  19. $flm = new FlowerLogModel();
  20. $total = $flm->where([['uid', $uid], ['id', '>', 1790400]])->count();
  21. $res = $flm->where([['uid', $uid], ['id', '>', 1790400]])->orderBy(
  22. 'id',
  23. 'desc'
  24. )->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  25. return [
  26. 'page' => $pages['page'],
  27. 'total' => $total,
  28. 'limit' => $pages['limit'],
  29. 'list' => $res
  30. ];
  31. }
  32. /**
  33. * 通过小花提升人气值时长
  34. * @param int $uid
  35. * @return bool
  36. * @throws \Exception
  37. */
  38. public function payPopularity(int $uid)
  39. {
  40. $red_flower = 1;
  41. $user = UserModel::findOrFail($uid);
  42. if ($user->red_flower < $red_flower) {
  43. throw new AlertException("余额不足", 403);
  44. }
  45. DB::beginTransaction();
  46. try {
  47. $systag = UserSysTagModel::firstOrCreate(['uid' => $user->uid], ['popularity_flower_end_at' => 0]);
  48. $addtime = $red_flower * 3600 * 2;
  49. $popularity_flower_end_at = $systag->popularity_flower_end_at;
  50. if ($systag->popularity_flower_end_at > time()) {
  51. $systag->popularity_flower_end_at += $addtime;
  52. } else {
  53. $systag->popularity_flower_end_at = time() + $addtime;
  54. }
  55. $systag->save();
  56. UserModel::where('uid', $uid)->decrement('red_flower', $red_flower);
  57. DB::table('kdgx_partner_charge_pay_logs')->insert([
  58. 'uid' => $uid,
  59. 'create_at' => time(),
  60. 'type' => 14,
  61. 'red_flower' => 0 - $red_flower,
  62. 'gold_flower' => 0,
  63. 'remark' => "增加人气值时长"
  64. ]);
  65. DB::commit();
  66. return true;
  67. } catch (\Exception $exception) {
  68. DB::rollBack();
  69. throw $exception;
  70. }
  71. }
  72. }