<?php

namespace Shop\Lib;

use Dever;

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

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

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

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