/**
  * 借给他
  */
 public function toTradeInvest()
 {
     $func = new Lib\Fun();
     $inuserid = $outuserid = $myMoney = 0;
     $sucurl = U('Loan/index');
     $vid = $func->checkint($func->getRequest('vid'), -1);
     if ($vid < 0) {
         $func->showMsg("参数错误");
     } else {
         $userInfo = $this->getUserinfo($this->userid);
         //$myMoney = $userInfo['f_usermoney'];
         $Trade = M("loan");
         $condition = array();
         $condition['f_id'] = $vid;
         $vInfo = $Trade->where($condition)->field('f_userid userid,f_money money,f_yield yield,f_horizon horizon,f_danbaomoney danbaomoney,f_state state')->select();
         $inuserid = intval($vInfo[0]['userid']);
         $outuserid = $this->userid;
         if ($inuserid == $outuserid) {
             $func->showMsg("不能与自己交易");
         }
         if (!empty($vInfo)) {
             $vState = $vInfo[0]['state'];
             $tradeMoney = $vInfo[0]['money'];
             //初始态才可交易
             if ($vState == 0) {
                 //插入交易流水 (暂不用事务处理)
                 $TradeLog = M("trade_log");
                 $data = array();
                 $data['f_type'] = 1;
                 $data['f_inuserid'] = $inuserid;
                 $data['f_outuserid'] = $outuserid;
                 $data['f_money'] = $tradeMoney;
                 $data['f_yield'] = $vInfo[0]['yield'];
                 $data['f_horizon'] = $vInfo[0]['horizon'];
                 $data['f_danbaomoney'] = $vInfo[0]['danbaomoney'];
                 $data['f_vid'] = $vid;
                 $data['f_addtime'] = time();
                 $res = $TradeLog->add($data);
                 if (!empty($res)) {
                     $tradeId = $res;
                     //更新贷款表订单状态
                     $data = array();
                     $data['f_state'] = 100;
                     //订单交易中
                     $data['f_tradeid'] = $tradeId;
                     $condition['f_id'] = $vid;
                     $condition['f_state'] = 0;
                     $upState = $Trade->where($condition)->save($data);
                     if ($upState !== false) {
                         //创建充值订单
                         $orderid = date('YmdHis', time()) . rand(10000, 99999);
                         $PayLog = D("paylog");
                         $data = array();
                         $data['f_userid'] = $this->userid;
                         $data['f_paycode'] = $orderid;
                         $data['f_tradeid'] = $vid;
                         $data['f_type'] = 1;
                         $data['f_tradetype'] = 2;
                         //1 投资发起订单 2 借给他订单 3 借款发起订单 4 找他借发起订单
                         $data['f_money'] = $tradeMoney;
                         $data['f_addtime'] = time();
                         $data['f_state'] = 0;
                         $res = $PayLog->add($data);
                         if (!empty($res)) {
                             //调用统统支付收银台
                             $sumPay = new Lib\SumPay();
                             $sumPay->recharge($this->userid, $orderid, $tradeMoney);
                         } else {
                             $func->showMsg("创建充值订单失败");
                         }
                         /*$User = M("userinfo");
                                                     //给贷款用户加款
                                                     $userInfo_In = $this->getUserinfo($inuserid);
                                                     $data = array();
                                                     $data['f_usermoney'] = $userInfo_In['f_usermoney'] +  $tradeMoney;
                                                     $condition['f_uid'] = $inuserid;
                                                     $addMoney = $User->where($condition)->save($data);
                         
                                                     if($addMoney !== false){
                                                         //插入账户流水
                                                         $this->insertUserAccountLog(4,$inuserid,$tradeMoney);
                         
                                                         //给投资用户扣款
                                                         $userInfo_Out = $this->getUserinfo($outuserid);
                                                         $data = array();
                                                         $data['f_usermoney'] = $userInfo_Out['f_usermoney'] -  $tradeMoney;
                                                         if($data['f_frozemoney'] < 0){
                                                             showMsg("余额不足,扣款失败");
                                                         }
                                                         $condition['f_uid'] = $outuserid;
                                                         $minusMoney = $User->where($condition)->save($data);
                                                         if($minusMoney !== false){
                                                             //插入账户流水
                                                             $this->insertUserAccountLog(5,$outuserid,$tradeMoney);
                         
                                                             $func->showMsg("交易成功",1,true,false,$sucurl);
                                                         }else{
                                                             $func->showMsg("给投资用户扣款失败");
                                                         }
                                                     }else{
                                                         $func->showMsg("给贷款用户加款失败");
                                                     }*/
                     } else {
                         $func->showMsg("更新订单状态失败");
                     }
                 } else {
                     $func->showMsg("插入交易流水失败");
                 }
             } else {
                 $func->showMsg("该订单不允许交易");
             }
         } else {
             $func->showMsg("不存在的订单");
         }
     }
 }
 /**
  * 充值完成后台回调函数
  */
 public function sumpay_bg()
 {
     $func = new Lib\Fun();
     $args['resp_code'] = $func->getRequest('resp_code');
     $args['resp_msg'] = $func->getRequest('resp_msg');
     $args['sign_type'] = $func->getRequest('sign_type');
     $args['sign'] = $func->getRequest('sign');
     $args['mer_id'] = $func->getRequest('mer_id');
     $args['order_no'] = $func->getRequest('order_no');
     $args['order_time'] = $func->getRequest('order_time');
     $args['serial_no'] = $func->getRequest('serial_no');
     $args['status'] = $func->getRequest('status');
     // 0 失败  1 成功
     $args['succ_time'] = $func->getRequest('succ_time');
     $args['succ_amt'] = $func->getRequest('succ_amt');
     $args['remark'] = $func->getRequest('remark');
     //通知成功
     if ($args['resp_code'] != '000000') {
         $resp_msg = 'resp_code: ' . $args['resp_code'] . ',resp_msg: ' . $args['resp_msg'];
         \Think\Log::record($resp_msg);
         exit('fail');
     }
     $sumPay = new Lib\SumPay();
     //设置sign -----test
     $args['sign'] = $sumPay->signArrayData($args);
     $str = $sumPay->sortArrayData($args);
     if (!$sumPay->verify($str, $args['sign'])) {
         \Think\Log::record('验签失败!');
         exit('验签失败!');
     }
     //更新充值订单表,及关联的投资|借款交易表
     $PayLog = M("paylog");
     $data = $condition = array();
     if ($args['status'] == 1) {
         $data['f_state'] = 100;
         $data['f_suctime'] = $args['succ_time'];
     } elseif ($args['status'] == 0) {
         $data['f_state'] = -100;
     }
     $condition['f_paycode'] = $args['order_no'];
     $condition['f_money'] = $args['succ_amt'];
     $condition['f_state'] = 0;
     $ret = $PayLog->where($condition)->save($data);
     if (empty($ret)) {
         $resp_msg = '更新充值订单表失败,ret: ' . $ret;
         \Think\Log::record($resp_msg);
         exit('fail');
     }
     $condition = array();
     $condition['f_paycode'] = $args['order_no'];
     $paylog_info = $PayLog->where($condition)->field('f_userid userid,f_paycode paycode,f_tradeid tradeid,f_money money,f_tradetype tradetype,f_state state')->select();
     //给用户加钱入账
     $data = $condition = array();
     $User = M("userinfo");
     $userid = $paylog_info[0]['userid'];
     $condition['f_uid'] = $userid;
     $vo = $User->where($condition)->field('f_usermoney money')->select();
     if (!empty($vo)) {
         $data = array();
         $data['f_usermoney'] = $vo[0]['money'] + $args['succ_amt'];
         $doRecharge = $User->where($condition)->save($data);
         if ($doRecharge !== false) {
             //插入账户流水
             TradeController::insertUserAccountLog(0, $userid, $args['succ_amt']);
         } else {
             $resp_msg = '更新用户账户信息失败,userid=' . $userid . ',usermoney=' . $data['f_usermoney'];
             \Think\Log::record($resp_msg);
             exit('fail');
         }
     } else {
         $resp_msg = '更新用户账户信息失败--用户不存在,userid=' . $userid;
         \Think\Log::record($resp_msg);
         exit('fail');
     }
     if (!empty($paylog_info)) {
         $order_status = $paylog_info[0]['state'];
         $tradeid = $paylog_info[0]['tradeid'];
         $tradetype = $paylog_info[0]['tradetype'];
         //投资发起、借给他
         //成功or失败
         if ($order_status != 0) {
             if ($tradetype == 1) {
                 //更新投资交易表
                 $Invest = M("invest");
                 $data = array();
                 if ($order_status == 100) {
                     $data['f_state'] = 200;
                 } elseif ($order_status == -100) {
                     $data['f_state'] = -100;
                 }
                 $condition = array();
                 $condition['f_id'] = $tradeid;
                 $condition['f_state'] = 100;
                 $ret = $Invest->where($condition)->save($data);
                 if (!empty($ret)) {
                     $resp_msg = '更新投资交易表成功,id= ' . $tradeid;
                     \Think\Log::record($resp_msg);
                 } else {
                     $resp_msg = '更新投资交易表失败,id= ' . $tradeid;
                     \Think\Log::record($resp_msg);
                     exit('fail');
                 }
             } elseif ($tradetype == 2) {
                 //更新借款交易表
                 $Loan = M("loan");
                 $data = array();
                 if ($order_status == 100) {
                     $data['f_state'] = 300;
                 } elseif ($order_status == -100) {
                     $data['f_state'] = -100;
                 }
                 $condition = array();
                 $condition['f_id'] = $tradeid;
                 $condition['f_state'] = 100;
                 $ret = $Loan->where($condition)->save($data);
                 if (!empty($ret)) {
                     $resp_msg = '更新借款交易表成功,id= ' . $tradeid;
                     \Think\Log::record($resp_msg);
                 } else {
                     $resp_msg = '更新借款交易表失败,id= ' . $tradeid;
                     \Think\Log::record($resp_msg);
                     exit('fail');
                 }
                 $Trade_log = M("trade_log");
                 $User = M("userinfo");
                 //给投资用户扣钱、借款用户加钱入账
                 $condition = array();
                 $condition['f_vid'] = $tradeid;
                 $condition['f_type'] = 1;
                 //借款订单
                 $condition['f_state'] = 0;
                 $vo = $Trade_log->where($condition)->field('f_money trademoney,f_inuserid inuserid,f_outuserid outuserid')->select();
                 $invest_userid = $vo[0]['outuserid'];
                 $loan_userid = $vo[0]['inuserid'];
                 $trademoney = $vo[0]['trademoney'];
                 //-money
                 $condition = array();
                 $condition['f_uid'] = $invest_userid;
                 $vo = $User->where($condition)->field('f_usermoney money')->select();
                 if (!empty($vo)) {
                     $data = array();
                     $data['f_usermoney'] = $vo[0]['money'] - $trademoney;
                     $doRecharge = $User->where($condition)->save($data);
                     if ($doRecharge !== false) {
                         //插入账户流水
                         TradeController::insertUserAccountLog(8, $invest_userid, $trademoney);
                     } else {
                         $resp_msg = '更新用户账户信息失败,userid=' . $invest_userid . ',usermoney=' . $data['f_usermoney'];
                         \Think\Log::record($resp_msg);
                         exit('fail');
                     }
                 } else {
                     $resp_msg = '更新用户账户信息失败--用户不存在,userid=' . $invest_userid;
                     \Think\Log::record($resp_msg);
                     exit('fail');
                 }
                 //+money
                 $condition = array();
                 $condition['f_uid'] = $loan_userid;
                 $vo = $User->where($condition)->field('f_usermoney money')->select();
                 if (!empty($vo)) {
                     $data = array();
                     $data['f_usermoney'] = $vo[0]['money'] + $trademoney;
                     $doRecharge = $User->where($condition)->save($data);
                     if ($doRecharge !== false) {
                         //插入账户流水
                         TradeController::insertUserAccountLog(9, $loan_userid, $trademoney);
                     } else {
                         $resp_msg = '更新用户账户信息失败,userid=' . $loan_userid . ',usermoney=' . $data['f_usermoney'];
                         \Think\Log::record($resp_msg);
                         exit('fail');
                     }
                 } else {
                     $resp_msg = '更新用户账户信息失败--用户不存在,userid=' . $loan_userid;
                     \Think\Log::record($resp_msg);
                     exit('fail');
                 }
                 //更新trade_log表
                 $Trade_log = M("trade_log");
                 $data = array();
                 if ($order_status == 100) {
                     $data['f_state'] = 100;
                 } elseif ($order_status == -100) {
                     $data['f_state'] = -100;
                 }
                 $condition = array();
                 $condition['f_vid'] = $tradeid;
                 $condition['f_type'] = 1;
                 $condition['f_state'] = 0;
                 $ret = $Trade_log->where($condition)->save($data);
                 if (!empty($ret)) {
                     $resp_msg = '更新trade_log表成功,id= ' . $tradeid;
                     \Think\Log::record($resp_msg);
                 } else {
                     $resp_msg = '更新trade_log表失败,id= ' . $tradeid;
                     \Think\Log::record($resp_msg);
                     exit('fail');
                 }
             }
             exit('success');
         }
     } else {
         $resp_msg = 'resp_code: ' . $args['resp_code'] . ',resp_msg: ' . $args['resp_msg'];
         \Think\Log::record($resp_msg);
         exit('fail');
     }
 }