diff --git a/app/admin/controller/Statistics.php b/app/admin/controller/Statistics.php index cf084ec..98d86ee 100755 --- a/app/admin/controller/Statistics.php +++ b/app/admin/controller/Statistics.php @@ -664,9 +664,11 @@ class Statistics extends Base } // 解析时间范围 + $hasTimeRange = false; $startTime = 0; - $endTime = time(); - if ($addtime) { + $endTime = 0; + if (!empty($addtime)) { + $hasTimeRange = true; $times = explode(' - ', $addtime); $startTime = strtotime($times[0]); $endTime = strtotime($times[1]); @@ -675,31 +677,99 @@ class Statistics extends Base // 获取测试用户ID $testUsers = User::where('istest', '>', 0)->column('id'); $testUserIds = empty($testUsers) ? [0] : $testUsers; - $testUserIdsStr = implode(',', $testUserIds); - // 查询单个盒子的统计数据 - $data = Db::name('goods')->alias('goods') - ->field([ - 'goods.id', - '(SELECT COALESCE(sum(use_money), 0) + COALESCE(sum(price), 0) FROM `order` WHERE status=1 AND (price>0 OR use_money>0) AND pay_time > ' . $startTime . ' and pay_time < ' . $endTime . ' AND goods_id=goods.id AND status=1 AND user_id NOT IN (' . $testUserIdsStr . ')) AS use_money', - '(SELECT COALESCE(sum(goodslist_money), 0) FROM `order_list` WHERE goods_id=goods.id AND user_id NOT IN (' . $testUserIdsStr . ') AND addtime > ' . $startTime . ' and addtime < ' . $endTime . ') AS sc_money', - '(SELECT COALESCE(sum(goodslist_money), 0) FROM `order_list` WHERE goods_id=goods.id AND LENGTH(recovery_num)>0 AND user_id NOT IN (' . $testUserIdsStr . ') AND addtime > ' . $startTime . ' and addtime < ' . $endTime . ') AS re_money', - '(SELECT COALESCE(sum(goodslist_money), 0) FROM `order_list` WHERE goods_id=goods.id AND LENGTH(send_num)>0 AND user_id NOT IN (' . $testUserIdsStr . ') AND addtime > ' . $startTime . ' and addtime < ' . $endTime . ') AS fh_money', - '(SELECT count(1) FROM `order_list` WHERE goods_id=goods.id AND user_id NOT IN (' . $testUserIdsStr . ') AND addtime > ' . $startTime . ' and addtime < ' . $endTime . ' and parent_goods_list_id=0 ) AS cj_count' - ]) - ->where('goods.id', '=', $goodId) + // 获取盒子基本信息 + $goodsInfo = Db::name('goods') + ->where('id', '=', $goodId) + ->field('id') ->find(); - - if (!$data) { + + if (!$goodsInfo) { return json(['code' => 1, 'msg' => '盒子不存在']); } - - // 处理数值,确保为浮点型 - $data['use_money'] = floatval($data['use_money']); - $data['sc_money'] = floatval($data['sc_money']); - $data['re_money'] = floatval($data['re_money']); - $data['fh_money'] = floatval($data['fh_money']); + // 查询1:获取充值金额和余额消费总和 + $orderQuery = Db::name('order') + ->where('status', '=', 1) + ->where(function ($query) { + $query->where('price', '>', 0) + ->whereOr('use_money', '>', 0); + }) + ->where('goods_id', '=', $goodId) + ->where('user_id', 'not in', $testUserIds); + + // 只有在有时间范围时才添加时间条件 + if ($hasTimeRange) { + $orderQuery->where('pay_time', '>', $startTime) + ->where('pay_time', '<', $endTime); + } + + // 分别获取price和use_money的总和后相加 + $priceSum = $orderQuery->sum('price'); + $useMoneySum = $orderQuery->sum('use_money'); + $useMoney = floatval($priceSum) + floatval($useMoneySum); + + // 查询2:获取出货成本 + $scMoneyQuery = Db::name('order_list') + ->where('goods_id', '=', $goodId) + ->where('user_id', 'not in', $testUserIds); + + if ($hasTimeRange) { + $scMoneyQuery->where('addtime', '>', $startTime) + ->where('addtime', '<', $endTime); + } + + $scMoney = $scMoneyQuery->sum('goodslist_money'); + + // 查询3:获取兑换成本 + $reMoneyQuery = Db::name('order_list') + ->where('goods_id', '=', $goodId) + ->where('LENGTH(recovery_num)', '>', 0) + ->where('user_id', 'not in', $testUserIds); + + if ($hasTimeRange) { + $reMoneyQuery->where('addtime', '>', $startTime) + ->where('addtime', '<', $endTime); + } + + $reMoney = $reMoneyQuery->sum('goodslist_money'); + + // 查询4:获取发货成本 + $fhMoneyQuery = Db::name('order_list') + ->where('goods_id', '=', $goodId) + ->where('LENGTH(send_num)', '>', 0) + ->where('user_id', 'not in', $testUserIds); + + if ($hasTimeRange) { + $fhMoneyQuery->where('addtime', '>', $startTime) + ->where('addtime', '<', $endTime); + } + + $fhMoney = $fhMoneyQuery->sum('goodslist_money'); + + // 查询5:获取抽奖次数 + $cjCountQuery = Db::name('order_list') + ->where('goods_id', '=', $goodId) + ->where('user_id', 'not in', $testUserIds) + ->where('parent_goods_list_id', '=', 0); + + if ($hasTimeRange) { + $cjCountQuery->where('addtime', '>', $startTime) + ->where('addtime', '<', $endTime); + } + + $cjCount = $cjCountQuery->count(); + + // 组合数据 + $data = [ + 'id' => $goodId, + 'use_money' => floatval($useMoney), + 'sc_money' => floatval($scMoney), + 're_money' => floatval($reMoney), + 'fh_money' => floatval($fhMoney), + 'cj_count' => intval($cjCount) + ]; + // 计算利润和利润率 $data['profit'] = $data['use_money'] - $data['sc_money']; $data['profit_rate'] = $data['use_money'] > 0 ? round(($data['profit'] / $data['use_money']) * 100, 2) : 0; @@ -708,4 +778,140 @@ class Statistics extends Base return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]); } + /** + * 获取所有盒子的统计数据汇总 + * @param Request $request + * @return \think\response\Json + */ + public function getSummaryStatistics(Request $request) + { + $goodId = trim(input('get.goodId')); + $title = trim(input('get.title')); + $status = trim(input('get.status')); + $type = trim(input('get.type')); + $addtime = trim(input('get.addtime')); + + // 解析时间范围 + $hasTimeRange = false; + $startTime = 0; + $endTime = 0; + if (!empty($addtime)) { + $hasTimeRange = true; + $times = explode(' - ', $addtime); + $startTime = strtotime($times[0]); + $endTime = strtotime($times[1]); + } + + // 构建查询条件 + $goodsWhere = [['delete_time', '=', null]]; + if ($goodId) { + $goodsWhere[] = ['id', '=', $goodId]; + } + if ($title) { + $goodsWhere[] = ['title', 'like', '%' . $title . '%']; + } + if ($status) { + $goodsWhere[] = ['status', '=', $status]; + } + if ($type) { + $goodsWhere[] = ['type', '=', $type]; + } + + // 获取符合条件的盒子ID列表 + $goodsIds = Db::name('goods') + ->where($goodsWhere) + ->column('id'); + + if (empty($goodsIds)) { + return json([ + 'code' => 0, + 'msg' => '没有找到符合条件的盒子', + 'data' => [ + 'totalIncome' => 0, + 'totalCost' => 0, + 'totalProfit' => 0, + 'totalReMoney' => 0, + 'totalFhMoney' => 0 + ] + ]); + } + + // 获取测试用户ID + $testUsers = User::where('istest', '>', 0)->column('id'); + $testUserIds = empty($testUsers) ? [0] : $testUsers; + + // 查询1:获取充值金额和余额消费总和 + $orderQuery = Db::name('order') + ->where('status', '=', 1) + ->where(function ($query) { + $query->where('price', '>', 0) + ->whereOr('use_money', '>', 0); + }) + ->where('goods_id', 'in', $goodsIds) + ->where('user_id', 'not in', $testUserIds); + + // 只有在有时间范围时才添加时间条件 + if ($hasTimeRange) { + $orderQuery->where('pay_time', '>', $startTime) + ->where('pay_time', '<', $endTime); + } + + // 分别获取price和use_money的总和后相加 + $priceSum = $orderQuery->sum('price'); + $useMoneySum = $orderQuery->sum('use_money'); + $totalIncome = floatval($priceSum) + floatval($useMoneySum); + + // 查询2:获取出货成本 + $scMoneyQuery = Db::name('order_list') + ->where('goods_id', 'in', $goodsIds) + ->where('user_id', 'not in', $testUserIds); + + if ($hasTimeRange) { + $scMoneyQuery->where('addtime', '>', $startTime) + ->where('addtime', '<', $endTime); + } + + $totalCost = floatval($scMoneyQuery->sum('goodslist_money')); + + // 查询3:获取兑换成本 + $reMoneyQuery = Db::name('order_list') + ->where('goods_id', 'in', $goodsIds) + ->where('LENGTH(recovery_num)', '>', 0) + ->where('user_id', 'not in', $testUserIds); + + if ($hasTimeRange) { + $reMoneyQuery->where('addtime', '>', $startTime) + ->where('addtime', '<', $endTime); + } + + $totalReMoney = floatval($reMoneyQuery->sum('goodslist_money')); + + // 查询4:获取发货成本 + $fhMoneyQuery = Db::name('order_list') + ->where('goods_id', 'in', $goodsIds) + ->where('LENGTH(send_num)', '>', 0) + ->where('user_id', 'not in', $testUserIds); + + if ($hasTimeRange) { + $fhMoneyQuery->where('addtime', '>', $startTime) + ->where('addtime', '<', $endTime); + } + + $totalFhMoney = floatval($fhMoneyQuery->sum('goodslist_money')); + + // 计算总利润 + $totalProfit = $totalIncome - $totalCost; + + // 返回汇总数据 + $data = [ + 'totalIncome' => $totalIncome, + 'totalCost' => $totalCost, + 'totalProfit' => $totalProfit, + 'totalReMoney' => $totalReMoney, + 'totalFhMoney' => $totalFhMoney + ]; + + return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]); + } + } diff --git a/app/admin/route/app.php b/app/admin/route/app.php index 46d2aa6..4c5375a 100755 --- a/app/admin/route/app.php +++ b/app/admin/route/app.php @@ -426,4 +426,6 @@ Route::rule('statistics_order', 'Statistics/orderList', 'GET'); Route::rule('statistics_orderList', 'Statistics/goodsList', 'GET'); Route::rule('statistics_exchangeList', 'Statistics/exchangeList', 'GET'); Route::rule('statistics_shipmentList', 'Statistics/shipmentList', 'GET'); -Route::rule('statistics_productsOverview', 'Statistics/productsOverview', 'GET'); \ No newline at end of file +Route::rule('statistics_productsOverview', 'Statistics/productsOverview', 'GET'); +Route::rule('statistics_getBoxStatistics', 'Statistics/getBoxStatistics', 'GET'); +Route::rule('statistics_getSummaryStatistics', 'Statistics/getSummaryStatistics', 'GET'); \ No newline at end of file diff --git a/app/admin/view/Statistics/profit.html b/app/admin/view/Statistics/profit.html index eb286ae..ceb22a3 100755 --- a/app/admin/view/Statistics/profit.html +++ b/app/admin/view/Statistics/profit.html @@ -1,19 +1,21 @@ {include file="Public:header3"/} -
+
-
+
- +
- +
@@ -35,7 +37,8 @@
- +
@@ -52,7 +55,11 @@
-
总收入
+
+ 总收入 + +
¥ 0.00
@@ -60,7 +67,11 @@
-
总出货价值
+
+ 总出货价值 + +
¥ 0.00
@@ -68,7 +79,11 @@
-
总利润
+
+ 总利润 + +
¥ 0.00
@@ -78,28 +93,28 @@
总兑换/发货价值
- ¥ 0.00 / + ¥ 0.00 / ¥ 0.00
- +
- + - + - + + \ No newline at end of file