VoteService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Services\Appearance;
  3. use App\Models\Appearance\AppearanceModel;
  4. use App\Models\Appearance\AppearanceVoteModel;
  5. use App\Models\Appearance\PartnerRankModel;
  6. use App\Models\Appearance\SchoolRankModel;
  7. use App\Models\SchoolModel;
  8. class VoteService
  9. {
  10. public $pick_partner = false;
  11. public $pick_school = false;
  12. /**
  13. * 投票卡片
  14. * @param $uid
  15. * @param $partnerId
  16. * @param $mediaId
  17. */
  18. public function votePartner($uid, $partnerId, $mediaId)
  19. {
  20. if (
  21. !AppearanceVoteModel::where('uid', $uid)
  22. ->where('type', '2')
  23. ->where('partner_id', $partnerId)
  24. ->today()
  25. ->first()
  26. ) {
  27. $appearance = AppearanceModel::where('partner_id', $partnerId)->firstOrFail();
  28. AppearanceVoteModel::create([
  29. 'uid' => $uid,
  30. 'type' => '2',
  31. 'partner_id' => $partnerId,
  32. 'media_id' => $mediaId,
  33. ]);
  34. // 月榜
  35. $type = 'monthly';
  36. $date = date("Y-m-01");
  37. if (
  38. $partnerRank = PartnerRankModel::where('partner_id', $partnerId)
  39. ->where('date', $date)
  40. ->where('type', $type)
  41. ->first()
  42. ) {
  43. $partnerRank->increment('count');
  44. } else {
  45. PartnerRankModel::create([
  46. 'type' => $type,
  47. 'sex' => $appearance->sex,
  48. 'partner_id' => $partnerId,
  49. 'count' => 1,
  50. 'date' => $date,
  51. 'school' => $appearance->school,
  52. ]);
  53. }
  54. // 周榜
  55. $type = 'weekly';
  56. $date = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  57. if (
  58. $partnerRank = PartnerRankModel::where('partner_id', $partnerId)
  59. ->where('date', $date)
  60. ->where('type', $type)
  61. ->first()
  62. ) {
  63. $partnerRank->increment('count');
  64. } else {
  65. PartnerRankModel::create([
  66. 'type' => $type,
  67. 'sex' => $appearance->sex,
  68. 'partner_id' => $partnerId,
  69. 'count' => 1,
  70. 'date' => $date,
  71. 'school' => $appearance->school,
  72. ]);
  73. }
  74. $this->pick_partner = true;
  75. $this->voteSchool($uid, $appearance->school, $mediaId);
  76. }
  77. }
  78. /**
  79. * 投票学校
  80. * @param $uid
  81. * @param $school
  82. * @param $mediaId
  83. */
  84. public function voteSchool($uid, $school, $mediaId)
  85. {
  86. if (
  87. !AppearanceVoteModel::where('uid', $uid)
  88. ->where('type', '1')
  89. ->where('school', $school)
  90. ->today()
  91. ->first()
  92. ) {
  93. $schoolModel = SchoolModel::where('school', $school)->firstOrFail();
  94. AppearanceVoteModel::create([
  95. 'uid' => $uid,
  96. 'type' => '1',
  97. 'school' => $schoolModel->school,
  98. 'media_id' => $mediaId,
  99. ]);
  100. // 月榜
  101. $type = 'monthly';
  102. $date = date("Y-m-01");
  103. if (
  104. $schoolRank = SchoolRankModel::where('school', $schoolModel->school)
  105. ->where('date', $date)
  106. ->where('type', $type)
  107. ->first()
  108. ) {
  109. $schoolRank->increment('count');
  110. } else {
  111. SchoolRankModel::create([
  112. 'type' => $type,
  113. 'province' => $schoolModel->province,
  114. 'city' => $schoolModel->city,
  115. 'count' => 1,
  116. 'date' => $date,
  117. 'school' => $schoolModel->school,
  118. ]);
  119. }
  120. // 周榜
  121. $type = 'weekly';
  122. $date = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  123. if (
  124. $partnerRank = SchoolRankModel::where('school', $schoolModel->school)
  125. ->where('date', $date)
  126. ->where('type', $type)
  127. ->first()
  128. ) {
  129. $partnerRank->increment('count');
  130. } else {
  131. SchoolRankModel::create([
  132. 'type' => $type,
  133. 'province' => $schoolModel->province,
  134. 'city' => $schoolModel->city,
  135. 'count' => 1,
  136. 'date' => $date,
  137. 'school' => $schoolModel->school,
  138. ]);
  139. }
  140. $this->pick_school = true;
  141. }
  142. }
  143. /**
  144. * 取消上架卡片
  145. * @param $uid
  146. */
  147. public function unApply($uid)
  148. {
  149. $appearance = AppearanceModel::where('uid', $uid)->firstOrFail();
  150. if ($appearance->state == 2) {
  151. $appearance->state = 1;
  152. $appearance->save();
  153. }
  154. }
  155. }