public function listFavoriteCrowd()
 {
     $token = Input::get('token', '');
     $u_id = Input::get('u_id');
     $per_page = Input::get('per_page', 0);
     try {
         $user = User::chkUserByToken($token, $u_id);
         $list = CrowdFunding::select('crowd_fundings.*', 'favorites.created_at')->with(['user', 'city', 'school', 'eventItem', 'praises' => function ($q) {
             $q->where('praises.u_id', '=', $this->u_id);
         }])->join('favoriables', function ($q) {
             $q->on('crowd_fundings.cf_id', '=', 'favoriables.favoriable_id')->where('favoriables.favoriable_type', '=', 'CrowdFunding');
         })->join('favorites', function ($q) {
             $q->on('favorites.id', '=', 'favoriables.favorite_id')->where('favorites.u_id', '=', $this->u_id);
         })->orderBy('favorites.created_at', 'DESC')->paginate($per_page);
         $data = [];
         foreach ($list as $key => $funding) {
             $tmp = $funding->showInList();
             $tmp['is_praised'] = 0;
             if (count($funding->praises) > 0) {
                 $tmp['is_praised'] = 1;
             }
             $data[] = $tmp;
         }
         $re = Tools::reTrue('获取用户列表成功', $data);
     } catch (Exception $e) {
         $re = Tools::reFalse($e->getCode(), '获取用户列表失败:' . $e->getMessage());
     }
     return Response::json($re);
 }
 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);
 }