<?php namespace Pay\Yspay;

use Dever;

class Cron
{
    # 定时更新银联账户余额信息
    public function account_api()
    {
        # 获取有银联商户号的门店
        $where = array();
        $mid = Dever::input('mid');
        if ($mid) {
            $where['mid'] = $mid;
        }
        $merchant = Dever::db('pay/yspay_merchant')->select($where);
        if (!$merchant) {
            return 'error';
        }
        foreach ($merchant as $k => $v) {
            if ($v['mid']) {
            	$config = Dever::db('pay/yspay')->one(array('account_id' => $v['account_id']));
            	if ($config['type'] == 1) {
            		$data = Dever::load('pay/yspay/account')->query($v['mid']);
            		$update['where_id'] = $v['id'];
	                $update['yue'] = $data;
	                Dever::db('pay/yspay_merchant')->update($update);
            	}
            }
        }
        return 'ok';
    }

    # 查询余额
    public function yue_api()
    {
        # 获取有银联商户号的门店
        $where = array();
        $where['mid'] = Dever::input('mid');
        $merchant = Dever::db('pay/yspay_merchant')->find($where);
        $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
        $data = Dever::load('pay/yspay/multi')->yue($config, $merchant);
        return $data;
    }

    # 定时获取银联提现信息
    public function tixian_api()
    {
        $num = Dever::input('num', 1);
        
        $start = Dever::input('start', date('Ymd', strtotime('-'.$num.' day')));
        $end = Dever::input('end', date('Ymd'));

        # 获取有银联商户号的门店
        $where = array();
        $mid = Dever::input('mid');
        if ($mid) {
            $where['mid'] = $mid;
        }
        $test = Dever::input('test');
        $merchant = Dever::db('pay/yspay_merchant')->select($where);
        if (!$merchant) {
            return 'error';
        }
        foreach ($merchant as $k => $v) {
            if ($v['mid']) {
                $data = Dever::load('pay/yspay/account')->record($v['mid'], $start, $end);
                if ($data) {
                    if ($test == 1) {
                        print_r($data);die;
                    }
                    foreach ($data as $k1 => $v1) {
                        $update = array();
                        $update['merchant_id'] = $v['id'];
                        $update['type'] = $v1['trxTypeDtl'];
                        $update['tdate'] = Dever::maketime($v1['trxTime']);
                        $update['mid'] = $v1['mchntNo'];
                        $update['cardNo'] = $v1['cardNo'];
                        $update['order_num'] = $v1['sysOrderId'];
                        $update['status'] = $v1['status'];
                        $update['clear'] = true;
                        $info = Dever::db('pay/yspay_tixian')->one($update);
                        $update['cash'] = $v1['transAmt'];
                        if (!$info) {
                            Dever::db('pay/yspay_tixian')->insert($update);
                        } else {
                            $update['where_id'] = $info['id'];
                            Dever::db('pay/yspay_tixian')->update($update);
                        }
                        $total = Dever::db('pay/yspay_tixian')->getTotal(array('shop_id' => $v['id'], 'status' => 4));
                        if ($total) {
                            $account_data['tx_cash'] = $total['cash'];
                            $account_data['where_id'] = $v['id'];
                            Dever::db('pay/yspay_merchant')->update($account_data);
                        }
                    }
                }
            }
        }
        return 'ok';
    }

    # 退款
    public function tui_api()
    {
        $order_num = Dever::input('order_num');
        $cash = Dever::input('cash');
        $param = array
        (
            'project_id' => 2,
            'channel_id' => 1,
            'system_source' => 5,
            'account_id' => 7,
            'order_id' => $order_num,
            'refund_cash' => $cash,
            //'refund_order_id' => '',
        );
        $result = Dever::load('pay/api.refund', $param);

        print_r($result);die;
    }
}