添加订单编号

This commit is contained in:
youda 2025-04-12 23:03:43 +08:00
parent a6a5bf03fa
commit 56002f390d
3 changed files with 78 additions and 110 deletions

View File

@ -20,19 +20,6 @@
</div> </div>
</div> </div>
<!-- 亏损排行榜排序选项 -->
<div class="layui-form loss-sort-option" {if $type!='loss'}style="display:none;"{/if}>
<div class="layui-form-item">
<label class="layui-form-label">排序方式:</label>
<div class="layui-input-inline">
<select name="sort_field" lay-filter="sort_field">
<option value="loss_money" {if $sort_field=='loss_money'}selected{/if}>亏损金额</option>
<option value="loss_rate" {if $sort_field=='loss_rate'}selected{/if}>亏损率</option>
</select>
</div>
</div>
</div>
<!-- 数据表格 --> <!-- 数据表格 -->
<table class="layui-table"> <table class="layui-table">
<thead> <thead>
@ -52,6 +39,7 @@
<!-- 亏损排行榜特有的列 --> <!-- 亏损排行榜特有的列 -->
{if $type=='loss'} {if $type=='loss'}
<th>消耗金额</th> <th>消耗金额</th>
<th>达达卷金额</th>
<th>出货金额</th> <th>出货金额</th>
<th>亏损率</th> <th>亏损率</th>
{/if} {/if}
@ -70,6 +58,7 @@
<!-- 亏损排行榜特有的列 --> <!-- 亏损排行榜特有的列 -->
{if $type=='loss'} {if $type=='loss'}
<td>{$vo.consume_money} 元</td> <td>{$vo.consume_money} 元</td>
<td>{$vo.dada_money} 元</td>
<td>{$vo.output_money} 元</td> <td>{$vo.output_money} 元</td>
<td>{$vo.loss_rate}%</td> <td>{$vo.loss_rate}%</td>
{/if} {/if}
@ -96,22 +85,8 @@
// 排行榜类型切换 // 排行榜类型切换
form.on('radio(rank_type)', function(data) { form.on('radio(rank_type)', function(data) {
var type = data.value; var type = data.value;
// 亏损补贴排行榜时显示排序选项
if (type === 'loss') {
$('.loss-sort-option').show();
} else {
$('.loss-sort-option').hide();
}
// 跳转到对应类型的排行榜页面 // 跳转到对应类型的排行榜页面
window.location.href = '{:url("/admin/user_rank")}?type=' + type + window.location.href = '{:url("/admin/user_rank")}?type=' + type;
'&sort_field=' + (type === 'loss' ? $('select[name="sort_field"]').val() : 'loss_money');
});
// 排序方式切换
form.on('select(sort_field)', function(data) {
var sortField = data.value;
window.location.href = '{:url("/admin/user_rank")}?type=loss&sort_field=' + sortField;
}); });
// 初始化分页 // 初始化分页
@ -128,7 +103,7 @@
url += '&page=' + obj.curr; url += '&page=' + obj.curr;
url += '&limit=' + obj.limit; url += '&limit=' + obj.limit;
if ('{$type}' === 'loss') { if ('{$type}' === 'loss') {
url += '&sort_field={$sort_field}'; url += '';
} }
window.location.href = url; window.location.href = url;
} }

View File

@ -86,7 +86,7 @@ class Order extends Base
// 获取订单详情 // 获取订单详情
$orderInfo = Db::name('order') $orderInfo = Db::name('order')
->field('goods_id, goods_title, goods_price, id, order_total, order_zhe_total, price, use_money, use_integral, use_money2, addtime, pay_time, prize_num, use_coupon') ->field('order_num,goods_id, goods_title, goods_price, id, order_total, order_zhe_total, price, use_money, use_integral, use_money2, addtime, pay_time, prize_num, use_coupon')
->where([ ->where([
'status' => 1, 'status' => 1,
'order_num' => $orderNum 'order_num' => $orderNum

View File

@ -318,19 +318,18 @@ class RankService
* @param int $endTime 结束时间戳 * @param int $endTime 结束时间戳
* @param int $page 页码 * @param int $page 页码
* @param int $limit 每页数量 * @param int $limit 每页数量
* @param string $sortField 排序字段loss_money(亏损金额)或loss_rate(亏损率)
* @return array 排行榜数据 * @return array 排行榜数据
*/ */
public function getLossRank($startTime, $endTime, $page, $limit, $sortField = 'loss_money') public function getLossRank($startTime, $endTime, $page, $limit, $sortField = 'loss_money')
{ {
// 构建订单查询条件 // 构建订单条件
$orderWhere = [ $orderWhere = [
['status', '=', 1], ['status', '=', 1],
[ [
'user_id', 'user_id',
'not in', 'not in',
function ($query) { function ($query) {
$query->name('user')->where('istest', '>', 0)->where('status', '=', 1)->field('id'); $query->name('user')->where('istest', '>', 0)->field('id');
} }
] ]
]; ];
@ -343,103 +342,97 @@ class RankService
$orderWhere[] = ['pay_time', '<=', $endTime]; $orderWhere[] = ['pay_time', '<=', $endTime];
} }
// 构建订单详情查询条件 // 查询订单数据,分组计算每个用户的消费金额
$orderListWhere = []; $orderData = Db::name('order')
->where($orderWhere)
->field('user_id, COALESCE(sum(price), 0) as money')
->group('user_id')
->select()
->toArray();
// 处理查询结果,计算每个用户的出货金额和达达卷金额
$userLossData = [];
foreach ($orderData as $order) {
$userId = $order['user_id'];
$money = $order['money']; // 微信消费金额
// 查询出货金额
$outputMoneyWhere = [];
if ($startTime > 0) { if ($startTime > 0) {
$orderListWhere[] = ['addtime', '>=', $startTime]; $outputMoneyWhere[] = ['addtime', '>=', $startTime];
} }
if ($endTime > 0) { if ($endTime > 0) {
$orderListWhere[] = ['addtime', '<=', $endTime]; $outputMoneyWhere[] = ['addtime', '<=', $endTime];
} }
$outputMoneyWhere[] = ['user_id', '=', $userId];
// 查询用户订单总金额(消耗金额) $outputMoney = Db::name('order_list')
$orderSubQuery = Order::where($orderWhere) ->where($outputMoneyWhere)
->field('user_id, SUM(price) + SUM(use_money) as money')
->group('user_id')
->buildSql();
// 查询用户出货金额
$usersWithOutputMoney = [];
$users = Order::where($orderWhere)->field('DISTINCT user_id')->select()->toArray();
foreach ($users as $user) {
$userId = $user['user_id'];
// 构建特定用户的查询条件
$userOrderListWhere = $orderListWhere;
$userOrderListWhere[] = ['user_id', '=', $userId];
// 查询用户出货金额
$outputMoney = \app\common\model\OrderList::where($userOrderListWhere)
->sum('goodslist_money'); ->sum('goodslist_money');
if ($outputMoney > 0) { // 查询达达卷金额
$usersWithOutputMoney[$userId] = $outputMoney; $dadaMoneyWhere = [];
if ($startTime > 0) {
$dadaMoneyWhere[] = ['addtime', '>=', $startTime];
} }
if ($endTime > 0) {
$dadaMoneyWhere[] = ['addtime', '<=', $endTime];
} }
$dadaMoneyWhere[] = ['user_id', '=', $userId];
// 计算亏损数据 $dadaMoney = Db::name('order_list_recovery')
$rankList = []; ->where($dadaMoneyWhere)
$index = 0; ->sum('money');
// 从订单总金额查询结果中获取数据 // 如果dadaMoney为null设置为0
$moneyResult = Db::table($orderSubQuery . ' as t')->where('money', '>', 0)->select()->toArray(); $dadaMoney = $dadaMoney ?: 0;
foreach ($moneyResult as $item) { // 计算亏损金额:(出货金额-(微信消费金额+达达卷金额))
$userId = $item['user_id']; $lossMoney = $outputMoney - ($money + $dadaMoney);
$consumeMoney = $item['money']; // 消耗金额
// 如果有出货金额数据 // 计算亏损率
if (isset($usersWithOutputMoney[$userId])) { $totalConsume = $money + $dadaMoney;
$outputMoney = $usersWithOutputMoney[$userId]; // 出货金额 $lossRate = $totalConsume > 0 ? round(($lossMoney / $totalConsume) * 100, 2) : 0;
// 计算亏损金额和亏损率 // 只有亏损的用户才添加到排行榜
$lossMoney = $consumeMoney - $outputMoney; if ($lossMoney < 0) {
$lossRate = $consumeMoney > 0 ? round(($lossMoney / $consumeMoney) * 100, 2) : 0; $userLossData[] = [
// 只有亏损的才纳入排行
if ($lossMoney > 0) {
$rankList[] = [
'user_id' => $userId, 'user_id' => $userId,
'consume_money' => $consumeMoney, // 消耗金额 'consume_money' => $money, // 微信消费金额
'output_money' => $outputMoney, // 出货金额 'output_money' => $outputMoney, // 出货金额
'dada_money' => $dadaMoney, // 达达卷金额
'loss_money' => $lossMoney, // 亏损金额 'loss_money' => $lossMoney, // 亏损金额
'loss_rate' => $lossRate // 亏损率(%) 'loss_rate' => $lossRate // 亏损率(%)
]; ];
} }
} }
}
// 根据排序字段对结果进行排序 // 按亏损金额从小到大排序(因为亏损是负数,所以越小表示亏损越多)
if ($sortField == 'loss_rate') { usort($userLossData, function ($a, $b) {
// 按亏损率降序排序 return $a['loss_money'] <=> $b['loss_money'];
usort($rankList, function ($a, $b) {
return $b['loss_rate'] <=> $a['loss_rate'];
}); });
} else {
// 默认按亏损金额降序排序
usort($rankList, function ($a, $b) {
return $b['loss_money'] <=> $a['loss_money'];
});
}
// 分页处理 // 分页处理
$offset = ($page - 1) * $limit; $offset = ($page - 1) * $limit;
$rankList = array_slice($rankList, $offset, $limit); $userLossData = array_slice($userLossData, $offset, $limit);
// 处理用户信息和排名 // 处理用户信息和排名
$result = []; $result = [];
foreach ($rankList as $index => $item) { foreach ($userLossData as $index => $item) {
$userInfo = User::field('nickname, headimg, mobile')->where('id', $item['user_id'])->find(); $userInfo = User::field('nickname, headimg, mobile')->where('id', $item['user_id'])->find();
if ($userInfo) { if ($userInfo) {
// 取亏损金额的绝对值来显示
$absoluteLossMoney = abs($item['loss_money']);
$result[] = [ $result[] = [
'rank' => $offset + $index + 1, 'rank' => $offset + $index + 1,
'user_id' => $item['user_id'], 'user_id' => $item['user_id'],
'nickname' => $userInfo['nickname'], 'nickname' => $userInfo['nickname'],
'headimg' => imageUrl($userInfo['headimg']), 'headimg' => imageUrl($userInfo['headimg']),
'consume_money' => $item['consume_money'], 'consume_money' => $item['consume_money'], // 微信消费金额
'output_money' => $item['output_money'], 'dada_money' => $item['dada_money'], // 达达卷金额
'value' => $item['loss_money'], // 主要展示值为亏损金额 'output_money' => $item['output_money'], // 出货金额
'value' => $absoluteLossMoney, // 主要展示值为亏损金额的绝对值
'loss_rate' => $item['loss_rate'], // 亏损率 'loss_rate' => $item['loss_rate'], // 亏损率
'unit' => '元' 'unit' => '元'
]; ];