<?php

namespace Pay\Yspay;

use Dever;

class Tixian
{
    private $num = 1000;
    #生成订单号
    public function getOrderId()
    {
        $where['order_num'] = Dever::order('TX');
        $where['clear'] = true;
        $state = Dever::db('pay/yspay_tixian_log')->one($where);
        if (!$state) {
            return $where['order_num'];
        } else {
            return $this->getOrderId();
        }
    }

    public function insert($id, $name, $data)
    {
        $merchant_id = Dever::param('merchant_id',$data);
        $cash = Dever::param('cash',$data);
        $desc = Dever::param('desc',$data);
        $account = Dever::db('shop/yspay_merchant')->one($merchant_id);
        if (!$account) {
        	Dever::alert('账户不存在');
        }
        if ($cash <= 0) {
        	Dever::alert('提现金额不能小于0');
        }
        $cash = $cash * $this->num;
        if ($cash > $account['yue']) {
        	Dever::alert('账户余额不足');
        }
    }

    public function update($id, $name, $data)
    {
    	$merchant_id = Dever::param('merchant_id',$data);
        $cash = Dever::param('cash',$data);
        $desc = Dever::param('desc',$data);

        $account = Dever::db('shop/yspay_merchant')->one($merchant_id);
        if (!$account) {
        	Dever::alert('账户不存在');
        }
        if ($cash <= 0) {
        	Dever::alert('提现金额不能小于0');
        }
        $cash = $cash * $this->num;
        if ($cash > $account['yue']) {
        	Dever::alert('账户余额不足');
        }
        $order_num = $this->getOrderId();
        $account['desc'] = $desc;
        $account['mtype'] = 2;
        Dever::load('pay/yspay/account')->action($account['mid'], $order_num, -1, 2, $cash, $account);
    }
}