request = $request; $this->flowPoolService = $flowPoolService; } /** * RD流量池列表 * * @return void */ public function real() { $list = $this->flowPoolService->real(); return res($list, 'RD流量池列表', 201); } /** * 后向套餐列表 * * @return void */ public function packages() { $list = $this->flowPoolService->packages(); return res($list, '后向套餐列表', 201); } /** * 列表. * * @return \Illuminate\Http\Response */ public function index() { $conditions = $this->request->all(); $flowPools = $this->flowPoolService->index($conditions); return res($flowPools, '流量池列表', 201); } /** * 详情. * * @return \Illuminate\Http\Response */ public function show() { $page = $this->request->get('page'); $conditions = $this->request->all(); $res = $this->flowPoolService->show($conditions); return res($res, '流量池详情', 201); } /** * 明细导出. * * @return \Illuminate\Http\Response */ public function detailExport() { $conditions = $this->request->only(['pool_id', 'month']); $pool_id = $conditions['pool_id']; $month = Carbon::parse($conditions['month']); $table = app(FlowPoolMonth::class)->getTable() . '_' . $month->format('Ym'); if (!Schema::hasTable($table)) { throw new NotAllowedException('无该月数据'); } $total = app(FlowPoolMonth::class)->setTable($table)->where('pool_id', $pool_id)->count(); try { $export = new FlowPoolExportDetailExport($conditions); $queue = $total > 30000; $url = ExportService::store($export, $disk = 'public', $queue); } catch (\Exception $e) { throw $e; } return res($url, '导出成功', 201); } /** * 创建. * * @return \Illuminate\Http\Response */ public function create() { $attributes = $this->request->all(); $flowPool = $this->flowPoolService->store($attributes); return res($flowPool, '创建成功'); } /** * 编辑. * * @return \Illuminate\Http\Response */ public function update($id) { $attributes = $this->request->all(); $attributes['id'] = $id; $flowPool = $this->flowPoolService->store($attributes); return res($flowPool, '修改成功'); } /** * 删除. * * @return \Illuminate\Http\Response */ public function destroy() { $ids = $this->request->ids(); $this->flowPoolService->destroy($ids); return res(true, '删除成功'); } /** * 编辑. * * @return \Illuminate\Http\Response */ public function setting() { $attributes = $this->request->all(); $setting = $this->flowPoolService->setting($attributes); return res($setting, $attributes['id'] ? '修改成功' : '添加成功'); } /** * 数据生成. * * @return \Illuminate\Http\Response */ public function flows() { $pool_id = $this->request->get('pool_id'); $month = $this->request->time('month'); if ($this->request->isMethod('GET')) { if (!$flowPool = app(FlowPoolRepository::class)->find($pool_id)) { throw new NotExistException('流量池不存在或已删除'); } $flowPool = FlowPoolService::transformer(collect([$flowPool]), $month)->first(); $package_ids = []; if ($flowPool->setting_status) { $repository = app(OrderCardPartitionRepository::class); $package_ids = $flowPool->package_ids; $conditions = [ 'type' => [0, 1, 2], 'month' => $month, 'company_id' => $flowPool->company_id, 'package_id' => $package_ids, 'unit_price' => 0 ]; $cards = $repository->select([ 'package_id', DB::raw('count(distinct sim) as total'), ])->withConditions($conditions)->groupBy('package_id')->get()->pluck('total', 'package_id'); } else { $cards = collect(); } $flowPoolData = FlowPoolData::where('month', $month->format('Ym'))->where('pool_id', $pool_id)->first(); if (!$flowPoolData) { $flowPoolData = [ 'pool_id' => $pool_id, 'month' => $month->format('Y-m'), 'total_flows' => 0, 'settings' => [], ]; } $settings = array_keyBy($flowPoolData['settings'], 'package_id'); $newSettings = []; foreach ($package_ids as $package_id) { if (!isset($settings[$package_id])) { $newSettings[] = [ 'package_id' => $package_id, 'package_name' => PackageService::load($package_id)['name'], 'total' => $cards[$package_id] ?? 0, 'news' => $cards[$package_id] ?? 0, ]; } else { $setting = $settings[$package_id]; $chunk = $setting['cards']; foreach ($chunk as &$value) { $value['flow_range'][0] = sprintf('%.02f', $value['flow_range'][0]); $value['flow_range'][1] = sprintf('%.02f', $value['flow_range'][1]); } $setting['cards'] = $chunk; $news = ($cards[$package_id] ?? 0) - array_sum(array_pluck($setting['cards'], 'counts')); $setting['news'] = $news < 0 ? 0 : $news; $newSettings[] = $setting; } } $flowPoolData['total_flows'] = sprintf('%.02f', $flowPoolData['total_flows']); return res(['flowPool' => $flowPool, 'settings' => $newSettings, 'total' => $cards->values()->sum(), 'total_flows' => $flowPoolData['total_flows']], '数据设置'); } $conditions = $this->request->all(); $res = $this->flowPoolService->flows($conditions); return res($res, '设置成功'); } /** * 导出. * * @return \Illuminate\Http\Response */ public function export() { $conditions = $this->request->except(['page', 'limit']); $conditions['limit'] = 0; try { $export = new FlowPoolExport($conditions); $url = ExportService::store($export, $disk = 'public'); } catch (\Exception $e) { throw $e; } return res($url, '导出成功', 201); } /** * 导入流量卡数据. * * @return \Illuminate\Http\Response */ public function importFlows() { begin_time_consuming(); $file = $this->request->file('file'); $data = ImportService::load($file, ['month', 'pool_id', 'sim', 'mebibyte']); foreach ($data as $row) { foreach (['month', 'pool_id', 'sim', 'mebibyte'] as $key) { if (!isset($row[$key])) { throw new InvalidArgumentException("字段{$key}不能为空 #:{$row[$key]}"); } $row[$key] = trim($row[$key]); if (!is_numeric($row[$key])) { throw new InvalidArgumentException("字段{$key}必须为数字 #:{$row[$key]}"); } } } $flowPools = app(FlowPoolRepository::class)->withConditions(['id' => array_unique(array_pluck($data, 'pool_id'))])->get()->keyBy('id')->toArray(); $simPackage = []; foreach (array_chunk($data, 10000) as $value) { $cards = app(OrderCardPartitionRepository::class)->select(['sim', 'package_id'])->withConditions([ 'type' => [0, 1, 2], 'sim' => array_pluck($value, 'sim'), 'now' => 1, ])->get()->pluck('package_id', 'sim')->toArray(); foreach ($cards as $sim => $package_id) { $simPackage[$sim] = $package_id; } } foreach ($data as &$item) { if (!isset($simPackage[$item['sim']])) { throw new NotExistException('卡不在VD上 #:' . $item['sim']); } $package_id = $simPackage[$item['sim']]; $pool = $flowPools[$item['pool_id']]; if (!in_array($package_id, $pool['package_ids'])) { throw new InvalidArgumentException("卡不属于流量池 {$item['pool_id']} #: {$item['sim']}"); } $item['package_id'] = $simPackage[$item['sim']]; } DB::transaction(function () use ($data) { $monthGroupBy = array_groupBy($data, 'month'); foreach ($monthGroupBy as $month => $values) { $table = FlowPoolService::checkTable($month); $simGroupBy = array_groupBy($values, 'sim'); foreach ($simGroupBy as $sim => $group) { if (count($group) > 1) { $temp = $group[0]; $temp['mebibyte'] = array_sum(array_pluck($group, 'mebibyte')); $simGroupBy[$sim] = [$temp]; } } $values = array_collapse($simGroupBy); foreach (array_chunk($values, 10000) as $news) { app(FlowPoolMonth::class)->setTable($table)->upsert($news, ['sim', 'month']); } } }); return res(true, '导入成功'); } }