<?php namespace app\statistics\controller; use app\admin\logic\statistic\TradeStatisticLogic; use app\common\controller\BaseLikeController; use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_product\StoreProduct; use app\common\model\system_store\SystemStore; use think\facade\Db; class DemoController extends BaseLikeController { public function index() { $time = $this->request->get('date', date('Y-m-d')); $store_id = $this->request->get('store_id', 0); $where = []; if ($store_id) { $where['store_id'] = $store_id; $name = SystemStore::where('id', $store_id)->value('name'); $title = $name . '农(特)产品交易大数据'; } else { $title = '泸县农(特)产品交易大数据'; } $res = Db::name('demo_order_money')->where($where)->whereDay('dates', $time)->sum('money'); $res = $res ? $res : 0; return $this->success('ok', ['dayPayPrice' => $res, 'title' => $title]); } /** * 门店统计 */ public function sotre_count() { $data=SystemStore::where('is_show', 1)->field('id,introduction street_name')->select()->each(function ($item) use (&$data) { $item['merchant_count'] = 1; return $item; }); return $this->success('ok', ['merchatCountList' => $data, 'merchantTotalCount' => count($data)]); } /** * 地方商品数量统计 */ public function product_count_sotre_count() { $time = $this->request->get('date', date('Y-m-d')); $data=SystemStore::where('is_show', 1)->field('id,introduction street_name')->select() ->each(function (&$item) { $item['product_count'] = Db::connect('demo')->name('store_branch_product')->where('store_id', $item['id'])->count(); }); //总商品 // $townProductCount = StoreProduct::where('create_time','<=',strtotime($time))->count(); $townProductCount = Db::connect('demo')->name('store_product')->where('create_time','<=',strtotime($time))->count(); //商品销量 $product_count = Db::connect('demo')->name('store_branch_product')->where('create_time','<=',strtotime($time))->group('product_id')->order('total_sales desc')->limit(20)->field('image,product_id,store_name,sum(sales) as total_sales')->select(); //总销量 $productRankingTotal = 0; foreach ($product_count as $item) { $productRankingTotal += $item['total_sales']; } return $this->success('ok', ['townProductCountList' => $data, 'productRankingList' => $product_count, 'townProductCount' => $townProductCount, 'productRankingTotal' => $productRankingTotal]); } public function bottomLeft() { $time = $this->request->get('date', date('Y-m')); $store_id = $this->request->get('store_id', 0); $where = []; if ($store_id) { $where['store_id'] = $store_id; } $arr = SystemStore::where('is_show', 1)->field('id,abbreviation street_name')->select() ->each(function ($item) use ($time) { $day = Db::name('demo_order_money')->where('store_id', $item['id'])->whereMonth('dates', $time)->sum('money'); $yester = Db::name('demo_order_money')->where('store_id', $item['id'])->whereMonth('dates', date('Y-m', strtotime('-1 month')))->sum('money'); $item['monthDayOrder'] = $day ?? 0; $item['monthYesterOrder'] = $yester ?? 0; return $item; })->toArray(); return $this->data($arr); } public function store_order_day_two() { $time = $this->request->get('date', date('Y-m-d')); $arr = SystemStore::where('is_show', 1)->field('id,abbreviation street_name')->select() ->each(function ($item) use ($time) { $today_order_amount = Db::name('demo_order_money')->where('store_id', $item['id'])->whereDay('dates', $time)->sum('money'); $yesterday_order_amount = Db::name('demo_order_money')->where('store_id', $item['id'])->whereDay('dates', date('Y-m-d', strtotime($time . '-1 day')))->sum('money'); $item['today_order_amount'] = $today_order_amount; $item['yesterday_order_amount'] = $yesterday_order_amount; return $item; }); return $this->success('ok', $arr?->toArray()); } /** * 成交用户数据 */ public function user_trade_count() { $store_id = $this->request->get('store_id', 0); $date = $this->request->get('date', ''); $a = new TradeStatisticLogic(); if ($date != '') { $time['start_time'] = $date; $time['end_time'] = date('Y-m-d', strtotime($date . '-1 month')); $aa = $a->TimeConvert($time); $time['start_time'] = $aa['end_time']; $time['end_time'] = $aa['start_time']; $time['days'] = 30; } else { $time['start_time'] = date('Y-01-01 00:00:00', time()); $time['end_time'] = date('Y-12-t 23:59:59', time()); $time['days'] = 365; } $where = []; if ($store_id) { $where['store_id'] = $store_id; } $left = $this->getCurveData($where, $time, 'sum(money)', 'dates'); $totalMoney = $a->trendYdata($left, $time); $datas['name'] = "门店订单金额曲线"; $datas['x'] = $totalMoney['x']; $datas['series'][0]['money'] = 0; $datas['series'][0]['value'] = array_values($totalMoney['y']); return $this->data($datas); } public function getCurveData($where, $time, $str, string $group = 'dates') { return Db::name('demo_order_money')->where($where) ->when(isset($time), function ($query) use ($time, $str, $group) { $query->whereBetweenTime($group, $time['start_time'], $time['end_time']); if ($time['days'] == 1) { $timeUinx = "%H"; } elseif ($time['days'] == 30) { $timeUinx = "%Y-%m-%d"; } elseif ($time['days'] == 365) { $timeUinx = "%Y-%m"; } elseif ($time['days'] > 1 && $time['days'] < 30) { $timeUinx = "%Y-%m-%d"; } elseif ($time['days'] > 30 && $time['days'] < 365) { $timeUinx = "%Y-%m"; } else { $timeUinx = "%Y-%m"; } $query->field("$str as number,DATE_FORMAT($group, '$timeUinx') as time"); $query->group("DATE_FORMAT($group, '$timeUinx')"); }) ->order("$group ASC")->select()->toArray(); } /** * 当日订单金额 */ public function street_currday_order_count() { $time = $this->request->get('date', date('Y-m-d')); $store_id = $this->request->get('store_id', 0); $where = []; if ($store_id) { $where['store_id'] = $store_id; } $startTime = strtotime($time . ' 00:00:00'); // 当天的开始时间戳 $endTime = strtotime($time . ' 23:59:59'); // 当天的结束时间戳 $interval = 4 * 60 * 60; // 4小时的秒数 $data = []; for ($time = $startTime; $time < $endTime; $time += $interval) { $endTimeSegment = $time + $interval; $startTimeSegment = date('Y-m-d H:i:s', $time); $yesterendTimeSegment = date('Y-m-d H:i:s', $endTimeSegment - 86400); $endTimeSegment = date('Y-m-d H:i:s', $endTimeSegment); $yesterstartTimeSegment = date('Y-m-d H:i:s', $time - 86400); // 统计当前时间段的订单 $todayAmount = Db::name('demo_order_money')->where($where) ->whereBetween('dates', [strtotime($startTimeSegment), strtotime($endTimeSegment)]) ->sum('money'); $yesterdayAmount = Db::name('demo_order_money')->where($where) ->whereBetween('dates', [strtotime($yesterstartTimeSegment), strtotime($yesterendTimeSegment)]) ->sum('money'); $data[] = [ 'todayAmount' => $todayAmount, 'yesterdayAmount' => $yesterdayAmount, ]; } return $this->success('ok', $data); } }