<?php
/**商品
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Mobile\Model;
use Think\Model;
class GoodsModel extends Model
{
    public function get_goods_index($param=array()){
        $root=array();
        $map=array();
        $join='';
        if(empty($param['page']))$param['page']=1;
        $p=$param['page'];
        if(!empty($param['keyword']))$map['goods_name']=array('like','%'.$param['keyword'].'%');
        if(!empty($param['cat_id']))$map['cat_id']=$param['cat_id'];
        if(!empty($param['brand_id']))$map['brand_id']=$param['brand_id'];
        //是否为自营
        if(!empty($param['is_self'])&& $param['is_self'] == 1)$map['shop_id']=0;
        //是否是商家包邮
        if(!empty($param['is_shipping'])&& $param['is_shipping'] == 1)$map['is_shipping']=1;
        //仅显示有货
        if(!empty($param['is_have'])&& $param['is_have'] == 1)$map['goods_number']=array('gt',0);
        /*排序开始*/
        $orders=D('Common/Goods')->cate_order($param);
        /*排序结束*/
        /*价格分级开始*/
        $price_list = get_price_map($catid,$param['price']);
        $pricelist=empty($price_list['pricelist'])?array():$price_list['pricelist'];
        if(!empty($price_list['shop_price']))$map['shop_price']=$price_list['shop_price'];
        /*价格分级结束*/
        /*属性筛选开始*/
        $type_id=get_cate_type($param['cat_id']);//类型ID
        $attr_list=D('Common/GoodsAttr')->get_attr($param,$type_id);
        if(!empty($attr_list)){
            $map['_string']=$attr_list['_string'];
        }
        /*属性筛选结束*/
        /*标签筛选开始*/
        //$labels=$param['label'];//if($labels)$labels=explode(',',urldecode($labels));
        $labels_map=D('Common/Goods')->get_label_map($param);
        if(!empty($labels_map['map']))$map['id']=$labels_map['map']['id'];

        /*标签筛选结束*/
		
		/* 排序 */
        $order_type=$param['sort'];
		if($param[$param['order_name']]==1){$order_type='desc';
		}elseif($param[$param['order_name']]==2){$order_type='asc';}
        if(!empty($param['order_name']) && $order_type)$order=$param['order_name'].' '.$order_type;
		
        /*拼团*/
        $goods_list = array();
        if($param['is_pt'] == 1){
			$pa_map = array();$pa_map['is_pt'] = 1;$pa_map['end_time'] = array('gt',NOW_TIME);
			$pt_ids = M('PromotAct')->where($pa_map)->getField('id',true);
			if($pt_ids){
                $map['pt_id'] = array('in',$pt_ids);
				$goods_list=D('Common/Goods')->get_goods_list(0,$map,$join,$order,$p,'',5);
			}
        }
        // $map['shop_id']=0;
		
        // $goods_list=D('Common/Goods')->get_goods_list(0,$map,$join,$order,$p,'',5);
        // $cate_name=empty($param['cat_id'])?'分类':idtoname($param['cat_id'],'GoodsCategory');
        $cate_name=empty($param['cat_id'])?'分类':idtoname($param['cat_id'],'PurchaseCategory');
        $root['cate_name']=$cate_name;
        $root['orders']=$orders;
        $root['pricelist']=$pricelist;
        $root['type_id']=$type_id;
        $root['labels']=$labels_map['labels'];
        $root['goods_list']=$goods_list;
        return $root;
    }

    /**商品详情页
     * @param $goods_id
     */
    public function get_goods_content($goods_id){
        //获取商品基本信息
        $root = array();
        $goods_info = D("Common/Goods")->get_goods_item($goods_id);
        if ($goods_info['status']) {
            //商品相册
            $where['goods_id']=$goods_id;
            $goods_imgs = M("GoodsGallery")->where($where)->select();
            foreach($goods_imgs as $key=>$val){
                $goods_imgs[$key]['goods_img']=$val['goods_img'];
            }
            if (empty($goods_imgs)) $goods_imgs[0]['goods_img'] = $goods_info['data']['img'];
            //获取商品属性和规格
            $data = D("Common/GoodsAttr")->get_shop_attr($goods_id);
			
            //总评论个数
            $c_map['id']=$goods_id;
            $dp_count=M("goods")->where($c_map)->getField('dp_count');

            //活动
            $activelist=D("Common/Promot")->goods_to_promot($goods_id,3);
            //判断是否有限时促销
            $active=$promotion=array();
            foreach($activelist['list'] as $key=>$val){
                if($val['style']==5){
                    $active[]=$val;
                    unset($activelist['list'][$key]);
                }
            }
            if($active){
                foreach($active as $k=>$v){
                    if($k==count($active)-1){
                        $remarks=unserialize($v['remarks']);
                        $v['remarks']=format_decimal($remarks['promot_price'][$goods_id]);
                        $promotion=$v;
                    }
                }
            }
            
            $promotion_remarks=$promotion['remarks'];
            $price_info=D("Common/Cart")->getminprice($goods_id);
            $root['attr'] = $data;//商品属性规格
            if($goods_info['data']['contract_list']){
                foreach($goods_info['data']['contract_list'] as $ke=>$va){
                    $ex=explode('/',$va['url']);
                    foreach($ex as $kk=>$vv){if(strstr($vv,'html')){$exp=explode('.',$vv);continue;}}
                    $goods_info['data']['contract_list'][$ke]['url']=U('article/article_cont',array('id'=>$exp[0]));
                }
            }
			
            $root['goods_info'] = $goods_info['data'];//商品基本信息
            $root['goods_imgs'] = $goods_imgs;//商品相册;
            $root['c_count'] = $dp_count;

            $root['activelist'] = $activelist;//活动
            $root['ecv_list'] = $ecv_list;//优惠券
            $root['promotion_end_time']=$promotion['end_time'];//限时促销
            $root['promotion_remarks'] = empty($promotion_remarks)?$promotion_remarks=0:$promotion_remarks;//限时促销
            $root['promotion'] = $promotion;//限时促销
			$root['price_info'] = $price_info;//店铺信息
			$root['is_stock'] = $stock['is_stock'];//是否有库存
            return $root;
        }
    }
	/**商品详情页
     * @param $goods_id
     */
    public function getmobile_goodscontent($goods_id)
    {
        //获取商品基本信息
        $root = array();
        $goods_info = D("Common/Goods")->get_goods_item($goods_id);
        if ($goods_info['status']) {
            //商品相册
            $where['goods_id']=$goods_id;
            $goods_imgs = M("GoodsGallery")->where($where)->select();
            foreach($goods_imgs as $key=>$val){
                $goods_imgs[$key]['goods_img']=$val['goods_img'];
            }
            if (empty($goods_imgs)) $goods_imgs[0]['goods_img'] = $goods_info['data']['img'];
            //获取商品属性和规格
            $data = D("Common/GoodsAttr")->getattr($goods_id);
            //判断商品是否有库存
            $address_info = checkuseraddress();//获取定位信息

            if($address_info['district']){
                $area_id=$address_info['district'];
            }else{
                $area_id=$address_info['city'];
            }
            $stock=D("Common/Cart")->get_area_fee($goods_info['data']['shop_id'],$area_id);
            //总评论个数
            $c_map['id']=$goods_id;
            $dp_count=M("goods")->where($c_map)->getField('dp_count');

            //活动
            $activelist=D("Common/Promot")->goods_to_promot($goods_id,0,3,$goods_info['data']['shop_id']);
            //判断是否有限时促销
            $active=$promotion=array();
            foreach($activelist['list'] as $key=>$val){
                if($val['style']==5){
                    $active[]=$val;
                    unset($activelist['list'][$key]);
                }
            }
            if($active){
                foreach($active as $k=>$v){
                    if($k==count($active)-1){
                        $remarks=unserialize($v['remarks']);
                        $v['remarks']=format_decimal($remarks['promot_price'][$goods_id]);
                        $promotion=$v;
                    }
                }
            }
            //优惠券
            $ecv_list=D("Common/Promot")->goods_to_ticket($goods_id,$goods_info['data']['shop_id']);
            //会员红包
            $member=checkuserauthtype();
            if($member){
                $bonus_map['user_id']=$member['id'];
                $memberbonus=M("MemberBonus")->where($bonus_map)->getField('bonus_id',true);
            }
            $promotion_remarks=$promotion['remarks'];
            $price_info=D("Common/Cart")->getminprice($goods_id);
            $root['attr'] = $data;//商品属性规格
            if($goods_info['data']['contract_list']){
                foreach($goods_info['data']['contract_list'] as $ke=>$va){
                    $ex=explode('/',$va['url']);
                    foreach($ex as $kk=>$vv){if(strstr($vv,'html')){$exp=explode('.',$vv);continue;}}
                    $goods_info['data']['contract_list'][$ke]['url']=U('article/article_cont',array('id'=>$exp[0]));
                }
            }

            $root['goods_info'] = $goods_info['data'];//商品基本信息
            $root['goods_imgs'] = $goods_imgs;//商品相册;
            $root['c_count'] = $dp_count;

            $root['activelist'] = $activelist;//活动
            $root['ecv_list'] = $ecv_list;//优惠券
//            $root['member_info'] = $memberbonus;
            $root['memberbonus'] = $memberbonus;
            $root['promotion_end_time']=$promotion['end_time'];//限时促销
            $root['promotion_remarks'] = empty($promotion_remarks)?$promotion_remarks=0:$promotion_remarks;//限时促销
            $root['promotion'] = $promotion;//限时促销
			$root['price_info'] = $price_info;//店铺信息
			$root['is_stock'] = $stock['is_stock'];//是否有库存
            return $root;
        }
    }
	/**商品详情页
     * @param $goods_id
     * @param $shop_id
     */
    public function getmobile_shops_goodscontent($goods_id,$shop_id){
        //获取商品基本信息
        $root = array();
        $ret = D("Common/Takeout")->get_goods_info($goods_id,$shop_id);
        if($ret){
            $status = $ret['status'];
            $goods_info = array();
            $goods_info = $ret['goods_info'];
        }else{
	        $status = 0;
        }
        $memberinfo = checkuserauthtype();
        $member_map['id'] = $memberinfo['id'];
        $rank_id = M("Member")->where($member_map)->getField('rank_id');
        // echo "<pre>";print_r($goods_info);exit;
        if ($status) {
            //商品相册
            $where['goods_id']=$goods_id;
            $goods_imgs = M("GoodsGallery")->where($where)->order('img_sort asc')->select();
            foreach($goods_imgs as $key=>$val){
                $goods_imgs[$key]['goods_img']=$val['goods_img'];
            }
            if (empty($goods_imgs)) $goods_imgs[0]['goods_img'] = $goods_info['img'];
            //获取商品属性和规格
            $data = D("Common/GoodsAttr")->get_shop_attr($goods_id);
            //判断商品是否有库存
	  $stock = D("Common/Cart")->shopgoodsstock($goods_id,$shop_id,$attr_key=0);
	  if($stock['goods_number'] > 0){
		  $goods_info['is_add'] = 1;
	  }else{
		  $goods_info['is_add'] = 0;
          $goods_info['add_msg'] = $stock['info'];//3-29 zjn
	  }
            // $commlist = D('Dp')->get_goods($goods_id);
            $commlist = D('Dp')->get_goods('',$goods_id,0);
            //总评论个数
            $dp_count=$commlist['count_dp'];
	  //活动
            $activelist=D("Common/Promot")->goods_to_promot($goods_id,0,3,$shop_id);
            //判断是否有限时促销
            $active = $active_arr = $promotion = array();
			
            foreach($activelist['list'] as $key=>$val){
                if($val['style']==5){
                    $active[]=$val;
                    unset($activelist['list'][$key]);
                }else{
		$active_arr = $val;
	      }
            }
	  if($active){
                foreach($active as $k=>$v){
                    if($k==count($active)-1){
                        $remarks=unserialize($v['remarks']);
                        $v['remarks']=format_decimal($remarks['promot_price'][$goods_id]);
                        $promotion=$v;
                    }
                }
            }

            //判断是否有限时促销
	  if($goods_info['is_promote']){
	      $tl_map = array();
	      $tl_map['status'] = 1;
	      $tl_map['start_time'] = array('elt',NOW_TIME);
          $tl_map['end_time'] = array('gt',NOW_TIME);
          $tl_map['user_rank'] = array('elt',$rank_id);
	      $tl_map['_string']="shop_id like '%,".$shop_id.",%' OR shop_id='0'";
	      $act_id = M('PromotAct')->where($tl_map)->order('end_time desc')->getField('id');

	      if($act_id){
	        $t_map=array();
	        $t_map['id']=$goods_id;
	        $t_map['goods_type']=5;
	        $t_map['status']=1;
	        $t_map['is_delete']=0;
	        $t_map['promote_id']=$act_id;
	        $t_map['is_promote']=1;
	        $t_map['promote_start_time']=array('elt',NOW_TIME);
	        $t_map['promote_end_time']=array('gt',NOW_TIME);
	        $promote=M('Goods')->where($t_map)->field(true)->order('sort asc')->find();
	      }
	  }

        // 3-21++ 参与成员
        if($goods_info['promote_id']>0 && NOW_TIME>=$goods_info['promote_start_time'] && NOW_TIME<$goods_info['data']['promote_end_time']){
            // 3-20 参与成员 zjn
            $map=array();
            $map['id']=$goods_info['promote_id'];
            $promat_info=M("PromotAct")->where($map)->field(true)->find();
            $parr=$oinfo=array();
            $parr['promot_limit_id'] = $goods_info['promote_id'];
            $parr['goods_id'] = $goods_id;
            $ogoods = M('OrderGoods')->where($parr)->getField('order_id',true);

            if($ogoods){
                $farr=implode(',',$ogoods);
                $oinfo['id']=array('IN',$farr);
                $ouid = M('OrderInfo')->where($oinfo)->getField('uid',true);
                if($ouid){
                    $marr=implode(',',$ouid);
                    $ma['id'] = array('IN',$marr);
                    $minfo= M('Member')->where($ma)->field('header_img')->limit(11)->select();
                    if($minfo) $promat_info['joinuser'] = $minfo;
                }
            }
            if(count($minfo)>0) $promat_info['joincount'] = count($minfo);
            else $promat_info['joincount'] = 0;
            // ---end----
            if($promat_info['shop_id']==0){
                $root['promat_info']=$promat_info;
            }else{
                 $shop_ids=array_filter(explode(',',$promat_info['shop_id']));
                if(in_array($param['shop_id'],$shop_ids)){
                    $root['promat_info']=$promat_info;
                }
            }
        }else{
            //3-21新加普通商品  参与人员
            $parr=$oinfo=array();
            $parr['goods_id'] = $goods_id;
            $ogoods = M('OrderGoods')->where($parr)->getField('order_id',true);

            if($ogoods){
                $farr=implode(',',$ogoods);
                $oinfo['id']=array('IN',$farr);
                $ouid = M('OrderInfo')->where($oinfo)->getField('uid',true);
                if($ouid){
                    $marr=implode(',',$ouid);
                    $ma['id'] = array('IN',$marr);
                    $mcount = M('Member')->where($ma)->field('header_img')->count();
                    $minfo= M('Member')->where($ma)->field('header_img')->limit(11)->select();
                    if($minfo) $goods_info['joinuser'] = $minfo;
                }
            }
            if(count($minfo)>0) $goods_info['joincount'] = $mcount;
            else $goods_info['joincount'] = 0;
            unset($parr,$oinfo);
            //---end===
        }
        // ---------end-----------


            $promotion_remarks=$promote['remarks'];
			
            $root['attr'] = $data;//商品属性规格
            if($goods_info['data']['contract_list']){
                foreach($goods_info['data']['contract_list'] as $ke=>$va){
                    $ex=explode('/',$va['url']);
                    foreach($ex as $kk=>$vv){if(strstr($vv,'html')){$exp=explode('.',$vv);continue;}}
                    $goods_info['data']['contract_list'][$ke]['url']=U('article/article_cont',array('id'=>$exp[0]));
                }
            }
            $root['goods_info'] = $goods_info;//商品基本信息
            $root['goods_imgs'] = $goods_imgs;//商品相册;
            $root['commlist'] = $commlist;//商品评论
            $root['c_count'] = $dp_count;
            $root['active_arr'] = $active_arr;//活动
            $root['promotion_end_time']=$promote['promote_end_time'];//限时促销
            $root['promotion_remarks'] = empty($promotion_remarks)?$promotion_remarks=0:$promotion_remarks;//限时促销
            $root['promotion'] = $promote;//限时促销
            return $root;
        }
    }
}



			