<?php
namespace Biz\Pay;
/**
 * 资金变动
 * @author UpdateMoney
 *  */
class UpdateMoney {
	/**
	 * 资金变更
	 * @param  string $orderId     订单ID
	 * @param  string $accountId   用户ID
	 * @param  float $money   变动金额:正为加，负为减
	 * @return [type]
	 */
	public function Index($orderId = '', $accountId = '', $money = '') {
		$moneyInfo = array();
		$userInfo = array();
		if ($orderId == '' && $accountId == '') {
			return -1;
		}
		if ($orderId == '') {
			//用户余额变更
			$userInfo = M('account_detailed')->select('cash,available_cash')->
				where('account_identity', $accountId)->find();
			$newCash = array(
				'cash' => $userInfo['cash'] + $money,
				'available_cash' => $userInfo['available_cash'] + $money,
			);
			$res = M('account_detailed')->where('account_identity', $accountId)->update($newCash);
			if ($res > 0) {
				return 1;
			} else {
				return -1;
			}
		} else {
			//用户资金明细、余额变更
			$moneyInfo = M('fund_detailed')->select('account_identity,money,status')->where('order_id', $orderId)->find();
			if ($moneyInfo['status'] == 1) {
				return 1;
			}
			$userInfo = M('account_detailed')->select('cash,available_cash')->
				where('account_identity', $moneyInfo['account_identity'])->find();
			$newCash = array(
				'cash' => $userInfo['cash'] + $moneyInfo['money'],
				'available_cash' => $userInfo['available_cash'] + $moneyInfo['money'],
			);

			$sw = new DBExtension();
			$sw->B();
			$sw->ST('fund_detailed')->W('order_id', $orderId)->U(array('status' => 1, 'cur_cash' => $newCash['available_cash']));
			$sw->ST('account_detailed')->W('account_identity', $moneyInfo['account_identity'])->U($newCash);
			$res = $sw->C();
			if ($res) {
				return 1;
			} else {
				return -1;
			}
		}
	}
}