<?php
/**外卖
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaol <xiaol@sina.cn>
 */
namespace Mobile\Model;
use Think\Model;
class TakeoutModel extends Model
{
    protected $tableName = 'goods';
    public function get_index() {
    }
    public function get_list($param){
        $model='Common/Takeout';
        $data=array();
        $data['status']=1;
        $shops_list=D($model)->get_shops_list($param);
		// dump($shops_list);exit;
        $data['shops_list']=$shops_list;
        return $data;
    }
    public function get_cate_list(){
        $return = array();
        $tpl = C('LIST_TPL');
        $return['status'] = 1;
        GLOBAL $PurchaseCategory;
        if(empty($PurchaseCategory))$PurchaseCategory=getcache('PurchaseCategory');


        if(empty($PurchaseCategory)){
            $return['status'] = 0;
        }else{
            $newcategory_list=array();
            foreach($PurchaseCategory as $k=>$v){
                if($v['status']!=1)continue;
                $newcategory_list[$v['pid']][]=$v;
            }
            foreach($newcategory_list[0] as $k=>$v){
                $category[$k]=$v;
                if(!empty($newcategory_list[$v['id']])){//有子集
                    $category[$k]['child']=count($newcategory_list[$v['id']]);
                    foreach($newcategory_list[$v['id']] as $ke=>$va){
                        $category[$k]['child_cate'][0] = array('id' => 0, 'name' => '全部');

                        $category[$k]['child_cate'][]=$va;
                    }
                }else{$category[$k]['child']=0;}
            }

            if($tpl=="shops_index_blue"){
                $category_info = array();
                foreach ($category as $key => $val) {
                    $category_info[0]['id'] = "0";
                    $category_info[0]['name'] = "店铺推荐";
                    $category_info[] = $val;
                }
                $return['cate_goods_list'] = $category_info;
            }else{
                $return['cate_goods_list'] = $category;
            }

        }return $return;
    }
    public function get_shops(){
        $model='Common/Takeout';
		$data=$promote_goodsids=array();$data['status']=1;
		$shop_id = session('main_shop_id');
		//获取商品
		$return=D($model)->get_goods_list();
		if($return['status'] == 1){
			$goods_list = $return['goods_list'];$category_list = $return['category'];
		}
		
		if($goods_list){
			foreach($goods_list as $k=>$v){
				if(empty($cate_goods_list[$v['cat_id']])) {
					if ($category_list[$v['cat_id']]) {
						$cate_goods_list[$v['cat_id']] = $category_list[$v['cat_id']];
					}
				}
				$cate_goods_list[$v['cat_id']]['goods_list'][$v['id']]=$v;
			}
		}
        $data['cate_goods_list']=$cate_goods_list;
        $data['delivery']=D($model)->get_shops_delivery($shop_id);
        return $data;
    }
    public function get_cart_list($shop_id){
        $model='Common/Takeout';$return=array();$return['status'] = 1;
        if(empty($shop_id)){$return['status'] = 0;return $return;}
        $data=D($model)->get_cart_goods_list($shop_id);
		if(empty($data)){
			$return['status'] = 0;return $return;
		}else{
			$return['data'] = $data;
		}
        return $return;
    }
    public function get_goods($id,$shop_id){
        $model='Common/Takeout';
		$data=array();$data['status']=1;
		
		if(empty($id) || empty($shop_id)){
			$data['status']=0;$data['info']='参数获取错误！';return $data;
		}
        $goods_info=D($model)->get_goods_info($id,$shop_id);
        if($goods_info['status']==0){
			$data['status']=0;$data['info']=$goods_info['info'];return $data;
		}
		$shops_info = D($model)->get_shops_info($shop_id);
        if($shops_info['status'] == 0){
			$data['status']=0;$data['info']=$shops_info['info'];return $data;
		}
		
        $data['shops_info'] = $shops_info['shops_info'];
        $data['goods_info']=$goods_info['goods_info'];
        $data['delivery']=D($model)->get_shops_delivery($shop_id);
        return $data;
    }
    public function add_cart($param){
        $model='Common/Takeout';
        $data=array();
        $shop_id = get_wxsmall_shop_id();
        if(!$param){
	  		$data['status']=0;$data['info']='请选择商品';return $data;
        }else{
		    if($param['spec']){
		      $attr_key = $param['spec'];
		    }else{
		      $attr_key = 0;
		    }
		    $stock = D("Common/Cart")->shopgoodsstock($param['goods_id'],$shop_id,$attr_key);
		    if($stock['goods_number'] > 0){
		        $return=D($model)->add_cart_item($param);
		        if($return['status'] == 1){
			    	$data['status']=1;$data['data'] = $return;return $data;
		        }else{
			    	$data['status']=0;$data['info']=$return['info'];return $data;
		        }
		    }else{
		        $data['status']=0;
		        $data['info']=$stock['info'];
		        if($stock['url'])$data['url']=$stock['url'];
		        return $data;
		    }
        }
    }
    public function add_spec_cart($param){
        $data = array();$data['status']=1;
        if(!$param){
			$data['status']=0;$data['info']='请选择商品';return $data;
		}
		$map_c = array();
		$map_c['id'] = $param['cart_id'];
		$map_c['goods_id'] = $param['goods_id'];
		$map_c['shop_id'] = $param['shop_id'];
		$map_c['cart_type'] = 3;
		$cart_info = M('Cart')->where($map_c)->field('id,shop_price,goods_num,goods_attr')->find();
		$goods_attr = unserialize($cart_info['goods_attr']);
		
		if($cart_info){
			if($param['is_add'] == 1){
				$save['goods_num'] = $cart_info['goods_num']+1;
			}else{
				$save['goods_num'] = $cart_info['goods_num']-1;
			}
			
			if($save['goods_num'] <= 0){
				$result = M('Cart')->where($map_c)->delete();
			}else{
				$s_map = array();
				$s_map['goods_id'] = $param['goods_id'];
				$s_map['shop_id'] = $param['shop_id'];
				$s_map['goods_stock'] = $goods_attr['key'];
				$goods_number=M('ShopsGoodsStock')->where($s_map)->getField('goods_number');
				if($save['goods_num'] > 0){
					if($goods_number < $save['goods_num']){
						$data['info'] = '库存不足!';
						$data['status'] = 0;
						return $data;
					}	
				}
				$save['all_price'] = $save['goods_num']*$cart_info['shop_price'];
				$result = M('Cart')->where($map_c)->save($save);
			}
			if($result){
				$data['cart_id'] = $cart_info['id'];
				$data['num'] = $save['goods_num'];
				$data['spec'] = $goods_attr['name'];
			}
		}
        return $data;
    }
    public function clear_cart($param){
        $model='Common/Takeout';$data = array();$data['status']=1;
		if(empty($param)){$data['status']=0;return $data;}
        $return=D($model)->clear_cart_item($param);
		if(empty($return)){$data['status']=0;return $data;}
		$data['data'] = $return;
        return $data;
    }
    public function get_order($shop_id,$addre_id,$ids=''){
        $model='Common/Takeout';$data=array();$data['status']=1;
        if(empty($shop_id)){$data['status']=0;$data['info']='参数错误';return $data;}
        $data=D($model)->get_order_info($shop_id,$addre_id,$ids);
        return $data;
    }
    public function count_order_price($param){
        $model='Common/OrderInfo';
        $data=D($model)->count_total($param);
        return $data;
    }
    public function sub_order($param){
        $model='Common/OrderInfo';
        $data=D($model)->sub_order($param);
        return $data;
    }
}