6
0
Fork 0
jiqu-library-server/app/Admin/Controllers/PointLogController.php

156 lines
6.8 KiB
PHP

<?php
namespace App\Admin\Controllers;
use App\Admin\Repositories\PointLog as PointLogRepository;
use App\Admin\Widgets\InfoBox;
use App\Enums\PointLogAction;
use App\Models\Order;
use App\Models\OrderProduct;
use App\Models\PointLog;
use App\Models\UserInfo;
use App\Models\UserVip;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Row;
class PointLogController extends AdminController
{
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
Admin::style(
<<<CSS
.card-header {
margin-top: 1.5rem !important;
margin-bottom: -1rem !important;
}
CSS
);
$builder = PointLogRepository::with(['user', 'administrator']);
return Grid::make($builder, function (Grid $grid) {
$grid->export()->disableExportAll()->titles([
'id' => 'ID',
'username' => '用户昵称',
'user_id' => __('point-log.fields.user.phone'),
'first_rechare_time' => '首次充值时间',
'first_rechare_type' => '首次充值类型',
'first_rechare_money' => '首次充值金额',
'renew_rechare_count' => '续费次数',
'renew_rechare_type' => '续费类型',
'renew_rechare_latest_time' => '最近一次续费时间',
'renew_rechare_money_total' => '续费金额合计',
'action' => __('point-log.fields.action'),
'change_points' => __('point-log.fields.change_points'),
'before_points' => __('point-log.fields.before_points'),
'after_points' => __('point-log.fields.after_points'),
'goods_cost_price' => '商品成本价',
'remark' => __('point-log.fields.remark'),
'administrator' => '操作人',
'created_at' => __('admin.created_at'),
])->rows(function ($rows) {
foreach ($rows as &$row) {
$userInfo = UserInfo::where('user_id', $row['user_id'])->first();
$firstVip = UserVip::where('user_id', $row['user_id'])->where('status', UserVip::STATUS_SUCCESS)->orderBy('success_time', 'asc')->first();
$vipCount = UserVip::where('user_id', $row['user_id'])->where('status', UserVip::STATUS_SUCCESS)->count();
$row['user_id'] = data_get($row, 'user.phone');
$row['username'] = data_get($userInfo, 'nickname');
$row['first_rechare_time'] = $firstVip ? $firstVip->success_time->format('Y-m-d H:i:s') : '';
$row['first_rechare_type'] = data_get($firstVip, 'name');
$row['first_rechare_money'] = data_get($firstVip, 'price');
$row['renew_rechare_count'] = '';
$row['renew_rechare_type'] = '';
$row['renew_rechare_latest_time'] = '';
$row['renew_rechare_money_total'] = '';
if ($vipCount > 1) {
$row['renew_rechare_count'] = $vipCount - 1;
$latestVip = UserVip::where('user_id', $row['user_id'])->where('status', UserVip::STATUS_SUCCESS)->orderBy('success_time', 'desc')->first();
$row['renew_rechare_type'] = data_get($latestVip, 'name');
$row['renew_rechare_latest_time'] = data_get($latestVip, 'success_time');
$sum = UserVip::where('user_id', $row['user_id'])->where('status', UserVip::STATUS_SUCCESS)->where('id', '!=', $firstVip->id)->sum('price');
$row['renew_rechare_money_total'] = $sum;
}
$row['action'] = PointLogAction::getLabel(data_get($row, 'action'));
$row['change_points'] = bcdiv(data_get($row, 'change_points'), 100, 2);
$row['before_points'] = bcdiv(data_get($row, 'before_points'), 100, 2);
$row['after_points'] = bcdiv(data_get($row, 'after_points'), 100, 2);
$row['remark'] = data_get($row, 'remark');
// 商品成本价
$row['goods_cost_price'] = '';
if ($row['loggable_type'] == (new Order)->getMorphClass()) {
$costPrice = OrderProduct::where('order_id', $row['loggable_id'])->sum('cost_price');
$row['goods_cost_price'] = bcdiv($costPrice, 100, 2);
}
$row['administrator'] = data_get($row, 'administrator.name');
$row['created_at'] = data_get($row, 'created_at');
}
return $rows;
});
$grid->model()->orderBy('id', 'desc');
$grid->column('id')->sortable();
$grid->column('user.phone')->copyable();
$grid->column('action')->display(fn ($action) => $action->label())->label();
$grid->column('change_points')->display(function ($value) {
return bcdiv($value, 100, 2);
});
$grid->column('before_points')->display(function ($value) {
return bcdiv($value, 100, 2);
});
$grid->column('after_points')->display(function ($value) {
return bcdiv($value, 100, 2);
});
$grid->column('remark');
$grid->column('administrator.name', '操作人');
$grid->column('created_at');
$grid->disableActions();
$grid->header(function ($collection) use ($grid) {
return tap(new Row(), function ($row) use ($grid) {
$query = PointLog::query();
$grid->model()->getQueries()->unique()->each(function ($value) use (&$query) {
if (in_array($value['method'], ['paginate', 'get', 'orderBy', 'orderByDesc'], true)) {
return;
}
$query = call_user_func_array([$query, $value['method']], $value['arguments'] ?? []);
});
$totalPoints = (clone $query)->sum('change_points');
$row->column(2, new InfoBox('积分总数', bcdiv($totalPoints, 100, 2), 'fa fa-ticket'));
});
});
$grid->filter(function (Grid\Filter $filter) {
$filter->panel(false);
$filter->equal('user.phone')->width(3);
$filter->like('administrator.name', '操作人')->width(3);
$filter->in('action')->multipleSelect(PointLogAction::options())->width(3);
$filter->between('created_at')->dateTime()->width(3);
});
return $grid;
});
}
}