<?php
/**订货商品
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Mobile\Model;
use Think\Model;
class PurGoodsModel extends Model
{
    protected $tableName = 'goods';
    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['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,5);
        /*排序结束*/
        /*价格分级开始*/
        $price_list = get_price_map($param['cat_id'],$param['price'],5);
        $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'],5);//类型ID
        $attr_list=D('Common/GoodsAttr')->get_attr($param,$type_id);
        if(!empty($attr_list)){
            $map['_string']=$attr_list['_string'];
        }
        
        
		
		/* 排序 */
        $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=D('Common/Goods')->get_goods_list(0,$map,$join,$order,$p,'',5);
        $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['goods_list']=$goods_list;
        return $root;
    }

    /**商品属性 库存
     * @param $goods_id
     */
    public function getmobile_goodscontent($goods_id)
    {
        //获取商品基本信息
        $root = array();
        $goods_info = D("Common/Goods")->get_goods_item($goods_id);
        $shops_info = checkuserauthtype(2);
        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 = getshopaddress($shops_info['shop_id']);//获取店铺地址
            
            if($address_info['district']){
                $area_id=$address_info['district'];
            }else{
                $area_id=$address_info['city'];
            }
            $stock=D("Common/Cart")->get_area_fees($goods_info['data']['suppliers_id'],$area_id);
           
            //供货商信息
            $suppliers_id=$goods_info['data']['suppliers_id'];
            if($suppliers_id){
                $g_map['suppliers_id']=$suppliers_id;
                $g_map['status']=1;
                $supplier_info = M('Suppliers')->where('id='.$suppliers_id)->find();
                $supplier_info['goods_num']=M('Goods')->where($g_map)->count();
            }

            $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['supplier_info'] = $supplier_info;//供货商信息
            $root['is_stock'] = $stock['is_stock'];//是否有库存
            return $root;
        }
    }
}