<?php

/**
 *--------------------------------------
 * 会员
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-3
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class MemberModl extends Modl
{
	public function get_memberInfo($memberId)
	{
		$_MI = $this->where('__MEMBER__.member_id = \''.$memberId.'\'')
			->join('__MEMBER_MODEL__ AS mm ON mm.member_model_id = __MEMBER__.member_model_id')
			->join('__MEMBER_LEVEL__ AS ml ON ml.member_level_id = __MEMBER__.member_level_id')
			->join('__MEMBER_CREDIT__ AS mc ON mc.member_id = __MEMBER__.member_id')
			->find();
		if(!empty($_MI))
		{
			/* 获取模型字段 */
			$at = get_instance('ATag', 'field');
			$at->tags = array();
			$at->parse_content($_MI['mm_fieldset']);
			$_MI['mm_field'] = $at->tags;

			/* 获取附加表信息 */
			$addon = M(parse_name($_MI['mm_addon_table'], 1))->where('member_id=\''.$_MI['member_id'].'\'')->find();
			$_MI = array_merge($_MI, $addon);
		}
		return $_MI;
	}

	public function add_member($data, $addon = true, $credit = true)
	{
		$result = array('data' => '', 'error' => '');

		$data['member_id'] = $this->insert($data);
		if(false === $data['member_id'])
		{
			$result['error'] = L('ADD_FAILED');
			return $result;
		}
		$result['data'] = $data['member_id'];

		/* 处理附加表数据 */
		if($addon)
		{
			$memberModelId = $data['member_model_id'];
			$_MMI = M('MemberModel')->get_modelInfo($memberModelId);
			/* 附加表模型 */
			if(false === M(parse_name($_MMI['mm_addon_table'], 1))->insert($data))
			{
				$result['error'] = L('ADD_ADDON_DATA_FAILED');
				return $result;
			}
		}

		/* 处理积分表 */
		if($credit)
		{
			if(false === M('MemberCredit')->insert($data))
			{
				$result['error'] = L('ADD_CREDIT_DATA_FAILED');
				return $result;
			}
		}

		return $result;
	}

	public function edit_member($data, $addon = true, $credit = true)
	{
		$result = array('data' => '', 'error' => '');

		if(false === $this->update($data))
		{
			$result['error'] = L('UPDATE_FAILED');
			return $result;
		}

		/* 处理附加表数据 */
		if($addon)
		{
			$memberModelId = $data['member_model_id'];
			$_MMI = M('MemberModel')->get_modelInfo($memberModelId);
			/* 处理附加表字段 */
			load('field#func');
			foreach($_MMI['mm_field'] as $tag => $params)
			{
				$data[$tag] = get_fieldValue($tag, $params, $data);
			}
			if(false === M(parse_name($_MMI['mm_addon_table'], 1))->update($data))
			{
				$result['error'] = L('UPDATE_ADDON_DATA_FAILED');
				return $result;
			}
		}

		/* 处理积分表 */
		if($credit)
		{
			if(false === M('MemberCredit')->update($data))
			{
				$result['error'] = L('UPDATE_CREDIT_DATA_FAILED');
				return $result;
			}
		}

		return $result;
	}

	public function delete_member($memberId)
	{

		$result = array('data' => '', 'error' => '');

		/* 检查是否管理组 */
		$isAdmin = M('Admin')->is_admin($memberId);
		if($isAdmin)
		{
			$result['error'] = L('ADMIN_IS_LOCKED');
			return $result;
		}

		$_MI = $this->get_memberInfo($memberId);
		if(empty($_MI))
		{
			$result['error'] = L('ITEM_NOT_EXIST');
			return $result;
		}

		if(false === $this->delete($memberId))
		{
			$result['error'] = L('DELETE_FAILED');
			return $result;
		}

		/* 删除附加表信息 */
		if(false === M(parse_name($_MI['mm_addon_table'], 1))->delete($memberId))
		{
			$result['error'] = L('DELETE_ADDON_DATA_FAILED');
			return $result;
		}

		/* 处理积分表 */
		if(false === M('MemberCredit')->delete($memberId))
		{
			$result['error'] = L('DELETE_CREDIT_DATA_FAILED');
			return $result;
		}

		/* 删除上传文件 */
		$_AUL = M('Upload')->where("`u_item_type` = 'member' and `u_item_id` = '".$memberId."'")->select();
		if(!empty($_AUL))
		{
			foreach($_AUL as $au)
			{
				/* 删除文件 */
				@unlink(realpath($_SERVER['DOCUMENT_ROOT'].$a['u_src']));
			}
		}
		/* 删除上传表信息 */
		M('Upload')->where("`u_item_type` = 'member' and `u_item_id` = '".$membereId."'")->delete();

		return $result;
	}

	public function check_userid($userid)
	{
		$userid = strtolower(trim($userid));
		$_o_m = M('Option')->get_option('member');
		if(in_array($userid, explode(',', $_o_m['name_ban'])))
		{
			return false;
		}
		$result = M('Member')->field('m_userid')->where("m_userid='".$userid."'")->find();
		if(!empty($result))
		{
			return false;
		}
		return true;
	}

	public function check_email($email)
	{
		$email = strtolower(trim($email));
		$result = M('Member')->field('m_email')->where("m_email='".$email."'")->find();
		if(!empty($result))
		{
			return false;
		}
		return true;
	}

	public function get_mlRank($memberId)
	{
		$mlId = $this->where('member_id = \''.$memberId.'\'')->get_field('member_level_id');
		return M('MemberLevel')->where('member_level_id = \''.$mlId.'\'')->get_field('ml_rank');
	}

	public function update_credit($memberId, $operation = 'publish', $add = true)
	{
		$data = array();
		$data['member_id'] = $memberId;
		$_mi = $this->where('member_id =\''.$memberId.'\'')->field('m_experience,m_points')->find();
		$_o_moc = M('Option')->get_option('member/'.$operation.'_credit');
		if($add)
		{
			$data['m_experience'] = $_mi['m_experience'] + $_o_moc['m_experience'];
			$data['m_points'] = $_mi['m_points'] + $_o_moc['m_points'];
		}
		else
		{
			$data['m_experience'] = (0 < $_mi['m_experience'] - $_o_moc['m_experience'])
				? ($_mi['m_experience'] - $_o_moc['m_experience'])
				: 0;
			$data['m_points'] = (0 < $_mi['m_points'] - $_o_moc['m_points'])
				? ($_mi['m_points'] - $_o_moc['m_points'])
				: 0;
		}
		$this->update($data);

		$data = array();
		$_mc = M('MemberCredit')->where('member_id =\''.$memberId.'\'')->find();
		foreach($_o_moc as $k => $v)
		{
			if('m_experience' == $k or 'm_points' == $k or 0 == $v)
			{
				continue;
			}
			if($add)
			{
				$data[$k] = $_mc[$k] + $v;
			}
			else
			{
				$data[$k] = (0 < $_mc[$k] - $v) ? ($_mc[$k] - $v) : 0;
			}
		}
		if(!empty($data))
		{
			$data['member_id'] = $memberId;
			M('MemberCredit')->update($data);
		}
	}

	public function pass_member($memberId)
	{
		$result = array('data' => '', 'error' => '');

		$_MI = $this->where('member_id = \''.$memberId.'\'')->find();
		if(empty($_MI))
		{
			$result['error'] = L('ITEM_NOT_EXSIT');
			return $result;
		}

		if(false === $this->where('member_id = \''.$memberId.'\'')->set_field('m_status', 1))
		{
			$result['error'] = L('PASS_FAILED');
			return $result;
		}

		return $result;
	}

	public function forbidden_member($memberId)
	{
		$result = array('data' => '', 'error' => '');

		$_MI = $this->where('member_id = \''.$memberId.'\'')->find();
		if(empty($_MI))
		{
			$result['error'] = L('ITEM_NOT_EXSIT');
			return $result;
		}

		if(false === $this->where('member_id = \''.$memberId.'\'')->set_field('m_status', 2))
		{
			$result['error'] = L('FORBIDDEN_FAILED');
			return $result;
		}

		return $result;
	}

}

?>