public function listCrowdFunding()
 {
     $per_page = Input::get('per_page', 30);
     $page = Input::get('page', 1);
     $cate = Input::get('cate', 0);
     $u_id = Input::get('u_id', 0);
     $range = Input::get('range', 1);
     $city = Input::get('city', 0);
     $province = Input::get('province', 0);
     $school = Input::get('school', 0);
     $filter_option = Input::get('filter_option', 0);
     //1-about to start, 2-hot, 3-about to end
     $key = Input::get('key', '');
     try {
         if (!$u_id) {
             throw new Exception("请传入用户id", 7001);
         }
         $user = User::find($u_id);
         $user->load('school');
         $now = Tools::getNow();
         $query = CrowdFunding::select('crowd_fundings.*')->with(['city', 'school', 'user', 'user.school', 'product', 'eventItem', 'praises' => function ($q) {
             $q->where('praises.u_id', '=', $this->u_id);
         }])->where('c_status', '>', 2)->join('event_ranges', function ($q) {
             $q->on('event_ranges.e_id', '=', 'crowd_fundings.e_id');
         })->join('event_items', function ($q) {
             $q->on('event_items.e_id', '=', 'crowd_fundings.e_id');
         });
         if ($cate) {
             $query = $query->where('c_cate', '=', $cate);
         }
         if ($school && $range == 3) {
             $query = $query->where('event_ranges.s_id', '=', $school);
         }
         if ($filter_option == 1) {
             $query = $query->where('event_items.e_start_at', '>', $now);
         }
         if ($filter_option == 2) {
             // time passed more than 20%, less than 50%, and gathered more than 60% quantity
             $query = $query->whereRaw('(DATEDIFF(CURDATE(), t_event_items.e_start_at)) > (t_crowd_fundings.c_time * 0.2)')->whereRaw('(DATEDIFF(CURDATE(), t_event_items.e_start_at)) < (t_crowd_fundings.c_time * 0.5)')->join('crowd_funding_products', function ($q) {
                 $q->on('crowd_fundings.cf_id', '=', 'crowd_funding_products.cf_id');
             })->whereRaw('t_crowd_funding_products.p_sold_quantity > (t_crowd_funding_products.p_target_quantity * 0.6)')->where('event_items.e_start_at', '<', $now);
         }
         if ($filter_option == 3) {
             // left time is less than 20%
             $query = $query->whereRaw('DATEDIFF(t_event_items.e_end_at, CURDATE()) < (t_crowd_fundings.c_time * 0.2)')->where('event_items.e_end_at', '>', $now);
         }
         if ($city && $province && $range == 2) {
             $query = $query->where(function ($q) use($city, $province) {
                 $q->where(function ($qq) use($city, $province) {
                     $qq->where('event_ranges.c_id', '=', $city)->where('event_ranges.p_id', '=', $province);
                 })->orWhere(function ($qq) {
                     $qq->where('event_ranges.c_id', '=', 0)->where('event_ranges.p_id', '=', 0)->where('event_ranges.s_id', '=', 0);
                 });
             });
         }
         if ($key) {
             $query = $query->where(function ($q) use($key) {
                 $q->where('event_items.e_title', 'LIKE', '%' . $key . '%')->orWhere('event_items.e_brief', 'LIKE', '%' . $key . '%')->orWhere('crowd_fundings.c_yield_desc', 'LIKE', '%' . $key . '%')->orWhere('crowd_fundings.c_content', 'LIKE', '%' . $key . '%');
             });
         }
         $query = $query->orderBy('crowd_fundings.created_at', 'DESC');
         $list = $query->paginate($per_page);
         $data = [];
         $start = 0;
         $end = 0;
         foreach ($list as $k => $funding) {
             $tmp = $funding->showInList();
             if ($k == 0) {
                 $start = $end = $tmp['created_at_timestamps'];
             } else {
                 $start = min($start, $tmp['created_at_timestamps']);
                 $end = max($end, $tmp['created_at_timestamps']);
             }
             $tmp['is_praised'] = 0;
             if (count($funding->praises) > 0) {
                 $tmp['is_praised'] = 1;
             }
             $tmp['item_type'] = 1;
             $data[] = $tmp;
         }
         if (!$key) {
             $start = $start > 0 ? date('Y-m-d H:i:s', $start) : null;
             if ($page == 1 && $list->getTotal() < $per_page) {
                 $start = null;
             }
             $end = $end > 0 && $page != 1 ? date('Y-m-d H:i:s', $end) : null;
             $ad = Advertisement::fetchAd(1, $start, $end, $school, $city, $province, $range);
             if ($ad && $data) {
                 $data = Advertisement::mergeArray($data, $ad);
             } 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);
 }
 public function flea()
 {
     $city = Input::get('city', 0);
     $province = Input::get('province', 0);
     $school = Input::get('school', 0);
     $key = Input::get('key', '');
     $range = Input::get('range', 0);
     $u_id = Input::get('u_id');
     $is_follow = Input::get('is_follow', 0);
     $cate = Input::get('cate', 0);
     $filter_option = Input::get('filter_option', 0);
     // 1-within 1 day, 2-within 3 days, 3-within 7 days
     $perPage = Input::get('per_page', 30);
     $page = Input::get('page', 1);
     try {
         if (!$school) {
             $range = 2;
         }
         if (!$u_id) {
             throw new Exception("请传入有效的用户id", 2001);
         }
         $user = User::find($u_id);
         $user->load('school');
         $query = Product::with(['user', 'booth' => function ($q) {
             $q->with(['school']);
         }, 'quantity', 'praises' => function ($q) {
             $q->where('praises.u_id', '=', $this->u_id);
         }]);
         $query = $query->select('products.*')->where('products.p_status', '=', 1)->where('products.p_type', '=', 2);
         $query = $query->leftJoin('booths', function ($q) {
             $q->on('booths.b_id', '=', 'products.b_id');
         });
         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;
         }
         if ($school && $range == 3) {
             $query = $query->where('booths.s_id', '=', $school);
         }
         if ($city && $province && $range == 2) {
             $query = $query->where('booths.c_id', '=', $city)->where('booths.pv_id', '=', $province);
         }
         if ($cate) {
             $query = $query->where('products.p_cate', '=', $cate);
         }
         if ($key) {
             $query = $query->where(function ($q) use($key) {
                 $q->where('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 . '%');
             });
         }
         if ($filter_option) {
             if ($filter_option == 1) {
                 $days = 1;
             } elseif ($filter_option == 2) {
                 $days = 3;
             } else {
                 $days = 7;
             }
             $now = Tools::getNow(false);
             $now->modify('-' . $days . ' days');
             $date = $now->format('Y-m-d H:i:s');
             $query = $query->where('products.active_at', '>', $date);
         }
         $list = $query->orderBy('products.created_at', 'DESC')->paginate($perPage);
         $data = [];
         $start = 0;
         $end = 0;
         foreach ($list as $k => $product) {
             $tmp = $product->showInList();
             if ($k == 0) {
                 $start = $end = $tmp['created_at_timestamps'];
             } else {
                 $start = min($start, $tmp['created_at_timestamps']);
                 $end = max($end, $tmp['created_at_timestamps']);
             }
             if (empty($tmp['booth']['school'])) {
                 $tmp['school'] = [];
             } else {
                 $tmp['school'] = $tmp['booth']['school'];
             }
             if (empty($tmp['booth']['city'])) {
                 $tmp['city'] = [];
             } else {
                 $tmp['city'] = $tmp['booth']['city'];
             }
             unset($tmp['booth']);
             if (count($product->praises) > 0) {
                 $tmp['is_praised'] = 1;
             } else {
                 $tmp['is_praised'] = 0;
             }
             $tmp['item_type'] = 1;
             $data[] = $tmp;
         }
         if (!$key) {
             $start = $start > 0 ? date('Y-m-d H:i:s', $start) : null;
             $end = $end > 0 && $page != 1 ? date('Y-m-d H:i:s', $end) : null;
             if ($page == 1 && $list->getTotal() < $per_page) {
                 $start = null;
             }
             $ad = Advertisement::fetchAd(3, $start, $end, $school, $city, $province, $range);
             if ($ad && $data) {
                 $data = Advertisement::mergeArray($data, $ad);
             } 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);
 }