<?php

namespace App\Http\Controllers\Wap\Web;

use App\Admin\Models\Product;
use App\Jobs\SendTexts;
use App\Modules\Activity\Services\ActivitySwitchService;
use App\Modules\Channel\Services\ChannelActivitiesService;
use App\Modules\Channel\Services\ChannelService;
use App\Modules\Cpa\Services\AdvertiseUserQueueService;
use App\Modules\Cpa\Services\AdvertiseUsersService;
use App\Modules\OfficialAccount\Services\ForceSubscribeService;
use App\Modules\Statistic\Services\DataAnalysisSelectUserService;
use App\Modules\Subscribe\Services\OrderService;
use App\Modules\User\Services\ForceSubscribeUserIService;
use App\Modules\User\Services\UserService;
use Illuminate\Http\Request;
use App\Http\Controllers\Wap\BaseController;
use Cookie;
use App\Modules\OfficialAccount\Services\CustomMsgService;
use App\Modules\Activity\Services\ActivityService;
use Redis;
use App\Modules\Subscribe\Models\Order;
use App\Modules\Activity\Models\Activity;
use App\Modules\OfficialAccount\Services\OfficialAccountService;
use Hashids;
use App\Modules\User\Services\ReadRecordService;
use App\Modules\Book\Services\BookConfigService;
use DB;
use App\Http\Controllers\Wap\Book\Transformers\BookTransformer;

class WelcomeController extends BaseController
{

    private $force_subscribe_info = null;
    /**
     * 首页
     */
    public function index(Request $request)
    {
        $url = $this->formWechatMsgJump($request);
        if ($url) {
            return redirect()->to($url);
        }
        //site标题
        $title = $this->getSiteTitle($request);
        //客服图片
        $customer_service = $this->getCustomerServiceImg();
        //活动配置
        $activity_options = $this->activityOptions();
        $url = $activity_options['url'];
        $is_show_activity = $activity_options['is_show_activity'];
        $activity_title = $activity_options['activity_title'];
        $bottom_show_type = $activity_options['bottom_show_type'];
        $activity_img = $activity_options['activity_img'];
        $user = $activity_options['user'];
        $hidden_reader_menus = false;
        $send_order_id = $this->send_order_id;
        //隐藏阅读页面菜单
        if ($send_order_id && in_array($send_order_id, explode(',', env('HIDDEN_READER_MENUS')))) {
            $hidden_reader_menus = true;
        }
        $hidden_reader_bootstrap = false;
        //隐藏阅读引导页
        if (env('HIDDEN_READ_BOOTSTRAP') &&
            in_array($this->distribution_channel_id, explode(',', env('HIDDEN_READ_BOOTSTRAP')))
        ) {
            $hidden_reader_bootstrap = true;
        }
        //隐藏活动底部banner
        if (env('HIDDEN_ACTIVITY_BANNER') &&
            in_array($this->distribution_channel_id, explode(',', env('HIDDEN_ACTIVITY_BANNER')))
        ) {
            $is_show_activity = 0;
        }
        //代付
        $showDaiChong = \App\Modules\OfficialAccount\Services\PaySubstituteSwitchService::getChannelSwitch($this->distribution_channel_id);
        //老年站点
        $isOld = 0;
        $channel = ChannelService::getDistributionChannel($this->distribution_channel_id);
        if ($channel) {
            $isOld = $channel->is_old_user_site;
        }
        //fromtype
        $from = $request->get('fromtype');
        if (!$from) {
            $from = Cookie::get('from') ? Cookie::get('from') : 'main';
        }
        $channel_activities_flag = $this->judgeChannelActivity($user);
        //最近阅读页面 推荐
        $latest_read_rec_books = $this->latest_read_rec_books($channel, $user->sex);
        //广告样本
        $ad_status = env('AD_STATUS', 1);
        $adTargetId = false;
        if ($ad_status) {
            $adTargetId = $this->isUserInAdSample();
        }
        //广告frame
        $is_show_ad_frame_setting = env('IS_SHOW_AD_FRAME_SETTING', '123');
        $is_show_ad_frame = false;
        if ($is_show_ad_frame_setting && in_array($this->distribution_channel_id, explode(',', $is_show_ad_frame_setting))) {
            $is_show_ad_frame = true;
        }
        //标题不显示书名
        $show_title_in_reader = false;
        $show_title_in_reader_setting = env('SHOW_TITLE_IN_READER_SETTING');
        if ($show_title_in_reader_setting && in_array($this->distribution_channel_id, explode(',', $show_title_in_reader_setting))) {
            $show_title_in_reader = true;
        }
        $hide_chapter_channels = explode(',', env('HIDE_CHAPTER_CONSUME_CHANNEL'));
        $is_hide_chapter_consume = in_array($this->distribution_channel_id, $hide_chapter_channels) ? 1 : 0;
        //广告
        
        // 屏蔽书名
        $self_config = ChannelService::check_channel_account_priv($this->distribution_channel_id,'hide_book_name');
        $hide_book_name = 0;
        if(!empty($self_config)){
        	$hide_book_name = 1;
        }
        //原创pv uv
        $yctj = false;
        $yctj_url = '';
        if(in_array($this->distribution_channel_id,[2,5,8,146,155,255,691,722,4053,4174,4364,4426])){
            $yctj = true;
            $yctj_url = 'https://m.ycsd.cn/continue';
        }
        //app广告
        $add_ad_status=[
            'appad'=>false,
            'appad_banner'=>'',
            'appad_url'=>''
        ];
        if(in_array($this->distribution_channel_id,explode(',',env('ADS_OPEN_SITE'))))  {
              $add_ad_status = $this->appad();
              $my_bid = $request->input('bid','');
              $ua = $_SERVER['HTTP_USER_AGENT'];
              if($add_ad_status['appad'] == true && $my_bid && preg_match('/Android/',$ua)) {
                  try{
                      AdvertiseUsersService::addAdAccessRec(['uid'=>$this->uid,
                          'bid'=>Hashids::decode($my_bid)[0],
                          'position'=>'READER',
                          'created_at'=>date('Y-m-d H:i:s'),
                          'updated_at'=>date('Y-m-d H:i:s'),
                          'remark'=>$request->url()
                      ]);
                  }catch(\Exception $e){
                      \Log::error('insert into advertise_access_rec failded:'.$e->getMessage());
                  }
              }
        }
        $options = json_encode([
            'distribution_channel_id' => (int)$this->distribution_channel_id,
            'send_order_id' => $send_order_id,
            'uid' => $this->uid,
            'pay_url' => env('CREATE_PAY_URL'),
            'title' => $title,
            'kefu' => $customer_service,
            'background' => env('WEB_READER_BACKGROUND'),
            'font_size' => env('WEB_READER_FONT_SIZE'),
            'page_style' => env('WEB_READER_PAGE_STYLE'),
            'from' => $from,
            'is_show_activity' => $is_show_activity,
            'activity_url' => $url,
            'activity_title' => $activity_title,
            'bottom_show_type' => $bottom_show_type,
            'activity_img' => $activity_img,
            'special_bottom_sub_banner' => $this->getSpecialBottomSubBanner(),
            'hidden_reader_menus' => $hidden_reader_menus,
            'hidden_reader_bootstrap' => $hidden_reader_bootstrap,
            'showDaiChong' => (int)$showDaiChong,
            'isOld' => $isOld,
            'sex' => $user ? $user->sex : 0,
            'latest_read_rec_books' => $latest_read_rec_books,
            'fission' => $this->fission(),//裂变
            'channel_activities' => $channel_activities_flag,
            'adTargetId' => $adTargetId,
            'ad_type' => '1',
            'is_show_ad_frame' => $is_show_ad_frame,
            'show_title_in_reader' => $show_title_in_reader,
            'is_hide_chapter_consume' => $is_hide_chapter_consume,
        	'hide_book_name' => $hide_book_name,
            'yctj'=>$yctj,
            'yctj_url'=>$yctj_url,
            'appad'=>$add_ad_status['appad'],
            'appad_banner'=>$add_ad_status['appad_banner'],
            'appad_url'=>$add_ad_status['appad_url']
        ]);
        if ($this->uid == env('SPECIAL_USER_LOG', 2)) {
            myLog('special')->info('welcome --------------------------');
            myLog('special')->info(json_decode($options, 1));
        }
        return view('wap.index', compact('options', 'title'));
    }

    private function judgeChannelActivity($user_info)
    {
        //新渠道送书币活动
        $channel_activities_hash = [
            500 => 'https://cdn-novel.iycdm.com/h5/five_hundred_coin.png',
            1000 => 'https://cdn-novel.iycdm.com/h5/thoudends_coin.png'
        ];
        $channel_activities_flag = [
            'shall_reward' => 0,
            'amount' => '',
            'image' => ''
        ];
        try {
            $channel_activities = ChannelActivitiesService::getValidActivity((int)$this->distribution_channel_id);
            if ($channel_activities) {
                if (time() - strtotime($user_info->created_at->format('Y-m-d H:i:s')) <= 5 * 60) { //对在5分钟之内新注册的用户进行打赏
                    $reward_info = DB::table('fission')->where([
                        ['type', '=', 2], ['uid', '=', $this->uid]
                    ])->first();

                    if (!$reward_info) { //未被打赏的新用户
                        //$user_info = UserService::getById($this->uid);

                        DB::beginTransaction();
                        $user_info->balance = $user_info->balance + $channel_activities->amount;
                        $user_info->reward_balance = $user_info->reward_balance + $channel_activities->amount;
                        if ($user_info->save()) {
                            $fission_data = [
                                'uid' => $this->uid,
                                'prize' => $channel_activities->amount,
                                'created_at' => date('Y-m-d H:i:s'),
                                'updated_at' => date('Y-m-d H:i:s'),
                                'type' => 2
                            ];

                            DB::table('fission')->insert($fission_data);
                            DB::commit();
                            $channel_activities_flag['shall_reward'] = 1;
                            $channel_activities_flag['amount'] = $channel_activities->amount;
                            $channel_activities_flag['image'] = $channel_activities_hash[$channel_activities->amount];

                            $force_sub_info = DB::table('force_subscribe_users')->where([['uid', '=', $this->uid], ['is_subscribed', '=', 1]])->first();
                            if ($force_sub_info) {
                                $bookUrl = env('PROTOCOL') . '://site' . encodeDistributionChannelId($this->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/';
                                $res['openid'] = $force_sub_info->openid;
                                $res['appid'] = $force_sub_info->appid;
                                $res['content'] = "亲爱的会员您好：\n恭喜您获得" . ($channel_activities->amount) . "书币奖励\n<a href='" . $bookUrl . "'>去书城看书</a>";
                                $res['type'] = 'one_task';
                                $res['send_time'] = date("Y-m-d H:i:s");
                                $res['task_id'] = md5('channel_activity_push');
                                $send_data = array(
                                    'send_time' => date("Y-m-d H:i:s"),
                                    'data' => $res
                                );
                                dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay(0)->onQueue('send_texts_list'));
                            }

                        } else {
                            DB::rollBack();
                        }
                    }
                }
            }
        } catch (\Exception $e) {
            \Log::error('channel activities error:channel_id:' . ($this->distribution_channel_id) . 'uid:' . ($this->uid) . ':' . ($e->getMessage()));
        }
        return $channel_activities_flag;
    }

    private function luckyUserForeverFree()
    {
        $lucky_user = 0;
        $lucky_user_img = '';
        if (!in_array($this->distribution_channel_id, [2, 14, 211])) return compact('lucky_user', 'lucky_user_img');
        try {
            $lucky_user = Redis::SISMEMBER('luckyYearOrderUser', $this->uid);
        } catch (\Exception $e) {
        }
        $lucky_user && $luckuser_img = 'https://cdn-novel.iycdm.com//h5/lucky.jpg';
        return compact('lucky_user', 'lucky_user_img');
    }

    private function latest_read_rec_books($channel, $sex)
    {
        $latest_read_rec_books = [];
        if ($channel->is_yq_move)//云栖迁移过来的站点
        {
            //用户性别
            if ($sex == 1)//男
            {
                $bids = [1896, 1969, 2115, 2526, 2610];
            } else {
                $bids = [1148, 1460, 1572, 1634, 2434, 2436];
                if (in_array($this->distribution_channel_id, [4042, 4043, 4044])) {
                    $bids = [2323, 1347, 2168, 1550, 1295, 1574];
                }
            }
            $latest_read_rec_books = collectionTransform(new BookTransformer(), BookConfigService::getBooksByIds($bids));
        }
        return $latest_read_rec_books;
    }

    /**
     * 获取123底部强管banner
     */
    private function getSpecialBottomSubBanner()
    {
        $week = date('N');
        return 'https://cdn-novel.iycdm.com/h5/bottom_sub_banner/' . $week . '.jpg';
    }

    /**
     * 裂变
     * @return int
     */
    private function fission()
    {
        $fission_send_order_id = env('FISSION_SEND_ORDER_ID');
        if ($fission_send_order_id && $this->send_order_id && in_array($this->send_order_id, explode(',', $fission_send_order_id))) {
            $is_exist = DB::table('fission')->where('uid', $this->uid)->select('uid')->first();
            if (!$is_exist) {
                DB::table('fission')->insert([
                    'uid' => $this->uid,
                    'prize' => 200,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s')
                ]);
                UserService::addBalance($this->uid, 200, 0, 200);
                return 1;
            }
        }
        return 0;
    }

    /**
     * 获取活动配置信息
     */
    private function activityOptions()
    {
        $h5_scheme = env('H5_SCHEME', 'https');
        $url = '';
        $is_show_activity = 0;
        $activity_title = '';
        $bottom_show_type = 1;
        $activity_img = '';
        $user = $this->userInfo($this->uid);
        /*$no_participate_activity = env('no_participate_activity', '');
        if ($no_participate_activity && in_array($this->distribution_channel_id, explode(',', $no_participate_activity))) {
            return compact('url', 'is_show_activity', 'activity_title', 'bottom_show_type', 'activity_img', 'user');
        }*/

        //自定义活动
        $show_custom_activity_channel = env('SHOW_CUSTOM_ACTIVITY_CHANNEL', '');
        if ($show_custom_activity_channel) {
            $custom_activity_page = ActivityService::getActiveCustomActivity($this->distribution_channel_id);
            if ($custom_activity_page) {
                if ($show_custom_activity_channel == 'all'
                    || in_array($this->distribution_channel_id, explode(',', $show_custom_activity_channel))
                ) {
                    $url = $h5_scheme . '://' . _domain() . $custom_activity_page->activity_page . '&fromtype=reader';;
                    $is_show_activity = 1;
                    $activity_title = '优惠活动';
                    $bottom_show_type = 1;
                    $activity_img = 'https://cdn-novel.iycdm.com/h5/2018-08-24-banner.png';
                }
            }
        }
        $activity_setting = ActivityService::getActivitySetting();

        if ($activity_setting) {
            $activity_id = isset($activity_setting['activity_id']) ? $activity_setting['activity_id'] : 0;
            $activity_info = Activity::find($activity_id);

            if ($activity_info &&
                time() > strtotime($activity_info->start_time) &&
                time() < strtotime($activity_info->end_time) &&
                $user &&
                isset($user->created_at) &&
                (time() - strtotime($user->created_at)) >= 86400 * 2
            ) {
                $activity_img = isset($activity_setting['wap_bottom_img']) ? $activity_setting['wap_bottom_img'] : '';
                $url = $h5_scheme . '://' . _domain() . $activity_info->activity_page . '&fromtype=reader';
                $activity_title = isset($activity_setting['activity_title']) ? $activity_setting['activity_title'] : '';
                $status = ActivitySwitchService::isShowInPage($activity_id, $this->distribution_channel_id);
                if ($status) {
                    $is_show_activity = 1;
                    $bottom_show_type = 2;
                    if($activity_setting['activity_id'] == env('OTHER_ACTIVITY_ID',0)){
                        if(!in_array($this->distribution_channel_id, explode(',', env('OTHER_ACTIVITY_CHANNEL', '1')))){
                            $is_show_activity = 0;
                        }
                    }
                }
            }
        }

        $year_activity_id = env('YEAR_ACTIVITY_ID');
        if ($year_activity_id) {
            $activity_info = Activity::find($year_activity_id);
            if ($activity_info &&
                time() > strtotime($activity_info->start_time) &&
                time() < strtotime($activity_info->end_time) &&
                $user &&
                isset($user->created_at) &&
                (time() - strtotime($user->created_at)) >= 86400 * 2
            ) {
                $sites = env('YEAR_ACTIVITY_CHANNEL', '');
                if ($sites && in_array($this->distribution_channel_id, explode(',', $sites))) {
                    $is_show_activity = 1;
                    $bottom_show_type = 2;
                    /*$old_discount_coupon = DB::table('discount_coupons')->where('uid', $this->uid)->whereIn('activity_id', [643, 827,1104])->count();
                    if ($old_discount_coupon) {
                        $activity_img = 'https://cdn-novel.iycdm.com/h5/activity-2018-08-29/reader-80.png';
                    } else {
                        $activity_img = 'https://cdn-novel.iycdm.com/h5/activity-2018-08-29/reader-50.png';
                    }*/

                    $activity_img = 'https://cdn-novel.iycdm.com/h5/activity-2018-12-20/reader-banner.jpg';
                    $url = $h5_scheme . '://' . _domain() . '/sale/yearActivity?fromtype=reader-year';
                    //$url = $h5_scheme . '://' . _domain() . '/sale/chongyang?fromtype=reader-year';
                    //$url = $h5_scheme . '://' . _domain() . '/sale/summer?fromtype=reader-year';
                }
            }
        }

        return compact('url', 'is_show_activity', 'activity_title', 'bottom_show_type', 'activity_img', 'user');
    }

    /**
     * 获取site标题
     */
    private function getSiteTitle(Request $request)
    {
        $title = "追书云";
        $get_force_subscribe_name = false;
        $subscribe = $request->cookie('force_subscribe_name');
        $appid = $request->get('appid');
        //用户进来携带的公众号信息
        if($appid){
            $force_subscribe_official = DB::table('official_accounts')->where('appid',$appid)->select('nickname')->first();
            if ($force_subscribe_official) {
                $title = $force_subscribe_official->nickname;
                if($title) return $title;
            }
        }
        if ($subscribe) return $subscribe;
        //强关公众号名称
        $force_subscribe = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
        $this->force_subscribe_info = $force_subscribe;
        if ($force_subscribe) {
            $force_subscribe_official = DB::table('official_accounts')->where('appid',$force_subscribe->appid)->select('nickname')->first();
            if ($force_subscribe_official) {
                $title = $force_subscribe_official->nickname;
                $get_force_subscribe_name = true;
                Cookie::queue('force_subscribe_name', $title, 12 * 3600);
            }
        }
        //可用公众号名称
        if (!$get_force_subscribe_name) {
            $res = OfficialAccountService::canUseOfficialAccountByChannelId(['distribution_channel_id' => $this->distribution_channel_id]);
            if ($res && isset($res->nickname) && !empty($res->nickname)) {
                $title = $res->nickname;
                Cookie::queue('force_subscribe_name', $title, 12 * 3600);
            }
        }
        return $title;
    }


    private function appad(){
        if($this->force_subscribe_info){
            $force_subscribe_info = $this->force_subscribe_info;
        }else{
            $force_subscribe_info = $this->force_subscribe_info = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
            //\Log::info('：'.($this->uid).'force_subs1:'.json_encode($force_subscribe_info));
        }
        if(!$force_subscribe_info){
            return ['appad'=>false,'appad_banner'=>'','appad_url'=>''];
        }
        $force_sub_user_by_openid = ForceSubscribeUserIService::getForceSubscribeByUidOpenid($this->uid);
        if($force_sub_user_by_openid){
            $force_subscribe_info = $force_sub_user_by_openid;
            //\Log::info('force_subs:'.json_encode($force_subscribe_info));
        }
        if($force_subscribe_info->subscribe_time &&
            (time()-strtotime($force_subscribe_info->subscribe_time) >=3*86400)
            && !OrderService::isPaidUserByOpenid($force_subscribe_info->openid) &&
            !AdvertiseUserQueueService::getUserAdvertise($this->uid) &&
            in_array($this->distribution_channel_id,explode(',',env('ADS_OPEN_SITE')))
        ){
            return [
                'appad'=>true,
                'appad_banner'=>'https://cdn-novel.iycdm.com/h5/app-ad-banner.jpg',
                'appad_url'=>'/advertise/show?uid='.Hashids::encode($this->uid)
            ];
        }
        return ['appad'=>false,'appad_banner'=>'','appad_url'=>''];
    }

    /**
     * 获取客服图片
     */
    private function getCustomerServiceImg()
    {
        $setting = CustomMsgService::customerImgUrlByChannelId($this->distribution_channel_id);
        if ($setting && $setting->customer_img_url) {
            $customer_service = $setting->customer_img_url;
        } else {
            $customer_service = "https://cdn-novel.iycdm.com/static/img/kefu20190319.jpg";
        }
        return $customer_service;
    }

    /**
     * 推广链接跳转到用户上一阅读的章节
     */
    private function formWechatMsgJump(Request $request)
    {
        $path = $request->path();
        if ($path != 'reader') {
            return null;
        }
        $source = $request->input('source');
        if (!$source || $source != 'wechatmsg') {
            return null;
        }
        $bid = 0;
        $bid_str = $request->input('bid');
        $cid = $request->input('cid');
        try {
            $bid_arr = Hashids::decode($bid_str);
            if (isset($bid_arr[0])) {
                $bid = $bid_arr[0];
            }
        } catch (\Exception $e) {
        }

        if (!$bid) return null;
        $record_info = '';
        try {
            $record_info = ReadRecordService::getRecordByUidBid($this->uid, $bid);
        } catch (\Exception $e) {

        }

        if (!$record_info) return null;
        $record_info_arr = explode('_', $record_info);
        $param = $request->except(['_url', 'source', 'cid']);
        $param['stats'] = 1;

        if (isset($record_info_arr[0])) {
            $param['cid'] = $record_info_arr[0];
            if ($cid && $cid != $record_info_arr[0]) {
                $h5_scheme = env('H5_SCHEME', 'https');
                $param_str = http_build_query($param);
                $url = $h5_scheme . '://' . _domain() . '/reader?' . $param_str;
                return $url;
            }
        }
        return null;
    }

    //用户是否在广告样本中
    private function isUserInAdSample()
    {
        $res = DataAnalysisSelectUserService::getByUidAndType($this->uid, 'AD_V2');
        if ($res) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 三天回本活动
     * @param Request $request
     * @return
     */
    public function activity(Request $request)
    {
        $send_time = Redis::hget('add_news_recovery_push_activity' . $this->uid, 'time');
        $send_time = time() - 100;
        if ($send_time) {
            if (time() - $send_time < 86400) {
                $activity_id = 1;
                $activity_info = ActivityService::getById($activity_id);
                if ($activity_info && $activity_info->product_id) {
                    $from = $request->input('fromtype', 'main');
                    Redis::sadd('push:activity:distribution_channel_id:' . $this->distribution_channel_id . 'from:' . $from . ':date:' . date('Y-m-d'), $this->uid);
                    Redis::hincrby('customer:push:click:activity:distribution_channel_id:' . $this->distribution_channel_id . 'from:' . $from, date('Y-m-d'), 1);
                    $param = [
                        'uid' => $this->uid,
                        'distribution_channel_id' => $this->distribution_channel_id,
                        'product_id' => $activity_info->product_id,
                        'activity_id' => $activity_id,
                        'fromtype' => $from,
                        'pay_redirect_url' => env('H5_SCHEME', 'https') . '://site' . encodeDistributionChannelId($this->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/person'
                    ];
                    Redis::hdel('add_news_recovery_push_activity' . $this->uid, 'time');
                    $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
                    return view('pay.order.activity', ['url' => $url, 'code' => 0]);
                }
            }
        }
        return redirect('/continue');
        //return view('pay.order.activity', ['code' => 1, 'url' => '']);
    }

    /**
     * 商户活动
     * @param Request $request
     * @return view
     */
    public function channelActivity(Request $request)
    {
        $token = $request->input('token');
        //return view('pay.order.saleactivity', ['url' => $token, 'code' => 0]);
        $activity_info = ActivityService::getByToken($token);
        if ($activity_info && $activity_info->product_id) {
            $from = $request->input('fromtype', 'main');
            $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
            $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
            $uv_key = sprintf($uv_key_format, $activity_info->id, $this->distribution_channel_id, date('Y-m-d'));
            $pv_key = sprintf($pv_key_format, $activity_info->id, $this->distribution_channel_id);
            Redis::sadd($uv_key, $this->uid);
            Redis::hincrby($pv_key, date('Y-m-d'), 1);
            $order = Order::where('uid', $this->uid)->where('status', 'PAID')->where('activity_id', $activity_info->id)->count();
            if (time() > strtotime($activity_info->start_time) && time() < strtotime($activity_info->end_time) && empty($order)) {
                // $from = $request->input('from', 'main');
                // $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
                // $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
                // $uv_key = sprintf($uv_key_format,$activity_info->id,$this->distribution_channel_id,date('Y-m-d'));
                // $pv_key = sprintf($pv_key_format,$activity_info->id,$this->distribution_channel_id);
                // Redis::sadd($uv_key,$this->uid);
                // Redis::hincrby($pv_key,date('Y-m-d'),1);
                $param = [
                    'uid' => $this->uid,
                    'distribution_channel_id' => $this->distribution_channel_id,
                    'product_id' => $activity_info->product_id,
                    'activity_id' => $activity_info->id,
                    'fromtype' => $from,
                    'pay_redirect_url' => env('H5_SCHEME', 'https') . '://site' . $this->distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person'
                ];
                $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
                return view('pay.order.saleactivity', ['url' => $url, 'code' => 0]);
            }
        }
        return view('pay.order.saleactivity', ['url' => '###', 'code' => 0]);
    }

    public function springActivityV1(Request $request)
    {
        $token = $request->input('token');
        $activity_info = ActivityService::getByToken($token);
        if ($activity_info) {
            $from = $request->input('fromtype', 'main');
            $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
            $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
            $uv_key = sprintf($uv_key_format, $activity_info->id, $this->distribution_channel_id, date('Y-m-d'));
            $pv_key = sprintf($pv_key_format, $activity_info->id, $this->distribution_channel_id);
            Redis::sadd($uv_key, $this->uid);
            Redis::hincrby($pv_key, date('Y-m-d'), 1);
            $redirect_url = env('H5_SCHEME', 'https') . '://site' . $this->distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person';
            if ($request->input('redirect_url')) {
                $redirect_url = $request->input('redirect_url');
                $redirect_url = urldecode($redirect_url);
            }
            if (time() > strtotime($activity_info->start_time) && time() < strtotime($activity_info->end_time)) {
                $param_fifty = [
                    'uid' => $this->uid,
                    'distribution_channel_id' => $this->distribution_channel_id,
                    'product_id' => env('SPRING_ACTIVITY_PRODUCT_FIFTY', 2),
                    'activity_id' => $activity_info->id,
                    'fromtype' => $from,
                    'pay_redirect_url' => $redirect_url
                ];
                $param_hundred = [
                    'uid' => $this->uid,
                    'distribution_channel_id' => $this->distribution_channel_id,
                    'product_id' => env('SPRING_ACTIVITY_PRODUCT_HUNDRED', 3),
                    'activity_id' => $activity_info->id,
                    'fromtype' => $from,
                    'pay_redirect_url' => $redirect_url
                ];
                $url_fifty = env('CREATE_PAY_URL') . '?' . http_build_query($param_fifty);
                $url_hundred = env('CREATE_PAY_URL') . '?' . http_build_query($param_hundred);
                return view('pay.order.springactivity', ['url' => ['fifty' => $url_fifty, 'hundred' => $url_hundred], 'code' => 0]);
            } else if (time() <= strtotime($activity_info->start_time)) {
                return view('pay.order.springactivity', ['url' => [], 'code' => 1]);
            } else {
                return view('pay.order.springactivity', ['url' => [], 'code' => 2]);
            }
        }
        return view('pay.order.springactivity', ['url' => [], 'code' => 3]);
    }

    public function springActivity(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $token = $request->input('token');
        $activity_info = ActivityService::getByToken($token);
        if (!$activity_info) {
            $default_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/';
            return redirect()->to($default_url);
        }

        $from = $request->input('fromtype', 'main');
        $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
        $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
        $uv_key = sprintf($uv_key_format, $activity_info->id, $this->distribution_channel_id, date('Y-m-d'));
        $pv_key = sprintf($pv_key_format, $activity_info->id, $this->distribution_channel_id);
        Redis::sadd($uv_key, $this->uid);
        Redis::hincrby($pv_key, date('Y-m-d'), 1);
        $redirect_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person';
        if ($request->input('redirect_url')) {
            $redirect_url = $request->input('redirect_url');
            $redirect_url = urldecode($redirect_url);
        }

        //未开始
        if (time() < strtotime($activity_info->start_time)) {
            return view('pay.order.springactivity2019', ['url' => ['javascript:void(0)', 'javascript:void(0)'], 'code' => -1]);
        }
        //结束
        if (time() > strtotime($activity_info->end_time)) {
            return view('pay.order.springactivity2019', ['url' => ['javascript:void(0)', 'javascript:void(0)'], 'code' => -2]);
        }
        $product_ids = [4063, 4064];
        $url = [];
        foreach ($product_ids as $product_id) {
            $param = [
                'uid' => $this->uid,
                'distribution_channel_id' => $this->distribution_channel_id,
                'product_id' => $product_id,
                'activity_id' => $activity_info->id,
                'fromtype' => $from,
                'pay_redirect_url' => $redirect_url,
                'limit' => 100,
            ];
            $url[] = env('CREATE_PAY_URL') . '?' . http_build_query($param);
        }

        return view('pay.order.springactivity2019', ['url' => $url, 'code' => 0]);
    }

    /**
     *
     */
    public function womenActivity(Request $request)
    {
        $token = $request->input('token');
        $activity_info = ActivityService::getByToken($token);
        if ($activity_info) {
            $from = $request->input('fromtype', 'main');
            $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
            $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
            $uv_key = sprintf($uv_key_format, $activity_info->id, $this->distribution_channel_id, date('Y-m-d'));
            $pv_key = sprintf($pv_key_format, $activity_info->id, $this->distribution_channel_id);
            Redis::sadd($uv_key, $this->uid);
            Redis::hincrby($pv_key, date('Y-m-d'), 1);
            $redirect_url = env('H5_SCHEME', 'https') . '://site' . $this->distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person';
            if ($request->input('redirect_url')) {
                $redirect_url = $request->input('redirect_url');
                $redirect_url = urldecode($redirect_url);
            }
            if (time() > strtotime($activity_info->start_time) && time() < strtotime($activity_info->end_time)) {
                $param = [
                    'uid' => $this->uid,
                    'distribution_channel_id' => $this->distribution_channel_id,
                    'product_id' => $activity_info->product_id,
                    'activity_id' => $activity_info->id,
                    'fromtype' => $from,
                    'pay_redirect_url' => $redirect_url
                ];

                $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
                return view('pay.order.womenactivity', ['url' => $url, 'code' => 0]);
            } else if (time() <= strtotime($activity_info->start_time)) {
                return view('pay.order.womenactivity', ['url' => [], 'code' => 1]);
            } else {
                return view('pay.order.womenactivity', ['url' => [], 'code' => 2]);
            }
        }
        return view('pay.order.womenactivity', ['url' => [], 'code' => 3]);
    }

    public function newUserActivity(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $from = $request->input('fromtype', 'main');
        $send_time = $request->input('send_time');

        $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
        $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
        $activity_id = env('NEW_USER_ACTIVITY_ID', 5);
        $activity_info = ActivityService::getById($activity_id);
        $uv_key = sprintf($uv_key_format, $activity_id, $this->distribution_channel_id, date('Y-m-d'));
        $pv_key = sprintf($pv_key_format, $activity_id, $this->distribution_channel_id);
        Redis::sadd($uv_key, $this->uid);
        Redis::hincrby($pv_key, date('Y-m-d'), 1);

        if (!$send_time) {
            return view('pay.order.newUserActivity', ['url' => '###', 'code' => -2, 'start_time' => date('Y-m-d', time() - 86400 * 2), 'end_time' => date('Y-m-d', time() - 86400)]);
        }
        $end_time_str = date('Y-m-d', $send_time + 86400);
        $end_time = strtotime(date('Y-m-d', $send_time + 86400) . ' 23:59:59');
        if ($send_time > $end_time) {
            return view('pay.order.newUserActivity', ['url' => '###', 'code' => -2, 'start_time' => date('Y-m-d', $send_time), 'end_time' => $end_time_str]);
        }

        if (time() > $end_time) {
            return view('pay.order.newUserActivity', ['url' => '###', 'code' => -2, 'start_time' => date('Y-m-d', $send_time), 'end_time' => $end_time_str]);
        }

        $order = Order::where('uid', $this->uid)->where('status', 'PAID')->where('activity_id', $activity_id)->count();
        if ($order) {
            return view('pay.order.newUserActivity', ['url' => '###', 'code' => -1, 'start_time' => date('Y-m-d', time() - 86400 * 2), 'end_time' => date('Y-m-d', time() - 86400)]);
            //return view('pay.order.newUserActivity', ['url' => '###', 'code' => -3, 'start_time' => date('Y-m-d', $send_time), 'end_time' => $end_time_str]);
        }
        $param = [
            'uid' => $this->uid,
            'distribution_channel_id' => $this->distribution_channel_id,
            'product_id' => $activity_info->product_id,
            'activity_id' => $activity_info->id,
            'fromtype' => $from,
            'pay_redirect_url' => env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person'
        ];
        $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
        return view('pay.order.newUserActivity', ['url' => $url, 'code' => 0, 'start_time' => date('Y-m-d', $send_time), 'end_time' => $end_time_str]);
    }


    public function channelActivityNine(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $token = $request->input('token');
        $activity_info = ActivityService::getByToken($token);
        if ($activity_info && $activity_info->product_id) {
            $from = $request->input('fromtype', 'main');
            $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
            $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
            $uv_key = sprintf($uv_key_format, $activity_info->id, $this->distribution_channel_id, date('Y-m-d'));
            $pv_key = sprintf($pv_key_format, $activity_info->id, $this->distribution_channel_id);
            Redis::sadd($uv_key, $this->uid);
            Redis::hincrby($pv_key, date('Y-m-d'), 1);
            //渠道不符合
            if ($activity_info->distribution_channel_id != $this->distribution_channel_id) {
                return view('pay.order.longactivitynine', ['url' => '###', 'code' => -1, 'start_time' => date('Y-m-d', time() - 86400 * 2), 'end_time' => date('Y-m-d', time() - 86400)]);
            }

            //活动未开始
            if (time() < strtotime($activity_info->start_time)) {
                return view('pay.order.longactivitynine', ['url' => '###', 'code' => -3, 'start_time' => '', 'end_time' => date('Y-m-d', strtotime($activity_info->end_time) - 100)]);
            }

            //活动结束
            if (time() > strtotime($activity_info->end_time)) {
                return view('pay.order.longactivitynine', ['url' => '###', 'code' => -1, 'start_time' => date('Y-m-d', strtotime($activity_info->start_time)), 'end_time' => date('Y-m-d', strtotime($activity_info->end_time) - 100)]);
            }
            $order = Order::where('uid', $this->uid)->where('status', 'PAID')->where('activity_id', $activity_info->id)->count();
            //参加过了
            if ($order) {
                return view('pay.order.longactivitynine', ['url' => '###', 'code' => -2, 'start_time' => date('Y-m-d', time() - 86400 * 2), 'end_time' => date('Y-m-d', time() - 86400)]);
            }

            if (time() > strtotime($activity_info->start_time) && time() < strtotime($activity_info->end_time) && empty($order)) {
                $not_allow_paid_user_channel_id = env('NOT_ALLOW_PAID_USER_CHANNEL_ID', 0);
                //不允许支付过的用户参见9.9的活动
                if ($not_allow_paid_user_channel_id &&
                    in_array($this->distribution_channel_id, explode(',', $not_allow_paid_user_channel_id)) &&
                    Order::where('uid', $this->uid)->where('status', 'PAID')->first()
                ) {
                    return view('pay.order.longactivitynine', ['url' => '', 'code' => -4, 'start_time' => '', 'end_time' => date('Y-m-d', strtotime($activity_info->end_time) - 100)]);
                }
                $param = [
                    'uid' => $this->uid,
                    'distribution_channel_id' => $this->distribution_channel_id,
                    'product_id' => $activity_info->product_id,
                    'activity_id' => $activity_info->id,
                    'fromtype' => $from,
                    'limit' => 1,
                    'pay_redirect_url' => env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person'
                ];
                $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
                return view('pay.order.longactivitynine', ['url' => $url, 'code' => 0, 'start_time' => date('Y-m-d', strtotime($activity_info->start_time)), 'end_time' => date('Y-m-d', strtotime($activity_info->end_time) - 100)]);
            }
        }
        return view('pay.order.longactivitynine', ['url' => '###', 'code' => -1, 'start_time' => date('Y-m-d', time() - 86400 * 2), 'end_time' => date('Y-m-d', time() - 86400)]);
    }

    public function channelActivitySixtyEight(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $token = $request->input('token');
        $img = 'https://cdn-novel.iycdm.com/h5/activity-chanel-custom/btn68.jpg';
        $activity_info = ActivityService::getByToken($token);
        if ($activity_info && $activity_info->product_id) {
            $from = $request->input('fromtype', 'main');
            $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
            $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
            $uv_key = sprintf($uv_key_format, $activity_info->id, $this->distribution_channel_id, date('Y-m-d'));
            $pv_key = sprintf($pv_key_format, $activity_info->id, $this->distribution_channel_id);
            Redis::sadd($uv_key, $this->uid);
            Redis::hincrby($pv_key, date('Y-m-d'), 1);
            //$order = Order::where('uid',$this->uid)->where('status','PAID')->where('activity_id',$activity_info->id)->count();
            //渠道不符合
            if ($activity_info->distribution_channel_id != $this->distribution_channel_id) {
                return view('pay.order.longactivitysixtyeight', ['url' => '###', 'img'=>$img,'code' => -1, 'start_time' => date('Y-m-d', time() - 86400 * 2), 'end_time' => date('Y-m-d', time() - 86400)]);
            }
            //活动未开始
            if (time() < strtotime($activity_info->start_time)) {
                return view('pay.order.longactivitysixtyeight', ['url' => '###', 'img'=>$img, 'code' => -3, 'start_time' => date('Y-m-d', strtotime($activity_info->start_time)), 'end_time' => date('Y-m-d', strtotime($activity_info->end_time) - 100)]);
            }
            //活动结束
            if (time() > strtotime($activity_info->end_time)) {
                return view('pay.order.longactivitysixtyeight', ['url' => '###', 'img'=>$img, 'code' => -1, 'start_time' => date('Y-m-d', strtotime($activity_info->start_time)), 'end_time' => date('Y-m-d', strtotime($activity_info->end_time) - 100)]);
            }

            if (time() > strtotime($activity_info->start_time) && time() < strtotime($activity_info->end_time)) {
                $param = [
                    'uid' => $this->uid,
                    'distribution_channel_id' => $this->distribution_channel_id,
                    'product_id' => $activity_info->product_id,
                    'activity_id' => $activity_info->id,
                    'fromtype' => $from,
                    'limit' => 100,
                    'pay_redirect_url' => env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person'
                ];
                $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
                return view('pay.order.longactivitysixtyeight', ['url' => $url, 'img'=>$img, 'code' => 0, 'start_time' => date('Y-m-d', strtotime($activity_info->start_time)), 'end_time' => date('Y-m-d', strtotime($activity_info->end_time) - 100)]);
            }
        }
        return view('pay.order.longactivitysixtyeight', ['url' => '###', 'img'=>$img, 'code' => -1, 'start_time' => date('Y-m-d', time() - 86400 * 2), 'end_time' => date('Y-m-d', time() - 86400)]);
    }

    public function channelActivityFortyEight(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $token = $request->input('token');
        $activity_info = ActivityService::getByToken($token);
        $img = 'https://cdn-novel.iycdm.com/h5/activity-chanel-custom/btn48.jpg';
        if ($activity_info && $activity_info->product_id) {
            $from = $request->input('fromtype', 'main');
            $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
            $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
            $uv_key = sprintf($uv_key_format, $activity_info->id, $this->distribution_channel_id, date('Y-m-d'));
            $pv_key = sprintf($pv_key_format, $activity_info->id, $this->distribution_channel_id);
            Redis::sadd($uv_key, $this->uid);
            Redis::hincrby($pv_key, date('Y-m-d'), 1);
            //$order = Order::where('uid',$this->uid)->where('status','PAID')->where('activity_id',$activity_info->id)->count();
            //渠道不符合
            if ($activity_info->distribution_channel_id != $this->distribution_channel_id) {
                return view('pay.order.longactivitysixtyeight', ['url' => '###', 'img'=>$img,'code' => -1, 'start_time' => date('Y-m-d', time() - 86400 * 2), 'end_time' => date('Y-m-d', time() - 86400)]);
            }
            //活动未开始
            if (time() < strtotime($activity_info->start_time)) {
                return view('pay.order.longactivitysixtyeight', ['url' => '###', 'img'=>$img, 'code' => -3, 'start_time' => date('Y-m-d', strtotime($activity_info->start_time)), 'end_time' => date('Y-m-d', strtotime($activity_info->end_time) - 100)]);
            }
            //活动结束
            if (time() > strtotime($activity_info->end_time)) {
                return view('pay.order.longactivitysixtyeight', ['url' => '###', 'img'=>$img, 'code' => -1, 'start_time' => date('Y-m-d', strtotime($activity_info->start_time)), 'end_time' => date('Y-m-d', strtotime($activity_info->end_time) - 100)]);
            }

            if (time() > strtotime($activity_info->start_time) && time() < strtotime($activity_info->end_time)) {
                $param = [
                    'uid' => $this->uid,
                    'distribution_channel_id' => $this->distribution_channel_id,
                    'product_id' => $activity_info->product_id,
                    'activity_id' => $activity_info->id,
                    'fromtype' => $from,
                    'limit' => 100,
                    'pay_redirect_url' => env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person'
                ];
                $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
                return view('pay.order.longactivitysixtyeight', ['url' => $url, 'img'=>$img, 'code' => 0, 'start_time' => date('Y-m-d', strtotime($activity_info->start_time)), 'end_time' => date('Y-m-d', strtotime($activity_info->end_time) - 100)]);
            }
        }
        return view('pay.order.longactivitysixtyeight', ['url' => '###', 'img'=>$img, 'code' => -1, 'start_time' => date('Y-m-d', time() - 86400 * 2), 'end_time' => date('Y-m-d', time() - 86400)]);
    }

    /**
     * 清明活动
     */
    public function qingMingActivity(Request $request)
    {
        $token = $request->input('token');
        $activity_info = ActivityService::getByToken($token);
        if ($activity_info) {
            $from = $request->input('fromtype', 'main');
            $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
            $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
            $uv_key = sprintf($uv_key_format, $activity_info->id, $this->distribution_channel_id, date('Y-m-d'));
            $pv_key = sprintf($pv_key_format, $activity_info->id, $this->distribution_channel_id);
            Redis::sadd($uv_key, $this->uid);
            Redis::hincrby($pv_key, date('Y-m-d'), 1);
            $redirect_url = env('H5_SCHEME', 'https') . '://site' . $this->distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person';
            if ($request->input('redirect_url')) {
                $redirect_url = $request->input('redirect_url');
                $redirect_url = urldecode($redirect_url);
            }
            if (time() > strtotime($activity_info->start_time) && time() < strtotime($activity_info->end_time)) {
                $param = [
                    'uid' => $this->uid,
                    'distribution_channel_id' => $this->distribution_channel_id,
                    'product_id' => $activity_info->product_id,
                    'activity_id' => $activity_info->id,
                    'fromtype' => $from,
                    'pay_redirect_url' => $redirect_url
                ];
                $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
                return view('pay.order.qingmingactivity', ['url' => $url, 'code' => 0]);
            } else if (time() <= strtotime($activity_info->start_time)) {
                return view('pay.order.qingmingactivity', ['url' => [], 'code' => 1]);
            } else {
                return view('pay.order.qingmingactivity', ['url' => [], 'code' => 2]);
            }
        }
        return view('pay.order.qingmingactivity', ['url' => [], 'code' => 3]);
    }

    public function readDayActivity(Request $request)
    {
        $token = $request->input('token');
        $activity_info = ActivityService::getByToken($token);
        if ($activity_info) {
            $from = $request->input('fromtype', 'main');
            $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
            $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
            $uv_key = sprintf($uv_key_format, $activity_info->id, $this->distribution_channel_id, date('Y-m-d'));
            $pv_key = sprintf($pv_key_format, $activity_info->id, $this->distribution_channel_id);
            Redis::sadd($uv_key, $this->uid);
            Redis::hincrby($pv_key, date('Y-m-d'), 1);
            $redirect_url = env('H5_SCHEME', 'https') . '://site' . $this->distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person';
            if ($request->input('redirect_url')) {
                $redirect_url = $request->input('redirect_url');
                $redirect_url = urldecode($redirect_url);
            }
            if (time() > strtotime($activity_info->start_time) && time() < strtotime($activity_info->end_time)) {
                $param = [
                    'uid' => $this->uid,
                    'distribution_channel_id' => $this->distribution_channel_id,
                    'product_id' => $activity_info->product_id,
                    'activity_id' => $activity_info->id,
                    'fromtype' => $from,
                    'pay_redirect_url' => $redirect_url,
                    'limit' => 100,
                ];
                $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
                return view('pay.order.laborDay', ['url' => $url, 'code' => 0]);
            } else if (time() <= strtotime($activity_info->start_time)) {
                return view('pay.order.laborDay', ['url' => [], 'code' => 1]);
            } else {
                return view('pay.order.laborDay', ['url' => [], 'code' => 2]);
            }
        }
        return view('pay.order.laborDay', ['url' => [], 'code' => 3]);
    }

    public function commonActivity(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $token = $request->input('token');
        $activity_info = ActivityService::getByToken($token);
        if ($activity_info && isset($activity_info->setting) && empty(!$activity_info->setting)) {
            $imgs = json_decode($activity_info->setting, 1);
            $from = $request->input('fromtype', 'main');
            $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
            $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
            $uv_key = sprintf($uv_key_format, $activity_info->id, $this->distribution_channel_id, date('Y-m-d'));
            $pv_key = sprintf($pv_key_format, $activity_info->id, $this->distribution_channel_id);

            $redirect_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person';
            if ($request->input('redirect_url')) {
                $redirect_url = $request->input('redirect_url');
                $redirect_url = urldecode($redirect_url);
            }
            if (time() > strtotime($activity_info->start_time) && time() < strtotime($activity_info->end_time)) {
                Redis::sadd($uv_key, $this->uid);
                Redis::hincrby($pv_key, date('Y-m-d'), 1);
                $param = [
                    'uid' => $this->uid,
                    'distribution_channel_id' => $this->distribution_channel_id,
                    'product_id' => $activity_info->product_id,
                    'activity_id' => $activity_info->id,
                    'fromtype' => $from,
                    'pay_redirect_url' => $redirect_url,
                    'limit' => 100,
                ];
                $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);

                return view('pay.order.common', ['url' => $url, 'code' => 0, 'img' => $imgs]);
            } else if (time() <= strtotime($activity_info->start_time)) {
                return view('pay.order.common', ['url' => [], 'code' => 1, 'img' => $imgs]);
            } else {
                return view('pay.order.common', ['url' => [], 'code' => 2, 'img' => $imgs]);
            }
        }
        $default_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/';
        return redirect()->to($default_url);
    }

    public function yearActivity(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $from = $request->input('fromtype', 'main');
        $sites = env('YEAR_ACTIVITY_CHANNEL', '');
        $not_access_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/';
        if (empty($sites)) {
            return redirect()->to($not_access_url);
        }
        $site_arr = explode(',', $sites);
        if (!in_array($this->distribution_channel_id, $site_arr)) {
            return redirect()->to($not_access_url);
        }
        $year_activity_id = env('YEAR_ACTIVITY_ID');
        if (!$year_activity_id) {
            return redirect()->to($not_access_url);
        }
        $activity_info = Activity::find($year_activity_id);
        if (!$activity_info) {
            return redirect()->to($not_access_url);
        }

        $activity_id = $year_activity_id;
        $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
        $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
        $uv_key = sprintf($uv_key_format, $activity_id, $this->distribution_channel_id, date('Y-m-d'));
        $pv_key = sprintf($pv_key_format, $activity_id, $this->distribution_channel_id);
        Redis::sadd($uv_key, $this->uid);
        Redis::hincrby($pv_key, date('Y-m-d'), 1);

        $start = $activity_info->start_time;
        $end = $activity_info->end_time;

        if (empty($start) || empty($end)) {
            return redirect()->to($not_access_url);
        }
        $male_site = env('YEAR_MALE_SITE');
        if ($male_site && in_array($this->distribution_channel_id, explode(',', $male_site))) {
            $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(explode(',', env('YEAR_MALE_BID'))));
        } else {
            $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(explode(',', env('YEAR_FEMALE_BID'))));
        }
        $is_get = 0;
        $discount_coupon = DB::table('discount_coupons')->where('uid', $this->uid)->where('activity_id', $activity_id)->count();
        //$old_discount_coupon = DB::table('discount_coupons')->where('uid', $this->uid)->where('activity_id', $old_year_activity_id)->count();
        $old_discount_coupon = DB::table('discount_coupons')->where('uid', $this->uid)->whereIn('activity_id', [643, 827, 1104, 1294])->count();

        if ($discount_coupon) {
            $is_get = 1;
        }

        if ($old_discount_coupon) {
            //51活动领取过 优惠80
            $discount = 80;
            $product_id = $activity_info->product_id;
        } else {
            //51活动领未取过 优惠50
            $discount = 50;
            $product_id = 1450;
        }
        if (time() < strtotime($start)) {
            //未开始
            return view('pay.order.yearactivityV3', ['url' => '', 'is_get' => 1, 'coupons_url' => '', 'code' => 1, 'books' => $books, 'activity_id' => $activity_id, 'discount' => $discount]);
        }

        if (time() > strtotime($end)) {
            //结束
            return view('pay.order.yearactivityV3', ['url' => '', 'is_get' => 1, 'coupons_url' => '', 'code' => 2, 'books' => $books, 'activity_id' => $activity_id, 'discount' => $discount]);
        }

        $order = Order::where('uid', $this->uid)->where('status', 'PAID')->where('activity_id', $activity_id)->count();

        //参加过了
        if ($order) {
            //return view('pay.order.yearactivityV2', ['url' => '', 'is_get' => 1, 'coupons_url' => '', 'code' => 3, 'books' => $books, 'activity_id' => $activity_id, 'discount' => $discount]);
        }

        $param = [
            'uid' => $this->uid,
            'distribution_channel_id' => $this->distribution_channel_id,
            'product_id' => $product_id,
            'activity_id' => $activity_id,
            'fromtype' => $from,
            'limit' => 10,
            'pay_redirect_url' => env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person'
        ];
        $this->getCoupons($activity_id);

        $coupons_url = $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
        $param['product_id'] = 5;
        $primary_url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
        return view('pay.order.yearactivityV3', ['url' => $primary_url, 'is_get' => $is_get, 'coupons_url' => $coupons_url, 'code' => 0, 'books' => $books, 'activity_id' => $activity_id, 'discount' => $discount]);
    }

    private function getCoupons($activity_id)
    {
        if (!DB::table('discount_coupons')->where('uid', $this->uid)->where('activity_id', $activity_id)->count()) {
            DB::table('discount_coupons')->insert([
                'uid' => $this->uid,
                'activity_id' => $activity_id,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ]);
        }
    }

    public function summerActivity(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $from = $request->input('fromtype', 'main');
        $sites = env('YEAR_ACTIVITY_CHANNEL', '');
        $not_access_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/';
        if (empty($sites)) {
            return redirect()->to($not_access_url);
        }
        if ($sites != 'all') {
            $site_arr = explode(',', $sites);
            if (!in_array($this->distribution_channel_id, $site_arr)) {
                return redirect()->to($not_access_url);
            }
        }
        $activity_id = 350;
        $activity_info = Activity::find($activity_id);
        $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
        $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
        $uv_key = sprintf($uv_key_format, $activity_id, $this->distribution_channel_id, date('Y-m-d'));
        $pv_key = sprintf($pv_key_format, $activity_id, $this->distribution_channel_id);
        $redirect_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person';
        if ($request->input('redirect_url')) {
            $redirect_url = $request->input('redirect_url');
            $redirect_url = urldecode($redirect_url);
        }
        if (time() > strtotime($activity_info->start_time) && time() < strtotime($activity_info->end_time)) {
            Redis::sadd($uv_key, $this->uid);
            Redis::hincrby($pv_key, date('Y-m-d'), 1);

            $url_array = [];
            $product_id_array = [2158, 2159, 2160, 2161];
            foreach ($product_id_array as $v) {
                $param = [
                    'uid' => $this->uid,
                    'distribution_channel_id' => $this->distribution_channel_id,
                    'product_id' => $v,
                    'activity_id' => $activity_id,
                    'fromtype' => $from,
                    'pay_redirect_url' => $redirect_url,
                    'limit' => 100,
                ];
                $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
                $url_array[] = $url;
            }
            return view('pay.order.summerActivity', ['url' => '', 'code' => 0, 'url_array' => $url_array]);
        } else if (time() <= strtotime($activity_info->start_time)) {
            return view('pay.order.summerActivity', ['url' => [], 'code' => 1]);
        } else {
            return view('pay.order.summerActivity', ['url' => [], 'code' => 2]);
        }
    }

    public function newUserSale(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $from = $request->input('fromtype', 'main');
        $send_time = $request->input('send_time');

        $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
        $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
        $activity_id = 402;
        $activity_info = ActivityService::getById($activity_id);
        $uv_key = sprintf($uv_key_format, $activity_id, $this->distribution_channel_id, date('Y-m-d'));
        $pv_key = sprintf($pv_key_format, $activity_id, $this->distribution_channel_id);
        Redis::sadd($uv_key, $this->uid);
        Redis::hincrby($pv_key, date('Y-m-d'), 1);
        // 2226,2227,2228,22229,2230

        if (!$send_time) {
            $url_info = $this->getUrls($activity_info->setting, $from, $this->distribution_channel_id, $encode_distribution_channel_id, $activity_id);
            return view('pay.order.newUserOtherActivity', ['url' => $url_info, 'code' => -1, 'start_time' => date('Y-m-d', time() - 86400 * 2), 'end_time' => date('Y-m-d', time() - 86400)]);
        }
        $end_time_str = date('Y-m-d', $send_time + 3 * 86400);
        $end_time = strtotime(date('Y-m-d', $send_time + 3 * 86400) . ' 23:59:59');
        if ($send_time > $end_time) {
            $url_info = $this->getUrls($activity_info->setting, $from, $this->distribution_channel_id, $encode_distribution_channel_id, $activity_id);
            return view('pay.order.newUserOtherActivity', ['url' => $url_info, 'code' => -1, 'start_time' => date('Y-m-d', $send_time), 'end_time' => $end_time_str]);
        }

        if (time() > $end_time) {
            $url_info = $this->getUrls($activity_info->setting, $from, $this->distribution_channel_id, $encode_distribution_channel_id, $activity_id);
            return view('pay.order.newUserOtherActivity', ['url' => $url_info, 'code' => -2, 'start_time' => date('Y-m-d', $send_time), 'end_time' => $end_time_str]);
        }

        $order = Order::where('uid', $this->uid)->where('status', 'PAID')->where('activity_id', $activity_id)->count();
        if ($order) {
            $url_info = $this->getUrls($activity_info->setting, $from, $this->distribution_channel_id, $encode_distribution_channel_id, $activity_id);
            return view('pay.order.newUserOtherActivity', ['url' => $url_info, 'code' => -3, 'start_time' => date('Y-m-d', $send_time), 'end_time' => $end_time_str]);
        }
        $url_info = $this->getUrls($activity_info->setting, $from, $this->distribution_channel_id, $encode_distribution_channel_id, $activity_id, true);
        return view('pay.order.newUserOtherActivity', ['url' => $url_info, 'code' => 0, 'start_time' => date('Y-m-d', $send_time), 'end_time' => $end_time_str]);
    }

    private function getUrls($activity_info_setting, $from, $channel_id, $encode_channel_id, $activity_id, $is_access = false)
    {
        $product = DB::table('products')
            ->whereIn('id', explode(',', $activity_info_setting))
            ->select('id', 'price', 'type', 'given')
            ->get();
        $url_info = [];
        $url_info['year'] = ['url' => '###'];
        $url_info['charge'] = [];
        $param = [
            'uid' => $this->uid,
            'distribution_channel_id' => $this->distribution_channel_id,
            'product_id' => $channel_id,
            'limit' => 1,
            'fromtype' => $from,
            'activity_id' => $activity_id,
            'pay_redirect_url' => env('H5_SCHEME', 'https') . '://site' . $encode_channel_id . '.' . env('CUSTOM_HOST') . '.com/person'
        ];
        if (!$product) {
            return $url_info;
        }
        foreach ($product as $v) {
            if ($is_access) {
                $param['product_id'] = $v->id;
                $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
            } else {
                $url = '###';
            }
            if ($v->type == 'YEAR_ORDER') {
                $url_info['year']['url'] = $url;
            } else {
                $temp = [];
                $temp['url'] = $url;
                $temp['price'] = (int)$v->price;
                $temp['given'] = (int)($v->given / 100);
                $temp['total'] = $v->price * 100 + $v->given;
                if ($v->price == 30) {
                    $temp['old_given'] = '无折扣';
                } elseif ($v->price == 50) {
                    $temp['old_given'] = '送40元';
                } elseif ($v->price == 100) {
                    $temp['old_given'] = '送100元';
                } elseif ($v->price == 200) {
                    $temp['old_given'] = '送200元';
                } else {
                    $temp['old_given'] = '';
                }
                $url_info['charge'][] = $temp;
            }
        }
        return $url_info;
    }


    public function nationalDayActivity(Request $request)
    {
        $token = $request->input('token', '');
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $activity_info = ActivityService::getByToken($token);
        if (!$activity_info) {
            $not_access_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/';
            return redirect()->to($not_access_url);
        }
        $from = $request->input('fromtype', 'main');
        $activity_id = $activity_info->id;

        $activty_ids = env('NATIONAL_DAY_ACTIVITY_IDS', '1,2,3');

        $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
        $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
        $uv_key = sprintf($uv_key_format, $activity_id, $this->distribution_channel_id, date('Y-m-d'));
        $pv_key = sprintf($pv_key_format, $activity_id, $this->distribution_channel_id);
        $redirect_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person';
        if ($request->input('redirect_url')) {
            $redirect_url = $request->input('redirect_url');
            $redirect_url = urldecode($redirect_url);
        }
        $activity_status = -1;
        if (time() > strtotime($activity_info->start_time) && time() < strtotime($activity_info->end_time)) {
            $activity_status = 1;
        }
        if (time() < strtotime($activity_info->start_time)) {
            //未开始
            $activity_status = -1;
        }

        if (time() > strtotime($activity_info->end_time)) {
            //结束
            $activity_status = -2;
        }

        Redis::sadd($uv_key, $this->uid);
        Redis::hincrby($pv_key, date('Y-m-d'), 1);
        $user_charge_count = (int)(Order::where('uid', $this->uid)->where('status', 'PAID')->count());
        if ($user_charge_count == 0) {
            $is_first_charge = 1;
        } else {
            $is_first_charge = 0;
        }

        $url_array = [];

        $product_id_array = explode(',', $activty_ids);
        foreach ($product_id_array as $key => $v) {
            if ($user_charge_count >= 1 && $key == 0) {
                //continue;
            }
            $param = [
                'uid' => $this->uid,
                'distribution_channel_id' => $this->distribution_channel_id,
                'product_id' => $v,
                'activity_id' => $activity_id,
                'fromtype' => $from,
                'pay_redirect_url' => $redirect_url,
                'limit' => 100,
            ];
            $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
            $url_array[] = $url;
        }
        return view('pay.order.nationalDayActivity', ['url' => $url_array, 'is_first_charge' => $is_first_charge, 'code' => $activity_status]);
    }

    public function cYactivity(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $default_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/';
        $site_arr = explode(',', env('YEAR_ACTIVITY_CHANNEL', '2'));
        if (!in_array($this->distribution_channel_id, $site_arr)) {
            return redirect()->to($default_url);
        }

        $activity_info = ActivityService::getById(929);
        if ($activity_info) {
            $from = $request->input('fromtype', 'main');
            $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
            $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
            $uv_key = sprintf($uv_key_format, $activity_info->id, $this->distribution_channel_id, date('Y-m-d'));
            $pv_key = sprintf($pv_key_format, $activity_info->id, $this->distribution_channel_id);

            $redirect_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person';
            if ($request->input('redirect_url')) {
                $redirect_url = $request->input('redirect_url');
                $redirect_url = urldecode($redirect_url);
            }
            if (time() > strtotime($activity_info->start_time) && time() < strtotime($activity_info->end_time)) {
                Redis::sadd($uv_key, $this->uid);
                Redis::hincrby($pv_key, date('Y-m-d'), 1);
                $param = [
                    'uid' => $this->uid,
                    'distribution_channel_id' => $this->distribution_channel_id,
                    'product_id' => $activity_info->product_id,
                    'activity_id' => $activity_info->id,
                    'fromtype' => $from,
                    'pay_redirect_url' => $redirect_url,
                    'limit' => 100,
                ];
                $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);

                return view('pay.order.cyangActivity', ['url' => $url, 'code' => 0,]);
            } else if (time() <= strtotime($activity_info->start_time)) {
                return view('pay.order.cyangActivity', ['url' => [], 'code' => 1,]);
            } else {
                return view('pay.order.cyangActivity', ['url' => [], 'code' => 2,]);
            }
        }

        return redirect()->to($default_url);
        return view('pay.order.cyangActivity', []);
    }

    public function newUserForeverActivity(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $from = $request->input('fromtype', 'main');
        $send_time = $request->input('send_time');
        $default_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/';
        if ($this->distribution_channel_id != 123) {
            return redirect()->to($default_url);
        }
        $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
        $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
        $activity_id = env('FOREVER_ACTIVITY_ID', 5);
        $activity_info = ActivityService::getById($activity_id);
        $uv_key = sprintf($uv_key_format, $activity_id, $this->distribution_channel_id, date('Y-m-d'));
        $pv_key = sprintf($pv_key_format, $activity_id, $this->distribution_channel_id);
        Redis::sadd($uv_key, $this->uid);
        Redis::hincrby($pv_key, date('Y-m-d'), 1);
        $books = $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(explode(',', env('FOREVER_ACTIVITY_BID'))));
        if (!$send_time) {
            return view('pay.order.foreverActivity', ['url' => '###', 'code' => -3, 'time' => ['day' => 0, 'hour' => 0, 'minute' => 0, 'second' => 0], 'books' => $books]);
        }

        $end_time = $send_time + 86400 * 3;
        $time_diff = $end_time - time();
        if ($time_diff <= 0) {
            return view('pay.order.foreverActivity', ['url' => '###', 'code' => -2, 'time' => ['day' => 0, 'hour' => 0, 'minute' => 0, 'second' => 0], 'books' => $books]);
        }

        /*$order = Order::where('uid', $this->uid)->where('status', 'PAID')->where('activity_id', $activity_id)->count();
        if ($order) {
            return view('pay.order.newUserActivity', ['url' => '###', 'code' => -3, 'start_time' => date('Y-m-d', $send_time), 'end_time' => $end_time_str]);
        }*/
        $books = $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(explode(',', env('FOREVER_ACTIVITY_BID'))));
        $day = floor($time_diff / 86400);
        $hour = floor(($time_diff - $day * 86400) / 3600);
        $minute = floor(($time_diff - $day * 86400 - $hour * 3600) / 60);
        $second = $time_diff - $day * 86400 - $hour * 3600 - $minute * 60;
        $param = [
            'uid' => $this->uid,
            'distribution_channel_id' => $this->distribution_channel_id,
            'product_id' => $activity_info->product_id,
            'activity_id' => $activity_info->id,
            'fromtype' => $from,
            'pay_redirect_url' => env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person'
        ];
        $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
        return view('pay.order.foreverActivity', ['url' => $url, 'code' => 0, 'time' => compact('day', 'hour', 'minute', 'second'), 'books' => $books]);
    }

    public function tempC()
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $param = [
            'uid' => $this->uid,
            'distribution_channel_id' => $this->distribution_channel_id,
            'product_id' => 1450,
            'activity_id' => 0,
            'fromtype' => 'tempC',
            'limit' => 10,
            'pay_redirect_url' => env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person'
        ];
        $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
        return redirect()->to($url);
    }

    /**
     * 68元包年活动
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\Swoft\Http\Message\Server\Response
     */
    public function otherYearActivity(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $from = $request->input('fromtype', 'main');
        $send_time = $request->input('send_time');
        $default_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/';
        if ($this->distribution_channel_id != 123) {
            return redirect()->to($default_url);
        }
        $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
        $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
        $activity_id = env('68_YEAR_ACTIVITY_ID', 5);
        $activity_info = ActivityService::getById($activity_id);
        $uv_key = sprintf($uv_key_format, $activity_id, $this->distribution_channel_id, date('Y-m-d'));
        $pv_key = sprintf($pv_key_format, $activity_id, $this->distribution_channel_id);
        Redis::sadd($uv_key, $this->uid);
        Redis::hincrby($pv_key, date('Y-m-d'), 1);
        //$books = $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(explode(',', env('FOREVER_ACTIVITY_BID'))));
        $books = '';
        $time_diff = 0;
        if (!$send_time) {
            return view('pay.order.otherYearActivity', ['url' => '###', 'code' => -3, 'time' => ['day' => 0, 'hour' => 0, 'minute' => 0, 'second' => 0], 'time_diff' => $time_diff]);
        }

        $end_time = $send_time + 86400 * 3;
        $time_diff = $end_time - time();
        if ($time_diff <= 0) {
            return view('pay.order.otherYearActivity', ['url' => '###', 'code' => -2, 'time' => ['day' => 0, 'hour' => 0, 'minute' => 0, 'second' => 0], 'time_diff' => $time_diff]);
        }

        /*$order = Order::where('uid', $this->uid)->where('status', 'PAID')->where('activity_id', $activity_id)->count();
        if ($order) {
            return view('pay.order.newUserActivity', ['url' => '###', 'code' => -3, 'start_time' => date('Y-m-d', $send_time), 'end_time' => $end_time_str]);
        }*/
        //$books = $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds(explode(',', env('FOREVER_ACTIVITY_BID'))));
        $day = floor($time_diff / 86400);
        $hour = floor(($time_diff - $day * 86400) / 3600);
        $minute = floor(($time_diff - $day * 86400 - $hour * 3600) / 60);
        $second = $time_diff - $day * 86400 - $hour * 3600 - $minute * 60;
        $param = [
            'uid' => $this->uid,
            'distribution_channel_id' => $this->distribution_channel_id,
            'product_id' => $activity_info->product_id,
            'activity_id' => $activity_info->id,
            'fromtype' => $from,
            'pay_redirect_url' => env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person'
        ];
        $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
        return view('pay.order.otherYearActivity', ['url' => $url, 'code' => 0, 'time' => compact('day', 'hour', 'minute', 'second'), 'time_diff' => $time_diff]);
    }


    /**
     * 双十一购物节统计
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\Swoft\Http\Message\Server\Response
     */
    public function shoppingDayActivity(Request $request)
    {
        $token = $request->input('token', '');
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $activity_info = ActivityService::getByToken($token);
        if (!$activity_info) {
            $not_access_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/';
            return redirect()->to($not_access_url);
        }
        $from = $request->input('fromtype', 'main');
        $activity_id = $activity_info->id;

        $activty_ids = env('SHOPPING_DAY_ACTIVITY_IDS', '1,2,3');

        $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
        $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
        $uv_key = sprintf($uv_key_format, $activity_id, $this->distribution_channel_id, date('Y-m-d'));
        $pv_key = sprintf($pv_key_format, $activity_id, $this->distribution_channel_id);
        $redirect_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person';
        if ($request->input('redirect_url')) {
            $redirect_url = $request->input('redirect_url');
            $redirect_url = urldecode($redirect_url);
        }
        $activity_status = -1;
        if (time() > strtotime($activity_info->start_time) && time() < strtotime($activity_info->end_time)) {
            $activity_status = 1;
        }
        if (time() < strtotime($activity_info->start_time)) {
            //未开始
            $activity_status = -1;
        }

        if (time() > strtotime($activity_info->end_time)) {
            //结束
            $activity_status = -2;
        }

        Redis::sadd($uv_key, $this->uid);
        Redis::hincrby($pv_key, date('Y-m-d'), 1);
        $user_charge_count = (int)(Order::where('uid', $this->uid)->where('status', 'PAID')->count());
        if ($user_charge_count == 0) {
            $is_first_charge = 1;
        } else {
            $is_first_charge = 0;
        }

        $url_array = [];
        $time_diff = strtotime($activity_info->end_time) - time();
        $day = 0;
        $hour = 0;
        $minute = 0;
        $second = 0;
        if ($time_diff > 0) {
            $day = floor($time_diff / 86400);
            $hour = floor(($time_diff - $day * 86400) / 3600);
            $minute = floor(($time_diff - $day * 86400 - $hour * 3600) / 60);
            $second = $time_diff - $day * 86400 - $hour * 3600 - $minute * 60;
        }


        $product_id_array = explode(',', $activty_ids);
        $product_infos = DB::table('products')->whereIn('id', $product_id_array)->select('id', 'price', 'given')->get();
        foreach ($product_infos as $key => $v) {
            if ($user_charge_count >= 1 && $key == 0) {
                continue;
            }
            $limit = 100;
            if ($key == 0) {
                $limit = 1;
            }
            $param = [
                'uid' => $this->uid,
                'distribution_channel_id' => $this->distribution_channel_id,
                'product_id' => $v->id,
                'activity_id' => $activity_id,
                'fromtype' => $from,
                'pay_redirect_url' => $redirect_url,
                'limit' => $limit,
            ];

            $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);

            $url_array[] = ['url' => $url, 'price' => (int)$v->price, 'given' => $v->given / 100, 'get' => $v->price * 100 + $v->given];
        }
        return view('pay.order.shoppingDayActivity', [
                'url' => $url_array,
                'is_first_charge' => $is_first_charge,
                'code' => $activity_status,
                'time' => compact('day', 'hour', 'minute', 'second'),
                'user_charge_count' => $user_charge_count,
                'time_diff' => $time_diff
            ]
        );
    }

    public function thanksGivingDayActivity(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $from = $request->input('fromtype', 'main');
        $sites = env('YEAR_ACTIVITY_CHANNEL', '');
        $not_access_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/';
        if (empty($sites)) {
            return redirect()->to($not_access_url);
        }
        $site_arr = explode(',', $sites);
        if (!in_array($this->distribution_channel_id, $site_arr)) {
            return redirect()->to($not_access_url);
        }
        $year_activity_id = 1367;

        $activity_info = Activity::find($year_activity_id);
        if (!$activity_info) {
            return redirect()->to($not_access_url);
        }

        $activity_id = $year_activity_id;
        $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
        $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
        $uv_key = sprintf($uv_key_format, $activity_id, $this->distribution_channel_id, date('Y-m-d'));
        $pv_key = sprintf($pv_key_format, $activity_id, $this->distribution_channel_id);
        Redis::sadd($uv_key, $this->uid);
        Redis::hincrby($pv_key, date('Y-m-d'), 1);

        $start = $activity_info->start_time;
        $end = $activity_info->end_time;

        if (empty($start) || empty($end)) {
            return redirect()->to($not_access_url);
        }
        $url = ['slow' => 'javascript:void(0)', 'height' => 'javascript:void(0)'];
        $order = Order::where('uid', $this->uid)->where('status', 'PAID')->select('id')->first();
        $charge_count = 0;
        if ($order) {
            $charge_count = 1;
        }
        if (time() < strtotime($start)) {
            //未开始
            return view('pay.order.thanksGivingDayActivity', ['url' => $url, 'code' => 1, 'charge_count' => $charge_count]);
        }

        if (time() > strtotime($end)) {
            //结束
            return view('pay.order.thanksGivingDayActivity', ['url' => $url, 'code' => 2, 'charge_count' => $charge_count]);
        }

        //3483  3484
        $param = [
            'uid' => $this->uid,
            'distribution_channel_id' => $this->distribution_channel_id,
            'product_id' => 3483,
            'activity_id' => $activity_id,
            'fromtype' => $from,
            'limit' => 1,
            'pay_redirect_url' => env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person'
        ];
        $this->getCoupons($activity_id);

        $url['slow'] = env('CREATE_PAY_URL') . '?' . http_build_query($param);
        $param['product_id'] = 3484;
        $param['limit'] = 100;
        $url['height'] = env('CREATE_PAY_URL') . '?' . http_build_query($param);
        return view('pay.order.thanksGivingDayActivity', ['url' => $url, 'code' => 0, 'charge_count' => $charge_count]);
    }

    public function greatColdActivity(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $token = $request->input('token');
        $activity_info = ActivityService::getByToken($token);
        if (!$activity_info) {
            $default_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/';
            return redirect()->to($default_url);
        }

        $from = $request->input('fromtype', 'main');
        $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
        $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
        $uv_key = sprintf($uv_key_format, $activity_info->id, $this->distribution_channel_id, date('Y-m-d'));
        $pv_key = sprintf($pv_key_format, $activity_info->id, $this->distribution_channel_id);
        Redis::sadd($uv_key, $this->uid);
        Redis::hincrby($pv_key, date('Y-m-d'), 1);
        $redirect_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person';
        if ($request->input('redirect_url')) {
            $redirect_url = $request->input('redirect_url');
            $redirect_url = urldecode($redirect_url);
        }
        $least_charge_amount = Order::select('id', 'price')
            ->where('uid', $this->uid)
            ->where('status', 'PAID')
            ->where('created_at', '>=', strtotime($activity_info->start_time) - 60 * 86400)
            ->max('price');
        $type = 'litter';
        $product_ids = [3962, 3963];
        $img = [
            'litter' => [
                'https://cdn-novel.iycdm.com/h5/activity-2019-01-17/8-active.png',
                'https://cdn-novel.iycdm.com/h5/activity-2019-01-17/8.png',
                'https://cdn-novel.iycdm.com/h5/activity-2019-01-17/23-active.png',
                'https://cdn-novel.iycdm.com/h5/activity-2019-01-17/23.png',
            ],
            'big' => [
                'https://cdn-novel.iycdm.com/h5/activity-2019-01-17/23-active.png',
                'https://cdn-novel.iycdm.com/h5/activity-2019-01-17/23.png',
                'https://cdn-novel.iycdm.com/h5/activity-2019-01-17/58-active.png',
                'https://cdn-novel.iycdm.com/h5/activity-2019-01-17/58.png',
            ],

        ];
        if ($least_charge_amount && $least_charge_amount >= 30) {
            $type = 'big';
            $product_ids = [3960, 3961];
        }

        //未开始
        if (time() < strtotime($activity_info->start_time)) {
            return view('pay.order.greatColdActivity', ['url' => ['javascript:void(0)', 'javascript:void(0)'], 'code' => -1, 'type' => $type, 'img' => $img]);
        }
        //结束
        if (time() > strtotime($activity_info->end_time)) {
            return view('pay.order.greatColdActivity', ['url' => ['javascript:void(0)', 'javascript:void(0)'], 'code' => -2, 'type' => $type, 'img' => $img]);
        }
        $url = [];
        foreach ($product_ids as $product_id) {
            $param = [
                'uid' => $this->uid,
                'distribution_channel_id' => $this->distribution_channel_id,
                'product_id' => $product_id,
                'activity_id' => $activity_info->id,
                'fromtype' => $from,
                'pay_redirect_url' => $redirect_url,
                'limit' => 100,
            ];
            $url[] = env('CREATE_PAY_URL') . '?' . http_build_query($param);
        }

        return view('pay.order.greatColdActivity', ['url' => $url, 'code' => 0, 'type' => $type, 'img' => $img]);
    }

    public function channelYearActivity(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $id = 2628;
        $activity_info = ActivityService::getById($id);
        if ($activity_info && $activity_info->product_id) {
            $from = $request->input('fromtype', 'main');
            $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
            $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
            $uv_key = sprintf($uv_key_format, $activity_info->id, $this->distribution_channel_id, date('Y-m-d'));
            $pv_key = sprintf($pv_key_format, $activity_info->id, $this->distribution_channel_id);
            Redis::sadd($uv_key, $this->uid);
            Redis::hincrby($pv_key, date('Y-m-d'), 1);

            $param = [
                'uid' => $this->uid,
                'distribution_channel_id' => $this->distribution_channel_id,
                'product_id' => $activity_info->product_id,
                'activity_id' => $activity_info->id,
                'fromtype' => $from,
                'limit' => 30,
                'pay_redirect_url' => env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person'
            ];
            $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
            return view('pay.order.channelYearActivity', ['url' => $url]);
        }
        $url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/';
        return redirect()->to($url);
    }

    //春分活动
    public function vernalEquinoxActivity(Request $request)
    {
        $encode_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
        $token = $request->input('token');
        $activity_info = ActivityService::getByToken($token);
        if (!$activity_info) {
            $default_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/';
            return redirect()->to($default_url);
        }

        $from = $request->input('fromtype', 'main');
        $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
        $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
        $uv_key = sprintf($uv_key_format, $activity_info->id, $this->distribution_channel_id, date('Y-m-d'));
        $pv_key = sprintf($pv_key_format, $activity_info->id, $this->distribution_channel_id);
        Redis::sadd($uv_key, $this->uid);
        Redis::hincrby($pv_key, date('Y-m-d'), 1);
        $redirect_url = env('H5_SCHEME', 'https') . '://site' . $encode_distribution_channel_id . '.' . env('CUSTOM_HOST') . '.com/person';
        if ($request->input('redirect_url')) {
            $redirect_url = $request->input('redirect_url');
            $redirect_url = urldecode($redirect_url);
        }
        $least_charge_amount = Order::select('id', 'price')
            ->where('uid', $this->uid)
            ->where('status', 'PAID')
            ->where('created_at', '>=', strtotime($activity_info->start_time) - 60 * 86400)
            ->max('price');
        $type = 'litter';
        if ($least_charge_amount && $least_charge_amount >= 30) {
            $img = 'https://cdn-novel.iycdm.com/h5/activity-2019-03-20/bg50.jpg';
            $product_ids = 4806;
        }else{
            $img = 'https://cdn-novel.iycdm.com/h5/activity-2019-03-20/bg23.jpg';
            $product_ids = 4805;
        }

        //未开始
        if (time() < strtotime($activity_info->start_time)) {
            return view('pay.order.vernalEquinoxActivity', ['url' => ['javascript:void(0)', 'javascript:void(0)'], 'code' => -1, 'type' => $type, 'img' => $img]);
        }
        //结束
        if (time() > strtotime($activity_info->end_time)) {
            return view('pay.order.vernalEquinoxActivity', ['url' => ['javascript:void(0)', 'javascript:void(0)'], 'code' => -2, 'type' => $type, 'img' => $img]);
        }

            $param = [
                'uid' => $this->uid,
                'distribution_channel_id' => $this->distribution_channel_id,
                'product_id' => $product_ids,
                'activity_id' => $activity_info->id,
                'fromtype' => $from,
                'pay_redirect_url' => $redirect_url,
                'limit' => 100,
            ];
            $url = env('CREATE_PAY_URL') . '?' . http_build_query($param);
        return view('pay.order.vernalEquinoxActivity', ['url' => $url, 'code' => 0, 'type' => $type, 'img' => $img]);
    }
}
