diff --git a/app/admin/controller/Statistics.php b/app/admin/controller/Statistics.php index 35c18a0..cf084ec 100755 --- a/app/admin/controller/Statistics.php +++ b/app/admin/controller/Statistics.php @@ -43,22 +43,39 @@ class Statistics extends Base if ($type) { $where[] = ['type', '=', $type]; } + return View::fetch("Statistics/profit"); + } +/** + * 盒子利润统计数据接口(用于前端分离模式) + * @param Request $request + * @return \think\response\Json + */ + public function profitData(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')); + $page = intval(input('page', 1)); + $limit = intval(input('limit', 20)); - // 解析时间范围 - $startTime = 0; - $endTime = time(); - if ($addtime) { - $times = explode(' - ', $addtime); - $startTime = strtotime($times[0]); - $endTime = strtotime($times[1]); + // 构建查询条件 + $where = [['delete_time', '=', null]]; + if ($goodId) { + $where[] = ['id', '=', $goodId]; + } + if ($title) { + $where[] = ['title', 'like', '%' . $title . '%']; + } + if ($status) { + $where[] = ['status', '=', $status]; + } + if ($type) { + $where[] = ['type', '=', $type]; } - // 获取测试用户ID - $testUsers = User::where('istest', '>', 0)->column('id'); - $testUserIds = empty($testUsers) ? [0] : $testUsers; - $testUserIdsStr = implode(',', $testUserIds); - - // 构建SQL查询 + // 构建SQL查询,只返回基本信息 $query = Db::name('goods')->alias('goods') ->field([ 'goods.id', @@ -67,54 +84,70 @@ class Statistics extends Base 'goods.price', 'goods.stock', 'goods.status', - 'goods.type', - '(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' + 'goods.type' ]) ->where($where) ->order(['goods.id' => 'desc']); - // 获取列表数据 - $list = $query->select()->toArray(); + // 获取总数量 + $total = $query->count(); + + // 分页 + $list = $query->page($page, $limit)->select()->toArray(); - // 计算总金额 - $totalIncome = 0; - $totalCost = 0; - $totalProfit = 0; - $totalReMoney = 0; - $totalFhMoney = 0; - - foreach ($list as &$item) { - // 计算单个盒子的利润和利润率 - $item['profit'] = $item['use_money'] - $item['sc_money']; - $item['profit_rate'] = $item['use_money'] > 0 ? round(($item['profit'] / $item['use_money']) * 100, 2) : 0; - $item['is_negative'] = $item['profit'] < 0; - - // 计算总金额 - $totalIncome += $item['use_money']; - $totalCost += $item['sc_money']; - $totalReMoney += $item['re_money']; - $totalFhMoney += $item['fh_money']; + // 获取盒子类型名称 + $typesList = $this->getGoodsTypes(); + $typesMap = []; + foreach ($typesList as $item) { + $typesMap[$item['value']] = $item['fl_name']; } - $totalProfit = $totalIncome - $totalCost; + // 初始化统计数据为0 + foreach ($list as &$item) { + // 添加类型名称 + $item['type_name'] = isset($typesMap[$item['type']]) ? $typesMap[$item['type']] : '未知类型'; + + // 初始化统计字段为0,后续会异步加载 + $item['use_money'] = 0; + $item['sc_money'] = 0; + $item['re_money'] = 0; + $item['fh_money'] = 0; + $item['cj_count'] = 0; + $item['profit'] = 0; + $item['profit_rate'] = 0; + $item['is_negative'] = false; + $item['loaded'] = false; // 标记是否已加载统计数据 + } - // 传递数据给视图 - View::assign([ - 'list' => $list, - 'totalIncome' => $totalIncome, - 'totalCost' => $totalCost, - 'totalProfit' => $totalProfit, - 'totalReMoney' => $totalReMoney, - 'totalFhMoney' => $totalFhMoney, + // 返回JSON数据 + return json([ + 'code' => 0, + 'msg' => '获取数据成功', + 'count' => $total, + 'data' => $list, + 'summary' => [ + 'totalIncome' => 0, + 'totalCost' => 0, + 'totalProfit' => 0, + 'totalReMoney' => 0, + 'totalFhMoney' => 0, + ] ]); - - return View::fetch("Statistics/profit"); } + /** + * 获取盒子类型列表(用于API调用) + */ + private function getGoodsTypes() + { + $types = Db::name('goods_type') + ->field('value, fl_name, remark') + ->where('is_fenlei', 1) + ->order('sort_order asc') + ->select() + ->toArray(); + return $types; + } /** * 解析时间范围 */ @@ -616,4 +649,63 @@ class Statistics extends Base return View::fetch('Statistics/productsOverview'); } + /** + * 获取单个盒子的统计数据 + * @param Request $request + * @return \think\response\Json + */ + public function getBoxStatistics(Request $request) + { + $goodId = intval(input('get.goods_id', 0)); + $addtime = trim(input('get.addtime', '')); + + if ($goodId <= 0) { + return json(['code' => 1, 'msg' => '参数错误']); + } + + // 解析时间范围 + $startTime = 0; + $endTime = time(); + if ($addtime) { + $times = explode(' - ', $addtime); + $startTime = strtotime($times[0]); + $endTime = strtotime($times[1]); + } + + // 获取测试用户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) + ->find(); + + if (!$data) { + 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']); + + // 计算利润和利润率 + $data['profit'] = $data['use_money'] - $data['sc_money']; + $data['profit_rate'] = $data['use_money'] > 0 ? round(($data['profit'] / $data['use_money']) * 100, 2) : 0; + $data['is_negative'] = $data['profit'] < 0; + + return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]); + } + } diff --git a/app/admin/route/app.php b/app/admin/route/app.php index bdc14d9..46d2aa6 100755 --- a/app/admin/route/app.php +++ b/app/admin/route/app.php @@ -415,4 +415,15 @@ Route::get('sign_config_coupons', 'SignConfig/getCoupons'); #UserRank.php用户排行榜 #============================ Route::rule('user_rank', 'UserRank/index', 'GET'); -Route::rule('user_rank_data', 'UserRank/getRankData', 'GET'); \ No newline at end of file +Route::rule('user_rank_data', 'UserRank/getRankData', 'GET'); + +#============================ +#Statistics.php统计管理 +#============================ +Route::rule('statistics_profit', 'Statistics/profit', 'GET'); +Route::rule('statistics/profitData', 'Statistics/profitData', 'GET'); +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 diff --git a/app/admin/view/Statistics/profit.html b/app/admin/view/Statistics/profit.html index 727f827..eb286ae 100755 --- a/app/admin/view/Statistics/profit.html +++ b/app/admin/view/Statistics/profit.html @@ -4,27 +4,25 @@