/**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function show(Request $request, $id)
 {
     $this->validate($request, ['templateId' => 'numeric']);
     //营销类型
     $templateId = $request->input('templateId', 1);
     //默认百度
     $posterTheme = PosterTheme::find($id);
     $posterList = [];
     $checkShow = $posterTheme->status == 1 && strtotime($posterTheme->end_at) + 3 * 24 * 60 * 60 > time();
     //是否显示广告
     if ($checkShow) {
         $posterTheme['image100x450'] && ($posterList[0] = ['image' => $posterTheme['image100x450'], 'url' => $posterTheme['site_url']]);
         $posterTheme['image1000x90'] && ($posterList[1] = ['image' => $posterTheme['image1000x90'], 'url' => $posterTheme['site_url']]);
     }
     $template = $posterTheme->template->template;
     if (TRUE || !isset($template) || empty($template)) {
         switch ($templateId) {
             case 1:
                 $template = 'poster.themes.baidu';
                 break;
             case 2:
                 $template = 'poster.themes.haosou';
                 break;
             case 3:
                 $template = 'poster.themes.sogou';
                 break;
             default:
                 $template = 'poster.themes.baidu';
         }
     }
     return view($template)->with('posterList', $posterList);
 }
 public function run()
 {
     $faker = Faker::create('zh_CN');
     $user = User::where('name', '=', 'user')->first();
     $userId = isset($user) ? $user->id : 0;
     $template = Template::first();
     $templateId = isset($template) ? $template->id : 0;
     $publicPath = app('path.public');
     $dirPath = $publicPath . '/uploads/images/fakers';
     $urlDirPath = '/uploads/images/fakers';
     $end_at = (new DateTime())->modify('+1 Year');
     DB::table('poster_themes')->delete();
     foreach (range(1, 3) as $index) {
         PosterTheme::create(['user_id' => $userId, 'template_id' => $templateId, 'site_url' => $faker->url, 'status' => 1, 'image100x450' => URL($urlDirPath . '/' . $faker->image($dirPath, 100, 450, null, false)), 'image1000x90' => URL($urlDirPath . '/' . $faker->image($dirPath, 1000, 90, null, false)), 'end_at' => $end_at, 'created_at' => new DateTime(), 'updated_at' => new DateTime()]);
     }
 }
 /**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function index()
 {
     $userId = Auth::user()->id;
     $posterThemes = PosterTheme::where('user_id', '=', $userId)->get();
     return view('member.index')->with('posterThemes', $posterThemes);
 }
 /**
  * 续费
  * 修改end_at
  *
  * @param $id
  */
 public function renew($id)
 {
     $years = 1;
     //再续多少年
     $posterTheme = PosterTheme::findOrFail($id);
     $member = Auth::user();
     $itemPrice = 1000;
     //折扣后的价格
     if ($member->userGroup) {
         $discount = $member->userGroup->discount;
         $discount && ($itemPrice = $itemPrice * $discount / 100);
     }
     $end_at = new \DateTime($posterTheme->end_at);
     $now = new \DateTime();
     if ($end_at->getTimestamp() < $now->getTimestamp()) {
         $posterTheme->end_at = $now->modify('+' . $years . ' Year');
     } else {
         $posterTheme->end_at = $end_at->modify('+' . $years . ' Year');
     }
     $member->amount = $member->amount - $years * $itemPrice;
     DB::beginTransaction();
     try {
         if ($member->amount >= 0) {
             //todo 减账户资金
             if ($member->save()) {
                 $posterTheme->save();
             }
             DB::commit();
             return Redirect::to('member/poster/themes');
         } else {
             //return Redirect::back()->withErrors('续费失败!');
             return Redirect::back()->withErrors('续费失败, 余额不足!');
         }
     } catch (\Exception $e) {
         DB::rollBack();
         throw $e;
     }
 }