<?php
/**
 * 用户表操作类
 */
class tab_mall_goods_category_attributed {
	static function get_save($pids , $name , $val) {
		$obj_rs = cls_obj::db()->get_one("select 1 from " . cls_config::DB_PRE . "mall_goods_category_attributed where attributed_pids='" . $pids . "' and attributed_name='" . $name . "' and attributed_val='" . $val . "'");
		if(!empty($obj_rs)) return;
		return array("attributed_pids" => $pids , "attributed_name" => $name , "attributed_val" => $val);
	}
	/*
	 *
	 */
	static function on_del($pids , $name , $val) {
		$where = " where attributed_pids='" . $pids . "'";
		if(!empty($name)) $where .= " and attributed_name='" . $name . "'";
		if(!empty($val)) $where .= " and attributed_val='" . $val . "'";
		cls_obj::db_w()->on_exe("delete from " . cls_config::DB_PRE . "mall_goods_category_attributed" . $where);
	}

	static function on_chk($pids , $name , $val) {
		$key = $name . tab_mall_goods::get_perms("sp2") . $val;
		$obj_rs = cls_obj::db()->get_one("select 1 from " . cls_config::DB_PRE . "mall_goods where goods_category_id in (" . $pids . ") and goods_category_attribute like '%" . $key . "%'");
		if(empty($obj_rs)) {
			self::on_del($pids , $name , $val);
		} else {
			$arr = self::get_save($pids , $name , $val);
			if(!empty($arr)) cls_obj::db_w()->on_insert_all(cls_config::DB_PRE . "mall_goods_category_attributed" , array($arr));
		}
	}

	static function on_goods_save($pids , $attribute , $attribute_old) {
		$arr_attribute = (is_array($attribute)) ? $attribute : explode(tab_mall_goods::get_perms("sp1") , $attribute);
		$arr_name = $arr_val = array();
		foreach($arr_attribute as $item) {
			$arr = explode(tab_mall_goods::get_perms("sp2") , $item);
			$arr_name[] = $arr[0];
			$arr_val[$arr[0]] = (isset($arr[1])) ? $arr[1] : "";
		}
		if(!empty($attribute_old)) {
			$attribute_old = (is_array($attribute_old)) ? $attribute_old : explode(tab_mall_goods::get_perms("sp1") , $attribute_old);
			foreach($attribute_old as $item) {
				$arr = explode(tab_mall_goods::get_perms("sp2") , $item);
				if(!isset($arr_val[$arr[0]]) || $arr_val[$arr[0]] != $arr[1] ) {
					self::on_chk($pids , $arr[0] , $arr[1]);
				} else {
					$arr_name = fun_base::array_remove($arr_name , $arr[0]);
				}
			}
		}
		if(empty($arr_name)) return;
		$key = implode("','" , $arr_name);
		$arr_name = array();
		if(empty($pids)) $pids = '0';
		$obj_result = cls_obj::db()->select('select attribute_name from ' . cls_config::DB_PRE . "mall_goods_category_attribute where attribute_category_id in(" . $pids . ") and attribute_type='list' and attribute_name in('" . $key . "')");
		while($obj_rs = cls_obj::db()->fetch_array($obj_result)) {
			$arr_name[] = $obj_rs['attribute_name'];
		}
		$arr_save_fields = array();
		foreach($arr_name as $item) {
			if(empty($arr_val[$item])) continue;
			$arr = self::get_save($pids , $item , $arr_val[$item]);
			if(!empty($arr)) $arr_save_fields[] = $arr;
		}
		if(!empty($arr_save_fields)) cls_obj::db_w()->on_insert_all(cls_config::DB_PRE . "mall_goods_category_attributed" , $arr_save_fields);
	}

	//取指定分类属性
	static function get_list($id , $pids = null) {
		$obj_db = cls_obj::db();
		$arr_return = $arr_name = array();
		if($pids !== null) {
			$obj_pids = $obj_db->get_one("select category_pids from " . cls_config::DB_PRE . "mall_goods_category where category_id='" . $id . "'");
			if(empty($obj_pids)) return array();
			$pids = (empty($obj_pids['category_pids'])) ? $id : $obj_pids['category_pids'] . "," . $id;
		}
		$obj_result = $obj_db->select("select attribute_name from " . cls_config::DB_PRE . "mall_goods_category_attribute where attribute_category_id in(" . $pids . ")");
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$arr_name[] = $obj_rs['attribute_name'];
		}
		if(empty($arr_name)) return array();
		$names = implode("','" , $arr_name);
		$obj_result = $obj_db->select("select attributed_name as 'name' , attributed_val as 'val' from " . cls_config::DB_PRE . "mall_goods_category_attributed where " . $obj_db->concat("," , 'attributed_pids',",") . " like '%," . $id . ",%' and attributed_name in('" . $names . "')");
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			if(!isset($arr_return[$obj_rs['name']]) || !in_array($obj_rs['val'] , $arr_return[$obj_rs['name']] ) ) $arr_return[$obj_rs['name']][] = $obj_rs['val'];
		}
		return $arr_return;
	}
}