<?php

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

class MemberLevelModl extends Modl
{
	public function get_levelList()
	{
		$_MLL = F('~mll');
		if(empty($_MLL))
		{
			$_MLL = $this->order('`ml_type` DESC, `ml_rank` ASC')->select();
			F('~mll', $_MLL);
		}

		return $_MLL;
	}

	public function get_levelId($experience)
	{
		$memberLevelId = 0;
		$mlRank = 0;
		foreach($this->get_levelList() as $ml)
		{
			if(1 == $ml['ml_type'] and $experience >= $ml['ml_min_experience'] and $mlRank <= $ml['ml_rank'])
			{
				$memberLevelId = $ml['member_level_id'];
				$mlRank = $ml['ml_rank'];
			}
		}
		return $memberLevelId;
	}

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

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

		F('~mll', null);

		return $result;
	}

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

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

		F('~mll', null);

		return $result;
	}

	public function delete_level($memberLevelId)
	{
		$result = array('data' => '', 'error' => '');

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

		F('~mll', null);

		return $result;
	}
}

?>