<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/4/11
 * Time: 上午12:49
 */

defined('InShopNC') or exit('Access Invalid!');

require_once(BASE_ROOT_PATH . '/helper/bonus_helper.php');
require_once(BASE_ROOT_PATH . '/helper/predeposit_helper.php');
require_once(BASE_ROOT_PATH . '/helper/session_helper.php');
require_once(BASE_ROOT_PATH . '/helper/sms_helper.php');
require_once(BASE_ROOT_PATH . '/helper/model_helper.php');
require_once(BASE_ROOT_PATH . '/helper/category_helper.php');
require_once(BASE_ROOT_PATH . '/helper/brand_helper.php');
require_once(BASE_ROOT_PATH . '/helper/search/tcp_client.php');
require_once(BASE_ROOT_PATH . '/helper/search/util.php');
require_once(BASE_ROOT_PATH . '/helper/search_param.php');
require_once(BASE_ROOT_PATH . '/helper/goods_helper.php');
require_once(BASE_ROOT_PATH . '/helper/session_helper.php');
require_once(BASE_ROOT_PATH . '/helper/login_helper.php');
require_once(BASE_ROOT_PATH . '/helper/third_author/wxauthor.php');
require_once(BASE_ROOT_PATH . '/helper/fcode/present_manager.php');
require_once(BASE_ROOT_PATH . '/helper/url_helper.php');
require_once(BASE_ROOT_PATH . '/helper/activity_helper.php');

class bonusexControl extends mobileControl
{
    const bonus_goods = 10;
    public function __construct() {
        parent::__construct();
    }

    private function fileter_typeinfos($type_infos,&$type_info,&$binded_info,&$avatars)
    {
        $type_info = bonus_helper::filter_type($type_infos['type_info']);
        $avatars = $type_infos['avatars'];

        $binded_info = array();
        $binded_infos = $type_infos['binded_info'];
        if(is_array($binded_infos))
        {
            foreach($binded_infos as $val)
            {
                $user_info = bonus_helper::filter_bonus($val);
                if(isset($val['user_mobile'])) {
                    unset($val['user_mobile']);
                }
                array_push($binded_info,$user_info);
            }
        }
    }

    private function isMineType(bonus\type $type) {
        return ($type->sender_id() == $_SESSION['member_id'] || $type->relayer_id() == $_SESSION['member_id']);
    }

    public function openOp()
    {
        if(!isset($_GET['type_sn']) || empty($_GET['type_sn'])) {
            return self::outerr(errcode::ErrParamter,"需要红包 type_sn 参数.");
        }
        $type_sn  = $_GET['type_sn'];
        $type_infos = bonus_helper::get_typeinfo($type_sn);
        if(empty($type_infos)) {
            return self::outerr(errcode::ErrBonus,"无此红包.");
        }
        if(session_helper::need_wechat_author())
        {
            $author = new thrid_author\wxauthor();
            $url = author_url::bonus_open_url($type_sn);
            $url = $author->enter($url);
            return self::outsuccess(['direct_uri' => $url],"redirect");
        }

        $this->fileter_typeinfos($type_infos,$type_info,$binded_info,$avatars);
        $type = bonus\type::create_by_paramer($type_infos['type_info']);

        $isMineType = $this->isMineType($type);
        if($type->isStart() == false) {
            return self::outsuccess(array('type_info' => $type_info),"bonus/unstart");
        }

        $grabable = bonus_helper::can_grab($type,$_SESSION['member_id'],$_SESSION['member_mobile']);
        $mine_bonus = bonus_helper::get_mine_by_typesn($type_sn);
        if($mine_bonus != false)
        {   //显示我抢到的红包，按照状态进行判断
            $bonus = bonus\user_bonus::create_by_param($mine_bonus);
            $mine_bonus = bonus_helper::filter_bonus($mine_bonus);

            if($bonus->isBinded() == false && session_helper::isVerfiyMobile())
            {
                if($grabable == false) {
                    return self::outsuccess(array('type_info' => $type_info,'msg' => "已注册用户不能领邀请红包～"),"bonus/over");
                }

                $ret = bonus_helper::bind_bonus($bonus->bonus_sn(),$_SESSION['MPHPSESSID'],session_helper::cur_mobile(),$new_sn);
                if($ret == true) {
                    $mine_bonus = bonus_helper::get_mine_by_bonussn($new_sn);
                }
            }

            $summarys = $this->matched_goods($type_sn);
            $data = array('type_info' => $type_info,
                'mine_bonus' => $mine_bonus,
                'binded_info' => $binded_info,
                'avatars' => $avatars,
                'summarys' => $summarys);
            if($bonus->isBinded() || $isMineType) {
                return self::outsuccess($data,"bonus/detail");
            }
            else
            {
                if($type->isEnd()) {
                    return self::outsuccess(array('type_info' => $type_info),"bonus/end");
                } else {
                    return self::outsuccess($data,"bonus/bind");
                }
            }
        }
        else
        {
            if($grabable == false) {
                return self::outsuccess(array('type_info' => $type_info,'msg' => "已注册用户不能领邀请红包～"),"bonus/over");
            }
            //我没有抢过的情况
            if($isMineType == false)
            {
                if($type->isEnd()) {
                    return self::outsuccess(array('type_info' => $type_info),"bonus/end");
                }
                else if($type->binded_over()) {
                    return self::outsuccess(array('type_info' => $type_info,'msg' => "手慢了，红包已派完～"),"bonus/over");
                } else {
                    return self::outsuccess(array('type_info' => $type_info),"bonus/open");
                }
            }
            else
            {
                if($type->isEnd() || $type->binded_over()) {
                    $summarys = $this->matched_goods($type_sn);
                    return self::outsuccess(array('type_info' => $type_info,
                        'binded_info' => $binded_info,
                        'avatars' => $avatars,
                        'summarys' => $summarys),
                        'bonus/detail');
                }
                else {
                    return self::outsuccess(array('type_info' => $type_info),"bonus/open");
                }
            }
        }
    }
    public function detailOp()
    {
        if(!isset($_GET['type_sn']) || empty($_GET['type_sn'])) {
            return self::outerr(errcode::ErrParamter,"需要红包 type_sn 参数.");
        }
        $type_sn = $_GET['type_sn'];

        $type_infos = bonus_helper::get_typeinfo($type_sn);
        if(empty($type_infos)) {
            return self::outerr(errcode::ErrBonus,"无此红包.");
        }
        $this->fileter_typeinfos($type_infos,$type_info,$binded_info,$avatars);

        $type = \bonus\type::create_by_paramer($type_infos['type_info']);
        if($type->isStart() == false) {
            return self::outsuccess(array('type_info' => $type_info),"bonus/unstart");
        }

        $mine_bonus = bonus_helper::get_mine_by_typesn($type_sn);
        if($mine_bonus != false) { //显示我抢到的红包，按照状态进行判断
            $mine_bonus = bonus_helper::filter_bonus($mine_bonus);
        }

        $summarys = $this->matched_goods($type_sn);
        return self::outsuccess(array('type_info' => $type_info,
            'mine_bonus' => $mine_bonus,
            'binded_info' => $binded_info,
            'avatars' => $avatars,
            'summarys' => $summarys),
            'bonus/detail');
    }

    public function grabOp()
    {
        if(!isset($_GET['type_sn']) || empty($_GET['type_sn'])) {
            return self::outerr(errcode::ErrParamter,"需要红包 type_sn 参数.");
        }
        $type_sn = $_GET['type_sn'];

        $type_infos = bonus_helper::get_typeinfo($type_sn);
        if(empty($type_infos)) {
            return self::outerr(errcode::ErrBonus,"无此红包.");
        }
        $this->fileter_typeinfos($type_infos,$type_info,$binded_info,$avatars);

        $type = \bonus\type::create_by_paramer($type_infos['type_info']);
        if($type->isStart() == false) {
            return self::outsuccess(array('type_info' => $type_info),"bonus/unstart");
        }

        $grabable = bonus_helper::can_grab($type,$_SESSION['member_id'],$_SESSION['member_mobile']);

        $mine_bonus = bonus_helper::get_mine_by_typesn($type_sn);
        if($mine_bonus != false)
        { //显示我抢到的红包，按照状态进行判断
            $bonus_obj = \bonus\user_bonus::create_by_param($mine_bonus);
            $mine_bonus = bonus_helper::filter_bonus($mine_bonus);

            if($bonus_obj->isBinded()) {
                $summarys = $this->matched_goods($type_sn);
                return self::outsuccess(array('type_info' => $type_info,
                    'mine_bonus' => $mine_bonus,
                    'binded_info' => $binded_info,
                    'avatars' => $avatars,
                    'summarys' => $summarys),
                    "bonus/detail");
            }
            elseif($type->isEnd()) {
                 return self::outsuccess(array('type_info' => $type_info),"bonus/end");
            }
            else
            {
                if(session_helper::isVerfiyMobile())
                {
                    if($grabable == false) {
                        return self::outsuccess(array('type_info' => $type_info,'msg' => "已注册用户不能领邀请红包～"),"bonus/over");
                    }

                    $ret = bonus_helper::bind_bonus($bonus_obj->bonus_sn(),$_SESSION['MPHPSESSID'],session_helper::cur_mobile(),$new_sn);
                    if($ret == true) {
                        $mine_bonus = bonus_helper::get_mine_by_bonussn($new_sn);
                        $summarys = $this->matched_goods($type_sn);
                        return self::outsuccess(array('type_info' => $type_info,
                            'mine_bonus' => $mine_bonus,
                            'binded_info' => $binded_info,
                            'avatars' => $avatars,
                            'summarys' => $summarys),
                            "bonus/detail");
                    } else {
                        return self::outsuccess(array('type_info' => $type_info,
                            'mine_bonus' => $mine_bonus,'binded_info' => $binded_info,
                            'avatars' => $avatars),"bonus/bind");
                    }
                } else {
                    return self::outsuccess(array('type_info' => $type_info, 'mine_bonus' => $mine_bonus,'binded_info' => $binded_info,
                        'avatars' => $avatars),"bonus/bind");
                }
            }
        }
        else
        {
            if($grabable == false) {
                return self::outsuccess(array('type_info' => $type_info,'msg' => "已注册用户不能领邀请红包～"),"bonus/over");
            }
            if($type->isEnd()) {
                return self::outsuccess(array('type_info' => $type_info),"bonus/end");
            }
            elseif($type->binded_over()) {
                return self::outsuccess(array('type_info' => $type_info,'msg' => "手慢了，红包已派完～"),"bonus/over");
            }
            else
            {
                $bonus = bonus_helper::grab_bonus($type_sn);
                if($bonus == false) {
                    return self::outsuccess(array('type_info' => $type_info,'msg' => "手慢了，红包已派完～"),"bonus/over");
                }
                else
                {
                    $bonus_obj = \bonus\user_bonus::create_by_param($bonus);
                    $mine_bonus = bonus_helper::filter_bonus($bonus);
                    if($bonus_obj->isBinded())
                    {
                        $type_infos = bonus_helper::get_typeinfo($bonus_obj->type_sn());
                        if(empty($type_infos)) {
                            return self::outerr(errcode::ErrBonus,"无此红包.");
                        }
                        $this->fileter_typeinfos($type_infos,$type_info,$binded_info,$avatars);
                        $summarys = $this->matched_goods($type_sn);
                        $data = array('type_info' => $type_info,
                            'mine_bonus' => $mine_bonus,
                            'binded_info' => $binded_info,
                            'avatars' => $avatars,
                            'summarys' => $summarys);
                        return self::outsuccess($data,"bonus/detail");
                    } else {
                        $data = array('type_info' => $type_info, 'mine_bonus' => $mine_bonus,'binded_info' => $binded_info,'avatars' => $avatars);
                        return self::outsuccess($data,"bonus/bind");
                    }
                }
            }
        }
    }

    public function bindOp()
    {
        if(!isset($_GET['bonus_sn']) || empty($_GET['bonus_sn'])) {
            return self::outerr(errcode::ErrParamter,"请输入红包序列号.");
        }
        $bonus_sn = $_GET['bonus_sn'];
        $mine_bonus = bonus_helper::get_mine_by_bonussn($bonus_sn);
        if($mine_bonus == false) {
            return self::outerr(errcode::ErrParamter, "您不能领没有抢到的红包.");
        }

        $bonus_obj = \bonus\user_bonus::create_by_param($mine_bonus);
        if($bonus_obj->isBinded()) {
            $type_sn = $bonus_obj->type_sn();
            $type_infos = bonus_helper::get_typeinfo($type_sn);
            $this->fileter_typeinfos($type_infos,$type_info,$binded_info,$avatars);
            $mine_bonus = bonus_helper::filter_bonus($mine_bonus);
            $summarys = $this->matched_goods($type_sn);
            return self::outsuccess(array('type_info' => $type_info,
                'mine_bonus' => $mine_bonus,
                'binded_info' => $binded_info,
                'avatars' => $avatars,
                'summarys' => $summarys),
                "bonus/detail");
        }

        $type_sn = $bonus_obj->type_sn();
        $type_infos = bonus_helper::get_typeinfo($type_sn);
        $type = \bonus\type::create_by_paramer($type_infos['type_info']);

        $grabable = bonus_helper::can_grab($type,$_SESSION['member_id'],$_SESSION['member_mobile']);
        if($grabable == false) {
            $this->fileter_typeinfos($type_infos,$type_info,$binded_info,$avatars);
            return self::outsuccess(array('type_info' => $type_info,'msg' => "已注册用户不能领邀请红包～"),"bonus/over");
        }

        if($type->isEnd()) {
            return self::outsuccess(array('type_info' => $type_infos['type_info']),"bonus/end");
        }
        elseif(session_helper::isVerfiyMobile()) {
            $mobile = session_helper::cur_mobile();
        }
        else
        {
            $mobile = $_GET['mobile'];
            $validator = new Validator();
            $validator->setValidate(Validator::verify_mobile($mobile));
            $err = $validator->validate();
            if ($err != '') {
                return self::outerr(errcode::ErrParamter, $err);
            }

            if(!isset($_GET['code']) || empty($_GET['code'])) {
                return self::outerr(errcode::ErrParamter, "请输入验证码.");
            }
            $code = $_GET['code'];
            $ret = sms_helper::check_code(Sms::getbonus_code,$code,$mobile);
            if(is_array($ret)) {
                return self::outerr($ret['code'], $ret['msg']);
            }
            else
            {
                $grabable = bonus_helper::can_grab($type,$_SESSION['member_id'],$mobile);
                login_helper::onBinded($mobile,$code,session_helper::relay_id());
            }
        }

        if($grabable)
        {
            $ret = bonus_helper::bind_bonus($bonus_sn,$_SESSION['MPHPSESSID'],$mobile,$new_bonus_sn);
            if($ret == true) {
                $mine_bonus = bonus_helper::get_mine_by_bonussn($new_bonus_sn);
                $bonus_obj = \bonus\user_bonus::create_by_param($mine_bonus);
                $type_infos = bonus_helper::get_typeinfo($bonus_obj->type_sn());
                $this->fileter_typeinfos($type_infos,$type_info,$binded_info,$avatars);

                return self::outsuccess(array('type_info' => $type_info,
                    'mine_bonus' => $mine_bonus,
                    'binded_info' => $binded_info,
                    'avatars' => $avatars));
            } else {
                return self::outerr($ret['code'],$ret['msg']);
            }
        }
        else
        {
            $this->fileter_typeinfos($type_infos,$type_info,$binded_info,$avatars);
            return self::outsuccess(array('type_info' => $type_info,'msg' => "已注册用户不能领邀请红包～"),"bonus/over");
        }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public function shakeOp()
    {
        if(!isset($_GET['bonus_sn']) || empty($_GET['bonus_sn'])) {
            return self::outerr(errcode::ErrParamter,"请输入红包序列号.");
        }
        $bonus_sn = $_GET['bonus_sn'];
        $mine_bonus = bonus_helper::get_mine_by_bonussn($bonus_sn);
        if($mine_bonus == false) {
            return self::outerr(errcode::ErrParamter, "领到红包后才可以摇.");
        }

        if(!isset($_POST['strength']) || empty($_POST['strength'])) {
            $strength = intval($_POST['strength']);
        } else {
            $strength = 3;
        }

        $bonus_obj = \bonus\user_bonus::create_by_param($mine_bonus);
        if($bonus_obj->can_shake())
        {
            $type_sn = $bonus_obj->type_sn();
            $type_infos = bonus_helper::get_typeinfo($type_sn);
            $type = \bonus\type::create_by_paramer($type_infos['type_info']);
            if($type->isEnd()) {
                return self::outsuccess(errcode::ErrBonus,"该红包已经过期,不能再摇啦~");
            }
            elseif($type->binded_over()) {
                return self::outsuccess(errcode::ErrBonus,"该红包已经被领光,摇不出更多啦~");
            }
            elseif($type->isFixedAmount()) {
                return self::outsuccess(errcode::ErrBonus,"只有拼收起红包才可以摇哦~");
            }
            else
            {
                $ret = bonus_helper::check_shake($bonus_sn,$first);
                if($ret != true) {
                    return self::outerr($ret['code'], $ret['msg']);
                }

                $direction = bonus_helper::get_direction($first);
                $ret = bonus_helper::shake($bonus_obj->bonus_sn(),$strength,$direction);
                if($ret == true)
                {
                    $bonus = bonus_helper::get_mine_by_bonussn($bonus_obj->bonus_sn());
                    $type_infos = bonus_helper::get_typeinfo($bonus_obj->type_sn());
                    $this->fileter_typeinfos($type_infos,$type_info,$binded_info,$avatars);
                    $summarys = $this->matched_goods($type_sn);
                    return self::outsuccess(array('type_info' => $type_info,
                        'mine_bonus' => $bonus,
                        'binded_info' => $binded_info,
                        'avatars' => $avatars,
                        'summarys' => $summarys),
                        "bonus/detail");
                } else {
                    return self::outsuccess(errcode::ErrBonus,"只有拼收起红包才可以摇哦~");
                }
            }
        }
        else {
            return self::outerr(errcode::ErrParamter, "抢到或者领到红包后才可以摇.");
        }
    }

    //红包留言
    public function commentOp()
    {
        if(!isset($_GET['bonus_sn']) || empty($_GET['bonus_sn'])) {
            return self::outerr(errcode::ErrParamter,"请输入红包序列号.");
        }
        $bonus_sn = $_GET['bonus_sn'];
        if(!isset($_GET['comment']) || empty($_GET['comment'])) {
            return self::outerr(errcode::ErrParamter,"请输入留言.");
        }
        $comment = urldecode($_GET['comment']);

        if(bonus_helper::comment($bonus_sn,urldecode($comment)) == true) {
            return self::outsuccess(array());
        } else {
            return self::outerr(errcode::ErrBonusComment,"留言失败.");
        }
    }

    private function random_cids($type_sn)
    {
        $time = 0;
        $cids = [];
        if(array_key_exists('goods_sampler',$_SESSION) && array_key_exists($type_sn,$_SESSION['goods_sampler']))
        {
            $cids = $_SESSION['goods_sampler'][$type_sn]['cids'];
            $time = $_SESSION['goods_sampler'][$type_sn]['time'];
        }

        if($time < time() - 86400 || empty($cids))
        {
            if(session_helper::is_man()) {
                $cids = activity\goods_sampler::instance()->fetch_male(self::bonus_goods);
            } else {
                $cids = activity\goods_sampler::instance()->fetch_female(self::bonus_goods);
            }

            if(!empty($cids)) {
                $_SESSION['goods_sampler'][$type_sn]['cids'] = $cids;
                $_SESSION['goods_sampler'][$type_sn]['time'] = time();
            }
        }

        return $cids;
    }

    private function matched_goods($type_sn)
    {
        if(session_helper::isapp())
        {

        }
        else
        {
            $cids = $this->random_cids($type_sn);
            if(empty($cids)) return false;

            $model_goods = Model('goods');
            $items = $model_goods->getGoodsListByColorDistinct(array('goods_commonid' => array('in',$cids)),goods_helper::fieldstr,'','');

            $tmp =[];
            foreach ($items as $item) {
                $commonid = intval($item['goods_commonid']);
                $tmp[$commonid] = $item;
            }

            $goods_list = [];
            foreach ($cids as $cid)
            {
                if(array_key_exists($cid,$tmp)) {
                    $goods_list[] = $tmp[$cid];
                }
            }

            $helper = new goods_helper();
            $ret = $helper->summary($goods_list,$related_goods);

            return $ret['summary'];
        }
    }
}

function bonus_output_unstart($output)
{
    $type_info = $output['type_info'];
    $type = \bonus\type::create_by_paramer($type_info);

    $local_tm = time();
    $start_tm = $type->get_start_time();

    $str_localtime = "<input type=\"hidden\" id=\"server_local_time\" value=\"{$local_tm}\"/>";
    $str_starttime = "<input type=\"hidden\" id=\"server_start_time\" value=\"{$start_tm}\"/>";

    echo($str_localtime);
    echo($str_starttime);
}

function bonus_output_end($output)
{
    $type_info = $output['type_info'];
    $type = \bonus\type::create_by_paramer($type_info);
    $start_tm = $type->get_start_time();
    $end_time = $type->get_end_time();

    $day_secs = 24 * 60 * 60;
    if(($end_time - $start_tm) == $day_secs) {
        return "该红包已超过24小时。";
    } else {
        return "该红包已过期。";
    }
}

function bonus_output_grabinfo($output)
{
    $type_info = $output['type_info'];
    $type = \bonus\type::create_by_paramer($type_info);

    $msg = $output['msg'];
    if($type->binded_over()) {
        echo("<p>{$msg}</p>");
    }
    else if($type->grabed_over())
    {
        echo("<p>{$msg}</p>");
        $last_time = $type->grab_lastime();
        if($last_time)
        {
            $wait_sec = $last_time + 600 - time();
            $wait_min = intval($wait_sec / 60);
            $wait_sec = $wait_sec % 60;
            if($wait_min > 0) {
                echo("<p style=\"font-size:1rem;margin-top:2rem\">{$wait_min}分钟后，您可以尝试抢一个未及时领取的红包.</p>");
            } else {
                echo("<p style=\"font-size:1rem;margin-top:2rem\">{$wait_sec}秒后，您可以尝试抢一个未及时领取的红包.</p>");
            }
        }
    }
    else {
        echo("<p>{$msg}</p>");
    }
}

function bonus_output_sender_header($output)
{
    $type_info = $output['type_info'];
    $avatars = $output['avatars'];

    if(!empty($type_info))
    {
        $type = \bonus\type::create_by_paramer($type_info);
        $user_id = $type->sender_id();
        if(array_key_exists($user_id,$avatars)) {
            $avatar = $avatars[$user_id]['avatar'];
        } else {
            $avatar = RESOURCE_SITE_URL . '/mobile/defimg/female.png';
        }
    }
    else {
        $avatar = '';
    }
    return $avatar;
}

function bonus_output_sender($output)
{
    $type_info = $output['type_info'];
    if(!empty($type_info)) {
        $type = \bonus\type::create_by_paramer($type_info);
        echo($type->sender_name());
    } else {
        echo("");
    }
}

function bonus_output_openurl($output)
{
    $type_info = $output['type_info'];
    $type = \bonus\type::create_by_paramer($type_info);
    $type_sn = $type->getType_sn();

    $url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=open&type_sn={$type_sn}";
    return $url;
}

function bonus_output_detailurl($output)
{
    $type_info = $output['type_info'];
    $type = \bonus\type::create_by_paramer($type_info);
    $type_sn = $type->getType_sn();

    $url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=detail&type_sn={$type_sn}&client_type=wap";
    return $url;
}

function bonus_output_graburl($output)
{
    $type_info = $output['type_info'];
    $type = \bonus\type::create_by_paramer($type_info);
    $type_sn = $type->getType_sn();
    return url_helper::bonus_grab_url($type_sn);
}

function bonus_output_type($output)
{
    echo '<div class="name">';
    echo '<p class="p p_name" style="color:#454545">';
    $type_info = $output['type_info'];
    $type = \bonus\type::create_by_paramer($type_info);
    echo($type->name());
    if($type->isRandomAmount()) {
        echo '<s class="icon_pin"></s>';
    }
    echo '</p>';
    echo '<p class="prompt">' . $type_info['type_bless'] . '</p>';
    echo '</div>';
}

function bonus_output_present($output)
{
    if(session_helper::first_order() == false) return;

    $fcode = fcode\present_manager::instance()->fetch(session_helper::cur_mobile(),session_helper::session_id());
    if($fcode == false) return;

    $common_id  = $fcode['common_id'];
    $batch_code = $fcode['batch_code'];
    $fcode_url = BASE_SITE_URL . "/mobile/index.php?act=fcode&op=index&common_id={$common_id}&batch_code={$batch_code}";

    $model_goods = Model('goods');
    $items = $model_goods->getGoodsListByColorDistinct(array('goods_commonid' => $common_id),goods_helper::fieldstr,'','');
    if(empty($items)) return;

    $helper = new goods_helper();
    $ret = $helper->summary($items,$related_goods);
    $summary = $ret['summary'][0];

    $str = "<div class=\"prompt text_left\">
                <p class=\"prompt pro\">丽人亲友首单福利</p>
            </div>";

    $str.= "<div class=\"first_goods_box overflow_h text_left\">
                <div class=\"first_goods_img f_left\">
                    <img src=\"{$summary['goods_image_url']}\">
                </div>
                <div class=\"first_goods_msg f_right\">
                    <p class=\"p first_goods_name\">{$summary['goods_mobile_name']}</p>
                    <p class=\"p first_goods_desc\">{$summary['goods_jingle']}</p>
                    <p class=\"p first_goods_price bonus_price\"><span class=\"bonus_icon\"></span>{$summary['goods_promotion_price']}元</p>
                    <p><a href=\"{$fcode_url}\" class=\"center\">查看详情</a></p>
                </div>
            </div>";
    echo $str;
}

function bonus_output_mine($output)
{
    if($_SESSION['is_app']) {
        $show_down = false;
    } else {
        $show_down = true;
    }
    $mine_bonus = $output['mine_bonus'];
    if(!empty($mine_bonus))
    {
        $bonus = \bonus\user_bonus::create_by_param($mine_bonus);
        echo('<div class="price">');
        echo('<p><span>' . $bonus->bonus_value() . "</span>元</p>");
        echo('</div>');
        echo('<input type="hidden" id="mine_bonus" value=' ."{$bonus->bonus_sn()}>");
        $mobile = $bonus->user_mobile();
        $show_down = $bonus->isBinded() && $show_down;
    } else {
        $show_down = false;
        $mobile = '';
    }

    if($show_down) {
        echo('<div class="prompt bouns_pro">');
        echo("<p>红包已放至账户<span>{$mobile}</span></p>");
        echo('</div>');
    }
}

function bonus_out_bindtimeout($output)
{
    $mine_bonus = $output['mine_bonus'];
    if(!empty($mine_bonus))
    {
        $bonus = \bonus\user_bonus::create_by_param($mine_bonus);
        $grab_time = $bonus->grab_time();
        $time_out = 600;

        if($grab_time > 0)
        {
            if($grab_time + $time_out <= time() - 60) {
                return '您的红包随时可能被抢走，抓紧领啊～';
            } else {
                $left_time = $grab_time + $time_out - time();
                $mins = $left_time / 60;
                return "请{$mins}分钟内，输入手机号领取红包";
            }
        }
    }

    return '';
}

function bonus_out_bindedtime($period)
{
    $min  = 60;
    $hour = 3600;
    $day  = 24 * 3600;

    $days = intval($period / $day);
    $period = $period % $day;

    $houres = intval($period / $hour);
    $period = $period % $hour;

    $mins = intval($period / $min);
    $period = $period % $min;
    if($period > 0) {
        $mins += 1;
    }

    $time_str = '';
    if($days > 0) {
        $time_str .= "{$days}天";
    }
    if($houres > 0) {
        $time_str .= "{$houres}小时";
    }
    if($mins > 0) {
        $time_str .= "{$mins}分钟";
    }

    $time_str .= "领完.";
    return $time_str;
}

function bonus_output_bindedinfo($output)
{
    $type_info = $output['type_info'];
    $avatars = $output['avatars'];
    $type = \bonus\type::create_by_paramer($type_info);

    echo('<div class="prompt text_left">');

    if($type->binded_over()) {
        $prompt = "{$type->getTotal_num()}个红包";
        $prompt .= ",";
        $period = $type_info['binded_period'];
        $prompt .= bonus_out_bindedtime($period);
    }
    else
    {
        $prompt = "领取 {$type->binded_num()}/{$type->getTotal_num()}";
        if($type->isEnd() == false)
        {
            if(util::from_wechat()) {
                $prompt = $prompt . "点击右上角继续分享此红包";
            }
        }
    }
    echo('<p class="prompt pro">' .$prompt. '</p>');
    echo('</div>');

    $bined_info = $output['binded_info'];
    if(empty($bined_info)) return;

    echo '<div class="list">';

    foreach($bined_info as $val)
    {
        $bonus = \bonus\user_bonus::create_by_param($val);
        $user_id = $bonus->user_id();
        if(array_key_exists($user_id,$avatars)) {
            $avatar = $avatars[$user_id]['avatar'];
            $discount = intval($avatars[$user_id]['discount'] * 100 + 0.5);
        } else {
            $avatar = RESOURCE_SITE_URL . '/mobile/defimg/female.png';
            $discount = 0;
        }

        echo('<div class="li">');
        echo('<div class="li_head_image">');
        echo('<img src="' . $avatar .'" />');
        echo('</div>');

        echo('<div class="li_left">');
        echo('<p class="p">' . $bonus->user_name() .'</p>');
        if($discount !== 0) {
            $discount = $discount / 100;
            echo "<p class=\"prompt_s\">已累计节省{$discount}元</p>";
        }

        if(empty($bonus->user_comment()))
        {
            if($bonus->user_mobile() == $_SESSION['member_mobile']) {
                echo('<p class="prompt" id="my_prompt"><a href="javascript:void(0)" id="message" style="color: #1927D1;display:inline-block;padding-top: 0.1rem;">留言</a></p>');
                echo('<p class="prompt">' . $bonus->get_time_format() . '</p>');
            } else {
                echo('<p class="prompt">' . $bonus->get_time_format() . '</p>');
                echo('<p class="prompt"></p>');
            }
        }
        else
        {
            echo('<p class="prompt">' . $bonus->user_comment() . '</p>');
            echo('<p class="prompt">' . $bonus->get_time_format() . '</p>');
        }

        echo('</div>');
        echo('<div class="li_right">');
        echo('<p class="p">' . $bonus->bonus_value() . '元</p>');
        echo('<p><span class="hilt">' . $bonus->bonus_rate() . '<span class="bonus_scale"></span></span></p>');
        if($type->binded_over() && $type->isMaxAmount($bonus->bonus_value()) && $type->isRandomAmount()) {
            echo('<p class="king_box"><s class="king"></s>手气最佳</p>');
        }
        echo('</div>');
        echo('</div>');
    }
    echo('</div>');
}

function android_downpath()
{
    $url = BASE_SITE_URL . '/data/upload/package/xmmz_release.apk';
    return $url;
}

function share_url($output)
{
    return url_helper::bonus_open_url($output['type_info']['type_sn']);
}

function share_title($output)
{
    $type_info = $output['type_info'];
    $type = \bonus\type::create_by_paramer($type_info);
    $share_id = $type->share_id();
    $share = bonus_helper::get_share($share_id);
    return $share['title'];
}

function share_subtitle($output)
{
    $type_info = $output['type_info'];
    $type = \bonus\type::create_by_paramer($type_info);
    $share_id = $type->share_id();
    $share = bonus_helper::get_share($share_id);
    return $share['sub_title'];
}

function share_image($output)
{
    $type_info = $output['type_info'];
    $type = \bonus\type::create_by_paramer($type_info);
    $share_id = $type->share_id();
    $share = bonus_helper::get_share($share_id);
    return $share['img_url'];
}

class tpl_goods
{
    private $mSummary;
    public function reinit($summary) {
        $this->mSummary = $summary;
    }
    public function base_info()
    {
        $country = $this->mSummary['brand_country'];
        if(empty($country)) {
            $str  = "<p class=\"text_left goods_name\">{$this->mSummary['goods_mobile_name']}</p>";
        }
        else {
            $str  = "<p class=\"text_left goods_name\">{$country}·{$this->mSummary['goods_mobile_name']}</p>";
        }
        $str .= " <p class=\"text_left goods_desc\">{$this->mSummary['goods_jingle']}</p>";
        return $str;
    }

    public function price()
    {
        $act_id = $this->mSummary['act_id'];
        if($act_id > 0) {
            return $this->groupbuy();
        } else {
            return $this->normal();
        }
    }
    private function normal()
    {
        $discount = $this->mSummary['goods_price'] - $this->mSummary['bonus_price'];
        $str = "<span class=\"f_right\">红包抵{$discount}</span>";

        $market_price = intval($this->mSummary['goods_price'] * 100 + 0.5) / 100;
        $outstr = "<p class=\"bonus_price_box text_left\">¥<span class=\"bonus_price\">{$this->mSummary['bonus_price']}</span></p>
                   <p class=\"overflow_h favourable\">
                        <span class=\"f_left\">专柜价{$market_price}</span>
                        {$str}
                   </p>";
        return $outstr;
    }

    private function groupbuy()
    {
        $discount = $this->mSummary['goods_price'] - $this->mSummary['goods_promotion_price'];
        $str  = "<span class=\"f_right\">立省{$discount}元</span>";

        $market_price = intval($this->mSummary['goods_price'] * 100 + 0.5) / 100;
        $outstr = "<p class=\"bonus_price_box text_left\">¥<span class=\"bonus_price\">{$this->mSummary['goods_promotion_price']}</span></p>
                   <p class=\"overflow_h favourable\">
                        <span class=\"f_left\">专柜价{$market_price}</span>
                        {$str}
                   </p>";
        return $outstr;
    }
    public function showicon()
    {
        $act_id = $this->mSummary['act_id'];
        if($act_id > 0) {
            $str = "<div class=\"grab_icon\"></div>";
        }
        else
        {
            if($this->mSummary['is_new']) {
                $str = "<div class=\"new_icon\"></div>";
            } else {
                $str = '';
            }
        }
        return $str;
    }
}

function bonnus_out_goods($output)
{
    $summarys = $output['summarys'];
    if($summarys == false) return;


    echo '<div class="prompt text_left"> 
          <p class="pro">热卖商品</p>
          </div>';

    $printer = new tpl_goods();
    echo '<div class="goods_items">';
    foreach ($summarys as $summary)
    {
        $printer->reinit($summary);
        echo "<div class=\"goods_box\">
            <a href=\"{$summary['goods_url']}\">
                <div class=\"goods\">
                    <div class=\"goods_img\">
                        <img src=\"{$summary['goods_image_url']}\">
                    </div>
                    <div class=\"goods_msg\">
                           {$printer->base_info()}
                        <div class=\"price_box\">
                           {$printer->price()}
                        </div>
                       {$printer->showicon()}
                    </div>
                </div>
            </a>
        </div>";
    }
    echo '</div>';
}

function bonus_out_rule()
{
    echo '<div class="briefing_address"></div>';
}

function bonus_out_download()
{
    if(!session_helper::isapp())
    {
        echo "<div class=\"fixed_bottom\">
                    <span class=\"flex_logo\"></span>
                    <div class=\"label\">
                        <p>熊猫美妆</p>
                        <p>下载APP即可得更多奖励</p>
                    </div>
                    <a href=\"#\" class=\"download_app_btn\" id=\"link\">打开APP</a>
              </div>";
    }
}

function bonus_out_brands()
{
    if($_SESSION['is_app']) {
        return;
    }

    $brandsex = brand_helper::instance()->brandex();
    if(empty($brandsex)) return;
    $brands = $brandsex['brands'];
    if(empty($brands)) return;


    echo '<div class="prompt text_left">
        <p class="pro">官方授权品牌</p>
    </div>
    <div class="brands_list">';

    $count = count($brands);
    foreach ($brands as $brand) {
        $logo = $brand['brand_logo'];
        echo "<div><img src=\"{$logo}\"/></div>";
    }
    $left = $count % 3;
    if($left > 0) {
        $left = 3 - $left;
    }
    for($index = 0; $index < $left; ++$index) {
        echo "<div></div>";
    }
    echo "</div>";
}

function bonus_canshare($output)
{
    $type_info = $output['type_info'];
    if(empty($type_info)) return 'false';

    $type = \bonus\type::create_by_paramer($type_info);
    $make_type = $type->send_type();
    if($type->binded_over() || $type->isEnd() || $make_type == bonus\type::MakeVoteType) {
        return 'false';
    } else {
        return 'true';
    }
}