AppearanceController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Appearance\AppearanceModel;
  4. use App\Models\Appearance\PartnerRankModel;
  5. use App\Models\Appearance\SchoolRankModel;
  6. use App\Models\User\UserModel;
  7. use App\Models\SchoolModel;
  8. use Illuminate\Http\Request;
  9. use App\Http\Controllers\Controller;
  10. class AppearanceController extends Controller
  11. {
  12. //
  13. public function province(Request $request)
  14. {
  15. $page = $request->get('page', 1);
  16. $per_page = $request->get('per_page', 20);
  17. $type = 'monthly';
  18. $date = $request->get("monthly", date("Y-m-01"));
  19. $date = "2019-05-01";
  20. $builder = SchoolRankModel::where('date', $date)
  21. ->where('type', $type)
  22. ->when($request->filled('province'), function ($query) use ($request) {
  23. return $query->where('province', $request->province);
  24. })->when($request->school, function ($query) use ($request) {
  25. return $query->where('school', $request->school);
  26. });
  27. $total = $builder->count();
  28. $ranks = $builder->orderBy('count', 'desc')->offset(($page - 1) * $per_page)->limit($per_page)->get();
  29. $last_page = ceil($total / $per_page);
  30. $meta = [
  31. 'current_page' => $page,
  32. 'per_page' => $per_page,
  33. 'last_page' => $last_page,
  34. 'total' => $total
  35. ];
  36. return response([
  37. 'code' => 200,
  38. 'data' => $ranks,
  39. 'meta' => $meta,
  40. ]);
  41. }
  42. public function school(Request $request)
  43. {
  44. $school = $request->get('school');
  45. $sex = $request->get('sex', 2);
  46. $page = $request->get('page', 1);
  47. $per_page = $request->get('per_page', 20);
  48. $type = 'monthly';
  49. $date = $request->get("date", date("Y-m-01"));
  50. $date = "2019-05-01";
  51. $builder = \DB::table("fpdx_appearance_partner_ranks as r")
  52. ->selectRaw("r.partner_id, r.sex, r.school, r.count")
  53. ->join("fpdx_appearances as a", "r.partner_id", '=', 'a.partner_id')
  54. ->where('r.date', $date)
  55. ->where('r.type', $type)
  56. ->where('r.school', $school)
  57. ->where('r.sex', $sex)
  58. ->where('a.state', 2);
  59. $total = $builder->count();
  60. $ranks = $builder->orderBy('count', 'desc')->offset(($page - 1) * $per_page)->limit($per_page)->get();
  61. foreach ($ranks as $rank) {
  62. $rank->user = UserModel::select('nickname', 'headimgurl')->where('partner_id', $rank->partner_id)->first();
  63. }
  64. $last_page = ceil($total / $per_page);
  65. $meta = [
  66. 'current_page' => $page,
  67. 'per_page' => $per_page,
  68. 'last_page' => $last_page,
  69. 'total' => $total,
  70. ];
  71. return response([
  72. 'code' => 200,
  73. 'data' => $ranks,
  74. 'meta' => $meta,
  75. ]);
  76. }
  77. /**
  78. * 学校增量
  79. * @param Request $request
  80. * @param $id
  81. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  82. */
  83. public function incrementSchool(Request $request)
  84. {
  85. $this->validate($request, [
  86. 'school' => 'required',
  87. 'count' => 'integer',
  88. ]);
  89. $school = $request->input('school');
  90. $count = $request->input('count', 1);
  91. $schoolModel = SchoolModel::where('school', $school)->firstOrFail();
  92. // 月榜
  93. $type = 'monthly';
  94. $date = date("Y-m-01");
  95. if (
  96. $schoolRank = SchoolRankModel::where('school', $schoolModel->school)
  97. ->where('date', $date)
  98. ->where('type', $type)
  99. ->first()
  100. ) {
  101. $schoolRank->increment('count', $count);
  102. } else {
  103. SchoolRankModel::create([
  104. 'type' => $type,
  105. 'province' => $schoolModel->province,
  106. 'city' => $schoolModel->city,
  107. 'count' => $count,
  108. 'date' => $date,
  109. 'school' => $schoolModel->school,
  110. ]);
  111. }
  112. // 活动榜
  113. $type = 'monthly';
  114. $date = date("Y-05-01");
  115. if (
  116. $schoolRank = SchoolRankModel::where('school', $schoolModel->school)
  117. ->where('date', $date)
  118. ->where('type', $type)
  119. ->first()
  120. ) {
  121. $schoolRank->increment('count', $count);
  122. } else {
  123. SchoolRankModel::create([
  124. 'type' => $type,
  125. 'province' => $schoolModel->province,
  126. 'city' => $schoolModel->city,
  127. 'count' => $count,
  128. 'date' => $date,
  129. 'school' => $schoolModel->school,
  130. ]);
  131. }
  132. // 周榜
  133. $type = 'weekly';
  134. $date = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  135. if (
  136. $schoolRank = SchoolRankModel::where('school', $schoolModel->school)
  137. ->where('date', $date)
  138. ->where('type', $type)
  139. ->first()
  140. ) {
  141. $schoolRank->increment('count', $count);
  142. } else {
  143. SchoolRankModel::create([
  144. 'type' => $type,
  145. 'province' => $schoolModel->province,
  146. 'city' => $schoolModel->city,
  147. 'count' => $count,
  148. 'date' => $date,
  149. 'school' => $schoolModel->school,
  150. ]);
  151. }
  152. return response([
  153. 'code' => 200,
  154. 'message' => 'OK',
  155. ]);
  156. }
  157. /**
  158. * 卡片增量
  159. * @param Request $request
  160. * @param $id
  161. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  162. */
  163. public function incrementPartner(Request $request)
  164. {
  165. $this->validate($request, [
  166. 'partner_id' => 'required|integer',
  167. 'count' => 'integer',
  168. ]);
  169. $partner_id = $request->input('partner_id');
  170. $count = $request->input('count', 1);
  171. if ($appearance = AppearanceModel::where('partner_id', $partner_id)->first()) {
  172. // 月榜
  173. $type = 'monthly';
  174. $date = date("Y-m-01");
  175. if (
  176. $partnerRank = PartnerRankModel::where('partner_id', $appearance->partner_id)
  177. ->where('date', $date)
  178. ->where('type', $type)
  179. ->first()
  180. ) {
  181. $partnerRank->increment('count', $count);
  182. } else {
  183. PartnerRankModel::create([
  184. 'type' => $type,
  185. 'sex' => $appearance->sex,
  186. 'partner_id' => $appearance->partner_id,
  187. 'count' => 1,
  188. 'date' => $date,
  189. 'school' => $appearance->school,
  190. ]);
  191. }
  192. // 活动榜
  193. $type = 'monthly';
  194. $date = date("Y-05-01");
  195. if (
  196. $partnerRank = PartnerRankModel::where('partner_id', $appearance->partner_id)
  197. ->where('date', $date)
  198. ->where('type', $type)
  199. ->first()
  200. ) {
  201. $partnerRank->increment('count', $count);
  202. } else {
  203. PartnerRankModel::create([
  204. 'type' => $type,
  205. 'sex' => $appearance->sex,
  206. 'partner_id' => $appearance->partner_id,
  207. 'count' => 1,
  208. 'date' => $date,
  209. 'school' => $appearance->school,
  210. ]);
  211. }
  212. // 周榜
  213. $type = 'weekly';
  214. $date = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  215. if (
  216. $partnerRank = PartnerRankModel::where('partner_id', $appearance->partner_id)
  217. ->where('date', $date)
  218. ->where('type', $type)
  219. ->first()
  220. ) {
  221. $partnerRank->increment('count', $count);
  222. } else {
  223. PartnerRankModel::create([
  224. 'type' => $type,
  225. 'sex' => $appearance->sex,
  226. 'partner_id' => $appearance->partner_id,
  227. 'count' => 1,
  228. 'date' => $date,
  229. 'school' => $appearance->school,
  230. ]);
  231. }
  232. return response([
  233. 'code' => 200,
  234. 'message' => 'success'
  235. ]);
  236. } else {
  237. return response([
  238. 'code' => 403000,
  239. 'message' => '卡片未上榜'
  240. ]);
  241. }
  242. }
  243. }