<?php namespace Pay\Yspay;
use Dever;

class Cash
{
	# 千分制
	private $num = 1000;
	# 入账：待确认
    public function add($merchant_id, $amount, $order_num, $source_order_num, $fenzhang = 0)
    {
    	$merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
    	if ($merchant) {
    		$data = array();
    		$data['account_id'] = $merchant['account_id'];
			$data['merchant_id'] = $merchant['id'];
    		$data['order_num'] = $order_num;
	        $data['source_order_num'] = $source_order_num;
	        $info = Dever::db('pay/yspay_cash')->find($data);
	        if ($amount && $amount > 0) {
	        	$this->getCash($amount, $merchant, $data, $fenzhang);
	        }
	        
	        $data['status'] = 1;
	        if ($info) {
	        	$data['where_id'] = $id = $info['id'];
	        	Dever::db('pay/yspay_cash')->update($data);
	        } else {
	        	$id = Dever::db('pay/yspay_cash')->insert($data);
	        }
	        
	        return $id;
    	}
    	return false;
    }

    # 修改状态
    public function up($id, $status = 2, $amount = false, $fenzhang = 0)
    {
    	if ($amount) {
    		$amount = $amount * 100;
    	}
    	if (is_numeric($id)) {
    		$where['id'] = $id;
    	} else {
    		$where['source_order_num'] = $id;
    	}
    	
    	$info = Dever::db('pay/yspay_cash')->find($where);
    	if ($info) {
    		$merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
    		if ($merchant) {
    			$update = array('status' => $status, 'where_id' => $info['id']);
	    		if ($status == 2) {
	    			if ($amount && $amount > 0 && $info['ycash'] != $amount) {
		        		$this->getCash($amount, $merchant, $update, $fenzhang);
	    			}
	    			$update['fdate'] = time();
		        }
	    		$state = Dever::db('pay/yspay_cash')->update($update);
	    		if ($state) {
	    			if ($status == 2) {
	    				$total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 2, 'merchant_id' => $info['merchant_id']));
	    				if ($total) {
	    					Dever::db('pay/yspay_merchant')->update(array('where_id' => $info['merchant_id'], 'cash' => $total['cash'], 'hf_cash' => $total['hf_cash'], 'fz_cash' => $total['fz_cash']));
	    				}
	    				$info = Dever::db('pay/yspay_cash')->find(array('id' => $info['id'], 'clear' => true));
	    				if ($info['fz_cash'] && $info['fz_cash'] > 0) {
	    					$state = $this->fenzhang_act($info);
		    				if ($state == 1) {
		    					$update = array();
			    				$update['where_id'] = $info['id'];
			    				if ($state == 1) {
			    					$update['fenzhang_status'] = 2;
			    				} else {
			    					$update['fenzhang_status'] = 3;
			    				}
			    				$update['rdate'] = time();
			    				Dever::db('pay/yspay_cash')->update($update);
		    				}
	    				}
	    			}
	    			return $state;
	    		}
    		}
    	}
    	return false;
    }

    private function getCash($amount, $merchant, &$data, $fenzhang = 0)
    {
    	$amount = $amount*10;
    	$yspay = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
		if ($yspay) {
			$cash_per = 0;
			if ($merchant['type'] == 1 && $yspay['type'] == 2) {
				$cash_per = $merchant['cash_per'];
	    		if (!$cash_per) {
	    			if ($yspay && $yspay['cash_per']) {
	    				$cash_per = $yspay['cash_per'];
	    			}
	    		}
	    		if (!$cash_per || $cash_per <= 0) {
	    			$cash_per = 0;
	    		} else {
	    			$cash_per = $cash_per/$this->num;
	    		}
			}
			
    		$per = $yspay['per']/$this->num;
    		$cash_jy_per = $yspay['cash_jy_per']/$this->num;

        	$data['ycash'] = $amount/$this->num;
        	$data['yl_cash'] = round($data['ycash'] * $per, 3);
        	$data['pt_cash'] = round($data['ycash'] * $cash_jy_per, 3);
	        $data['cash'] = round($data['ycash'] - $data['yl_cash'] - $data['pt_cash'], 3);
	        if ($yspay['cash_type'] == 1) {
	        	$fz_cash = $data['ycash'];
	        } else {
	        	$fz_cash = $data['cash'];
	        }
	        //$data['fz_cash'] = round($fz_cash*$cash_per, 3);
	        $data['fz_cash'] = 0;
	        if ($fenzhang && $fenzhang > 0) {
	        	$data['fz_cash'] += $fenzhang;
	        }
	        $data['hf_cash'] = round($data['cash'] - $data['fz_cash'], 3);

	        $data['ycash'] *= $this->num;
	        $data['yl_cash'] *= $this->num;
			$data['pt_cash'] *= $this->num;
			$data['cash'] *= $this->num;
			$data['fz_cash'] *= $this->num;
			$data['hf_cash'] *= $this->num;
	    }
    }

    private function huafu_act($info)
    {
    	$merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
    	if ($merchant) {
    		$config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
    		if ($config) {
    			return Dever::load('pay/yspay/multi')->huafu($config, $merchant, $info);
    		}
    	}

    	return false;
    }

    private function fenzhang_act($info)
    {
    	$merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
    	if ($merchant) {
    		$config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
    		if ($config) {
    			return Dever::load('pay/yspay/multi')->fenzhang($config, $merchant, $info);
    		}
    	}

    	return false;
    }

    private function tixian_act($info)
    {
    	$merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
    	if ($merchant) {
    		$config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
    		if ($config) {
    			return Dever::load('pay/yspay/account')->act($config, $merchant, $info);
    		}
    	}

    	return false;
    }

    # 将测试数据改成待入账
    public function test_edit_api()
    {
    	$data = Dever::db('pay/yspay_cash')->select(array('order_num' => 'test123', 'status' => 1));
    	if ($data) {
    		foreach ($data as $k => $v) {
    			$this->up($v['id'], 2);
    		}
    	} 
    	return 'ok';
    }

    # 资金划付
    public function huafu_commit_api()
    {
    	$where = array('status' => 2);
    	$account_id = Dever::input('account_id');
    	if ($account_id) {
    		$where['account_id'] = $account_id;
    	}
    	$merchant_id = Dever::input('merchant_id');
    	if ($merchant_id) {
    		$where['merchant_id'] = $merchant_id;
    	}
    	$data = Dever::db('pay/yspay_cash')->select($where);
    	if ($data) {
    		foreach ($data as $k => $v) {
    			$this->up($v['id'], 3);
    		}
    	}
    	return 'ok';
    }

    # 随机获取平台商户
    public function getMid($account_id)
    {
    	$merchant = Dever::db('pay/yspay_merchant')->select(array('type' => 2, 'account_id' => $account_id, 'status' => 1));

    	if ($merchant) {
    		$key = array_rand($merchant);
    		if (isset($merchant[$key])) {
    			return $merchant[$key];
    		}
    	}
    	return false;
    }

    public function tixian($id, $name, $data)
    {
    	$merchant_id = Dever::param('merchant_id', $data);
    	$cash = Dever::param('cash', $data);
    	$merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
    	if ($merchant) {
    		$cash = $cash*$this->num;
    		$yue = $merchant['hf_cash'] - $merchant['hf_tx_cash'] - $this->num*100;
    		if ($yue < $cash) {
    			Dever::alert('余额不足');
    		}

    		$info['merchant_id'] = $merchant_id;
    		$info['hf_cash'] = $cash;
    		$info['order_num'] = Dever::order('TX');
    		$status = $this->huafu_act($info);
    		if ($status == 1) {
    			Dever::db('pay/yspay_merchant')->updateTxCash(array('where_id' => $merchant_id, 'hf_tx_cash' => $cash));
    		}
    	}
    	return false;
    }
}