<?php
class datasource_inc {
	//取指定父级分类列表
	function get_category( $pid = 0) {
		$obj_db = cls_obj::db();
		$arr_where = $arr_childs = $arr_list = array();
		$arr_where[] = "category_pid='" . $pid . "'";
		$where = (empty($arr_where)) ? "" : " where " . implode(" and " , $arr_where);
		$obj_result = $obj_db->select("select category_id,category_name,category_pid,category_pic from " . cls_config::DB_PRE . "mall_goods_category" . $where . " order by category_depth desc,category_sort");
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$arr_list[] = $obj_rs;
		}
		return $arr_list;
	}
	//取分类列表
	function get_category_all( $pid = -1 , $depth = 0) {
		$obj_db = cls_obj::db();
		$arr_where = $arr_childs = $arr_list = array();
		if(!empty($pid)) $arr_where[] = $obj_db->concat(",","category_pids", ",") . " like '%," . $pid . ",%'";
		if(!empty($depth)) $arr_where[] = "category_depth<='" . $depth . "'";
		$where = (empty($arr_where)) ? "" : " where " . implode(" and " , $arr_where);
		$obj_result = $obj_db->select("select category_id,category_name,category_pid from " . cls_config::DB_PRE . "mall_goods_category" . $where . " order by category_depth desc,category_sort");
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			//if(!isset($arr_list['id_' . $obj_rs['category_pid']])) $arr_list['id_' . $obj_rs['category_pid']] = array();
			$obj_rs['childs'] = (isset($arr_childs['id_' . $obj_rs['category_id']])) ? $arr_childs['id_' . $obj_rs['category_id']] : array();
			if(empty($obj_rs['category_pid'])) {
				$arr_list[] = $obj_rs;
			} else {
				$arr_childs['id_' . $obj_rs['category_pid']][] = $obj_rs;
			}
		}
		return $arr_list;
	}
	//商品详情
	function get_goods_detail($id) {
		$obj_db = cls_obj::db();
		$obj_goods = $obj_db->get_one("select * from " . cls_config::DB_PRE . "mall_goods a left join " . cls_config::DB_PRE . "mall_goods_ext b on a.goods_id=b.ext_goods_id where goods_id='" . $id . "'");
		if(empty($obj_goods)) return array();
		$obj_goods['goods_detail'] = fun_get::filter($obj_goods['goods_detail'],true);
		//相册列表
		$obj_goods['pics'] = array();
		$obj_result = $obj_db->select("select * from " . cls_config::DB_PRE . "mall_goods_pic where pic_goods_id='" . $id . "' order by pic_sort,pic_id");
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$obj_rs['pic_url'] = fun_get::html_url($obj_rs['pic_url']);
			$obj_goods['pics'][] = $obj_rs;
		}
		$arr_ids = array();
		//规格列表
		$arr_format_detail = $arr_format_name = $arr_format_list = array();
		$obj_result = $obj_db->select("select * from " . cls_config::DB_PRE . "mall_goods_detail where detail_goods_id='" . $id . "' order by detail_id");
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$obj_rs['detail_format'] = unserialize($obj_rs['detail_format']);
			foreach($obj_rs['detail_format'] as $item) {
				$arr_ids[] = $item['id'];
				$arr_format['id_' . $item['id']][] = $item['name'];
			}
			$store = $obj_rs['detail_store'] - $obj_rs['detail_sold'];
			$arr_format_detail[] = array("id" => $obj_rs['detail_id']  , "name" => $obj_rs['detail_name'] , "sotre" => $store , "price" => $obj_rs['detail_price'] , "weight" => $obj_rs['detail_weight'] , "pic" => $obj_rs['detail_pic']);
		}
		if(!empty($arr_ids)) {
			$arr_ids = array_unique($arr_ids);
			$str_ids = implode("," , $arr_ids);
			$obj_result = cls_obj::db()->select('select format_id,format_name,format_val from ' . cls_config::DB_PRE . 'mall_goods_format where format_id in (' . $str_ids . ')');
			while($obj_format = cls_obj::db()->fetch_array($obj_result)) {
				$arr_format['id_' . $obj_format['format_id']] = array_unique($arr_format['id_' . $obj_format['format_id']]);
				$arr_format_list[] = array("id"=>$obj_format["format_id"] , "name"=>$obj_format["format_name"] , "list"=>$arr_format['id_' . $obj_format['format_id']]);
			}
		}
		$obj_goods['format_list'] = $arr_format_list;
		$obj_goods['format'] = $arr_format_detail;
		$obj_goods['is_collect'] = false;
		//是否收藏
		if(cls_obj::get("cls_user")->is_login()) {
			$obj_collect = cls_obj::db()->get_one("select 1 from " . cls_config::DB_PRE . "mall_goods_collect where collect_user_id='". cls_obj::get("cls_user")->uid . "' and collect_goods_id='" . $id . "'");
			if(!empty($obj_collect)) $obj_goods['is_collect'] = true;
		}
		return $obj_goods;
	}

	//取指定分类子分类
	function get_category_childs($id) {
		$arr_return = array();
		$obj_db = cls_obj::db();
		$obj_result = $obj_db->select('select category_id,category_name from ' . cls_config::DB_PRE . "mall_goods_category where category_pid='" . $id . "'");
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$arr_return[] = $obj_rs;
		}
		return $arr_return;
	}
	//取路径
	function get_path( $id , $pids = null) {
		$arr_return = array();
		$obj_db = cls_obj::db();
		if(empty($pids)) {
			$obj_pid = $obj_db->get_one("select category_pids from " . cls_config::DB_PRE . "mall_goods_category where category_id='" . $id . "'");
			if(!empty($obj_pid)) $pids = $obj_pid['category_pids'];
		}
		$pids = (empty($pids)) ? $id : $pids . "," . $id;
		$obj_result = $obj_db->select('select category_id,category_name from ' . cls_config::DB_PRE . "mall_goods_category where category_id in(". $pids . ") order by category_depth");
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$arr_return[] = $obj_rs;
		}
		return $arr_return;
	}
	function get_brand_list($cid , $len = 0) {
		$obj_db = cls_obj::db();
		$arr_list = array();
		$limit = ($len > 0) ? " limit 0," . $len : "";
		$obj_result = $obj_db->select("select brand_id,brand_name,brand_pic from " . cls_config::DB_PRE . "mall_brand where brand_id in(select brand_id from " . cls_config::DB_PRE . "mall_goods_category_brand a left join " . cls_config::DB_PRE . "mall_goods_category b on a.brand_category_id=b.category_id where " . $obj_db->concat(",","category_pids", ",") . " like '%," . $cid . ",%')" . $limit);
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$arr_list[] = $obj_rs;
		}
		return $arr_list;
	}
	function get_brand_info($brand_id) {
		$brand_id = (int)$brand_id;
		$obj_rs = cls_obj::db()->edit(cls_config::DB_PRE."mall_brand" , "brand_id='".$brand_id."'");
		return $obj_rs;
	}
}