提交代码

This commit is contained in:
baji 2025-03-09 14:01:46 +08:00
parent a16643cc08
commit bab5601c6a
4 changed files with 188 additions and 165 deletions

View File

@ -16,12 +16,10 @@ use think\facade\Db;
use app\common\model\Shang; use app\common\model\Shang;
use app\common\model\ProfitExpenses; use app\common\model\ProfitExpenses;
use app\common\model\ProfitRvenue; use app\common\model\ProfitRvenue;
class Statistics extends Base class Statistics extends Base
{ {
/**
* 利润统计
*/
public function profit(Request $request) public function profit(Request $request)
{ {
$title = trim(input('get.title')); $title = trim(input('get.title'));
@ -29,178 +27,170 @@ class Statistics extends Base
$status = trim(input('get.status')); $status = trim(input('get.status'));
$type = trim(input('get.type')); $type = trim(input('get.type'));
$addtime = trim(input('get.addtime')); $addtime = trim(input('get.addtime'));
$whe3 = array();
$whe4 = array();
$whe = array();
if ($title) {
$whe[] = ['title', 'like', '%' . $title . '%'];
}
if ($id) {
$whe[] = ['id', '=', $id];
} // 构建查询条件
if ($status) { $whe = [];
if ($title)
$whe[] = ['title', 'like', '%' . $title . '%'];
if ($id)
$whe[] = ['id', '=', $id];
if ($status)
$whe[] = ['status', '=', $status]; $whe[] = ['status', '=', $status];
} if ($type)
if ($type) {
$whe[] = ['type', '=', $type]; $whe[] = ['type', '=', $type];
// 解析时间范围
$whe3 = [];
$whe4 = [];
if ($addtime) {
[$start_time, $end_time] = $this->parseTimeRange($addtime);
$whe3[] = ['addtime', 'BETWEEN', [$start_time, $end_time]];
$whe4[] = ['addtime', 'BETWEEN', [$start_time, $end_time]];
} }
// 获取商品ID列表
$goodList = GoodsModel::where($whe)->column('id');
if (empty($goodList)) {
return View::fetch("Statistics/profit", ['list' => [], 'count' => 0, 'page' => '']);
}
// 获取测试用户ID列表
$userList = User::where('istest', 1)->column('id');
// 订单筛选条件
$whe3[] = ['user_id', 'not in', $userList];
$whe3[] = ['status', '=', 1];
$whe3[] = ['goods_id', 'in', $goodList];
$whe4[] = ['user_id', 'not in', $userList];
$whe4[] = ['goods_id', 'in', $goodList];
// 统计订单数据
$orderTotals = OrderModel::where($whe3)
->field('SUM(order_total) as order_total_all,
SUM(order_zhe_total) as order_zhe_total_all,
SUM(price) as price_all,
SUM(use_money) as use_money_all')
->find();
$goodslistMoneyAll = OrderList::where($whe4)->sum('goodslist_money');
// 获取商品列表
$data = GoodsModel::getList($whe, "*", "id desc", 20);
// 计算每个商品的订单详情
foreach ($data['list'] as &$value) {
$value = array_merge($value, $this->calculateOrderDetails($value['id'], $userList, $addtime));
}
// 计算利润
$sum_shijilirun = round($orderTotals->order_zhe_total_all - $goodslistMoneyAll, 2);
$sum_dingdanlirun = round($orderTotals->order_total_all - $goodslistMoneyAll, 2);
// 传递数据给视图
View::assign([
'sum_dingdan' => $orderTotals->order_total_all,
'sum_shiji' => $orderTotals->order_zhe_total_all,
'sum_chuhuo' => $goodslistMoneyAll,
'price_all' => $orderTotals->price_all,
'sum_shijilirun' => $sum_shijilirun,
'sum_dingdanlirun' => $sum_dingdanlirun,
'use_money_all' => $orderTotals->use_money_all,
'order_total_all' => $orderTotals->order_total_all,
'list' => $data['list'],
'count' => $data['count'],
'page' => $data['page'],
]);
return View::fetch("Statistics/profit");
}
/**
* 解析时间范围
*/
private function parseTimeRange($addtime)
{
$time = explode(' - ', $addtime);
$start_time = strtotime($time[0]);
$end_time = strtotime($time[1]) - 1;
if ($start_time > $end_time) {
throw new \Exception('开始时间不能大于结束时间');
}
return [$start_time, $end_time];
}
/**
* 计算商品的订单详情
*/
private function calculateOrderDetails($goodsId, $userList, $addtime)
{
$whe1 = [['goods_id', '=', $goodsId], ['status', '=', 1], ['user_id', 'not in', $userList]];
$whe2 = [['user_id', 'not in', $userList], ['goods_id', '=', $goodsId]];
if ($addtime) { if ($addtime) {
$time = explode(' - ', $addtime); [$start_time, $end_time] = $this->parseTimeRange($addtime);
$start_time = strtotime($time[0]); $whe1[] = ['addtime', 'BETWEEN', [$start_time, $end_time]];
$end_time = strtotime($time[1]) - 1; $whe2[] = ['addtime', 'BETWEEN', [$start_time, $end_time]];
if ($start_time > $end_time) {
$this->err('开始时间不能大于结束时间');
}
$whe3[] = ['addtime', 'BETWEEN', array($start_time, $end_time)];
$whe4[] = ['addtime', 'BETWEEN', array($start_time, $end_time)];
} }
$goodList = GoodsModel::where($whe)->field('id')->select();
$field = "*";
$order = "id desc";
$data = GoodsModel::getList($whe, $field, $order, 20);
$userList = User::where('istest', '=', 1)->field('id')->select();
$userArray = array_column($userList->toArray(), 'id');
$goodArray = array_column($goodList->toArray(), 'id');
$whe3[] = ['user_id', 'not in', $userArray];
$whe3[] = ['status', '=', 1];
$whe3[] = ['goods_id', 'in', $goodArray];
$whe4[] = ['user_id', 'not in', $userArray]; // 订单统计
$whe4[] = ['goods_id', 'in', $goodArray]; $orderStats = OrderModel::where($whe1)
->field('COUNT(*) as count,
SUM(order_total) as order_total,
SUM(price) as count_price,
SUM(use_money) as count_yue,
SUM(use_integral) / 100 as use_integral,
SUM(use_score) as use_score,
SUM(use_coupon) as use_coupon,
SUM(order_zhe_total) as order_zhe_total')
->find();
$order_total_all = OrderModel::where($whe3)->sum('order_total'); if (!$orderStats) {
$order_zhe_total_all = OrderModel::where($whe3)->sum('order_zhe_total'); $orderStats = (object) [
$goodslist_money_all = OrderList::where($whe4)->sum('goodslist_money'); 'count' => 0,
$price_all = OrderModel::where($whe3)->sum('price'); 'order_total' => 0,
$use_money_all = OrderModel::where($whe3)->sum('use_money'); 'count_price' => 0,
// $value['lirulv'] = round(($order_zhe_total - $goodslist_price1) / $goodslist_price1 * 100, 2); 'count_yue' => 0,
'use_integral' => 0,
View::assign('sum_dingdan', $order_total_all); 'use_score' => 0,
View::assign('sum_shiji', $order_zhe_total_all); 'use_coupon' => 0,
View::assign('sum_chuhuo', $goodslist_money_all); 'order_zhe_total' => 0
View::assign('price_all', $price_all); ];
View::assign('sum_shijilirun', round(($order_zhe_total_all - $goodslist_money_all), 2));
View::assign('sum_dingdanlirun', round(($order_total_all - $goodslist_money_all), 2));
View::assign('use_money_all', $use_money_all);
View::assign('order_total_all', $order_total_all);
//订单收入:实际收入:出货价值:实际利润率:订单利润率:
$sum_dingdan = 0;
$sum_shiji = 0;
$sum_chuhuo = 0;
$sum_shijilirun = 0;
$sum_dingdanlirun = 0;
foreach ($data['list'] as &$value) {
$whe1 = array();
$whe2 = array();
$whe1[] = ['goods_id', '=', $value['id']];
$whe1[] = ['status', '=', 1];
$whe1[] = ['user_id', 'not in', $userArray];
$whe2[] = ['user_id', 'not in', $userArray];
if ($addtime) {
$time = explode(' - ', $addtime);
$start_time = strtotime($time[0]);
$end_time = strtotime($time[1]) - 1;
if ($start_time > $end_time) {
$this->err('开始时间不能大于结束时间');
}
$whe1[] = ['addtime', 'BETWEEN', array($start_time, $end_time)];
$whe2[] = ['addtime', 'BETWEEN', array($start_time, $end_time)];
}
$list1 = OrderModel::where($whe1)->select();
$count = count($list1);
// $price = $list1 -> count('price');
$numbers = $list1->toArray();
$count_price = array_sum(array_map(function ($price) {
return $price['price']; //支付金额 2
}, $numbers));
$count_yue = array_sum(array_map(function ($price) {
return $price['use_money']; //用户余额 2
}, $numbers));
$use_integral = array_sum(array_map(function ($price) {
return $price['use_integral']; //潮币抵扣 2
}, $numbers));
$use_score = array_sum(array_map(function ($price) {
return $price['use_score']; //积分抵扣 2
}, $numbers));
$use_coupon = array_sum(array_map(function ($price) {
return $price['use_coupon']; //优惠卷抵扣 2
}, $numbers));
$order_zhe_total = array_sum(array_map(function ($price) {
return $price['order_zhe_total']; //折扣后总金额 2
}, $numbers));
$order_total = array_sum(array_map(function ($price) {
return $price['order_total']; //实际总金额 2
}, $numbers));
if ($use_integral > 0) {
$use_integral = $use_integral / 100;
}
// 订单价值
$whe2[] = ['goods_id', '=', $value['id']];
$orderList = OrderList::where($whe2)->select();
$orderArray = $orderList->toArray();
$count_OrderList = count($orderList);
$goodslist_price = 0;
if ($count_OrderList > 0) {
$goodslist_price = array_sum(array_map(function ($goodslist) {
return $goodslist['goodslist_money']; //实际总金额 2
}, $orderArray));
}
$value['order_count'] = $count;
$value['count_price'] = $count_price;
$value['order_total'] = $order_total;
$value['count_yue'] = $count_yue;
$value['count_use_integral'] = $use_integral;
$value['count_use_score'] = $use_score;
$value['order_zhe_total'] = $order_zhe_total;
$value['count_use_coupon'] = $use_coupon;
$value['count_heji'] = $count_price + $count_yue + $use_integral + $use_score + $use_coupon;
$value['count_heji_yanzheng'] = $count_OrderList * $value['price'];
$value['count_OrderList'] = $count_OrderList;
$goodslist_price1 = round($goodslist_price, 2);
$value['goodslist_price'] = $goodslist_price1;
$value['lirun'] = round($order_zhe_total - $goodslist_price1, 2);
$count_heji_yanzheng = $count_OrderList * $value['price'];
$value['liruns'] = round($count_heji_yanzheng - $goodslist_price1, 2);
if ($goodslist_price1 == 0) {
$value['lirulv'] = 0;
$value['lirulvs'] = 0;
} else {
$value['lirulv'] = round(($order_zhe_total - $goodslist_price1) / $goodslist_price1 * 100, 2);
$value['lirulvs'] = round(($count_heji_yanzheng - $goodslist_price1) / $goodslist_price1 * 100, 2);
}
$sum_dingdan += $count_OrderList * $value['price'];
$sum_shiji += $order_zhe_total;
$sum_chuhuo += $goodslist_price1;
$sum_shijilirun += $value['lirun'];
$sum_dingdanlirun += $value['liruns'];
} }
View::assign('list', $data['list']); $goodslistMoney = OrderList::where($whe2)->sum('goodslist_money');
View::assign('count', $data['count']);
View::assign('page', $data['page']);
// View::assign('sum_dingdan', $sum_dingdan);
// View::assign('sum_shiji', $sum_shiji);
// View::assign('sum_chuhuo', $sum_chuhuo);
// View::assign('sum_shijilirun', $sum_shijilirun);
// View::assign('sum_dingdanlirun', $sum_dingdanlirun);
return View::fetch("Statistics/profit");
// 计算利润
$lirun = round($orderStats->order_zhe_total - $goodslistMoney, 2);
$liruns = round(($orderStats->count_price + $orderStats->count_yue + $orderStats->use_integral + $orderStats->use_score + $orderStats->use_coupon) - $goodslistMoney, 2);
return [
'order_count' => round($orderStats->count, 2),
'count_price' =>round($orderStats->count_price, 2),
'order_total' =>round( $orderStats->order_total, 2),
'count_yue' => round($orderStats->count_yue, 2),
'count_use_integral' => round($orderStats->use_integral, 2),
'count_use_score' => round($orderStats->use_score, 2),
'count_use_coupon' => round($orderStats->use_coupon, 2),
'count_heji' => round($orderStats->count_price + $orderStats->count_yue + $orderStats->use_integral + $orderStats->use_score + $orderStats->use_coupon, 2),
'order_zhe_total'=>round($orderStats->order_zhe_total, 2),
// 订单列表数量
'count_OrderList' => OrderList::where($whe2)->count(), // ✅ 添加这一行
// 订单总价验证
// 'count_heji_yanzheng' => $count_OrderList * $value['price'], // ✅ 使用 count_OrderList
'goodslist_price' => round($goodslistMoney, 2),
'lirun' => $lirun,
'liruns' => $liruns,
'lirulv' => $goodslistMoney == 0 ? 0 : round(($lirun / $goodslistMoney) * 100, 2),
'lirulvs' => $goodslistMoney == 0 ? 0 : round(($liruns / $goodslistMoney) * 100, 2),
];
} }
/** /**
* 数据统计 * 数据统计
* @param \think\Request $request * @param \think\Request $request

View File

@ -658,7 +658,7 @@ class User extends Base
if ($user_id) { if ($user_id) {
$user_id = $user_id - 1260; $user_id = $user_id - 1260;
$whe[] = ['pid', '=', $user_id]; $whe[] = ['pid', '=', $user_id];
} }
//select * from ( SELECT pid,count(1) n FROM xinglanmh_shequt_test.`user` where pid>0 group by pid ) t where n>1 order by n desc LIMIT 10 //select * from ( SELECT pid,count(1) n FROM xinglanmh_shequt_test.`user` where pid>0 group by pid ) t where n>1 order by n desc LIMIT 10
$list = UserModel::where($whe)-> $list = UserModel::where($whe)->
where('pid', '>', 0) where('pid', '>', 0)
@ -680,8 +680,11 @@ class User extends Base
$user_info = UserModel::field('nickname,headimg,mobile')->where('id', '=', $pid)->find(); $user_info = UserModel::field('nickname,headimg,mobile')->where('id', '=', $pid)->find();
if ($user_info != null) { if ($user_info != null) {
$user_list = UserModel::field('id,nickname,addtime')->where($whe1)->where('pid', '=', $pid)->order('addtime desc')->select(); $user_list = UserModel::field('id,nickname,addtime,mobile')->where($whe1)->where('pid', '=', $pid)->order('addtime desc')->select();
$u_index = 1; $u_index = 1;
$u_price = 0;
$u_order = 0;
$u_mobile_count = 0;
foreach ($user_list as $user_item) { foreach ($user_list as $user_item) {
$user_item['adddate'] = date('Y-m-d H:i:s', $user_item['addtime']); $user_item['adddate'] = date('Y-m-d H:i:s', $user_item['addtime']);
$user_item['index'] = $u_index; $user_item['index'] = $u_index;
@ -691,6 +694,11 @@ class User extends Base
$user_item['user_item_order'] = $user_item_order; $user_item['user_item_order'] = $user_item_order;
$user_item['user_item_price'] = $user_item_price; $user_item['user_item_price'] = $user_item_price;
$u_index++; $u_index++;
$u_price += $user_item_price;
$u_order += $user_item_order;
if ($user_item['mobile'] && $user_item['mobile'] != "") {
$u_mobile_count++;
}
} }
$data[] = [ $data[] = [
'index' => $index, 'index' => $index,
@ -698,7 +706,10 @@ class User extends Base
'invitenumber' => $item['n'], 'invitenumber' => $item['n'],
'nickname' => $user_info['nickname'], 'nickname' => $user_info['nickname'],
'headimg' => imageUrl($user_info['headimg']), 'headimg' => imageUrl($user_info['headimg']),
'info' => $user_list 'info' => $user_list,
'sum_order' => $u_order,
'sum_price' => $u_price,
'count_mobile' => $u_mobile_count,
]; ];
$index++; $index++;
} }

View File

@ -161,7 +161,7 @@
优惠卷抵扣:{$vo['count_use_coupon']} 优惠卷抵扣:{$vo['count_use_coupon']}
</td> </td>
<th> <th>
订单收入:{$vo['count_heji_yanzheng']}</br> 订单收入:{$vo['order_total']}</br>
实际收入:<span style="color: red;">{$vo['order_zhe_total']}</span>(去除折扣)</br> 实际收入:<span style="color: red;">{$vo['order_zhe_total']}</span>(去除折扣)</br>
收入核算1:{$vo['order_total']}(订单数量*价格)</br> 收入核算1:{$vo['order_total']}(订单数量*价格)</br>
收入核算2:{$vo['count_heji']}(微信+余额+各种抵扣)</br> 收入核算2:{$vo['count_heji']}(微信+余额+各种抵扣)</br>

View File

@ -39,7 +39,12 @@
<th lay-data="{field:'nickname', width:150}">用户昵称/id</th> <th lay-data="{field:'nickname', width:150}">用户昵称/id</th>
<th lay-data="{field:'headimg', width:150}">用户头像</th> <th lay-data="{field:'headimg', width:150}">用户头像</th>
<th lay-data="{field:'invitenumber', width:150}">邀请人数</th> <th lay-data="{field:'invitenumber', width:150}">邀请人数</th>
<th lay-data="{field:'info'}">邀请信息</th> <th lay-data="{field:'info'}">邀请信息</th>
<th lay-data="{field:'invitenumber', width:150}">绑定手机数</th>
<th lay-data="{field:'invitenumber', width:150}">消费人数</th>
<th lay-data="{field:'invitenumber', width:150}">消费总金额</th>
<th lay-data="{field:'xxx', width:150}">操作</th> <th lay-data="{field:'xxx', width:150}">操作</th>
</tr> </tr>
</thead> </thead>
@ -52,6 +57,7 @@
<img style="width: 48px;" src="{$vo['headimg']}" /> <img style="width: 48px;" src="{$vo['headimg']}" />
</td> </td>
<td>{$vo['invitenumber']}人</td> <td>{$vo['invitenumber']}人</td>
<td class="aaaa"> <td class="aaaa">
<div style="height:150px;overflow:auto;"> <div style="height:150px;overflow:auto;">
<ui> <ui>
@ -69,6 +75,9 @@
</div> </div>
<!-- {$vo['info']} --> <!-- {$vo['info']} -->
</td> </td>
<td>{$vo['count_mobile']}人</td>
<td>{$vo['sum_order']}人</td>
<td>{$vo['sum_price']}人</td>
<td> <td>
<!-- 查看详细信息 --> <!-- 查看详细信息 -->
@ -133,6 +142,19 @@
/* width: 157px; */ /* width: 157px; */
height: 35px !important; height: 35px !important;
} }
.laytable-cell-1-0-6 {
/* width: 157px; */
height: 35px !important;
}
.laytable-cell-1-0-7 {
/* width: 157px; */
height: 35px !important;
}
.laytable-cell-1-0-8 {
/* width: 157px; */
height: 35px !important;
}
</style> </style>
<script type="text/javascript"> <script type="text/javascript">
layui.use(['layer', 'laydate', 'table'], function () { layui.use(['layer', 'laydate', 'table'], function () {