示例#1
0
 /**
  * 借给他
  */
 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("不存在的订单");
         }
     }
 }