<?php
/**
 * 物流入口文件
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */
namespace Common\Model;
use Think\Model;
class DrageditModel extends Model{
    /** 
     * 获取筛选条件数组
     * @return array
     */
    public function getorderlist(){
        return array(
            array('order'=>'sort desc','order_name'=>'序号越大越靠前'),
            array('order'=>'sort asc','order_name'=>'序号越小越靠前'),
            array('order'=>'is_hot desc','order_name'=>'最热排在靠前'),
            array('order'=>'addtime asc','order_name'=>'创建时间越早越靠前'),
            array('order'=>'addtime desc','order_name'=>'创建时间越晚越靠前'),
            array('order'=>'sell_count desc','order_name'=>'销量越高越靠前'),
            array('order'=>'sell_count asc','order_name'=>'销量越低越靠前'),
        );
    }
    /** 
     * 获取筛选商品类型条件数组
     * @return array
     */
    public function getgoodstypelist(){
        //（1.直营商品 2.赠品 3.积分 4.服务 5.订货商品 6.套餐）
        return array(
            '1'=>array('id'=>'1','name'=>'直营商品','model'=>'Mobile/Goods/goods_cont'),
            '4'=>array('id'=>'4','name'=>'服务','model'=>'Mobile/Goods/service_cont'),
            '5'=>array('id'=>'5','name'=>'订货商品','model'=>'Mobile/takeout/goods_index'),
            '6'=>array('id'=>'6','name'=>'套餐','model'=>'Mobile/Goods/package_cont'),
        );
    }
    /** 
     * 获取卡券类型数组
     * @return array
     */
    public function getkaquanlist($code='',$val=''){
        $getkaquanlist = array(
            'GENERAL_COUPON'=>array('name'=>'优惠券'),
            'DISCOUNT'=>array('name'=>'折扣券'),
            'GIFT'=>array('name'=>'礼品券'),
            'CASH'=>array('name'=>'代金券'),
            'GROUPON'=>array('name'=>'团购券'),
        );
        if($code&&$val)return $getkaquanlist[$code][$val];
        return $getkaquanlist;
    }
    /**
     * 组装分页链接
     * @param $listRows 分页数
     * @param $totalRows 数据总数
     * @param $nowPage 当前页数
     * @return string
     */
    public function pageshow($listRows,$totalRows,$nowPage) {
        $page_arr = array();
        if(0 == $totalRows) return $page_arr;
        $rollPage = 10;//分页栏显示的页数

        /* 计算分页信息 */
        $totalPages = ceil($totalRows / $listRows); //总页数
        if(!empty($totalPages) && $nowPage > $totalPages) {
            $nowPage = $totalPages;
        }

        /* 计算分页零时变量 */
        $now_cool_page = $rollPage/2;
        $now_cool_page_ceil = ceil($now_cool_page);
        //上一页
        $up_row  = $nowPage - 1;
        $up_page = $up_row > 0 ? '<a href="javascript:;" onclick="getgoodslist('.$up_row.')" class="prev"></a>' : '<a href="javascript:;" class="prev disabled"></a>';
        //下一页
        $down_row  = $nowPage + 1;
        $down_page = ($down_row <= $totalPages) ? '<a href="javascript:;" @click="getgoodslist('.$down_row.')" class="next"></a>' : '<a href="javascript:;" class="next disabled"></a>';
        //第一页
        $the_first = '';
        if($totalPages > $rollPage && ($nowPage - $now_cool_page) >= 1){
            $the_first = '<a href="javascript:;" onclick="getgoodslist(1)">1...</a>';
        }

        //最后一页
        $the_end = '';
        if($totalPages > $rollPage && ($nowPage + $now_cool_page) < $totalPages){
            $the_end = '<a href="javascript:;" onclick="getgoodslist('.$totalPages.')">'.$totalPages.'...</a>';
        }
        //数字连接
        $link_page = "";
        for($i = 1; $i <= $rollPage; $i++){
            if(($nowPage - $now_cool_page) <= 0 ){
				$page = $i;
			}elseif(($nowPage + $now_cool_page - 1) >= $totalPages){
				$page = $totalPages - $rollPage + $i;
			}else{
				$page = $nowPage - $now_cool_page_ceil + $i;
            }
            $class = '';
            $click = 'getgoodslist('.$page.')';
            if($nowPage==$i){
                $class = 'cur';
                $click = 'return false';
            }
            if($page > 0){
                if($page <= $totalPages){
                    $link_page .= '<a href="javascript:;" onclick="'.$click.'" class="'.$class.'">'.$page.'</a>';
                }else{
                    break;
                }
            }
        }
		return $the_first.$up_page.$link_page.$down_page.$the_end;
    }
    /**
     * 得到节点树
     */
    public function getcatelist(){
        // $GoodsCategory = getcache('GoodsCategory');
        $GoodsCategory = getcache('PurchaseCategory');
        $Tree = new \Common\Vendor\Tree();
        $newarr = $Tree->treemini($GoodsCategory);
        foreach($newarr as $k=>$v){
            $list[$k]['name'] = str_repeat('&nbsp;&nbsp;',$v['Count']).'|--'.$v['name']; 
            $list[$k]['fname'] = $v['name'];
            $list[$k]['id'] = $v['id'];
        }
        $add = array('id'=>0,'name'=>'请选择分类');
        array_unshift($list, $add);
        return $list;
    }
    /**
     * 格式化diy数据【存储JSON数据】
     * @param array $data
     */
    public function formatdiydata($data){
        $Fpostdata = json_decode(htmlspecialchars_decode($data),true);
        $list = array();
        foreach($Fpostdata as $k=>$v){
            $list[$v['sort']] = $v;
            unset($list[$k]['sort']);
            if($v['content']['nav_list']||$v['content']['poster_list']){
                $sonlist = array();
                if(isset($v['content']['nav_list'])){
                    $sonlist = $v['content']['nav_list'];
                    $stype = 'nav_list';
                }else if(isset($v['content']['poster_list'])){
                    $sonlist = $v['content']['poster_list'];
                    $stype = 'poster_list';
                }
                foreach($sonlist as $sk=>$sv){
                    if(!$sv['is_url']){
                        $urlstr = '';
                        $urlarr = array();
                        if(isset($sv['link']['url_navtype'])&&$sv['link']['url_navtype']){
                            $urlarr[] = $sv['link']['url_navtype'];
                            $urlarr[] = isset($sv['link']['url_modulename'])?$sv['link']['url_modulename']:'';
                            $urlarr[] = ($sv['is_param'])?$sv['url_param']:'';
                            $urlstr = 'url:'.implode(':',$urlarr);
                        }
                        $list[$v['sort']]['content'][$stype][$sk]['url'] = $urlstr;
                    }
                }
            }
        }
        ksort($list);
        return $list;
    }
    /**
     * 格式化diy数据【格式化JSON数据，模板展示】
     * @param array $data
     * rank 会员等级
     */
    public function formatdiylist($data,$page='',$postdata='',$rank=''){
        $list = json_decode($data,true);
        $orderlist = $this->getorderlist();

        if($rank !=''){
            $ran= $rank;
        }
      
        $member_info=session('member_info');$uid=$member_info['id'];
        if(isset($member_info['rank_id'])){
            $ran= $member_info['rank_id'];
        }
        $shop_id = get_wxsmall_shop_id();
        foreach($list as $k=>$v){
            $list[$k]['shop_id']=$shop_id;
            //格式化为模板通用链接
            if($v['content']['nav_list']||$v['content']['poster_list']){
                $sonlist = array();
                if(isset($v['content']['nav_list'])){
                    $sonlist = $v['content']['nav_list'];
                    $stype = 'nav_list';
                }else if(isset($v['content']['poster_list'])){
                    $sonlist = $v['content']['poster_list'];
                    $stype = 'poster_list';
                }
                foreach($sonlist as $sk=>$sv){
                    $list[$k]['content'][$stype][$sk]['url'] = rulestourl($sv['url'],true,'',array('eid'=>$uid));
                }
            }
            //格式化为模板可用的商品列表
            if($v['data_type']==3){//格式化商品链接18.03.23
                if($v['content']['goodslist']){
                    if($shop_id)unset($list[$k]['content']['initgoodslist']);
                    if(empty($is_show)){
                        $s_map=array();
                        $s_map['shop_id']=$shop_id;
                        $s_map['goods_number']=array('gt',0);
                        $goodsstock=M("ShopsGoodsStock")->where($s_map)->getField('goods_id',true);
                        $goodsstock=array_unique($goodsstock);
                    }
                    $getgoodstypelist = $this->getgoodstypelist();
                    foreach($v['content']['goodslist'] as $sk=>$sv){
                        $map=array();
                        $map['id']=$sv['id'];
                        $goods_info=M("Goods")->where($map)->field('goods_name,shop_price,img,pt_id')->find();
                        $list[$k]['content']['goodslist'][$sk]['url'] = geturl($getgoodstypelist[1]['model'],array('id'=>$sv['id'],'eid'=>$uid));
                        $list[$k]['content']['goodslist'][$sk]['img'] = thumb($goods_info['img'],350,350,'noimg_430x430.png');
                        $list[$k]['content']['goodslist'][$sk]['activity_img'] = thumb($goods_info['activity_img'],'','','noimg_500_250.jpg');
                        $list[$k]['content']['goodslist'][$sk]['price'] =format_price($goods_info['shop_price']);
                        $goods_number=D("Common/Cart")->shopgoodsstock($sv['id'],$shop_id,'');
                        $list[$k]['content']['goodslist'][$sk]['stock']=$goods_number;
                        if($sv['pt_id']){
                            $list[$k]['content']['goodslist'][$sk]['pin_url'] = geturl("FightGroup/cont",array('id'=>$v['id'],'eid'=>$uid));
                        }
                        $map = array();$map['goods_id'] = $sv['id'];$map['shop_id'] = $shop_id;
                        $sell_count = M('ShopsGoodsStock')->where($map)->getField('sell_count');
                        $list[$k]['content']['goodslist'][$sk]['sell_count'] = floatval($sell_count);
                        if($goodsstock && !in_array($sv['id'],$goodsstock)){
                            unset($list[$k]['content']['goodslist'][$sk]);
                        }

                    }
                }
            }
            //格式化为模板可用的商品列表
            if($v['data_type']==4){//类型为4为商品列表模块
                $firstPriority = $orderlist[$v['content']['firstPriority']]['order'];
                $secondPriority = $orderlist[$v['content']['secondPriority']]['order'];
                $thirdPriority = $orderlist[$v['content']['thirdPriority']]['order'];
                $stype = 'goodslist';
                $order = $firstPriority;
                if($firstPriority!=$secondPriority)$order.=','.$secondPriority;
                if($firstPriority!=$thirdPriority&&$secondPriority!=$thirdPriority)$order.=','.$thirdPriority; 
                $map = array();
                $shop_id = get_wxsmall_shop_id();//用户选中的门店id
                if(empty($postdata))$postdata['shop_id']=$shop_id;
                if($v['content']['is_timelimit']==2){//调取限时促销的商品					
                    $map['is_limit_time']=1;
                    $map['pid']=0;
                    $map['start_time']=array('lt',NOW_TIME);
                    $map['end_time']=array('gt',NOW_TIME);
					$map['_string']="shop_id like '%,".$shop_id.",%' OR shop_id='0'";
                    $act_info=M('PromotAct')->where($map)->field('id,end_time')->find();
                    if($act_info){
                        $map = array();
                        //商品条件
                        $map['is_promote']=1;
                        $map['promote_price']=array('gt',0);
                        $map['promote_id']=$act_info['id'];
                        $map['promote_start_time']=array('lt',NOW_TIME);
                        $map['promote_end_time']=array('gt',NOW_TIME);
                        $maps = array();
                        $maps['pagesize'] = $v['content']['goodsNum'];
                        $maps['type'] = 1;
                        $result = $this->get_goods_list('Goods',$maps,$order,$map,$postdata,$ran);
                        $list[$k]['content']['end_time'] = $act_info['end_time'];
                        $list[$k]['content'][$stype] = !empty($result['status'])?$result['list']:array();
                    }else{
                        $list[$k]['content'][$stype] = array();
                    }
                }else{

                    $maps = array();
                    $maps['pagesize'] = $v['content']['goodsNum'];
                    $maps['type'] = 1;
                    $result = $this->get_goods_list('Goods',$maps,$order,$map,$postdata,$ran);
//                    $list[$k]['content']['end_time'] = $act_info['end_time'];
                    $list[$k]['content'][$stype] = !empty($result['status'])?$result['list']:array();
                }
            }
            //格式化为模板可用的附近门店列表
//            if($v['data_type']==17){
//                $location_list = $map = $service_list = $takeout = $active = array();
//                $stype = 'location_list';
//                $map['status'] = 1;
//				$map['id'] = get_wxsmall_shop_id();
//                if($map['id']){
//                    $location_list = M('Shops')->where($map)->field('id,name,phone,picture,province,city_id,district,address')->limit($v['content']['num'])->select();
//                    foreach($location_list as $sk=>$sv){
//                        $location_list[$sk]['url'] = geturl('Mobile/Shop/mendian_index',array('id'=>$sv['id'],'eid'=>MID));
//                        $location_list[$sk]['title'] = $sv['name'];
//                        $location_list[$sk]['img'] = $sv['picture'];
//                        $province = idtoname($sv['province'],'Area');
//                        $city_id = idtoname($sv['city_id'],'Area');
//                        $district = idtoname($sv['district'],'Area');
//                        $location_list[$sk]['address'] = $province.$city_id.$district.$address;
//                        //调取门店参与的活动
//                        $active = D('Common/Takeout')->get_shops_active($sv['id']);
//                        if($active['is_firstorderdiscount']){
//                            foreach($active['is_firstorderdiscount'] as $tk=>$tv){
//                                $location_list[$sk]['is_firstorderdiscount']['dec'] .= $tv['dec'].' ';
//                            }
//                        }
//                        if($active['is_payonlinediscount']){
//                            foreach($active['is_payonlinediscount'] as $tk=>$tv){
//                                $location_list[$sk]['is_payonlinediscount']['dec'] .= $tv['dec'].' ';
//                            }
//                        }
//                        //调取服务列表
//                        $shop_ids = '';
//                        $map = array();
//                        $shop_ids = ','.$sv['id'].',';
//                        $map['goods_type'] = 4;
//                        $map['status'] = 1;
//                        $map['is_delete'] = 0;
//                        $map['publish_wait'] = 1;
//                        $map['_string'] = "shop_ids like '%".$shop_ids."%' OR shop_ids='0'";
//                        $service_list = array();
//                        if($v['content']['servicenum']>0){
//                            $service_list = M('Goods')->where($map)->field('id,goods_name,shop_price,sell_count')->order('sort desc,id desc')->limit($v['content']['servicenum'])->select();
//                            foreach($service_list as $tk=>$tv){
//                                $service_list[$tk]['url'] = geturl('Mobile/Goods/service_cont',array('id'=>$tv['id'],'eid'=>MID));
//                                $service_list[$tk]['shop_price'] = format_price($tv['shop_price']);
//                            }
//                        }
//                        $location_list[$sk]['service_list'] = $service_list;
//                        //调取商品列表
//                        $map = $goodsids = $takeout_list = array();
//                        if($v['content']['tekeoutsnum']>0){
//                            $map['shop_id']=$sv['id'];
//                            $map['goods_number']=array('gt',0);
//                            $goodsids=M('ShopsGoodsStock')->where($map)->getField('goods_id',true);//该门店下首页商品id
//                            $goodsids = array_unique($goodsids);
//                            if(!empty($goodsids)){
//                                $map = array();
//                                $map['id']=array('in',$goodsids);
//                                $map['goods_type']=5;
//                                $map['status']=1;
//                                $map['is_delete']=0;
//                                $map['publish_wait'] = 1;
//                                $takeout_list=M('Goods')->where($map)->field('id,goods_name,shop_price,market_price,sell_count,img')->order('sort desc,id desc')->limit($v['content']['tekeoutsnum'])->select();
//                                foreach($takeout_list as $tk=>$tv){
//                                    $takeout_list[$tk]['url'] = geturl('Mobile/takeout/goods_index',array('id'=>$tv['id'],'shop_id'=>$sv['id'],'eid'=>MID));
//                                    $takeout_list[$tk]['shop_price'] = format_price($tv['shop_price']);
//                                    $takeout_list[$tk]['market_price'] = format_price($tv['market_price']);
//                                }
//                            }
//                        }
//                        $location_list[$sk]['takeout_list'] = $takeout_list;
//                    }
//                }
//                $list[$k]['content'][$stype] = $location_list;
//            }
            //格式化为模板可用的优惠活动列表
            if($v['data_type']==18){
                $map = $result = array();
                $stype = 'promot_list';
                $map['start_time'] = array('lt', NOW_TIME);
                $map['end_time'] = array('gt', NOW_TIME);
                $map['act_type'] = array('in', array(1,2,3,4));
                $map['is_pt'] = 0;//排除拼团
                $map['is_limit_time'] = 0;//限时促销
                $result = page_list('PromotAct', $map, 'sort desc', '*', $v['content']['num'], '', 1);
                $promot_list = $result['list'];
                foreach ($promot_list as $sk => $sv) {
//                    if ($sv['act_type'] == 1) {
//                        $promot_list[$sk]['title_name'] = '满' . $sv['min_amount'] . '减' . $sv['act_type_ext'];
//                    } elseif ($sv['act_type'] == 2) {
//                        $promot_list[$sk]['title_name'] = '满' . $sv['min_amount'] . '享' . ($sv['act_type_ext'] / 10) . '折';
//                    } else {
//                        $promot_list[$sk]['title_name'] = $sv['act_name'];
//                    }
                    $promot_list[$sk]['url'] = geturl('Mobile/promot_act/cont',array('id'=>$sv['id'],'eid'=>$uid));
                }
                $list[$k]['content'][$stype] = $promot_list; 
            }
            //格式化为模板可用的卡券列表
            if($v['data_type']==19){
                $map = $where = $coupons_list = array();
                $stype = 'coupons_list';
                $map['status'] = 1;
                $map['coupons_type'] = 1;  
                $map['_string'] = 'type = 2 or (type = 1 and end_timestamp > '.NOW_TIME.')';     
                $coupons_list = M('Coupons')->where($map)->field('id,logo_url,rank_id,title,type,begin_timestamp,end_timestamp,fixed_begin_term,fixed_term')->order('sort,add_time desc')->limit($v['content']['num'])->select();  
                foreach($coupons_list as $sk=>$sv){
                    $remarks = '';
                    $coupons_list[$sk]['url'] = geturl('Mobile/Index/kaquan',array('eid'=>$uid));
                    $coupons_list[$sk]['logo_url'] = thumb($sv['logo_url'],76,78);
                    $coupons_list[$sk]['rank_id'] = $this->getkaquanlist($sv['rank_id'],'name');
                    if($sv['type']==1){
                        $remarks = time_format($sv['begin_timestamp'],'Y-m-d').'~'.time_format($sv['end_timestamp'],'Y-m-d');
                    }else{
                        $remarks = '领取后'.$sv['fixed_begin_term'].'天后生效,有效期'.$sv['fixed_term'].'天';
                    }
                    $coupons_list[$sk]['remarks'] = $remarks;
                }  
                $list[$k]['content'][$stype] = $coupons_list; 
            }
            //格式化为模板可用的广告位
            if($v['data_type']==11||$v['data_type']==12||$v['data_type']==13||$v['data_type']==14||$v['data_type']==15){
                $weigh_height = array(
                    '11'=>array(array('340','700'),array('340','340'),array('340','340')),
                    '12'=>array(array('340','360'),array('450','180'),array('225','180'),array('225','180')),
                    '13'=>array(array('225','180'),array('225','180'),array('225','180'),array('300','180'),array('225','180'),array('225','180')),
                    '14'=>array(array('375','180'),array('375','180'),array('375','180'),array('375','180'),array('375','180'),array('375','180')),
                    '15'=>array(array('180','180'),array('180','180'),array('180','180'),array('180','180'))
                );
                if($v['content']['poster_list']){
                    foreach($v['content']['poster_list'] as $sk=>$sv){
                        $list[$k]['content']['poster_list'][$sk]['img'] = thumb($sv['img'],$weigh_height[$v['data_type']][$sk][0],$weigh_height[$v['data_type']][$sk][1]);
                    }
                }
            }
        }
        return json_encode($list);
    }
    public function get_goods_list($name='',$data=array(),$order='',$param=array(),$postdata=array(),$rank=''){
        $return['status'] = 0;
        $data['is_delete'] = 0;
        $data['publish_wait'] = 1;
        /*
            $rank 会员等级
        */
        $member_info=session('member_info');
        if($member_info){
           
            $rank_id=$member_info['rank_id'];
        }
        if($rank !=''){
            $rank_id=$rank;
        }
       // $member_info=session('member_info');$uid=$member_info['id'];
        $map = D('Admin/'.$name)->index_condition($data,5);
        unset($map['shop_id']);
        if(!empty($param)){//调取限时商品条件
            $map = array_unique(array_merge($map,$param));
        }
        // $map['goods_type'] = isset($data['goods_type'])?$data['goods_type']:1;//类型（1.直营商品 2.赠品 3.积分 4.服务 5.订货商品 6.套餐）
        $map['goods_type'] = 5;
        $map['is_internet']=1;
        $map['is_offline']=1;//线上
		$map['is_jinsheng'] = 0;
        $map['status']=1;
        $map['is_delete']=0;
        //获取分类筛选
        if($data['type']!=1) $return['catelist'] = $this->getcatelist();
        $pagesize = isset($data['pagesize'])?$data['pagesize']:8;
        $REQUEST = (array)I('request.');
        if(!isset($REQUEST['p'])&&isset($REQUEST['page']))$REQUEST['p']=$REQUEST['page'];
        if(intval($REQUEST['p'])<=0)$REQUEST['p']=1;
        $data['page']=$REQUEST['p'];
        if(!isset($data['page']))$data['page']=1;
        $preve = (intval($data['page'])-1)*$pagesize;
        $limit = "$preve,$pagesize";
        //获取diy分页模板
        if(isset($data['showpage'])&&$data['showpage']==1){
            $count = M($name)->where($map)->count();
            $return['pagelist'] = $this->pageshow($pagesize,$count,$data['page']);
        }
        if(empty($order))$order = 'sort desc,id desc';
        $list = M($name)->where($map)->field(true)->order($order)->limit($limit)->select();
        if($list){$member_info=session('member_info');$uid=$member_info['id'];
            $shop_id=get_wxsmall_shop_id();
            $getgoodstypelist = $this->getgoodstypelist();
            foreach($list as $k=>$v){
                $goods_number=D("Common/Cart")->shopgoodsstock($v['id'],$shop_id,'');
                $list[$k]['stock']=$goods_number;
                $list[$k]['url'] = geturl($getgoodstypelist[1]['model'],array('id'=>$v['id'],'eid'=>$uid));            
                $price_info=D('Common/Takeout')->get_min_price($shop_id,$v['id']);
                $list[$k]['price_info']=$price_info;                
                $list[$k]['title'] = $v['goods_name'];
                $list[$k]['price'] = format_price($v['shop_price']);
                $list[$k]['type_show']=1;
                $goods_stock=D('Common/Cart')->shopgoodsstock($v['id'],$postdata['shop_id']);
                $list[$k]['goods_number'] =$v['goods_number']=$goods_stock['goods_number'];
                //319新加
                $list[$k]['promote_type']=0;
                $list[$k]['is_ptdue'] = 2; //拼团已过期
                $list[$k]['is_overdue'] = 2; //活动结束
                $list[$k]['pover_number'] =2;
                if($v['is_promote']){
                    $list[$k]['promote_type']=1;
                    $list[$k]['is_promote'] =$v['is_promote'];
                    $list[$k]['promote_price'] = $v['promote_price'];
                    // $list[$k]['promote_number'] = intval($v['sell_count']); //已抢
                    // if($v['goods_number']>0) $list[$k]['pover_number'] = intval($v['goods_number'])-intval($v['sell_count']); //仅剩
                    // if($v['goods_number']==-1) $list[$k]['pover_number'] = 99999; //仅剩
                    $promote_info = M('PromotAct')->where('id='.$v['promote_id'])->field(true)->find();
                    $p_num=D('Common/Takeout')->pro_num($promote_info,'',$v['id'],1);
                    $list[$k]['promote_number'] = $p_num; //已抢
                    $list[$k]['pover_number'] =intval($v['goods_max_number'])-intval($p_num);

                     /*
                     *
                     *  promote_id 促销id 
                     */
                    if($v['promote_end_time']<NOW_TIME || ($rank_id<$promote_info['user_rank'])){
                        $list[$k]['is_overdue'] = 1; //活动结束
                        $list[$k]['type_show']=1;
                        $list[$k]['is_promote']=0;
                    }else if($v['promote_start_time']<=NOW_TIME && $v['promote_end_time']>NOW_TIME){
                        $list[$k]['is_overdue'] = 0;
                        $list[$k]['shop_price'] = $v['promote_price'];
                        $list[$k]['type_show']=0;
                        $list[$k]['deil_name'] ='限时抢购';
                        $discount=format_decimal($v['promote_price']/$v['shop_price']*10,1);
                        $list[$k]['discount_mark']='限时'.$discount.'折';
                        $list[$k]['down_name'] ='已抢光';
                    }
                    unset($oi_ids,$og_count);
                }else if($v['pt_id']){$list[$k]['promote_type']=2;
                    $ptid = M('PromotAct')->where('id='.$v['pt_id'])->field(true)->find();
                    $p_num=D('Common/Takeout')->pro_num($ptid,'',$v['id'],1);
                    $pt_arr=array();
                    $pt_arr['act_id'] = $ptid['id'];
                    $pt_arr['style_ext_id'] = $v['id'];
                    $pt_arr['style'] = 4;
                    $pt_money = M('PromotExt')->where($pt_arr)->getField('remarks');
                    $pt_money = unserialize($pt_money);
                    if($pt_money>0) {$list[$k]['pt_price'] = format_decimal($pt_money,2);}
                    $list[$k]['pt_id'] = $v['pt_id'];
                    // $list[$k]['goods_number'] = $v['goods_number']; //累计售出
                    $list[$k]['promote_number'] = $p_num; //已抢
                    $list[$k]['pover_number'] =intval($v['goods_max_number'])-intval($p_num);
                    
                    if($ptid['end_time']<NOW_TIME || ($rank_id<$ptid['user_rank'])){
                        // $list[$k]['is_overdue'] = 1; //拼团已过期
                        $list[$k]['is_ptdue'] = 1;
                        $list[$k]['type_show']=1;
                        $list[$k]['pt_id']=0;
                    }else if($ptid['start_time']<=NOW_TIME && $ptid['end_time']>NOW_TIME){
                        $list[$k]['shop_price']=$list[$k]['pt_price'];
                        // $list[$k]['is_overdue'] = 0;                        
                        $list[$k]['is_ptdue'] = 0; 
                        $list[$k]['type_show']=0;
                        $list[$k]['deil_name'] ='限时拼团';
                        $list[$k]['pin_mark']=$ptid['group_number'].'人团';
                        $list[$k]['down_name'] ='已抢光';
                        $list[$k]['url'] = geturl('Mobile/FightGroup/cont',array('id'=>$v['id'],'eid'=>$uid));                     
                    }
                    //已拼
                    $oarrs=array();
                    $oarrs['pt_id'] = $v['pt_id'];
                    $oarrs['pay_status'] = 3;
                    $oarrs['status'] = array('neq',4);
                    $oarrs['is_delete'] = 0;
                    $oi_ids = M('OrderInfo')->where($oarrs)->getField('id',true);
                    if($oi_ids){
                        $maph = array();
                        $maph['order_id'] = array('in',$oi_ids);
                        $maph['goods_id'] = $v['id'];
                        $og_count = M('OrderGoods')->where($maph)->sum('goods_number');
                    }else{
                        $og_count = 0;
                    }
                    $list[$k]['yp_number'] = intval($og_count);
                }
                $list[$k]['mprice'] = format_price($v['market_price']);
                $list[$k]['sprice'] = format_price($price_info['goods_price']);
                $list[$k]['del_price'] = format_price($v['market_price']);
                $list[$k]['img'] = thumb($v['img'],350,350,'noimg_430x430.png');
                $list[$k]['best_img'] = thumb($v['best_img'],700,350,'noimg_300x140.png'); //jxr
                $list[$k]['xprice']=format_decimal($v['shop_price']);
                $map = array();$map['goods_id'] = $v['id'];$map['shop_id'] = get_wxsmall_shop_id();
                $sell_count = M('ShopsGoodsStock')->where($map)->getField('sell_count');
                $list[$k]['sell_count'] = floatval($sell_count);
            }
            $return['status'] = 1;
            $return['list'] = $list;
        }
        return $return;
    }
}

?>