<?php
/* 快捷商城系统
 * 版本号：2.0
 * 官网：http://www.kjcms.cn
 * 2015-02-16
 */
class mod_goods extends inc_mod_default {
	function get_category_info( $id ) {
		$arr_return = array("pids" => "" , "id" => $id , "name" => "" , "wap_show" => '' , "pid" => 0 , "child" => 0);
		$obj_category = cls_obj::db()->get_one("select category_pids as 'pids',category_id as 'id',category_name as 'name',category_wap_show as 'wap_show',category_pid as 'pid',category_child as 'child' from " . cls_config::DB_PRE . "mall_goods_category where category_id='" . $id . "'");
		if(empty($obj_category)) return $arr_return;
		if(!empty($obj_category['pids']) && empty($obj_category['wap_show']) ) {
			$obj_rs = cls_obj::db()->get_one("select category_wap_show from " . cls_config::DB_PRE . "mall_goods_category where category_id in(" . $obj_category['pids'] . ") and category_wap_show>0 order by category_depth desc");
			if(!empty($obj_rs)) $obj_category['wap_show'] = $obj_rs['category_wap_show'];
		}
		return $obj_category;

	}
	//商品列表
	function get_goods_list( $cid , $attribute = array() , $lng_pagesize = 20) {
		$arr_return = array();
		$obj_db = cls_obj::db();
		$arr_where = array('goods_state>0');
		if(!empty($cid)) $arr_where[] = "(" . $obj_db->concat("," , "c.category_pids", ",") . " like '%," . $cid . ",%' or a.goods_category_id='" . $cid . "')";
		//取属性查找条件
		if(!empty($attribute)) {
			$arr_attribute_name = array_keys($attribute);
			foreach($arr_attribute_name as $index => $name) {
				if( !fun_is::set("attri".$index) ) continue;
				$i = (int)fun_get::get("attri".$index);
				if(!isset($attribute[$name][$i])) continue;
				$arr_where[] = "goods_category_attribute like '%" . $name . tab_mall_goods::get_perms("sp2") . $attribute[$name][$i] . "%'";
			}
		}
		$s_key = fun_get::get("s_key");
		if(!empty($s_key)) $arr_where[] = "goods_name like '%" . $s_key . "%'";
		$sort = (int)fun_get::get("sort");
		$sortby = fun_get::get("sortby");
		$order = " order by ";
		switch($sort) {
			case 1:
				$order .= " goods_id";
				break;
			case 2:
				$order .= " goods_price";
				break;
			case 3:
				$order .= " goods_sold";
				break;
			case 4: 
				$order .= " goods_hits";
				break;
			default:
				$order .= " goods_tj1 desc,goods_tj2 desc";
		}
		if($sortby == 'up') {
			$order .= " asc";
		} else if(!empty($sort)) {
			$order .= " desc";
		}
		$lng_page = (int)fun_get::get("page");
		$str_where = (empty($arr_where)) ? "" : " where " . implode(" and " , $arr_where);
		$arr_return["list"] = array();
		$arr_return["pageinfo"] = $obj_db->get_pageinfo(cls_config::DB_PRE."mall_goods a left join " . cls_config::DB_PRE . "mall_goods_category c on a.goods_category_id=c.category_id" , $str_where , $lng_page , $lng_pagesize);
		$obj_result = $obj_db->select("SELECT goods_id,goods_name,goods_price,goods_price_vip,goods_price_market,goods_pic_small,goods_pic,goods_sold,goods_comment_num,goods_attribute_new,goods_attribute_tj,goods_attribute_hot,goods_format,goods_postage_areas 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 left join " . cls_config::DB_PRE . "mall_goods_category c on a.goods_category_id=c.category_id" . $str_where . $order . $arr_return['pageinfo']['limit']);
		while( $obj_rs = $obj_db->fetch_array($obj_result) ) {
			$obj_rs['goods_format'] = empty($obj_rs['goods_format']) ? array() : unserialize($obj_rs['goods_format']);
			$obj_rs['pic'] = (empty($obj_rs['goods_pic_small'])) ? $obj_rs['goods_pic'] : $obj_rs['goods_pic_small'];
			$obj_rs['pic'] = fun_get::html_url($obj_rs['pic']);
			$obj_rs['goods_price'] = cls_mall::get_price($obj_rs['goods_price'] , $obj_rs['goods_price_vip']);
			$obj_rs['price'] = fun_kj::get_price($obj_rs['goods_price']);
			$obj_rs['name'] = $obj_rs['goods_name'];
			if(!empty($s_key)) $obj_rs['goods_name'] = str_replace($s_key , "<font style='color:#ff3300'>" . $s_key . "</font>" , $obj_rs['goods_name']);
			$arr_return['list'][] = $obj_rs;
		}
		$agent = fun_get::agent();
		$basename = cls_config::get("basename");
		$pagebtns = 10;
		if(!in_array($agent,array('','ipad')) || $basename == 'wap.php') $pagebtns = 5;
		
		$arr_return['pagebtns']   = fun_kj::get_pagebtns($arr_return['pageinfo'] , $pagebtns);
		return $arr_return;
	}

	//商品详情
	function get_goodsinfo($msgid) {
		$obj_db = cls_obj::db();
		$arr = explode("-" , $msgid);
		$id = (int)$arr[0];
		$detailid = (count($arr)>1) ? (int)$arr[1] : 0;
		$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)) cls_error::on_exit("exit","商品信息不存在");
		$obj_goods['goods_pic'] = fun_get::html_url($obj_goods['goods_pic']);
		$obj_goods['pics'] = $arr_format = array();
		$obj_goods['price'] = cls_mall::get_price($obj_goods['goods_price'],$obj_goods['goods_price_vip']);
		$obj_goods['goods_format'] = empty($obj_goods['goods_format']) ? array() : unserialize($obj_goods['goods_format']);
		$obj_goods['comment_rate'] = $obj_goods['goods_comment_val']/5*100;
		if($obj_goods['comment_rate']>100) $obj_goods['comment_rate'] = 100;
		if($obj_goods['comment_rate']<0) $obj_goods['comment_rate'] = 0;
		$obj_goods['goods_comments'] = tab_mall_goods_comment::get_list_rate($obj_goods['goods_comments']);
		foreach($obj_goods['goods_format'] as $format) {
			$arr_format['id_' . $format['id']] = $format;
		}
		$obj_goods['goods_detail'] = fun_get::filter($obj_goods['goods_detail'] , true);
		$obj_result = $obj_db->select("select * from " . cls_config::DB_PRE . "mall_goods_pic where pic_goods_id='" . $id . "' order by pic_sort");
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$obj_goods['pics'][] = fun_get::html_url($obj_rs['pic_url']);
		}
		$obj_goods['detail'] = $obj_goods["format_sel"] = array();
		if(!empty($arr_format)) {
			$arr_x = array();
			$obj_result = $obj_db->select("select * from " . cls_config::DB_PRE . "mall_goods_detail where detail_goods_id='" . $id . "'");
			while($obj_rs = $obj_db->fetch_array($obj_result)) {
				if($obj_rs['detail_price']==0) $obj_rs['detail_price'] = $obj_goods['price'];
				if($obj_rs['detail_price_market'] == 0) $obj_rs['detail_price_market'] = $obj_goods['goods_price_market'];
				$obj_rs['price'] = cls_mall::get_price($obj_rs['detail_price'],$obj_rs['detail_price_vip']);
				$obj_rs['detail_format'] = empty($obj_rs['detail_format']) ? array() : unserialize($obj_rs['detail_format']);
				$obj_rs['pic'] = $obj_rs['detail_pic'];
				if(empty($obj_rs['pic'])) $obj_rs['pic'] = $obj_goods['goods_pic'];
				$obj_rs['pic'] = fun_get::html_url($obj_rs['detail_pic']);
				if(!empty($obj_rs['detail_pic'])) $obj_goods['pics'][] = $obj_rs['pic'];
				foreach($obj_rs["detail_format"] as $item){
					if( $obj_rs['detail_id'] == $detailid ) {
						$obj_goods["format_sel"]['id_'.$item["id"]] = $item["name"];
						$obj_goods["format_sel"]['pic'] = $obj_rs['pic'];
					}
					if(!isset($obj_goods["format_".$item["id"]]) || !isset($obj_goods["format_".$item["id"]][$item["name"]])) $obj_goods["format_".$item["id"]][$item["name"]] = array('name' => $item["name"] , 'pic' => $obj_rs['pic']);
				}
				$arr_x2=array_values($obj_rs["detail_format"]);
				$arr_x2[0]["detailid"]=$obj_rs["detail_id"];
				$arr_x2[0]["price"] = $obj_rs['price'];
				$arr_x2[0]["mprice"] = $obj_rs['detail_price_market'];
				$arr_x2[0]["pic"] = $obj_rs['pic'];
				$arr_x2[0]["gname"] = $obj_rs['detail_name'];
				$arr_x[]=$arr_x2;
				$obj_goods['detail'][] = $obj_rs;
			}
			$obj_goods["format_val"]=fun_format::json($arr_x);
		} else {
			$obj_goods["format_val"] = '';
			$obj_goods["format_val"] = fun_format::json(array());
		}
		if(!isset($obj_goods["format_".$detailid])) $detailid = 0;
		$obj_goods['detailid'] = $detailid;
		return $obj_goods;
	}
}