<?php
/**
 * 前台函数库
 * @Copyright (C) 2016 汉朝 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */

/**
 * 热销商品
 * @number int   显示的数量
 * @goos_type tinyint   商品类型
 * @return mixed
 */
function hotlist($number,$goods_type=1){
    $number=$number?$number:5;
    $order='sell_count desc,is_hot desc';
    $field='id,goods_name,img,sell_count,shop_price,market_price';
    $map['status']=1;$map['shop_id']=0;
    $map['goods_type']=$goods_type;
    if(C('IS_WAREHOUSE')==1&&$goods_type==5){
        $shops_info=session('shops_info');
        $area = idtoname($shops_info['shop_id'],'Shops','province,city_id,district');
        $area_id='';
        if($area['district']){
            $area_id = $area['district'];
        }
        if(!$area_id){
            $area_id = $area['city_id'];
        }
        if(!$area_id){
            $area_id = $area['province'];
        }
        $ids = library_goods($area_id);
        if(count($ids)>0){
            $map['id']=array('in',$ids);
        }
    }
    $list['list']=M('Goods')->where($map)->order($order)->limit($number)->getField($field,true);
    return $list['list'];
}
/**
 * 新品商品
 * @number int   显示的数量
 * @return mixed
 */
function newlist($number){
	$number=$number?$number:5;
	$order='addtime desc,is_new desc,updatetime desc,id desc';
	$field='id,goods_name,img,shop_price,market_price';
	$map['status']=1;$map['shop_id']=0;
	$list=M('Goods')->where($map)->order($order)->limit($number)->getField($field,true);
    return $list;
}
/**
 * 最新文章
 * @return mixed
 */
function new_article(){
	$num=C('NEW_ARTICLE_NUM')?C('NEW_ARTICLE_NUM'):5;
    $list=page_list('Article',$map,'addtime desc','id,title',$num,'',1);
    return $list['list'];
}
/**
 * 历史记录
 * @return mixed
 */
function historylist(){
    $his_map=array();
    $his_count=-C('BROWSING_HISTORY_NUM');
    $historyids=session('historyids');$historyids=array_unique($historyids);
    $historyids=array_slice($historyids,$his_count);
    if(!empty($historyids)){
        $his_map['id']=array('in',$historyids);
        $list=D('Goods')->get_goods_list(0,$his_map,'','',1,5);
        return $list['list'];
    }
}
/**
 * 历史记录
 * @return mixed
 */
function phistorylist(){
    $his_map=array();
    $his_count=-C('BROWSING_HISTORY_NUM');
    $historyids=session('phistoryids');$historyids=array_unique($historyids);
    $historyids=array_slice($historyids,$his_count);
    if(!empty($historyids)){
        $his_map['id']=array('in',$historyids);
        $list=D('Goods')->get_goods_list(0,$his_map,'','',1,5);
        return $list['list'];
    }
}
/**
 * 购物车小页
 * @return string
 */
function min_cart(){
    $View= new \Common\Vendor\TplView();
    $city_data=init_city();
    $View->assign($city_data);

    $data=D('Common/Cart')->cart_index();
    $View->assign($data);

    $html=$View->fetch('Public/min_cart');
    return $html;
}
/**
 * 订货购物车小页
 * @return string
 */
function pmin_cart(){
    $View= new \Common\Vendor\TplView();
    $city_data=init_city();
    $View->assign($city_data);

    $data=D('Common/Cart')->cart_indexp();
    $View->assign($data);

    $html=$View->fetch('Public/pmin_cart');
    return $html;
}
/**
 * 城市切换
 * @return array
 */
function init_city(){
    $result = D('Common/Area')->init();//初始化地区信息
    $regioncache = get_arearegion_cache(1);
    $result_format = D('Common/Area')->area_format($regioncache,1);//格式化省信息
    $root['regioncache']=$result_format;
    $root['address_info_format']=$result['address_info_format'];
    $province = Intval($result['address_info']['province']);
    $city = Intval($result['address_info']['city']);
    if($province>0){
        $regioncaches = get_arearegion_cache(0);
        $result_format = D('Common/Area')->area_format($regioncaches[$province]);//格式化市信息
        $root['regioncitycache']=$result_format;
        $root['regioncityon']=$province;
        if($city>0){
            $result_format = D('Common/Area')->area_format($regioncaches[$city]);//格式化区县信息
            $root['regiondistrictcache']=$result_format;
            $root['regiondistricton']=$city;
        }
    }
    return $root;
}
/**
    * 获取首页楼层内容
    * @return str 返回完整的楼层数据
    */
function get_index_floor_content(){
    /*$where['status'] = 1;
    $where['device'] = 0;
    $floor_list=M('Floor')->where($where)->field(true)->order('sort asc,id desc')->select();
    $floorontent='';
    $GoodsCategory=getcache('GoodsCategory');
    foreach($floor_list as $key=>$val){
        $goods_num = 8;//调取商品数量
        $index_list_goods=$recommend_cate_arr=array();
        if(empty($val['recommend_cate'])){
            $val['recommend_cate']=$val['bind_cateid'];
        }
        $recommend_cate=explode(',',$val['recommend_cate']);
        foreach($recommend_cate as $k=>$v){
            $v_map['cat_id']=$v;
            $recommend_cate_arr[$v]=$GoodsCategory[$v];
            $index_list_goods[$v]=D('Common/Goods')->get_goods_list(0,$v_map,'','',1,$goods_num);
        }
        $View= new \Common\Vendor\TplView();
        $View->assign('floorinfo',$val);
        $View->assign('recommend_cate_arr',$recommend_cate_arr);
        $View->assign('index_list_goods',$index_list_goods);
        $floorontent.=$View->fetch('FloorTheme/'.$val['theme']);
    }
    return $floorontent;*/
}
/**
    * 获取首页楼层内容
    * @return str 返回完整的楼层数据
    */
function get_purindex_categoods($cat_id){
	/*GLOBAL $PurchaseCategory;
	if (empty($PurchaseCategory)) $PurchaseCategory = getcache('PurchaseCategory');
	$map = $ids = array();
	
	$map['cat_id'] = array('in',$PurchaseCategory[$cat_id]['allson']);
	$map['goods_type'] = 5;
	if(C('IS_WAREHOUSE')==1){//仓库开启
		$shops_info=session('shops_info');
		$area = idtoname($shops_info['shop_id'],'Shops','province,city_id,district');
		$area_id='';
		if($area['district']){
			$area_id = $area['district'];
		}
		if(!$area_id){
			$area_id = $area['city_id'];
		}
		if(!$area_id){
			$area_id = $area['province'];
		}
		$ids = library_goods($area_id);
		if(count($ids)>0){
			$map['id']=array('in',$ids);
		}else{
			$map['id']='bcz';
		}

	}
	$goodslist = M('Goods')->where($map)->field(true)->select();
	return $goodslist;*/
}
function call_info($xml){
    /*if(empty($xml))return;
    $add['caller'] = $xml['Caller'];
    $add['calles'] = $xml['Callee'];
    $add['status'] = 1;
    $add['addtime'] = NOW_TIME;
    $add['call_type'] = 2;
    $uid = M('Member')->where('mobile='.$xml['Caller'])->getField('id');
    $shop_id = M('Shops')->where('shops_call='.$xml['Callee'])->getField('id');
    if($uid) $add['uid'] = $uid;
    if($shop_id) $add['shop_id'] = $shop_id;
    $result = M('CallLog')->add($add);
     
    $where['shops_call'] = $xml['Callee'];
    $shop_id = M('Shops')->where($where)->getField('id');
    $mobiles = M('ShopsAccount')->where('shop_id='.$shop_id)->getField('mobile',true);
        //print_r($mobiles);exit;
    
    $msg = 'caller,'.$xml['Caller'].',type:1';
    sendIm($mobiles,$msg);//发送及时通讯消息
    */
}
function getnexnum($i,$notin=array()){
    $next=$i+1;
    if($notin && in_array($next,$notin))return getnexnum($next,$notin);
    return $next;
}
?>