RankService.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace App\Services\Appearance;
  3. use App\Exceptions\AlertException;
  4. use App\Models\Appearance\AppearanceModel;
  5. use App\Models\Appearance\PartnerRankModel;
  6. use App\Models\Fpdx\MediaModel;
  7. use App\Models\PartnerModel;
  8. use App\Models\SchoolModel;
  9. use Illuminate\Database\QueryException;
  10. use Illuminate\Support\Facades\DB;
  11. class RankService
  12. {
  13. /**
  14. * 学校第一名
  15. * @param $type
  16. * @param $date
  17. * @param $schools
  18. * @return array
  19. */
  20. public function getSchoolsFirstPartner($type, $date, array $schools)
  21. {
  22. $build = DB::table("fpdx_appearance_partner_ranks as r")
  23. ->join("fpdx_appearances as a", 'r.partner_id', '=', 'a.partner_id')
  24. ->where(['r.date' => $date, 'r.type' => $type, 'r.sex' => 1])
  25. ->where("a.state", 2)
  26. ->whereIn('r.school', $schools)
  27. ->orderBy("r.count", "desc")
  28. ->select(['r.partner_id', 'r.school', 'r.count']);
  29. $menSchools = DB::table(DB::raw("({$build->toSql()}) as tab"))->setBindings($build->getBindings())->groupBy('school')->get()->toArray();
  30. $menSchools = array_combine(array_column($menSchools, 'partner_id'), $menSchools);
  31. $menPartners = PartnerModel::whereIn('id', array_keys($menSchools))->get(['id', 'photo_src'])->toArray();
  32. $menPartners = array_combine(array_column($menPartners, 'id'), $menPartners);
  33. array_walk($menSchools, function (&$value) use ($menPartners) {
  34. $value = json_decode(json_encode($value), true);
  35. $value['partner'] = $menPartners[$value['partner_id']];
  36. });
  37. $men = array_combine(array_column($menSchools, 'school'), $menSchools);
  38. $build = DB::table("fpdx_appearance_partner_ranks as r")
  39. ->join("fpdx_appearances as a", 'r.partner_id', '=', 'a.partner_id')
  40. ->where(['r.date' => $date, 'r.type' => $type, 'r.sex' => 2])
  41. ->where("a.state", 2)
  42. ->whereIn('r.school', $schools)
  43. ->orderBy("r.count", "desc")
  44. ->select(['r.partner_id', 'r.school', 'r.count']);
  45. $womenSchools = DB::table(DB::raw("({$build->toSql()}) as tab"))->setBindings($build->getBindings())->groupBy('school')->get()->toArray();
  46. $womenSchools = array_combine(array_column($womenSchools, 'partner_id'), $womenSchools);
  47. $womenPartners = PartnerModel::whereIn('id', array_keys($womenSchools))->get(['id', 'photo_src'])->toArray();
  48. $womenPartners = array_combine(array_column($womenPartners, 'id'), $womenPartners);
  49. array_walk($womenSchools, function (&$value) use ($womenPartners) {
  50. $value = json_decode(json_encode($value), true);
  51. $value['partner'] = $womenPartners[$value['partner_id']];
  52. });
  53. $women = array_combine(array_column($womenSchools, 'school'), $womenSchools);
  54. $schoolsRank = array();
  55. array_map(function ($value) use (&$schoolsRank, $men, $women) {
  56. $tmp = array(
  57. 'men' => isset($men[$value]) ? $men[$value] : "",
  58. 'women' => isset($women[$value]) ? $women[$value] : "",
  59. );
  60. $schoolsRank[$value] = $tmp;
  61. }, $schools);
  62. return $schoolsRank;
  63. }
  64. /**
  65. * 学校第一名
  66. * @param $type
  67. * @param $date
  68. * @param $school
  69. * @return array
  70. */
  71. public function getPartners($type, $date, $school)
  72. {
  73. $men_partner_id = \DB::table("fpdx_appearance_partner_ranks as r")
  74. ->join("fpdx_appearances as a", 'r.partner_id', '=', 'a.partner_id')
  75. ->where([
  76. 'r.date' => $date,
  77. 'r.type' => $type,
  78. 'r.school' => $school,
  79. 'r.sex' => 1,
  80. ])->where("a.state", 2)
  81. ->orderBy("r.count", "desc")
  82. ->value('r.partner_id');
  83. $men = PartnerModel::where('id', $men_partner_id)->value('photo_src');
  84. $women_partner_id = \DB::table("fpdx_appearance_partner_ranks as r")
  85. ->join("fpdx_appearances as a", 'r.partner_id', '=', 'a.partner_id')
  86. ->where([
  87. 'r.date' => $date,
  88. 'r.type' => $type,
  89. 'r.school' => $school,
  90. 'r.sex' => 2,
  91. ])->where("a.state", 2)
  92. ->orderBy("r.count", "desc")
  93. ->value('r.partner_id');
  94. $women = PartnerModel::where('id', $women_partner_id)->value('photo_src');
  95. $partners = [
  96. 'men' => $men ? "https://oss.pocketuniversity.cn" . $men : '',
  97. 'women' => $women ? "https://oss.pocketuniversity.cn" . $women : '',
  98. ];
  99. return $partners;
  100. }
  101. /**
  102. * 获取学校
  103. */
  104. public function getSchoolFirstMedia($school)
  105. {
  106. $media_id = \DB::table('pocket.kdgx_activity_enrolls')->where('state', 1)->where(
  107. 'school',
  108. $school
  109. )->orderBy('pick', 'desc')->value('media_id');
  110. return MediaModel::select('public_name', 'head_img')->where('media_id', $media_id)->first();
  111. }
  112. /**
  113. * 设置上榜
  114. * @param $uid
  115. * @param bool $bool 是否主动上榜,被动时下榜(状态1)不置为上榜(状态2)
  116. * @return bool
  117. * @throws AlertException
  118. */
  119. public function passive($uid, $bool = false)
  120. {
  121. $appearance = AppearanceModel::where('uid', $uid)->first();
  122. $partner = PartnerModel::where('uid', $uid)->first();
  123. if (!$partner) {
  124. return false;
  125. }
  126. $school = SchoolModel::where('school', $partner->school)->first();
  127. // 满足上榜条件
  128. if (
  129. $partner
  130. && $school
  131. && $partner->sex
  132. && $partner->is_sell == 1
  133. ) {
  134. try {
  135. if (!$appearance) {
  136. AppearanceModel::create([
  137. 'uid' => $uid,
  138. 'partner_id' => $partner->id,
  139. 'sex' => $partner->sex,
  140. 'province' => $school->province,
  141. 'city' => $school->city,
  142. 'school' => $school->school,
  143. 'state' => 2,
  144. ]);
  145. } else {
  146. // 同步学校
  147. if ($appearance->school != $school->school) {
  148. $appearance->province = $school->province;
  149. $appearance->city = $school->city;
  150. $appearance->school = $school->school;
  151. }
  152. // 同步性别
  153. if ($appearance->sex != $partner->sex) {
  154. $appearance->sex = $partner->sex;
  155. }
  156. // 修改状态为上榜
  157. if ($appearance->state != 1 || $bool) {
  158. $appearance->state = 2;
  159. }
  160. $appearance->save();
  161. }
  162. } catch (QueryException $e) {
  163. throw new AlertException("操作太快了");
  164. }
  165. // 初始月榜
  166. $type = 'monthly';
  167. $date = date("Y-m-01");
  168. if (
  169. !PartnerRankModel::where('partner_id', $partner->id)
  170. ->where('date', $date)
  171. ->where('type', $type)
  172. ->first()
  173. ) {
  174. PartnerRankModel::create([
  175. 'type' => $type,
  176. 'sex' => $partner->sex,
  177. 'partner_id' => $partner->id,
  178. 'count' => $partner->praises ?: 1,
  179. 'date' => $date,
  180. 'school' => $partner->school,
  181. ]);
  182. }
  183. // 初始周榜
  184. $type = 'weekly';
  185. $date = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  186. if (
  187. !PartnerRankModel::where('partner_id', $partner->id)
  188. ->where('date', $date)
  189. ->where('type', $type)
  190. ->first()
  191. ) {
  192. PartnerRankModel::create([
  193. 'type' => $type,
  194. 'sex' => $partner->sex,
  195. 'partner_id' => $partner->id,
  196. 'count' => $partner->praises ?: 1,
  197. 'date' => $date,
  198. 'school' => $partner->school,
  199. ]);
  200. }
  201. return true;
  202. } else {
  203. // 上架
  204. if ($appearance) {
  205. $appearance->state = -1;
  206. $appearance->save();
  207. }
  208. return false;
  209. }
  210. }
  211. }