diff --git a/app/Domains/Stats/Http/Controllers/QuantityController.php b/app/Domains/Stats/Http/Controllers/QuantityController.php new file mode 100644 index 00000000..1251b9db --- /dev/null +++ b/app/Domains/Stats/Http/Controllers/QuantityController.php @@ -0,0 +1,38 @@ +request = $request; + $this->quantityService = $quantityService; + } + + /** + * 列表. + * + * @return \Illuminate\Http\Response + */ + public function index() + { + $conditions = $this->request->all(); + + $res = $this->quantityService->index($conditions); + + return res($res, '数量统计', 201); + } +} diff --git a/app/Domains/Stats/Services/QuantityService.php b/app/Domains/Stats/Services/QuantityService.php new file mode 100644 index 00000000..71b2aa3c --- /dev/null +++ b/app/Domains/Stats/Services/QuantityService.php @@ -0,0 +1,94 @@ += '2019-12-31 00:00:00' AND created_at <= '2019-12-31 00:00:00' AND type = 0 THEN 1 ELSE 0 END) AS all_month, + // SUM(CASE WHEN created_at >= '2019-12-31 00:00:00' AND created_at <= '2019-12-31 00:00:00' AND type = 0 THEN 1 ELSE 0 END) AS charge_month, + // SUM(CASE WHEN created_at >= '2019-12-31 00:00:00' AND created_at <= '2019-12-31 00:00:00' AND type = 0 THEN 1 ELSE 0 END) AS new_month, + // SUM(CASE WHEN created_at >= '2019-12-31 00:00:00' AND created_at <= '2019-12-31 00:00:00' AND type = 0 THEN 1 ELSE 0 END) AS expire_month, + // SUM(CASE WHEN created_at >= '2019-12-31 00:00:00' AND created_at <= '2019-12-31 00:00:00' AND type = 0 THEN 1 ELSE 0 END) AS renew_month, + // SUM(CASE WHEN created_at >= '2019-12-31 00:00:00' AND created_at <= '2019-12-31 00:00:00' AND type = 0 THEN 1 ELSE 0 END) AS activate_month, + // SUM(CASE WHEN created_at >= '2019-12-31 00:00:00' AND created_at <= '2019-12-31 00:00:00' AND type = 0 THEN 1 ELSE 0 END) AS stop_month, + // FROM virtual_order_cards_partition" + + // $diff = Carbon::parse($conditions['end_time'])->diffInMonths(Carbon::parse($conditions['start_time'])); + + // $start = Carbon::parse($conditions['start_time']); + // for ($i=0; $i < $diff; $i++) { + // $month = $start->copy()->addMonths($i); + + + // # code... + // } + + $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; + } +} diff --git a/app/Domains/Virtual/Http/Controllers/FlowPoolController.php b/app/Domains/Virtual/Http/Controllers/FlowPoolController.php index dd4388c2..ee7b2aae 100644 --- a/app/Domains/Virtual/Http/Controllers/FlowPoolController.php +++ b/app/Domains/Virtual/Http/Controllers/FlowPoolController.php @@ -208,7 +208,7 @@ class FlowPoolController extends Controller 'company_id' => $flowPool->company_id, 'package_id' => $package_ids, 'unit_price' => 0, - 'uncancelled' => 1, + // 'uncancelled' => 1, ]; $cards = $repository->skipCache()->select([ diff --git a/app/Domains/Virtual/Services/FlowPoolService.php b/app/Domains/Virtual/Services/FlowPoolService.php index 2aba4da8..df3617ea 100644 --- a/app/Domains/Virtual/Services/FlowPoolService.php +++ b/app/Domains/Virtual/Services/FlowPoolService.php @@ -141,7 +141,7 @@ class FlowPoolService extends Service 'company_id' => $company_ids, 'package_id' => $package_ids, 'unit_price' => 0, - 'uncancelled' => 1, + // 'uncancelled' => 1, ]; $cards = app(OrderCardPartitionRepository::class)->skipCache()->select([ @@ -455,7 +455,7 @@ class FlowPoolService extends Service 'company_id' => $flowPool->company_id, 'package_id' => array_keys($settings), 'unit_price' => 0, - 'uncancelled' => 1, + // 'uncancelled' => 1, ]; $cards = app(OrderCardPartitionRepository::class)->skipCache()->selectRaw('distinct sim as sim, package_id')->withConditions($cardConditions)->get();