123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Models\Appearance\AppearanceModel;
- use App\Models\Appearance\PartnerRankModel;
- use App\Models\Appearance\SchoolRankModel;
- use App\Models\User\UserModel;
- use App\Models\SchoolModel;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- class AppearanceController extends Controller
- {
- //
- public function province(Request $request)
- {
- $page = $request->get('page', 1);
- $per_page = $request->get('per_page', 20);
- $type = 'monthly';
- $date = $request->get("monthly", date("Y-m-01"));
- $date = "2019-05-01";
- $builder = SchoolRankModel::where('date', $date)
- ->where('type', $type)
- ->when($request->filled('province'), function ($query) use ($request) {
- return $query->where('province', $request->province);
- })->when($request->school, function ($query) use ($request) {
- return $query->where('school', $request->school);
- });
- $total = $builder->count();
- $ranks = $builder->orderBy('count', 'desc')->offset(($page - 1) * $per_page)->limit($per_page)->get();
- $last_page = ceil($total / $per_page);
- $meta = [
- 'current_page' => $page,
- 'per_page' => $per_page,
- 'last_page' => $last_page,
- 'total' => $total
- ];
- return response([
- 'code' => 200,
- 'data' => $ranks,
- 'meta' => $meta,
- ]);
- }
- public function school(Request $request)
- {
- $school = $request->get('school');
- $sex = $request->get('sex', 2);
- $page = $request->get('page', 1);
- $per_page = $request->get('per_page', 20);
- $type = 'monthly';
- $date = $request->get("date", date("Y-m-01"));
- $date = "2019-05-01";
- $builder = \DB::table("fpdx_appearance_partner_ranks as r")
- ->selectRaw("r.partner_id, r.sex, r.school, r.count")
- ->join("fpdx_appearances as a", "r.partner_id", '=', 'a.partner_id')
- ->where('r.date', $date)
- ->where('r.type', $type)
- ->where('r.school', $school)
- ->where('r.sex', $sex)
- ->where('a.state', 2);
- $total = $builder->count();
- $ranks = $builder->orderBy('count', 'desc')->offset(($page - 1) * $per_page)->limit($per_page)->get();
- foreach ($ranks as $rank) {
- $rank->user = UserModel::select('nickname', 'headimgurl')->where('partner_id', $rank->partner_id)->first();
- }
- $last_page = ceil($total / $per_page);
- $meta = [
- 'current_page' => $page,
- 'per_page' => $per_page,
- 'last_page' => $last_page,
- 'total' => $total,
- ];
- return response([
- 'code' => 200,
- 'data' => $ranks,
- 'meta' => $meta,
- ]);
- }
- /**
- * 学校增量
- * @param Request $request
- * @param $id
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function incrementSchool(Request $request)
- {
- $this->validate($request, [
- 'school' => 'required',
- 'count' => 'integer',
- ]);
- $school = $request->input('school');
- $count = $request->input('count', 1);
- $schoolModel = SchoolModel::where('school', $school)->firstOrFail();
- // 月榜
- $type = 'monthly';
- $date = date("Y-m-01");
- if (
- $schoolRank = SchoolRankModel::where('school', $schoolModel->school)
- ->where('date', $date)
- ->where('type', $type)
- ->first()
- ) {
- $schoolRank->increment('count', $count);
- } else {
- SchoolRankModel::create([
- 'type' => $type,
- 'province' => $schoolModel->province,
- 'city' => $schoolModel->city,
- 'count' => $count,
- 'date' => $date,
- 'school' => $schoolModel->school,
- ]);
- }
- // 活动榜
- $type = 'monthly';
- $date = date("Y-05-01");
- if (
- $schoolRank = SchoolRankModel::where('school', $schoolModel->school)
- ->where('date', $date)
- ->where('type', $type)
- ->first()
- ) {
- $schoolRank->increment('count', $count);
- } else {
- SchoolRankModel::create([
- 'type' => $type,
- 'province' => $schoolModel->province,
- 'city' => $schoolModel->city,
- 'count' => $count,
- 'date' => $date,
- 'school' => $schoolModel->school,
- ]);
- }
- // 周榜
- $type = 'weekly';
- $date = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
- if (
- $schoolRank = SchoolRankModel::where('school', $schoolModel->school)
- ->where('date', $date)
- ->where('type', $type)
- ->first()
- ) {
- $schoolRank->increment('count', $count);
- } else {
- SchoolRankModel::create([
- 'type' => $type,
- 'province' => $schoolModel->province,
- 'city' => $schoolModel->city,
- 'count' => $count,
- 'date' => $date,
- 'school' => $schoolModel->school,
- ]);
- }
- return response([
- 'code' => 200,
- 'message' => 'OK',
- ]);
- }
- /**
- * 卡片增量
- * @param Request $request
- * @param $id
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function incrementPartner(Request $request)
- {
- $this->validate($request, [
- 'partner_id' => 'required|integer',
- 'count' => 'integer',
- ]);
- $partner_id = $request->input('partner_id');
- $count = $request->input('count', 1);
- if ($appearance = AppearanceModel::where('partner_id', $partner_id)->first()) {
- // 月榜
- $type = 'monthly';
- $date = date("Y-m-01");
- if (
- $partnerRank = PartnerRankModel::where('partner_id', $appearance->partner_id)
- ->where('date', $date)
- ->where('type', $type)
- ->first()
- ) {
- $partnerRank->increment('count', $count);
- } else {
- PartnerRankModel::create([
- 'type' => $type,
- 'sex' => $appearance->sex,
- 'partner_id' => $appearance->partner_id,
- 'count' => 1,
- 'date' => $date,
- 'school' => $appearance->school,
- ]);
- }
- // 活动榜
- $type = 'monthly';
- $date = date("Y-05-01");
- if (
- $partnerRank = PartnerRankModel::where('partner_id', $appearance->partner_id)
- ->where('date', $date)
- ->where('type', $type)
- ->first()
- ) {
- $partnerRank->increment('count', $count);
- } else {
- PartnerRankModel::create([
- 'type' => $type,
- 'sex' => $appearance->sex,
- 'partner_id' => $appearance->partner_id,
- 'count' => 1,
- 'date' => $date,
- 'school' => $appearance->school,
- ]);
- }
- // 周榜
- $type = 'weekly';
- $date = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
- if (
- $partnerRank = PartnerRankModel::where('partner_id', $appearance->partner_id)
- ->where('date', $date)
- ->where('type', $type)
- ->first()
- ) {
- $partnerRank->increment('count', $count);
- } else {
- PartnerRankModel::create([
- 'type' => $type,
- 'sex' => $appearance->sex,
- 'partner_id' => $appearance->partner_id,
- 'count' => 1,
- 'date' => $date,
- 'school' => $appearance->school,
- ]);
- }
- return response([
- 'code' => 200,
- 'message' => 'success'
- ]);
- } else {
- return response([
- 'code' => 403000,
- 'message' => '卡片未上榜'
- ]);
- }
- }
- }
|