117 lines
3.8 KiB
PHP
117 lines
3.8 KiB
PHP
<?php
|
|
namespace App\Domains\Stats\Services;
|
|
|
|
use App\Core\Service;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Exceptions\NotAllowedException;
|
|
use App\Domains\Virtual\Services\CommonService;
|
|
use App\Domains\Virtual\Repositories\OrderRepository;
|
|
use App\Domains\Virtual\Repositories\CompanyRepository;
|
|
use App\Domains\Virtual\Repositories\PackageRepository;
|
|
|
|
class OrderService extends Service
|
|
{
|
|
protected $companyRepository;
|
|
protected $packageRepository;
|
|
protected $orderRepository;
|
|
|
|
protected static $classes = [
|
|
\App\Domains\Virtual\Repositories\OrderCardRepository::class,
|
|
\App\Domains\Virtual\Repositories\OrderRenewalCardRepository::class,
|
|
\App\Domains\Virtual\Repositories\OrderRenewalPackageCardRepository::class,
|
|
\App\Domains\Virtual\Repositories\OrderFlowPackageCardsRepository::class,
|
|
];
|
|
|
|
/**
|
|
* 构造函数
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(
|
|
CompanyRepository $companyRepository,
|
|
PackageRepository $packageRepository,
|
|
OrderRepository $orderRepository
|
|
) {
|
|
$this->companyRepository = $companyRepository;
|
|
$this->packageRepository = $packageRepository;
|
|
$this->orderRepository = $orderRepository;
|
|
}
|
|
|
|
|
|
/**
|
|
* 统计
|
|
*
|
|
* @return void
|
|
*/
|
|
public function index($type, array $conditions = [])
|
|
{
|
|
$conditions['type'] = $type;
|
|
$conditions['source'] = 1;
|
|
|
|
$companies = $this->companyRepository->withTrashed()->get()->pluck('name', 'id')->toArray();
|
|
$packages = $this->packageRepository->withTrashed()->get()->pluck('name', 'id')->toArray();
|
|
|
|
$select = [
|
|
DB::raw("string_agg(DISTINCT id, ',') as id"),
|
|
'company_id',
|
|
'package_id',
|
|
'product_id',
|
|
'unit_price',
|
|
'pay_channel',
|
|
DB::raw('SUM(counts) as counts'),
|
|
DB::raw('SUM(custom_price) as custom_price'),
|
|
];
|
|
|
|
$orders = $this->orderRepository->select($select)->withConditions($conditions)->applyConditions()
|
|
->groupBy(['company_id', 'product_id', 'pay_channel'])->paginate($conditions['limit']);
|
|
|
|
$orders->map(function ($item) use ($companies, $packages) {
|
|
$item->company_name = $packages[$item->company_id];
|
|
$item->package_name = $packages[$item->package_id];
|
|
$item->pay_channel_name = CommonService::namePayChannel($item->pay_channel);
|
|
});
|
|
|
|
return $orders;
|
|
}
|
|
|
|
/**
|
|
* 明细
|
|
*
|
|
* @return void
|
|
*/
|
|
public function detail(array $conditions = [])
|
|
{
|
|
if (!$class = self::$classes[$conditions['type']]) {
|
|
throw new NotAllowedException('统计类型不存在');
|
|
}
|
|
|
|
$repository = app($class);
|
|
|
|
$companies = $this->companyRepository->withTrashed()->get()->pluck('name', 'id')->toArray();
|
|
$packages = $this->packageRepository->withTrashed()->get()->pluck('name', 'id')->toArray();
|
|
|
|
$repository->withConditions($conditions)->applyConditions()->paginate($conditions['limit']);
|
|
|
|
$select = [
|
|
'id',
|
|
'company_id',
|
|
'package_id',
|
|
'unit_price',
|
|
'pay_channel',
|
|
DB::raw('SUM(counts) as counts'),
|
|
DB::raw('SUM(custom_price) as custom_price'),
|
|
];
|
|
|
|
$orders = $this->orderRepository->select($select)->withConditions($conditions)->applyConditions()
|
|
->groupBy(['company_id', 'product_id', 'pay_channel'])->paginate($conditions['limit']);
|
|
|
|
$orders->map(function ($item) use ($companies, $packages) {
|
|
$item->company_name = $packages[$item->company_id];
|
|
$item->package_name = $packages[$item->package_id];
|
|
$item->pay_channel_name = CommonService::namePayChannel($item->pay_channel);
|
|
});
|
|
|
|
return $orders;
|
|
}
|
|
}
|