按百分比从db获取结果

时间:2017-04-18 15:27:14

标签: php mysql laravel

我现在正在laravel 5.0项目上工作,我有带有潜在客户历史记录的表格,我有表格,用户可以选择多少线索,以及有多少是新的,在历史记录表中存在的是新的,我整天都想弄清楚,我无法找到解决方案,我希望有人在这里有答案

编辑:我终于做到了!我的回答低于旧方法

我的方法(编辑前)

 static public function getLeads($input)
{

    self::$input = $input;

    $amount = !empty($input['amount']) ? $input['amount'] : null;

    $leads = Lead::where(function ($query) {

        $input = self::$input;

        $with_name = !empty($input['with_name']) ? (int)$input['with_name'] : null;
        $countries = !empty($input['country']) ? $input['country'] : null;
        $dateFrom = !empty($input['date_from']) ? $input['date_from'] : null;
        $dateTo = !empty($input['date_to']) ? $input['date_to'] : null;
        $percentSent = !empty($input['percent_first']) ? $input['percent_first'] : null;
        $percentNotSent = !empty($input['percent_second']) ? $input['percent_second'] : null;

        if (isset($with_name) && $with_name === 2) {
            $query->where('full_name', '!=', '');
        }

        if (isset($with_name) && $with_name === 3) {
            $query->where('full_name', '!=', '');
            $query->where('email', '!=', '');
        }

        if (isset($countries)) {
            $query->whereIn('country', $countries);
        }

        if (isset($dateFrom)) {
            $query->where('date', '>=', $dateFrom);
        }

        if (isset($dateTo)) {
            $query->where('date', '<=', $dateTo);
        }

        if (isset($percentSent) && $percentSent >= 1) {
            //precent of sent
            $total = self::$input['amount'];
            $percentSent = $percentSent / 100;
            $total = $percentSent * $total;
            $total = round($total);
            //the new total is number of old leads
        }

    })->limit($amount)->get(['phone'])->toArray();

    return $leads;
}

新方法:

static public function getLeads($input)
{

    self::$input = $input;

    $amount = !empty($input['amount']) ? $input['amount'] : null;
    $percentSent = !empty($input['percent_first']) ? $input['percent_first'] : null;
    $percentNotSent = !empty($input['percent_second']) ? $input['percent_second'] : null;

    $total = $amount;
    $percentSent = $percentSent / 100;
    $total = $percentSent * $total;
    $total = round($total);

    $leads = Lead::where(function ($query) {

        $input = self::$input;
        $with_name = !empty($input['with_name']) ? (int)$input['with_name'] : null;
        $countries = !empty($input['country']) ? $input['country'] : null;
        $dateFrom = !empty($input['date_from']) ? $input['date_from'] : null;
        $dateTo = !empty($input['date_to']) ? $input['date_to'] : null;
        $amount = !empty($input['amount']) ? $input['amount'] : null;

        $percentSent = !empty($input['percent_first']) ? $input['percent_first'] : null;
        $percentNotSent = !empty($input['percent_second']) ? $input['percent_second'] : null;
        $total = $amount;
        $percentSent = $percentSent / 100;
        $total = $percentSent * $total;
        $total = round($total);


        if (isset($with_name) && $with_name === 2) {
            $query->where('full_name', '!=', '');
        }

        if (isset($with_name) && $with_name === 3) {
            $query->where('full_name', '!=', '');
            $query->where('email', '!=', '');
        }

        if (isset($countries)) {
            $query->whereIn('country', $countries);
        }

        if (isset($dateFrom)) {
            $query->where('date', '>=', $dateFrom);
        }

        if (isset($dateTo)) {
            $query->where('date', '<=', $dateTo);
        }

        if($total == $amount){
            $query->where('lead_history.time', '!=', '');
        }

    })->leftJoin('lead_history', 'lead_history.lead_id', '=', 'leads.id')
        ->groupBy('leads.id')
        ->havingRaw('COUNT(lead_history.lead_id) <= ' . $total)
        ->orderBy('leads.date')
        ->limit($amount)->get(['*'])->toArray();

    dd($leads);

    return $leads;

}

全部谢谢!

0 个答案:

没有答案
相关问题