<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\api\controller\user;

use app\api\controller\Base;
use think\Db;
use app\common\model\bbfxshop\Dougong;
use app\common\model\bbfxshop\User;

class Txaccount extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function submit(){
        $params = $this->param(['name/s','cert_type/s','cert_no/s','cert_validity_type/d','cert_begin_date/s','cert_end_date/s','mobile_no/s','card_type/d','bankname/s','bankopen/s','card_no/s','prov_id/s','area_id/s']);
        
        if(empty($params['name'])){
            __error("请输入姓名");
        }
        if(empty($params['cert_type'])){
            // __error("请选择个人证件类型");
        }
        if(empty($params['cert_no'])){
            __error("请输入个人证件号码");
        }
        if(empty($params['cert_validity_type'])){
            // __error("请选择个人证件有效期类型");
        }
        if(empty($params['cert_begin_date'])){
            __error("请选择个人证件有效期开始日期");
        }
        if($params['cert_validity_type'] != 1 && empty($params['cert_end_date'])){
            __error("请选择个人证件有效期结束日期");
        }
        if(empty($params['mobile_no'])){
            __error("请输入手机号");
        }
        if(empty($params['card_type'])){
            // __error("请选择卡类型");
        }
        if(empty($params['card_no'])){
            __error("请输入银行卡号");
        }
        if(empty($params['prov_id'])){
            __error("请选择银行所在省");
        }
        if(empty($params['area_id'])){
            __error("请选择银行所在市");
        }
        if(empty($params['bankname'])){
            __error("请输入开户行");
        }
        if(empty($params['bankopen'])){
            __error("请输入开户行支行");
        }
        
        $dougong = new Dougong();
        $config = $dougong->getSet();
        if($config['status'] != 1){
            __error("未开启汇付支付");
        }

        $txaccount = Db::name("bbfx_user_txaccount")->where(['uniacid'=>UNIACID,'dg_huifu_id'=>$config['dg_huifu_id'],'user_id'=>$this->user['id']])->find();
        
        $data = [
            'uniacid'=>UNIACID,
            'user_id'=>$this->user['id'],
            'dg_huifu_id'=>$config['dg_huifu_id'],
            'name'=>$params['name'],
            'cert_type'=>$params['cert_type'],
            'cert_no'=>$params['cert_no'],
            'cert_validity_type'=>$params['cert_validity_type'],
            'cert_begin_date'=>$params['cert_begin_date'],
            'mobile_no'=>$params['mobile_no'],
            'card_type'=>$params['card_type'],
            'card_no'=>$params['card_no'],
            'bankname'=>$params['bankname'],
            'bankopen'=>$params['bankopen'],
            'prov_id'=>$params['prov_id'],
            'area_id'=>$params['area_id'],
        ];

        if($params['cert_validity_type'] != 1){
            $data['cert_end_date'] = $params['cert_end_date'];
        }

        try {
            if(!empty($txaccount)){
                if($txaccount['kh_status'] == 1){
                    unset($data['name']);
                    unset($data['cert_type']);
                    unset($data['cert_no']);
                }
                $id = $txaccount['id'];
                $update_res = Db::name("bbfx_user_txaccount")->where(['id'=>$id])->update($data);
                $update_res1 = $update_res;
                if(!$update_res && $txaccount['rz_status'] == 0){
                    $update_res = true;
                }
            }
            else{
                $data['createtime'] = time();
                $id = Db::name("bbfx_user_txaccount")->insertGetId($data);
                $txaccount = $data;
                $txaccount['id'] = $id;      
                $update_res = true;                                                                                                                                                                                                                                                                                                                              
            }

            $txaccount = Db::name("bbfx_user_txaccount")->where(['id'=>$id])->find();

            $udata = [
                'bank_realname'=>$data['name'],
                'bankname'=>$data['bankname'],
                'bankcard'=>$data['card_no'],
                'bankopen'=>$data['bankopen'],
            ];
            Db::name("bbfx_user")->where(['id'=>$this->user['id']])->update($udata);

        } catch (\Exception $e) {
            __error($e->getMessage());
        }


        $kh_status = $txaccount['kh_status'];
        
        if($kh_status == 0 || ($kh_status == 1 || $update_res1)){
            $result = $dougong->kaihu($txaccount);
            if($result['code'] == 1){
                if($kh_status == 0){
                    $udata = [
                        'kh_status'=>1,
                        'kh_time'=>time(),
                        'huifu_id'=>$result['data']['huifu_id'],
                        'login_name'=>$result['data']['login_name'],
                        'login_password'=>$result['data']['login_password'],
                    ];
                    $kh_status = 1;
                    $txaccount['kh_status'] = 1;
                    $txaccount['huifu_id'] = $udata['huifu_id'];
                    Db::name("bbfx_user_txaccount")->where(['id'=>$id])->update($udata);
                }
            }
            else{
                __result($result);
            }
        }

        $update_res = true;
        $rz_status = $txaccount['rz_status'];
        if($kh_status == 1 && ($update_res || $txaccount['qx_status'] != 1)){
            $result = $dougong->ruzhu($txaccount);
            if($result['code'] == 1){

                $resp_business = json_decode($result['data']['resp_business'],true);
                
                $udata = [];
                foreach($resp_business as $key => $val){
                    if($val['type'] == 1 && $val['code'] == 'S'){
                        $rz_status = 1;
                        $udata['rz_status'] = 1;
                        $udata['rz_time'] = time();
                        $udata['token_no'] = $result['data']['token_no'];
                    }
                    else if($val['type'] == 2 && $val['code'] == 'S'){
                        $udata['qx_status'] = 1;
                    }
                    else if($val['type'] == 3 && $val['code'] == 'S'){
                        $udata['js_status'] = 1;
                    }
                }

                if(!empty($result['data']['apply_no'])){
                    $udata['apply_no'] = $result['data']['apply_no'];
                }
                
                if(!empty($udata)){
                    Db::name("bbfx_user_txaccount")->where(['id'=>$id])->update($udata);
                }
            }
            else{
                __result($result);
            }
        }

        if($rz_status == 1){
            __success("提交成功！");
        }

        __success("提交成功！");
    }

    public function info(){
        $dougong = new Dougong();
        $config = $dougong->getSet();
        $txaccount = Db::name("bbfx_user_txaccount")->where(['uniacid'=>UNIACID,'dg_huifu_id'=>$config['dg_huifu_id'],'user_id'=>$this->user['id']])->find();
        if(empty($txaccount)){
            __error("未提交数据");
        }

        __success("操作成功",$txaccount);
    }

    public function account(){
        $dougong = new Dougong();
        $config = $dougong->getSet();
        $txaccount = Db::name("bbfx_user_txaccount")->where(['uniacid'=>UNIACID,'dg_huifu_id'=>$config['dg_huifu_id'],'user_id'=>$this->user['id']])->find();
        $money = 0;
        if(!empty($txaccount) && $txaccount['rz_status'] == 1){
            $result = $dougong->queryAcct($txaccount['huifu_id']);
            if($result['code'] == 1){
                $acctInfo_list = toJsonde($result['data']['acctInfo_list']);
                echo "<pre>";
                print_r($acctInfo_list);die;
                $money = round($acctInfo_list[0]['avl_bal'],2);

                $money_ok = Db::name("bbfx_withdrawal_log")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id'],'withdrawal_type'=>8,'status'=>0])->sum("money");
                $money = round($money - $money_ok,2);
            }
        }

        $result = [
            'money'=>$money,
            'kh_status'=>intval($txaccount['kh_status']),
            'rz_status'=>intval($txaccount['rz_status']),
            'qx_status'=>intval($txaccount['qx_status'])
        ];
        
        __success("操作成功",$result);
    }

    public function getTixianList(){
        
        $params = $this->param();
        
        $list = Db::name("bbfx_withdrawal_log")->where(["uniacid"=>UNIACID,"user_id"=>$this->user['id'],'withdrawal_type'=>8])->order("id desc")->select();
        foreach ($list as &$val){
            $val['info'] = iunserializer($val['info']);
            $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
        }
        unset($val);
        
        __success("操作成功",array("list"=>$list));
    }

    public function tixian(){
        global $_W;
        $dougong = new Dougong();
        $config = $dougong->getSet();

        if($config['status'] != 1){
            __error("未开启汇付支付");
        }

        $txaccount = Db::name("bbfx_user_txaccount")->where(['uniacid'=>UNIACID,'dg_huifu_id'=>$config['dg_huifu_id'],'user_id'=>$this->user['id']])->find();
        if(empty($txaccount) || (!empty($txaccount) && $txaccount['kh_status'] != 1)){
            __error("用户未开户，请前往提现账户完整资料");
        }

        if($txaccount['rz_status'] != 1){
            __error("您还未完成业务入驻，请前往提现账户重新提交资料");
        }

        if($txaccount['qx_status'] != 1){
            __error("您还未开通提现，请前往提现账户重新提交资料开通提现");
        }
        
        $params = $this->param();
        $user = $this->user;
        User::checkStatus($user);

        $base = $_W['base']; 
        
        $money = round($params['money'],2);
        
        if($money <= 0 || empty($money)){
            __error("请输入大于0的金额");
        }

        $max_money = 0;
        $result = $dougong->queryAcct($txaccount['huifu_id']);
        if($result['code'] == 1){
            $acctInfo_list = toJsonde($result['data']['acctInfo_list']);
            $max_money = round($acctInfo_list[0]['avl_bal'],2);

            $money_ok = Db::name("bbfx_withdrawal_log")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id'],'withdrawal_type'=>8,'status'=>0])->sum("money");
            $max_money = round($max_money - $money_ok,2);
        }

        if($money > $max_money){
            __error('余额不足，请重新输入提现金额');
        }
        
        $service_charge = round($money * 0.0002,2);
        $realmoney = $money - $service_charge;
        
        $withdrawal_info = [];
        $data = [
            "uniacid"=>UNIACID,
            "user_id"=>$user['id'],
            "withdrawal_no"=>date('YmdHis').random(8,true),
            "money"=>$money,
            "realmoney"=>$realmoney,
            "service_charge"=>$service_charge,
            "type"=>4,
            "withdrawal_type"=>8,
            "realname"=>$txaccount['name'],
            "bankname"=>$txaccount['bankname'],
            "bankopen"=>$txaccount['bankopen'],
            "bankcard"=>$txaccount['card_no'],
            "info"=>iserializer($txaccount),
            "createtime"=>time()
        ];
        
        if(empty($user['bankcard'])){
            $udata = [
                'bank_realname'=>$data['realname'],
                'bankname'=>$data['bankname'],
                'bankcard'=>$data['bankcard'],
                'bankopen'=>$data['bankopen'],
            ];
            Db::name("bbfx_user")->where(['id'=>$user['id']])->update($udata);
        }
        
        try {
            Db::startTrans();
            
            $id = Db::name("bbfx_withdrawal_log")->insertGetId($data);
            if(empty($id)){
                throw new \Exception("提现失败");
            }
            
            $data['id'] = $id;
            
            Db::commit();
        } 
        catch (\Exception $e) {
            Db::rollback();
        	$msg = $e->getMessage();
        	__error($msg);
        }
        
        
        __success("提交成功，请等待审核");
    }
}