vd/app/Domains/Stats/Services/RenewalService.php
2020-03-06 17:40:52 +08:00

69 lines
2.0 KiB
PHP

<?php
namespace App\Domains\Stats\Services;
use App\Core\Service;
use Illuminate\Support\Facades\DB;
use App\Domains\Virtual\Services\CompanyService;
class RenewalService extends Service
{
/**
* 构造函数
*
* @return void
*/
public function __construct() { }
/**
* 统计
*
* @return void
*/
public function index(array $conditions = [])
{
$sql = 'SELECT c.company_id, count(*) AS activates, SUM(CASE WHEN p.id IS NOT NULL THEN 1 ELSE 0 END) AS renewals ' .
'FROM virtual_order_cards AS c ' .
'LEFT JOIN virtual_order_cards_partition AS p ON p.type IN (1, 2) AND p.sim = c.sim ';
$where = 'WHERE c.service_start_at IS NOT NULL ';
$group = 'GROUP BY c.company_id ';
if($conditions['company_id']){
$where .= 'AND c.company_id = ' . $conditions['company_id'] . ' ';
}
if($conditions['activate_start_time']){
$where .= "AND c.service_start_at >= '" . $conditions['activate_start_time'] . "' ";
}
if($conditions['activate_end_time']){
$where .= "AND c.service_end_at <= '" . $conditions['activate_end_time'] . "' ";
}
if($conditions['renewal_start_time']){
$where .= "AND p.service_start_at >= '" . $conditions['renewal_start_time'] . "' ";
}
if($conditions['renewal_start_time']){
$where .= "AND p.service_start_at >= '" . $conditions['renewal_start_time'] . "' ";
}
$sql = $sql . $where . $group;
$data = DB::select($sql);
if(!$data){
return [];
}
foreach ($data as &$item) {
$item = (array)$item;
$item['company_name'] = CompanyService::load($item['company_id'])['name'];
$item['ratio'] = $item['activates'] == 0 ? 0 : $item['renewals']/$item['activates'];
$item['ratio'] = sprintf('%.02f', $item['ratio']);
}
return $data;
}
}