public function postCrowdFunding()
 {
     $token = Input::get('token', '');
     $u_id = Input::get('u_id', 0);
     $title = Input::get('title', '');
     $cate = Input::get('cate', 1);
     $amount = Input::get('amount', 0);
     $time = Input::get('time', 0);
     $yield_time = Input::get('yield_time', 0);
     $shipping = Input::get('shipping', 0);
     $shipping_fee = Input::get('shipping_fee', 0);
     $brief = Input::get('brief', '');
     $yield_desc = Input::get('yield_desc', '');
     $content = Input::get('content', '');
     $open_file = Input::get('open_file', 0);
     $active_at = Input::get('active_at');
     $local_only = Input::get('local_only', 0);
     $is_schedule = Input::get('is_schedule', 0);
     if (empty($active_at)) {
         $active_at = Tools::getNow();
     }
     $mobile = Input::get('mobile', '');
     $price = Input::get('price', 0);
     $quantity = Input::get('quantity', 0);
     $is_limit = Input::get('is_limit', 0);
     $img_token = Input::get('img_token', '');
     $img_token_2 = Input::get('img_token_2', '');
     $apartment_no = Input::get('apartment_no', '');
     $content = urldecode($content);
     DB::beginTransaction();
     try {
         $user = User::chkUserByToken($token, $u_id);
         if ($cate == 8 && $user->u_type != 2) {
             throw new Exception("错误的发布类型", 2001);
         }
         $base = new DateTime();
         $base->modify('+90 days');
         $target = new DateTime($active_at);
         if ($base < $target) {
             throw new Exception("最大预发布时间不能超过3个月", 2001);
         }
         $user->load('booth', 'profileBase', 'school');
         if ($apartment_no) {
             $tmp_base = TmpUserProfileBase::find($user->u_id);
             $tmp_base->u_apartment_no = $apartment_no;
             $tmp_base->save();
         }
         if (empty($user->booth)) {
             $booth = new Booth();
             $booth->u_id = $u_id;
             $booth->b_type = 7;
             $booth->c_id = $user->school->t_city;
             $booth->s_id = $user->school->t_id;
             $booth->b_with_fund = 0;
             $booth->latitude = $user->latitude;
             $booth->longitude = $user->longitude;
             $booth->save();
         } else {
             $booth = $user->booth;
         }
         // add event
         $event = new EventItem();
         $event->e_title = $title;
         $event->e_range = 0;
         $event->e_brief = $brief;
         $event->e_start_at = $active_at;
         $date_obj = new DateTime($active_at);
         $date_obj->modify('+' . $time . ' days');
         $event->e_end_at = $date_obj->format('Y-m-d H:i:s');
         $event->addEvent();
         $range = new EventRange();
         $range->e_id = $event->e_id;
         $range->s_id = $user->school->t_id;
         $range->c_id = $user->school->t_city;
         $range->p_id = $user->school->t_province;
         $range->save();
         // add funding
         $crowd_funding = new CrowdFunding();
         $crowd_funding->u_id = $u_id;
         $crowd_funding->b_id = $booth->b_id;
         $crowd_funding->e_id = $event->e_id;
         $crowd_funding->c_is_schedule = $is_schedule;
         $crowd_funding->c_yield_desc = $yield_desc;
         $crowd_funding->c_content = $content;
         $crowd_funding->c_yield_time = $yield_time;
         $crowd_funding->u_mobile = $mobile;
         $crowd_funding->c_time = $time;
         $crowd_funding->c_shipping = $shipping;
         $crowd_funding->c_shipping_fee = $shipping_fee;
         $crowd_funding->c_target_amount = $amount;
         $crowd_funding->c_cate = $cate;
         $crowd_funding->c_local_only = $local_only;
         $crowd_funding->c_open_file = $open_file;
         if ($amount <= 50000) {
             $crowd_funding->c_status = 4;
         } else {
             $crowd_funding->c_status = 1;
             $msg = new MessageDispatcher($u_id);
             $msg->fireTextToUser('你此次众筹总金额已超过50000元,我们将在24小时以内进行审核,请耐心等待。');
         }
         // if the user is an official user, set funding type to offical
         if ($user->u_type == 2) {
             $crowd_funding->c_cate = 8;
         }
         $crowd_funding->addCrowdFunding();
         if ($img_token) {
             $imgObj = new Img('event', $img_token);
             $event->cover_img = $imgObj->getSavedImg($event->e_id);
             $event->save();
         }
         if ($img_token_2) {
             $imgObj = new Img('crowd_funding', $img_token_2);
             $crowd_funding->c_imgs = $imgObj->getSavedImg($crowd_funding->cf_id);
             $crowd_funding->save();
         }
         // add funding product
         $funding_product = new CrowdFundingProduct();
         $funding_product->cf_id = $crowd_funding->cf_id;
         $funding_product->u_id = $u_id;
         $funding_product->b_id = $booth->b_id;
         $funding_product->p_title = $title;
         $funding_product->p_desc = '';
         $funding_product->p_price = $price;
         $funding_product->p_target_quantity = $quantity;
         $funding_product->p_sort = 0;
         if ($is_limit) {
             $funding_product->p_max_quantity = $quantity;
         } else {
             $funding_product->p_max_quantity = 0;
         }
         $funding_product->addProduct();
         $re = Tools::reTrue('添加众筹成功');
         DB::commit();
     } catch (Exception $e) {
         $re = Tools::reFalse($e->getCode(), '添加众筹失败:' . $e->getMessage());
         DB::rollback();
     }
     return Response::json($re);
 }
 public function postFunding()
 {
     $u_id = Tools::getOfficialUserId();
     $b_id = Tools::getOfficialBoothId();
     $title = Input::get('title', '');
     $amount = Input::get('amount', 0);
     $time = Input::get('time', 0);
     $yield_time = Input::get('yield_time', 0);
     $shipping = Input::get('shipping', 0);
     $shipping_fee = Input::get('shipping_fee', 0);
     $brief = Input::get('brief', '');
     $yield_desc = Input::get('yield_desc', '');
     $content = Input::get('content', '');
     $open_file = Input::get('open_file', 0);
     $active_at = Input::get('active_at');
     $local_only = Input::get('local_only', 0);
     $range = Input::get('range', 1);
     $cities = Input::get('cities', 0);
     $schools = Input::get('schools', 0);
     if (empty($active_at)) {
         $active_at = Tools::getNow();
     }
     $mobile = Input::get('mobile', '');
     $price = Input::get('price', 0);
     $quantity = Input::get('quantity', 0);
     $is_limit = Input::get('is_limit', 0);
     $img_token = Input::get('img_token', '');
     $content = urldecode($content);
     DB::beginTransaction();
     try {
         $user = User::find($u_id);
         $user->load('profileBase', 'school');
         $booth = Booth::find($b_id);
         // add event
         $event = new EventItem();
         $event->e_title = $title;
         $event->e_brief = $brief;
         $event->e_range = $range;
         $event->e_start_at = $active_at;
         $date_obj = new DateTime($active_at);
         $date_obj->modify('+' . $time . ' days');
         $event->e_end_at = $date_obj->format('Y-m-d H:i:s');
         $event->addEvent();
         $e_id = $event->e_id;
         // add funding
         $crowd_funding = new CrowdFunding();
         $crowd_funding->u_id = $u_id;
         $crowd_funding->b_id = $booth->b_id;
         $crowd_funding->c_yield_desc = $yield_desc;
         $crowd_funding->c_content = $content;
         $crowd_funding->c_yield_time = $yield_time;
         $crowd_funding->u_mobile = $mobile;
         $crowd_funding->c_time = $time;
         $crowd_funding->c_shipping = $shipping;
         $crowd_funding->c_shipping_fee = $shipping_fee;
         $crowd_funding->c_target_amount = $amount;
         $crowd_funding->c_amount = 0.0;
         $crowd_funding->c_local_only = $local_only;
         $crowd_funding->c_praise_count = 0;
         $crowd_funding->c_remark = '';
         $crowd_funding->c_open_file = $open_file;
         $crowd_funding->c_status = 4;
         $crowd_funding->c_cate = 8;
         $crowd_funding->e_id = $e_id;
         $crowd_funding->addCrowdFunding();
         if ($img_token) {
             $imgObj = new Img('crowd_funding', $img_token);
             $crowd_funding->c_imgs = $imgObj->getSavedImg($crowd_funding->cf_id);
             $crowd_funding->save();
             $imgObj = new Img('event', $img_token);
             $event->cover_img = $imgObj->getSavedImg($event->e_id);
             $event->save();
         }
         // add funding product
         $funding_product = new CrowdFundingProduct();
         $funding_product->cf_id = $crowd_funding->cf_id;
         $funding_product->u_id = $u_id;
         $funding_product->b_id = $booth->b_id;
         $funding_product->p_title = $title;
         $funding_product->p_desc = '';
         $funding_product->p_price = $price;
         $funding_product->p_target_quantity = $quantity;
         $funding_product->p_sort = 0;
         if ($is_limit) {
             $funding_product->p_max_quantity = $quantity;
         } else {
             $funding_product->p_max_quantity = 0;
         }
         $funding_product->addProduct();
         if ($range == 1) {
             $event_range = new EventRange(['c_id' => 0, 'p_id' => 0, 's_id' => 0]);
             $event->ranges()->save($event_range);
         }
         if ($cities && $range == 2) {
             $city_sets = explode(',', $cities);
             foreach ($city_sets as $key => $set) {
                 $array = explode('|', $set);
                 $event_range = new EventRange(['c_id' => $array[0], 'p_id' => $array[1]]);
                 if ($key) {
                     $new_event = $crowd_funding->cloneCrowdFunding();
                 } else {
                     $new_event = $event;
                 }
                 $new_event->ranges()->save($event_range);
             }
         }
         if ($schools && $range == 3) {
             $schools = explode(',', $schools);
             foreach ($schools as $key => $school) {
                 $event_range = new EventRange(['s_id' => $school]);
                 if ($key) {
                     $new_event = $crowd_funding->cloneCrowdFunding();
                 } else {
                     $new_event = $event;
                 }
                 $new_event->ranges()->save($event_range);
             }
         }
         $re = Tools::reTrue('添加众筹成功');
         DB::commit();
     } catch (Exception $e) {
         $re = Tools::reFalse($e->getCode(), '添加众筹失败:' . $e->getMessage());
         DB::rollback();
     }
     return Response::json($re);
 }