<?php
/* 快捷商城系统
 * 版本号：2.0
 * 官网：http://www.kjcms.cn
 * 2015-02-16
 */
class tab_mall_goods_category_brand {
	static function on_save($arr_fields) {
		$arr_return = array("code"=>0,"id"=>0,"msg"=>"");
		if(!isset($arr_fields['brand_category_id']) || !isset($arr_fields['brand_id'])) return array("code" => 500 , "msg" => "分类或品牌不能为空");
		if(empty($arr_fields['brand_category_id']) || empty($arr_fields['brand_id'])) return array("code" => 500 , "msg" => "分类或品牌不能为空");
		$obj_rs = cls_obj::db()->get_one("select * from " . cls_config::DB_PRE . "mall_goods_category_brand where brand_id='" . $arr_fields['brand_id'] . "' and brand_category_id='" . $arr_fields['brand_category_id'] . "'");
		if(!empty($obj_rs)) return array("code" => 500 , "msg" =>"同分类下品牌不能重复");
		$arr_return = cls_obj::db_w()->on_insert(cls_config::DB_PRE."mall_goods_category_brand",$arr_fields);
		return $arr_return;
	}

	/* 删除函数
	 * arr_id : 要删除的 id数组
	 * where : 删除附加条件
	 */
	static function on_delete($arr_id , $where = '') {
		$arr_return = array("code"=>0,"msg"=>"");
		$str_id = fun_format::arr_id($arr_id);
		if( empty($str_id) && empty($where) ){
			$arr_return["code"] = 22;
			$arr_return["msg"]=cls_language::get("not_where");
			return $arr_return;
		}
		if( !empty($str_id) ) {
			(is_numeric($str_id)) ? $arr_where[] = "brand_id='".$str_id."'" : $arr_where[] = "brand_id in(".$str_id.")";
		}
		if( !empty($where) ) {
			if(stristr($where , " or ") && substr(trim($where),0,1) != "(") $where = "(" . $where . ")";
			$arr_where[] = $where;
		}
		$where = implode(" and " , $arr_where);

		$arr_return=cls_obj::db_w()->on_delete(cls_config::DB_PRE."mall_goods_category_brand" , $where);
		return $arr_return;
	}

	//将attribute_val 转换成数组
	function format_val($val) {
		$arr_return = array();
		$arr = explode(chr(10) , $val);
		foreach($arr as $item) {
			$arr_return[] = explode("=>" , $item);
		}
		return $arr_return;
	}
}