<?php
/* 快捷商城系统
 * 版本号：2.0
 * 官网：http://www.kjcms.cn
 * 2015-02-16
 */
class fun_kj{
	/* 取指定id广告信息
	 * id 广告id
	 */
	static function get_ads($position , $type = 0 , $ads_shop_id = 0) {
		$obj_db = cls_obj::db();
		$where = " where ads_position='" . $position . "' and ads_state>0 and ads_starttime<'" . TIME . "' and ads_endtime>'" . TIME . "'";
		if($ads_shop_id>0) $where .= " and ads_shop_id='" . (int)$ads_shop_id . "'";
		$obj_rs = $obj_db->get_one("select ads_html,ads_cont from " . cls_config::DB_PRE . "other_ads" . $where);
		if(!empty($obj_rs)) {
			$html = fun_get::filter($obj_rs['ads_html'],true);
			if($type == 1) {
				$arr = array();
				$obj_rs['ads_cont'] = unserialize($obj_rs['ads_cont']);
				$arr = tab_other_ads::get_html2cont($obj_rs['ads_cont']);
				$arr['html'] = $html;
				return $arr;
			} else {
				return $html;
			}
		} else {
			return ($type == 1)? array("html"=>'') : '';
		}
	}
	//
	static function get_pagebtns( $arr_info , $listnum = 10 ) {
		$arr_return = array("pre" => 0, "next" => 0,"list" => array() , "start" => 1 , "end" => $arr_info['pages']);
		if($arr_info['total'] < 1) return $arr_return;
		$arr_return['pre'] = max(0,$arr_info['page']-1);
		$arr_return['next'] = $arr_info['page']+1;
		if($arr_return['next'] > $arr_info['pages']) $arr_return['next'] = 0;

		if($arr_info["pages"] > $listnum) {
			$ii = intval($listnum/2);
			if($arr_info['page'] > $ii){
				$lng_pre=$arr_info['page'] - $ii;
				$lng_next=$arr_info['page'] + $ii;
			}else{
				$lng_pre=1;
				$lng_next = $listnum;
			}
		}else{
			$lng_pre=1;
			$lng_next=$arr_info['pages'];
		}
		if($lng_pre < 1) $lng_pre = 1;
		if( $lng_next >= $arr_info['pages'] ) $lng_next = $arr_info['pages'];
		$arr_return['start'] = $lng_pre;
		$arr_return['end'] = $lng_next;

		for($i=$lng_pre;$i<=$lng_next;$i++){
			$arr_return['list'][] = $i;
		}
		return $arr_return;
	}
	/* 取缓存信息
	 * type : 缓存字段分类
     * format 表示输入格式：取值范围:空，json
	 */
	static function get_cache_words($type , $val , $format = '' ,$top = 0) {
		$arr_return = array();
		$obj_db = cls_obj::db();
		$arr_where = array("words_type like '" . $type . "%'");
		if(!empty($val)) {
			$arr_where[] = "(words_val like '" . $val . "%' or words_pin like '" . $val . "' or words_jian like '" . $val . "%')";
		}
		$limit = '';
		if(!empty($limit)) $limit = 'limit 0,' . $top;
		$where = implode(" and " , $arr_where);
		$obj_result = $obj_db->select("select words_val from " . cls_config::DB_PRE . "sys_cache_words where " . $where . " order by words_num desc" . $limit);
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$arr_return[] = $obj_rs["words_val"];
		}
		if($format == 'json') $arr_return = fun_format::json($arr_return);
		return $arr_return;
	}
	//格式化价格
	static function get_price($price , $tag = 'i') {
		$arr = explode("." , $price);
		$val = $arr[0];
		if(isset($arr[1]) && (int)$arr[1]>0) {
			if(substr($arr[1],1,1)=='0') $arr[1] = substr($arr[1],0,1);
			$val = $val . "<" . $tag . ">." . $arr[1] . "</" . $tag . ">";
		}
		$val = '￥' . $val;
		return $val;
	}
	//取当前站点信息
	static function get_site() {
		$cfgdomain = cls_config::get('domain' , 'weixin' , '' , '');
		$domain = fun_format::domain($_SERVER["HTTP_HOST"]);

		$arr_return = array(
			"uname" => cls_config::get('uname' , 'weixin' , '' , '') ,
			"appid" => cls_config::get('appid' , 'weixin' , '' , '') ,
			"appsecret" => cls_config::get('appsecret' , 'weixin' , '' , '') ,
			"token" => cls_config::get('token' , 'weixin' , '' , '') ,
			"mediatype" => cls_config::get('mediatype' , 'weixin' , '' , '') ,
			"site_id" => 0,
			"shop_id" => 0,
			"msgmode" => cls_config::get("msgmode" , "weixin" , '' , ''),
			"certify" => cls_config::get("certify" , "weixin" , '' , ''),
			"site_name" => '默认',
			"customview" => 0,
			"pic" => '',
			"pic_small" => '',
		);
		return $arr_return;
	}
	function get_area_all() {
		$arr_return = array("list" => "" , "default" => array() ,"depth" => 0 , "area" => "");
		$obj_db = cls_obj::db();
		$arr_area = $arr_default = $arr_list = array();
		$str_where = "";
		$obj_result = $obj_db->query("select area_id as 'id',area_name as 'name',area_pid as 'pid',area_depth as 'depth',area_val as 'val' from " . cls_config::DB_PRE . "sys_area" . $str_where . " order by area_depth,area_sort,area_name");
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			if(empty($obj_rs["name"])) $obj_rs["name"] = $obj_rs["val"];
			if(empty($obj_rs["val"])) $obj_rs["val"] = $obj_rs["name"];
			if($obj_rs['val'] == $obj_rs['name']) unset($obj_rs['val']);
			$arr_list["id_" . $obj_rs["pid"]][] = $obj_rs["id"];
			$arr_area["id_" . $obj_rs["id"]] = $obj_rs;
			if($obj_rs["pid"] == 0) $arr_return["default"][] = $obj_rs;
			if($arr_return["depth"]<$obj_rs['depth']) $arr_return["depth"] = $obj_rs['depth'];
		}
		$arr_return["area"] = $arr_area;
		$arr_return["list"] = $arr_list;
		return $arr_return;
	}
	//取当前登录用户所有收货信息
	static function get_address($shop_id = 0) {
		$obj_db = cls_obj::db();
		$arr_info = array() ;
		$arr_area_id = array();
		$lng_uid = cls_obj::get("cls_user")->uid;
		if(empty($lng_uid)) $lng_uid = -1;
		if(empty($shop_id)) {
			$sql = "select * from ".cls_config::DB_PRE."sys_user_address where address_user_id='" . $lng_uid . "' order by address_order_time desc";
		} else {
			$sql = "select * from ".cls_config::DB_PRE."sys_user_address where address_user_id='" . $lng_uid . "' and address_area_id in (select area_id from " . cls_config::DB_PRE . "meal_dispatch a left join " . cls_config::DB_PRE . "sys_area b on " . $obj_db->concat("," , 'b.area_pids' , ',') . " like " . $obj_db->concat("%," , "a.dispatch_area_id" , ",%") . " where a.dispatch_shop_id='" . $shop_id . "' order by address_order_time desc)";
		}
		$obj_result = $obj_db->query($sql);
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$arr_info[] = $obj_rs;
		}
		return $arr_info;
	}
}