Ejemplo n.º 1
0
function autoInvest($borrow_id)
{
    $binfo = M("borrow_info")->field('borrow_uid,borrow_money,borrow_type,repayment_type,borrow_interest_rate,borrow_duration,has_vouch,has_borrow,borrow_max,borrow_min,can_auto')->find($borrow_id);
    if ($binfo['can_auto'] == '0') {
        exit;
    }
    $map['a.is_use'] = 1;
    $map['borrow_type'] = 1;
    if ($map['a.end_time'] > 0) {
        $map['a.end_time'] = array("gt", time());
    }
    $autolist = M("auto_borrow a")->join(C('DB_PREFIX') . "member_money m ON a.uid=m.uid")->join(C('DB_PREFIX') . "members ms ON a.uid=ms.id")->field("a.*,ms.usrid, m.account_money+m.back_money as money")->where($map)->order("a.invest_time asc")->select();
    $needMoney = $binfo['borrow_money'] - $binfo['has_borrow'];
    $pre = C('DB_PREFIX');
    $info2 = M("members m")->field("m.usrid")->join("{$pre}borrow_info s ON s.borrow_uid=m.id")->where("s.id = " . $borrow_id)->find();
    $borrowerid = $info2['usrid'];
    $borrowid = $borrow_id;
    $usrid = array();
    $transamt = array();
    foreach ($autolist as $key => $v) {
        if (!$needMoney) {
            break;
        }
        if ($v['uid'] == $binfo['borrow_uid']) {
            continue;
        }
        $num_max1 = intval($v['money'] - $v['account_money']);
        //账户余额-设置的最少剩余金额,即可用的投资金额数
        $num_max4 = $binfo['borrow_money'] / 10;
        //不能超过10%
        //大于最大投标且设置最大投标
        if ($investMoney > $binfo['borrow_max'] && $binfo['borrow_max'] > 0) {
            $investMoney = $binfo['borrow_max'];
        }
        if ($num_max1 > $v['invest_money']) {
            //如果可用的投资金额大于最大投资金额,则投资金额等于最大投资金额
            $investMoney = $v['invest_money'];
        } else {
            $investMoney = $num_max1;
            //如果未设置投标后账户余额,则会投出全部余额
        }
        if ($investMoney > $needMoney) {
            $investMoney = $needMoney;
        } else {
            if ($binfo['borrow_min']) {
                //设置了最小投标    如果直接满标则不考虑最小投标
                if ($investMoney < $binfo['borrow_min']) {
                    // 小于最低投标
                    continue;
                    //不符合最低投资金额
                } elseif ($needMoney - $investMoney > 0 && $needMoney - $investMoney < $binfo['borrow_min']) {
                    // 剩余金额小于最小投标金额
                    if ($investMoney - $binfo['borrow_min'] >= $binfo['borrow_min']) {
                        // 投资金额- 最小投资金额 大于最小投资金额
                        $investMoney = $investMoney - $binfo['borrow_min'];
                        // 投资 = 投资-最小投资(保证下次投资金额大于最小投资金额)
                    } else {
                        continue;
                    }
                }
            }
        }
        //投资金额不能大于借款金额的10%
        if ($investMoney > $num_max4) {
            $investMoney = $num_max4;
        }
        if ($investMoney % $binfo['borrow_min'] != 0) {
            continue;
        }
        $tiaojian = "{$needMoney} > 0";
        //可投金额大于0
        if ($v['interest_rate'] > 0) {
            $tiaojian .= " && " . $binfo['borrow_interest_rate'] . " >= " . $v['interest_rate'];
            //利率范围
        }
        if ($v['duration_from'] > 0 && $v['duration_to'] > 0 && $v['duration_from'] <= $v['duration_to']) {
            $tiaojian .= " && (" . $binfo['borrow_duration'] . " >= " . $v['duration_from'] . " || " . $v['duration_from'] . "==0) && (" . $binfo['borrow_duration'] . " <= " . $v['duration_to'] . " || " . $v['duration_to'] . "==0)";
            //借款期限范围
        }
        $tiaojian .= " && " . $investMoney . " >= " . $v['min_invest'];
        $tiaojian .= " && (" . $v['money'] . " - " . $v['account_money'] . ") >= " . $investMoney;
        //余额限制
        if ($tiaojian) {
            $usrid[] = $v['usrid'];
            $transamt[] = $investMoney;
            $needMoney = $needMoney - $investMoney;
            // 减去剩余已投金额
            MTip('chk27', $v['uid'], $borrow_id, $v['id']);
            //sss
            M('auto_borrow')->where('id = ' . $v['id'])->save(array("invest_time" => time()));
        }
    }
    /////////////////////////////////////////////////////汇付托管 2014-10-09///////////////////////////////////////////////////////////////
    import("ORG.Huifu.Huifu");
    $huifu = new Huifu();
    $huifu->autoTender_multi($usrid, $transamt, $borrowerid, $borrowid);
    /////////////////////////////////////////////////////汇付托管 END///////////////////////////////////////////////////////////////
    return true;
}