<?php
/**
 * 活动模型
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaol <1094186793@qq.com>
 */

namespace Common\Model;
use Think\Model;
class PromotModel extends Model
{
    protected $tableName = 'promot_act';
    /**
     * 根据订货商品搜活动
     * @param $id 商品id
     * @return mixed
     */
    public function purgoods_to_promot($id)
    {
        $return = $map = $list = array();
        return $return;
       
    }
    /**
     * 根据商品搜活动
     * @param $id 商品id
     * @param $is_limit 是否需要限时促销
     * @param $type 商品类型 1云店,3外卖
     * @return mixed
     */
    public function goods_to_promot($id, $is_limit = 0,$type=1,$shop_id=0)
    {
        $return = $map = $list = array();
        if($is_limit){
            $result= $this->get_limit_promot($id);
            if($result){
                $return['list'][$result['id']] = $result;
            }
        }
        $style_ext_id = $this->goods_promot($id,$type,$shop_id);
        if (empty($style_ext_id)){
            $return['count'] = count($return['list']);
            return $return;
        }
        $map['pa.id'] = array('in', $style_ext_id);
        $map['pa.is_check'] = array('elt', 1);
        if($type==1){
            $list = M('PromotAct')->field('pa.*,pe.style,pe.style_ext_id,pe.remarks')->alias('pa')->where($map)->join('LEFT JOIN __PROMOT_EXT__ pe ON pe.act_id=pa.id')->order('end_time desc')->select();
        }else{
            $map['pa.is_online']=1;
            $map['_string'] = "pa.shop_id = ".$shop_id." OR pa.shop_id='0'";
            $list = M('PromotAct')->field('pa.*,pe.style,pe.style_ext_id,pe.remarks')->alias('pa')->where($map)->join('LEFT JOIN __PROMOT_EXT__ pe ON pe.act_id=pa.id')->order('end_time desc')->limit(1)->select();
        }
        foreach ($list as $key => $val) {
            $return['list'][$val['id']] = $val;
        }
        $return['count'] = count($return['list']);
        return $return;
    }

    /**
     * 根据商品id搜活动id
     * @param $ids 商品id
     * @param $is_limit 是否需要限时促销
     * @param $type 1云店,3外卖
     * @return array 活动id
     */
    public function goods_promot($ids,$type=1,$shop_id)
    {
        if(!$shop_id){
	  $shop_id=get_wxsmall_shop_id();
        }
        $return = $map = $pro_emap = array();
        if (is_numeric($ids)) {
            $id = $ids;
            $ids = array();
            $ids[] = $id;
        }
        $map['id'] = array('in', $ids);
        $cat_brand_id = M('Goods')->where($map)->field('cat_id,brand_id')->select();
        foreach ($cat_brand_id as $k => $v) {
            if ($v['cat_id'] != 0) $cat_ids[] = $v['cat_id'];
            if ($v['brand_id'] != 0) $brand_ids[] = $v['brand_id'];
        }
        //获取活动id    $type_values
        $now_time = NOW_TIME;
        $pro_emap['_string'] = ' ( pa.shop_id = ' . $shop_id . ' or pa.shop_id = 0 and (pa.start_time = 0 or pa.start_time <= ' . $now_time . ') and (pa.end_time = 0 or pa.end_time >= ' . $now_time . ')) and ((';
        if(count($ids)>0){
            $pro_emap['_string'] .= 'pe.style = 3 and pe.style_ext_id in (' . implode(',', $ids) . ')) ';
        }
        if (count($cat_ids)>0) {
            if($type==1){
                foreach ($cat_ids as $k => $v) {
                    $cat_ids[] = getallparent($v, '', 'GoodsCategory');
                }
            }else{
                foreach ($cat_ids as $k => $v) {
                    $cat_ids[] = getallparent($v, '', 'PurchaseCategory');
                }
            }
            $pro_emap['_string'] .= ' or (pe.style = 1 and pe.style_ext_id in (' . implode(',', $cat_ids) . ') ) ';
        }
        if (count($brand_ids)>0) $pro_emap['_string'] .= ' or (pe.style = 2 and pe.style_ext_id in (' . implode(',', $brand_ids) . ') ) ';
        $pro_emap['_string'] .= ')';
        $return = M('PromotExt')->alias('pe')->where($pro_emap)->join('LEFT JOIN __PROMOT_ACT__ pa ON pe.act_id=pa.id')->getField('act_id', true);
        $return = array_unique($return);
        return $return;
    }
    /**
     * 根据商品id搜限时促销
     * @param $id 商品id
     * @return array 活动信息
     */
    public function get_limit_promot($id)
    {
        $now_time = NOW_TIME;
        $return = $map = array();
        $map['_string'] = ' g.id = '.$id.' and g.is_delete=0 and g.publish_wait=1 and g.promote_end_time > '.$now_time;
        $goods_ids = M('PromotAct')->field('pa.*,g.promote_price,g.id as goodsid')->alias('pa')->where($map)->join('LEFT JOIN __GOODS__ g ON g.promote_id=pa.id')->find();
        if(!empty($goods_ids)) {
            $return = $goods_ids;
            $return['style'] = 5;
            $return['style_ext_id'] = $return['goodsid'];
            $remarks['promot_price'][$return['style_ext_id']] = $return['promote_price'];
            $return['remarks'] = serialize($remarks);
        }
        return $return;
    }

    /**
     * 根据商品搜优惠券
     * @param $ids 商品id
     * @param $type 1云店3外卖
     * @return mixed
     */
    public function goods_to_ticket($ids,$shop_id=0,$type)
    {
        $return = $where = array();
        $type_values = $this->goods_promot($ids,$type,$shop_id);
        //搜索对应活动的红包
        // $type_values[]=0;
        // $where['type_value'] = array('in', $type_values);
        $now_time = NOW_TIME;
        $where['send_type'] = 0;
        $where['type'] = 0;
		if($shop_id != 0){
			$where['_string'] = ' (use_start_date <= ' . $now_time . ' or use_start_date = 0) and (use_end_date >= ' . $now_time . ' or use_end_date = 0) and (shop_id = '.$shop_id.' or shop_id=0 )';
		}else{
			$where['_string'] = ' (use_start_date <= ' . $now_time . ' or use_start_date = 0) and (use_end_date >= ' . $now_time . ' or use_end_date = 0)';
		}
        $bonus = M('Bonu')->where($where)->select();
        $return['get_count']=0;
        foreach ($bonus as $key => $val) {
            $bonus[$key] = $this->format_bonus($val);
            if($bonus[$key]['is_get']==1){$return['get_count']++;}
        }
        $return['list'] = $bonus;
        $return['count'] = count($bonus);
        return $return;
    }

    /**
     * 格式化红包
     * @param array $data
     * @return array
     */
    public function format_bonus($data = array())
    {
        $return = $mb_map = array();
        $return['id'] = $data['id'];
        $return['type_name'] = $data['type_name'];
        if($data['type_value']==0){$return['type_name'].='(本店通用)';}
        $return['type_money'] = round($data['type_money']);
        $return['send_type'] = $data['send_type'];
        $return['every_num'] = $data['every_num'];
        $return['odds'] = $data['odds'];
        $return['use_start_date'] = $data['use_start_date'];
        $return['use_start_date_format'] = time_format($data['use_start_date'], 'Y.m.d');
        $return['use_end_date'] = $data['use_end_date'];
        $return['use_end_date_format'] = time_format($data['use_end_date'], 'Y.m.d');
        $return['min_goods_amount'] = $data['min_goods_amount'];
        $return['type'] = $data['type'];
        $return['type_value'] = $data['type_value'];
        //判断红包是否领取,使用
        //$member = session('member_info');
        $member = checkuserauthtype();
        if (!empty($member)) {
            $mb_map['bonus_id'] = $data['id'];
            $mb_map['user_id'] = $member['id'];
            $mb_map['used_time'] = 0;
            $member_bonu = M('MemberBonus')->where($mb_map)->field('count(*) as get_count,used_time,id')->find();
            $return['is_get'] = $member_bonu['get_count'] > 0 ? 1 : 0;
            $return['member_bonusid'] = $member_bonu['id'];
           // $return['is_use'] = empty($member_bonu['used_time']) ? 0 : 1;
        } else {
            $return['is_get'] = 0;
//            $return['is_use'] = 0;
        }
        return $return;
    }

    /**
     * 会员领取优惠券
     * @param int $bonus_id
     */
    public function member_get_bonus($bonus_id = 0)
    {
        $return = $bonus_map = $use_map = $data = array();
        $return['status'] = 0;//2未登录
        if (empty($bonus_id)) {
            $return['info'] = '参数错误！';
            return $return;
        }
        $member = checkuserauthtype();
        if (empty($member)) {
            $return['status'] = 0;
            $return['info'] = '请先登录！';
            $return['url'] = getjumpurl('JUMP_1');
            return $return;
        }
        $bonus_map['id'] = $bonus_id;
        $bonus = M('Bonu')->where($bonus_map)->field(true)->find();
        if (empty($bonus)) {
            $return['info'] = '没有该红包吖！';
            return $return;
        }
        $use_map['user_id'] = $member['id'];
        $use_map['bonus_id'] = $bonus_id;
        $use_map['used_time'] = array('eq', 0);

        if($bonus['bonu_type'] == 2){
            if($bonus['every_num'] == 0){
                $return['status'] = 2;
                $return['info'] = '该代金券已领完！';
                $this->ajaxReturn($return);
            }
            $use_map['bonus_type'] = 2;
            $use_map['start_time'] = array('lt',NOW_TIME);
            $use_map['end_time'] = array('gt',NOW_TIME);
        }
        $mb_id = M('MemberBonus')->where($use_map)->getField('id');
        if (!empty($mb_id)) {
            $return['info'] = '你已经领过该红包了吖！';
            return $return;
        }
        $data['bonus_id'] = $bonus['id'];
        $data['bonus_name'] = $bonus['type_name'];
        $data['user_id'] = $member['id'];
        $data['money'] = $bonus['type_money'];
        $data['min_goods_money'] = $bonus['min_goods_amount'];
        $data['send_time'] = NOW_TIME;
        $data['start_time'] = $bonus['use_start_date'];
        $data['end_time'] = $bonus['use_end_date'];
        $data['type'] = $bonus['type'];
        $data['type_value'] = $bonus['type_value'];
        $data['bonus_type'] = $bonus['bonu_type'];
        $addbonus = M('MemberBonus')->add($data);
        if ($addbonus !== false) {
            $return['status'] = 1;
            if($bonus['bonu_type'] == 2 && $bonus['every_num'] != -1){
                M('MemberBonus')->where('id='.$bonus['id'])->setDec('every_num',1);
            }
            $return['info'] = '领取成功！';
        } else {
            $return['status'] = 2;
            $return['info'] = '领取失败！';
        }
        return $return;
    }

    /**
     * 获取某一活动的商品
     * @param $getdata 活动参数
     * @return mixed
     */
    public function get_active_goods($getdata){
        $id = $getdata['id'];
        //根据活动id获取条件
        $Extinfo = M('PromotExt')->where('act_id=' . $id)->field(true)->select();
		GLOBAL $GoodsCategory;
		$catinfo=$map=$brand_ids=$goods_ids=array();
        foreach ($Extinfo as $k => $v) {
            if($v['style'] == 1){$catinfo[] = $GoodsCategory[$v['style_ext_id']];}
            if($v['style'] == 2){$brand_ids[] = $v['style_ext_id'];}
            if($v['style'] == 3){$goods_ids[] = $v['style_ext_id'];}
        }
        if (!empty($brand_ids)) $map['brand_id'] = array('in', $brand_ids);
        if (!empty($goods_ids)) $map['id'] = array('in', $goods_ids);
        if (!empty($catinfo)) {
			foreach($catinfo as $key=>$val){$ids[]=explode(',',$val['allson']);}
			foreach($ids as $ke=>$va){
				foreach($va as $k=>$v){$idss[]=$v;}
			}
			$cat_ids=array_unique($idss);
			$map['cat_id'] = array('in', $cat_ids);
		}
        //排序条件
        $order_list['default'] = array('name' => '默认', 'class' => 1, 'icon' => 0, 'url' => U('PromotAct/cont', array('id' => $id, 'default' => 0)));
        $order_list['shop_price'] = array('name' => '价格', 'class' => 0, 'icon' => 1, 'url' => U('PromotAct/cont', array('id' => $id, 'shop_price' => 1)));
//        $order_list['updatetime'] = array('name' => '时间', 'class' => 0, 'icon' => 1, 'url' => U('PromotAct/cont', array('id' => $id, 'updatetime' => 1)));
        $order_list['sell_count'] = array('name' => '销量', 'class' => 0, 'icon' => 1, 'url' => U('PromotAct/cont', array('id' => $id, 'sell_count' => 1)));
        $order = '';
        foreach ($order_list as $k => $v) {
            if ($getdata[$k] == 1) {
                $order_list['default']['class'] = 0;
                $order_list[$k]['class'] = 1;
                $order_list[$k]['icon'] = 1;
                $order_list[$k]['url'] = U('PromotAct/cont', array('id' => $id, $k => 2));
                $order = ' ' . $k . ' desc';
            }
            if ($getdata[$k] == 2) {
                $order_list['default']['class'] = 0;
                $order_list[$k]['class'] = 1;
                $order_list[$k]['icon'] = 2;
                $order_list[$k]['url'] = U('PromotAct/cont', array('id' => $id, $k => 1));
                $order = ' ' . $k . ' asc';
            }
        }
        $return['order'] = $order_list;

        //查询商品，格式化数据
        $map['is_internet']=1;
        $join = '';
        $p = empty($getdata['page']) ? 1 : $getdata['page'];
        $goods_info = page_list('Goods', $map, $order, true, 20, $join, $p);
		$goods_list = array();
		foreach($goods_info['list'] as $k => $v){
			$goods_list[$k] = $v;
			$goods_list[$k]['format_spec'] = D("Common/GoodsAttr")->get_shop_attr($v['id']);
		}
		$goods_info['list'] = $goods_list;
        $return['goods_info'] = $goods_info;
        return $return;
    }
	/**
	* 添加、编辑限时促销
	* @param $postdata mixed
	* @return  mixed
	*/
	/**
    * 添加、编辑限时促销
    * @param $postdata mixed
    * @return  mixed
    */
    public function add_update($post){
        $return=array('status'=>0,'info'=>'');
        //时间
        $start_time=$post['start_time'];
        $end_time=$post['end_time'];
        if(empty($start_time) || empty($end_time)){
            $return['info']='请选择开始时间或结束时间';return $return;
        }elseif(strtotime($end_time) < strtotime($start_time)){
            $return['info']='结束时间应大于开始时间';return $return;
        }
        
        //促销商品信息
        $goods_id=$post['goods_id'];
        $promot_price=$post['promot_price'];
        $promot_goods_number=$post['goods_number'];
        $promot_goods_min_number=$post['goods_min_number'];
        $promotes=array();
        /*foreach($goods_id as $k=>$v){
            $promotes[$k]['id']=$v;
            $promotes[$k]['promote_price']=$promot_price[$v];
        }*/
        foreach($goods_id as $k=>$v){
            $promotes[$v]['id']=$v;
            $promotes[$v]['promote_price']=$promot_price[$k];
            $promotes[$v]['goods_number']=$promot_goods_number[$k];
            $promotes[$v]['goods_min_number']=$promot_goods_min_number[$k];
        }
        if($post['is_show'] == 0){
            $post['shop_id'] = 0;
            $post['is_part'] = 0;
        }else{
            if($post['shop_ids'])$post['shop_id'] = ','.implode(',',$post['shop_ids']).',';
        }
        unset($post['is_show']);
        //编辑或添加
        if($post){
            $data['act_name']=$post['act_name'];
            $data['img']=$post['img'];
            $data['big_img']=$post['big_img'];
            $data['start_time']=strtotime($post['start_time']);
            $data['end_time']=strtotime($post['end_time']);
            $data['shop_id'] = $post['shop_id'];
            $data['is_limit_time'] = 1;
            $data['is_part'] = $post['is_part'];
            $data['status'] = $post['status'];
            $data['pid'] = $post['pid'];
            $data['buy_limit'] = $post['buy_limit'];
            $data['presell_time'] = $presell_time; //jxr 2019.03.22
            $data['user_rank'] = $post['user_rank'];
            if($post['id']){//修改
                //判断时间
                $in_map['status']=$post['status'];
                $in_map['pid']=0;
                $in_map['is_limit_time']=1;
                $in_map['id']=array('neq',$post['id']);
                $in_map['start_time']=array('lt',$data['end_time']);
                $in_ids=M('PromotAct')->where($in_map)->getField('id',true);                
                if($in_ids){
                    $map_in['id']=array('in',$in_ids);
                    $last_time= M('PromotAct')->where($map_in)->order('end_time desc')->getField('end_time');
                    if($last_time > $data['start_time']){$return['info']='该时间段内已有促销，请选择其他时间段';return $return;}
                }
                //修改信息
                if($post['pid'] == 0){
                    $res = M('PromotAct')->where('id='.$post['id'])->save($data);
                    unset($data['pid']);
                    unset($data['shop_id']);
                    $res_pid = M('PromotAct')->where('pid='.$post['id'])->save($data);
                }else{
                    $res_pid = M('PromotAct')->where('pid='.$post['id'])->save($data);
                    unset($data['pid']);
                    unset($data['shop_id']);
                    $res = M('PromotAct')->where('id='.$post['id'])->save($data);
                }
                $goods_infos=M('Goods')->where('promote_id='.$post['id'])->field(true)->select();
                foreach($goods_infos as $k=>$v){//清除商品以前的促销信息
                    $goods_data=array();
                    $goods_data['promote_id']=0;
                    $goods_data['is_promote']=0;
                    $goods_data['promote_start_time']=0;
                    $goods_data['promote_end_time']=0;
                    $goods_data['promote_price']=0;
                    $goods_data['goods_max_number']=0;
                    $goods_data['goods_min_number']=0;
                    $goods_data['presell_time'] = 0; //jxr 2019.03.22
                    $map=array();
                    $map['id']=$v['id'];
                    M('Goods')->where($map)->save($goods_data);     
                }
                foreach($promotes as $k=>$v){//添加新的促销信息
                    $goods_data=array();
                    $goods_data['is_promote']=1;
                    $goods_data['promote_id']=$post['id'];
                    $goods_data['promote_start_time']=strtotime($post['start_time']);
                    $goods_data['promote_end_time']=strtotime($post['end_time']);
                    $goods_data['promote_price']=$promotes[$v['id']]['promote_price'];
                    $goods_data['goods_max_number']=$promotes[$v['id']]['goods_number'];
                    $goods_data['goods_min_number']=$promotes[$v['id']]['goods_min_number'];
                    $goods_data['presell_time'] = $presell_time; //jxr 2019.03.22
                    $map=array();
                    $map['id']=$v['id'];
                    M('Goods')->where($map)->save($goods_data);
                }
            }else{//添加
                //判断时间
                $max['status']=$post['status'];
                $max['is_limit_time']=1;
                $max['pid']=0;
                $last_time= M('PromotAct')->where($max)->order('end_time desc')->getField('end_time');
                
                if(strtotime($start_time) < $last_time){$return['info']='该时间段内已有促销，请选择其他时间段';return $return;}
                // print_r($data);exit;
                //添加信息
                $res = M('PromotAct')->add($data);
                if($res){
                    $goods_data=array();
                    $goods_data['is_promote']=1;
                    $goods_data['promote_id']=$res;
                    $goods_data['promote_start_time']=strtotime($post['start_time']);
                    $goods_data['promote_end_time']=strtotime($post['end_time']);
                    foreach($promotes as $k=>$v){
                        $goods_data['promote_price']=$v['promote_price'];
                        $goods_data['goods_max_number']=$v['goods_number'];
                        $goods_data['goods_min_number']=$v['goods_min_number'];
                        $map=array();
                        $map['id']=$v['id'];
                        M('Goods')->where($map)->save($goods_data);
                    }
                }
            } 
            $return['status']=1;
            if($post['id']){
                $return['info']='编辑成功';
            }else{
                $return['info']='添加成功';
            }
            return $return;
        }       
    }
	/**
     * 获取限时促销的商品
     * @param $getdata 活动参数
     * @return mixed
     */
    public function get_promot_goods($getdata){
        $pagesize=empty($pagesize)?C('WEB_GOODS_PAGE'):$pagesize;
		$act_id = $getdata['act_id'];
		$act_ids=M('PromotAct')->where('pid='.$act_id)->getField('id',true);
		$act_ids[]=$act_id;
		//商品条件
        $g_map['status']=1;
		$g_map['is_promote']=1;
		$g_map['publish_wait']=1;
		$g_map['promote_price']=array('gt',0);
		$g_map['promote_id']=array('in',$act_ids);
		$g_map['promote_start_time']=array('lt',NOW_TIME);
		$g_map['promote_end_time']=array('gt',NOW_TIME);
        if($getdata['catid'])$g_map['cat_id']=$getdata['catid'];
		//排序条件
        $order_list['default'] = array('name' => '默认', 'class' => 1, 'icon' => 0, 'url' => U('PromotSale/index', array('default' => 0)));
        $order_list['shop_price'] = array('name' => '价格', 'class' => 0, 'icon' => 1, 'url' => U('PromotSale/index', array('shop_price' => 1)));
        $order_list['updatetime'] = array('name' => '时间', 'class' => 0, 'icon' => 1, 'url' => U('PromotSale/index', array('updatetime' => 1)));
        $order_list['sell_count'] = array('name' => '销量', 'class' => 0, 'icon' => 1, 'url' => U('PromotSale/index', array('sell_count' => 1)));
        $order = '';
        foreach ($order_list as $k => $v) {
            if ($getdata[$k] == 1) {
                $order_list['default']['class'] = 0;
                $order_list[$k]['class'] = 1;
                $order_list[$k]['icon'] = 1;
                $order_list[$k]['url'] = U('PromotSale/index', array($k => 2));
                $order = ' ' . $k . ' desc';
            }
            if ($getdata[$k] == 2) {
                $order_list['default']['class'] = 0;
                $order_list[$k]['class'] = 1;
                $order_list[$k]['icon'] = 2;
                $order_list[$k]['url'] = U('PromotSale/index', array($k => 1));
                $order = ' ' . $k . ' asc';
            }
        }
		$p = empty($getdata['page']) ? 1 : $getdata['page'];
		$goods_list=page_list('Goods',$g_map,$order,true,$pagesize,'',$p);
        if($goods_list){
            foreach($goods_list['list'] as $k=>$v){
                $goods_list['list'][$k]['sell_count']=$this->getprosalenum($v);
            }
        }
        $return['order'] = $order_list;
        $return['goods_info'] = $goods_list;
        return $return;
    }
    public function getprosalenum($goods){
        $map=array();
        $map['g.goods_id']=$goods['id'];
        $map['o.pay_status']=3;
        $map['o.addtime'][]=array('egt',$goods['promote_start_time']);
        $map['o.addtime'][]=array('elt',$goods['promote_end_time']);
        $map['o.status']=array('in','1,2,3,6,8');
        $map['o.is_delete']=0;
        $num=M('OrderInfo')->alias('o')->join('__ORDER_GOODS__ g on o.id=g.order_id')->where($map)->sum('g.goods_number');
        return intval($num);
    }

    /**获取赠品（只适用于加价购和满赠活动）
     * @param $act_id//活动id
     * @return array
     */
    public function get_gift_goods($act_id){
        $return=array('status'=>1);
        $gift_list=array();
        if(empty($act_id)){
            $return['status']=0;
            $return['msg']="活动不能为空";
            return $return;
        }
        $map=array();
        $map['id']=$act_id;
        $act_info=M("PromotAct")->where($map)->field(true)->find();
        if(empty($act_info)){
            $return['status']=0;
            $return['msg']='活动不存在';
            return $return;
        }
        if($act_info['act_good_ids']){
            $goods_ids=$act_info['act_good_ids'];
            $goods_attr='';
            if(stripos($goods_ids,'-')){
                $goods_ids=array_filter(explode(',',$goods_ids));
                foreach($goods_ids as $key=>$val){
                    if(stripos($val,'-')){
                        $ids[$key]=explode('-',$val);
                    }else{
                        $ids[$key]=array(0=>$val);
                    }
                }
                foreach($ids as $key=>$val){
                    $map=array();
                    $map['status']=1;
                    $map['is_internet']=1;
                    $map['id']=$val[0];
                    $goods_attr[$val[0]]=$val[1];
                    //$map['goods_stock']=$val[1];
                    $gift_list[]=M("Goods")->where($map)->field(true)->find();
                }
            }else{
                $goods_ids=array_filter(explode(',',$goods_ids));
                $map=array();
                $map['id']=array('in',$goods_ids);
                $map['status']=1;
                $map['is_internet']=1;
                $gift_list=M("Goods")->where($map)->field(true)->select();
            }
            $gift_list=array_filter($gift_list);
            if(!empty($gift_list)) {
                foreach ($gift_list as $key => $val) {
                    $gift_list[$key]['img'] = thumb($val['img']);
                    $gift_list[$key]['goods_name'] = $val['goods_name'];
                    $gift_list[$key]['all_price'] = $act_info['act_type_ext'];
                    $gift_list[$key]['goods_attr'] = $goods_attr[$val['id']];
                    if ($goods_attr[$val['id']]) {
                        $gift_list[$key]['goods_attr_format'] = D("Common/GoodsAttr")->goods_to_attrname($goods_attr[$val['id']]);
                    }
                }
            }
        }
        if($gift_list){
            $return['gift_list']=$gift_list;
        }
        return $return;
    }
}
?>