<?php
/**
 * Created by PhpStorm.
 * User: scstf
 * Date: 2018/9/4
 * Time: 17:21
 */

namespace App\Commons\Model;

use Biz\Account\AccountManager;
use \System\Model;
use Illuminate\Support\Facades\DB;
use DB as DataBase;

class SystemBank extends Model
{
    public $timestamps = false;
    protected $table = 'system_bank';

    public static function getNative($type = 2)
    {


        $groups = $_SESSION['uinfo']['group_code'];
        if(empty($groups)){
            $account    =   new AccountManager();
            $groups = $account->getCurrentUser()->group_code;
        }
        $arr = explode (',', $groups);
        if (!$arr) return -1999;
        $where = '';
        foreach ($arr as $k => $v) {
            if ($v) {
                $where .= "groups like '%," . $v . ",%' or ";
            }
        }
        $where = trim ($where, 'or ');
        if (!$where) {
            return $ret = null;
            //return $ret = self::where (['type' => $type, 'status' => 1,])->get (['id', 'bank_num', 'bank', 'remark', 'infoname', 'type']);
        } else {
            $where = "({$where})";
            return $ret = self::where (['type' => $type, 'status' => 1,])->whereRaw ($where)->get (['id', 'bank_num', 'bank', 'remark', 'infoname', 'type']);
        }

    }

    public static function getOnlineNatives()
    {
        $userInfo=(new AccountManager())->getCurrentUser();
        $groups = $userInfo?$userInfo['group_code']:'';
        $arr = explode (',', $groups);
        if (!$arr) return -1999;
        $where = '';
        foreach ($arr as $k => $v) {
            if ($v) {
                $where .= "groups like '%," . $v . ",%' or ";
            }
        }
        $where = trim ($where, 'or ');
        $where = "({$where})";
        $sql = 'select count(*) as num,type from system_bank where type<>1 and status=1 and ' . $where . '  group by type order by type asc';
        $data = M ('')->join ($sql);
        if (empty($data)) {
            return null;
        }
        $json = '[{"id": 2, "name": "微信", "imgSrc": "/Public/img/banks/wxpay.png"},
                {"id": 3, "name": "支付宝", "imgSrc": "/Public/img/banks/alipay.png"},
                {"id": 4, "name": "QQ", "imgSrc": "/Public/img/banks/qqpay.png"},
                {"id": 5, "name": "银联", "imgSrc": "/Public/img/banks/unionpay.png"},
                {"id": 6, "name": "京东", "imgSrc": "/Public/img/banks/jdpay.png"},
                {"id": 7, "name": "多合一", "imgSrc": "/Public/img/banks/aiopay.png"}]';
        $arr = json_decode ($json, 1);
        $tmp = [];
        foreach ($data as $k => $v) {
            $tmp[$v['type']] = $v['num'];//array[1=>7,]
        }
        foreach ($arr as $k => $v) {
            if (!isset($tmp[$v['id']])) {
                unset($arr[$k]);
            }
        }
        //dd(array_values($arr));
        return array_values ($arr);
    }
}