<?php

/**
 *--------------------------------------
 * 会员订单管理
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-11-16
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class MemberOrderCtrlr extends ManageCtrlr
{
	public function list_order()
	{
		$where = array();

		/* 筛选会员 */
		$memberId = ARequest::get('member_id') ? ARequest::get('member_id') : 0;
		if($memberId > 0)
		{
			$where['member_id'] = array('EQ', $memberId);
		}

		/* 筛选类型 */
		$oProductType = ARequest::get('mo_product_type') ? ARequest::get('mo_product_type') : '';
		if(!empty($oProductType))
		{
			$where['mo_product_type'] = array('EQ', $oProductType);
		}

		/* 筛选状态 */
		$oStatus = ARequest::get('mo_status') ? ARequest::get('mo_status') : '';
		if('n' == $oStatus)
		{
			$where['mo_status'] = array('EQ', 0);
		}
		elseif('p' == $oStatus)
		{
			$where['mo_status'] = array('EQ', 1);
		}

		/* 列表排序 */
		$orderBy = ARequest::get('order_by') ? ARequest::get('order_by') : 'member_order_id';
		$orderTurn = ARequest::get('order_turn') ? ARequest::get('order_turn') : 'desc';
		$order = "`{$orderBy}` {$orderTurn}";

		/* 获取分页信息 */
		$pageSize = (ARequest::get('page_size') > 0 ? ARequest::get('page_size') : 20);
		$_GET[C('VAR.PAGE')] = ARequest::get(C('VAR.PAGE')) ? ARequest::get(C('VAR.PAGE')) : 1;
		$rowsNum = M('MemberOrder')->where($where)->count();
		$p = new APage($rowsNum, $pageSize, Url::U('member_order/list_order?member_id='.$memberId
			.'&mo_product_type='.$oProductType
			.'&mo_status='.$oStatus
			.'&order_by='.$orderBy
			.'&order_turn='.$orderTurn
			.'&'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		/* 收藏列表 */
		$_MOL = M('MemberOrder')->get_orderList($where, $order, $limit);
		$this->assign('_MOL', $_MOL);

  		$this->display();
	}

	public function delete_order_do()
	{
  		$memberOrderId = ARequest::get('member_order_id');
		$_L_ID = is_array($memberOrderId) ? implode(',', $memberOrderId) : $memberOrderId; /* 日志记录ID */

		if(is_array($memberOrderId))
		{
			foreach($memberOrderId as $memberOrderId)
			{
				$result = M('MemberOrder')->delete_order($memberOrderId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MEMBER_ORDER').': MEMBER_ORDER_ID['.$memberOrderId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('member_order/list_order'));
				}
			}
		}
		else
		{
			$result = M('MemberOrder')->delete_order($memberOrderId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MEMBER_ORDER').': MEMBER_ORDER_ID['.$memberOrderId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('member_order/list_order'));
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MEMBER_ORDER').': MEMBER_ORDER_ID['.$_L_ID.']');
		$this->success(L('DELETE_SUCCESS'), Url::U('member_order/list_order'));
	}

	public function pay_order_do()
	{
		$memberOrderId = ARequest::get('member_order_id');
		$_L_ID = is_array($memberOrderId) ? implode(',', $memberOrderId) : $memberOrderId; /* 日志记录ID */

		if(is_array($memberOrderId))
		{
			foreach($memberOrderId as $memberOrderId)
			{
				$result = M('MemberOrder')->pay_order($memberOrderId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('PAY').L('MEMBER_ORDER').': MEMBER_ORDER_ID['.$memberOrderId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('member_order/list_order'));
				}
			}
		}
		else
		{
			$result = M('MemberOrder')->pay_order($memberOrderId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('PAY').L('MEMBER_ORDER').': MEMBER_ORDER_ID['.$memberOrderId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('member_order/list_order'));
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('PAY').L('MEMBER_ORDER').': MEMBER_ORDER_ID['.$_L_ID.']');
		$this->success(L('PAY_SUCCESS'), Url::U('member_order/list_order'));
	}
}


?>