/**
  * Run the migrations.
  *
  * @return void
  */
 public function up()
 {
     Schema::table('booths', function ($table) {
         $table->integer('pv_id');
         // 省id
     });
     Schema::table('crowd_fundings', function ($table) {
         $table->integer('pv_id');
         // 省id
     });
     Schema::table('promotion_infos', function ($table) {
         $table->integer('pv_id');
     });
     $list = Booth::with(['school'])->get();
     foreach ($list as $key => $booth) {
         $pv_id = $booth->school->t_province;
         $booth->pv_id = $pv_id;
         $booth->save();
     }
     $list = CrowdFunding::with(['school'])->get();
     foreach ($list as $key => $funding) {
         $pv_id = $funding->school->t_province;
         $funding->pv_id = $pv_id;
         $funding->save();
     }
     $list = PromotionInfo::with(['school'])->get();
     foreach ($list as $key => $promo) {
         $pv_id = $promo->school->t_province;
         $promo->pv_id = $pv_id;
         $promo->save();
     }
 }
Esempio n. 2
0
 public function hot()
 {
     $city = Input::get('city', 0);
     $province = Input::get('province', 0);
     $school = Input::get('school', 0);
     $key = Input::get('key', '');
     $range = Input::get('range', 3);
     $u_id = Input::get('u_id');
     $is_follow = Input::get('is_follow', 0);
     $cate = Input::get('cate', 0);
     $perPage = Input::get('per_page', 30);
     $page = Input::get('page', 1);
     try {
         if (!$u_id) {
             throw new Exception("请传入有效的用户id", 2001);
         }
         $user = User::find($u_id);
         $user->load('school');
         $query = PromotionInfo::with(['city', 'school', 'booth' => function ($q) {
             $q->with(['user']);
         }, 'product' => function ($q) {
             $q->with(['promo', 'quantity', 'praises' => function ($qq) {
                 $qq->where('praises.u_id', '=', $this->u_id);
             }]);
         }]);
         $query = $query->select('promotion_infos.*');
         $query = $query->leftJoin('products', function ($q) {
             $q->on('products.p_id', '=', 'promotion_infos.p_id')->where('products.p_status', '=', 1)->where('products.p_type', '=', 1);
         })->leftJoin('booths', function ($q) {
             $q->on('booths.b_id', '=', 'promotion_infos.b_id')->where('booths.b_status', '=', 1);
         });
         if ($is_follow) {
             $query = $query->rightJoin('booth_follows', function ($q) use($u_id) {
                 $q->on('booths.b_id', '=', 'booth_follows.b_id')->where('booth_follows.u_id', '=', $u_id);
             });
             $school = 0;
             $city = 0;
             $range = 1;
         }
         $query = $query->where('promotion_infos.p_range', '<=', $range);
         if ($school && $range == 3) {
             $query = $query->where('promotion_infos.s_id', '=', $school);
         }
         if ($city && $province && $range == 2) {
             $query = $query->where('promotion_infos.c_id', '=', $city)->where('promotion_infos.pv_id', '=', $province);
         }
         if ($cate) {
             $query = $query->where('products.p_cate', '=', $cate);
         }
         if ($key) {
             $query = $query->where(function ($q) use($key) {
                 $q->where('promotion_infos.p_content', 'LIKE', '%' . $key . '%')->orWhere('booths.b_product_source', 'LIKE', '%' . $key . '%')->orWhere('booths.b_product_category', 'LIKE', '%' . $key . '%')->orWhere('booths.b_desc', 'LIKE', '%' . $key . '%')->orWhere('booths.b_title', 'LIKE', '%' . $key . '%')->orWhere('products.p_title', 'LIKE', '%' . $key . '%')->orWhere('products.p_desc', 'LIKE', '%' . $key . '%');
             });
         }
         $list = $query->orderBy('promotion_infos.created_at', 'DESC')->paginate($perPage);
         $data = [];
         foreach ($list as $k => $product) {
             $tmp = $product->showInListWithProduct();
             if (!empty($product->product->praises)) {
                 $tmp['is_praised'] = 1;
             } else {
                 $tmp['is_praised'] = 0;
             }
             $tmp['item_type'] = 1;
             $data[] = $tmp;
         }
         if (!$key) {
             $ad = Advertisement::fetchAd(2, $school, $city, $province, $range);
             if ($ad && $data) {
                 $data = array_merge($data, $ad);
                 $collection = new Collection($data);
                 $data = array_values($collection->toArray());
             } elseif ($ad && !$data && $page < 2) {
                 $data = $ad;
             }
         }
         $re = Tools::reTrue('获取首页商品成功', $data, $list);
     } catch (Exception $e) {
         $re = Tools::reFalse($e->getCode(), '获取首页商品失败:' . $e->getMessage());
     }
     return Response::json($re);
 }