success('生成成功'); } catch (DbException $e) { BaseModel::rollbackTrans(); return app('json')->fail($e->getMessage()); } } /** * 系统报表 * @return mixed * @throws Exception */ public function systemReportList() { list($page, $limit) = UtilService::getMore([['page', 1], ['limit', 10]], $this->request, true); //今日系统报表 $today = [ 'today_reg' => User::whereDay('add_time', 'today')->count(), 'today_visit' => User::whereDay('last_time', 'today')->count(), 'today_order' => StoreOrder::whereDay('add_time', 'today')->count(), 'today_order_money' => StoreOrder::where('paid', 1) ->where('is_del', 0) ->where('refund_status', 0) ->whereDay('pay_time', 'today') ->sum('pay_price') ]; $pass_list = SystemDailyReport::order('report_date', 'desc')->page($page, $limit)->select(); if (count($pass_list) > 0) { $pass_list->toArray(); } else { $pass_list = []; } return app('json')->success('ok', compact('today', 'pass_list')); } /** * 商户报表 * @return mixed * @throws Exception */ public function merchantReportList() { list($page, $limit, $mer_id, $date) = UtilService::getMore([ ['page', 1], ['limit', 10], ['mer_id', ''], ['date', ''] ], $this->request, true); if ($mer_id) { if (!Merchant::be($mer_id)) { return app('json')->fail('无此商户'); } //今日系统报表 $today = [ 'today_reg' => User::merSet($mer_id)->whereDay('add_time', 'today')->count(), 'today_visit' => User::merSet($mer_id)->whereDay('last_time', 'today')->count(), 'today_order' => StoreOrder::merSet($mer_id)->whereDay('add_time', 'today')->count(), 'today_order_money' => StoreOrder::where('mer_id', $mer_id) ->where('paid', 1) ->where('is_del', 0) ->where('refund_status', 0) ->whereDay('pay_time', 'today') ->sum('pay_price') ]; } $model = MerchantDailyReport::order('report_date', 'desc'); if ($mer_id) { $model->where('mer_id', $mer_id); } if ($date) { list($startTime, $endTime) = explode('-', $date); $model = $model->where('report_date', '>=', $startTime); $model = $model->where('report_date', '<=', $endTime); } $pass_list = $model->page($page, $limit)->select(); if (count($pass_list) > 0) { $pass_list->toArray(); } else { $pass_list = []; } return app('json')->success('ok', isset($today) ? compact('today', 'pass_list') : ['pass_list' => $pass_list]); } /** * 商户排行 * @param $type * @param $sort * @return mixed * @throws Exception */ public function merchantRank($type, $sort) { if (!in_array(strtolower($type), ['sum_reg', 'sum_visit', 'sum_order', 'sum_order_money'])) { return app('json')->fail('查询的排行键值有误'); } if (!in_array(strtolower($sort), ['desc', 'asc'])) { return app('json')->fail('查询的排行方式有误'); } list($page, $limit) = UtilService::getMore([['page', 1], ['limit', 10]], $this->request, true); $report = MerchantDailyReport::alias('r')->group('mer_id') ->field('r.mer_id, m.name, m.logo, SUM(r.today_reg) as sum_reg,SUM(r.today_visit) as sum_visit,SUM(r.today_order) as sum_order,SUM(r.today_order_money) as sum_order_money') ->join('merchant m', 'r.mer_id = m.id') ->order(strtolower($type), strtolower($sort)) ->page($page, $limit)->select(); foreach($report as $key => $value) { $value['id'] = $key + 1; } return app('json')->success('ok', $report ? $report->toArray() : []); } /** * 首页头部统计数据 * @return mixed */ public function homeStatics($mer_id) { //TODO 销售额 //今日销售额 $today_sales = StoreOrder::merSet($mer_id) ->where('paid', 1) ->where('is_del', 0) ->where('refund_status', 0) ->whereDay('pay_time') ->sum('pay_price'); //昨日销售额 $yesterday_sales = StoreOrder::merSet($mer_id) ->where('paid', 1) ->where('is_del', 0) ->where('refund_status', 0) ->whereDay('pay_time', 'yesterday') ->sum('pay_price'); //日同比 $sales_today_ratio = $this->growth($today_sales, $yesterday_sales); //周销售额 //本周 $this_week_sales = StoreOrder::merSet($mer_id) ->where('paid', 1) ->where('is_del', 0) ->where('refund_status', 0) ->whereWeek('pay_time') ->sum('pay_price'); //上周 $last_week_sales = StoreOrder::merSet($mer_id) ->where('paid', 1) ->where('is_del', 0) ->where('refund_status', 0) ->whereWeek('pay_time', 'last week') ->sum('pay_price'); //周同比 $sales_week_ratio = $this->growth($this_week_sales, $last_week_sales); //总销售额 $total_sales = StoreOrder::merSet($mer_id) ->where('paid', 1) ->where('is_del', 0) ->where('refund_status', 0) ->sum('pay_price'); $sales = [ 'today' => $today_sales, 'yesterday' => $yesterday_sales, 'today_ratio' => $sales_today_ratio, 'week' => $this_week_sales, 'last_week' => $last_week_sales, 'week_ratio' => $sales_week_ratio, 'total' => $total_sales . '元', 'date' => '昨日' ]; //TODO:用户访问量 //今日访问量 $today_visits = User::merSet($mer_id)->WhereDay('last_time')->count(); //昨日访问量 $yesterday_visits = User::merSet($mer_id)->whereDay('last_time', 'yesterday')->count(); //日同比 $visits_today_ratio = $this->growth($today_visits, $yesterday_visits); //本周访问量 $this_week_visits = User::merSet($mer_id)->WhereWeek('last_time')->count(); //上周访问量 $last_week_visits = User::merSet($mer_id)->WhereWeek('last_time', 'last week')->count(); //周同比 $visits_week_ratio = $this->growth($this_week_visits, $last_week_visits); //总访问量 $total_visits = User::merSet($mer_id)->count(); $visits = [ 'today' => $today_visits, 'yesterday' => $yesterday_visits, 'today_ratio' => $visits_today_ratio, 'week' => $this_week_visits, 'last_week' => $last_week_visits, 'week_ratio' => $visits_week_ratio, 'total' => $total_visits . 'Pv', 'date' => '昨日' ]; //TODO 订单量 //今日订单量 $today_order = StoreOrder::merSet($mer_id)->whereDay('add_time')->count(); //昨日订单量 $yesterday_order = StoreOrder::merSet($mer_id)->whereDay('add_time', 'yesterday')->count(); //订单日同比 $order_today_ratio = $this->growth($today_order, $yesterday_order); //本周订单量 $this_week_order = StoreOrder::merSet($mer_id)->whereWeek('add_time')->count(); //上周订单量 $last_week_order = StoreOrder::merSet($mer_id)->whereWeek('add_time', 'last week')->count(); //订单周同比 $order_week_ratio = $this->growth($this_week_order, $last_week_order); //总订单量 $total_order = StoreOrder::merSet($mer_id)->where('is_system_del', 0)->count(); $order = [ 'today' => $today_order, 'yesterday' => $yesterday_order, 'today_ratio' => $order_today_ratio, 'week' => $this_week_order, 'last_week' => $last_week_order, 'week_ratio' => $order_week_ratio, 'total' => $total_order . '单', 'date' => '昨日' ]; //TODO 用户 //今日新增用户 $today_user = User::merSet($mer_id)->whereDay('add_time')->count(); //昨日新增用户 $yesterday_user = User::merSet($mer_id)->whereDay('add_time', 'yesterday')->count(); //新增用户日同比 $user_today_ratio = $this->growth($today_user, $yesterday_user); //本周新增用户 $this_week_user = User::merSet($mer_id)->whereWeek('add_time')->count(); //上周新增用户 $last_week_user = User::merSet($mer_id)->whereWeek('add_time', 'last week')->count(); //新增用户周同比 $user_week_ratio = $this->growth($this_week_user, $last_week_user); //所有用户 $total_user = User::merSet($mer_id)->count(); $user = [ 'today' => $today_user, 'yesterday' => $yesterday_user, 'today_ratio' => $user_today_ratio, 'week' => $this_week_user, 'last_week' => $last_week_user, 'week_ratio' => $user_week_ratio, 'total' => $total_user . '人', 'date' => '昨日' ]; // $qrcode = MerchantMiniprogram::vaildWhere()->where('mer_id', $this->merId)->field('test_qrcode_url,qrcode_url')->find(); $info = array_values(compact('sales', 'visits', 'order', 'user')); $info[0]['title'] = '销售额'; $info[1]['title'] = '用户访问量'; $info[2]['title'] = '订单量'; $info[3]['title'] = '新增用户'; $info[0]['total_name'] = '总销售额'; $info[1]['total_name'] = '总访问量'; $info[2]['total_name'] = '总订单量'; $info[3]['total_name'] = '总用户'; return $this->success(compact('info')); } //增长率 public function growth($left, $right) { if ($right) $ratio = bcmul(bcdiv(bcsub($left, $right, 2), $right, 4), 100, 2); else { if ($left) $ratio = 100; else $ratio = 0; } return $ratio; } /** * 订单图表 */ public function orderChart($mer_id) { $cycle = $this->request->param('cycle') ?: 'thirtyday';//默认30天 $datalist = []; switch ($cycle) { case 'thirtyday': $datebefor = date('Y-m-d', strtotime('-30 day')); $dateafter = date('Y-m-d'); //上期 $pre_datebefor = date('Y-m-d', strtotime('-60 day')); $pre_dateafter = date('Y-m-d', strtotime('-30 day')); for ($i = -30; $i < 0; $i++) { $datalist[date('m-d', strtotime($i . ' day'))] = date('m-d', strtotime($i . ' day')); } $order_list = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$datebefor, $dateafter]) ->field("FROM_UNIXTIME(add_time,'%m-%d') as day,count(*) as count,sum(pay_price) as price") ->group("FROM_UNIXTIME(add_time, '%Y%m%d')") ->order('add_time asc') ->select()->toArray(); if (empty($order_list)) return app('json')->success(); foreach ($order_list as $k => &$v) { $order_list[$v['day']] = $v; } $cycle_list = []; foreach ($datalist as $dk => $dd) { if (!empty($order_list[$dd])) { $cycle_list[$dd] = $order_list[$dd]; } else { $cycle_list[$dd] = ['count' => 0, 'day' => $dd, 'price' => '']; } } $chartdata = []; $data = [];//临时 $chartdata['yAxis']['maxnum'] = 0;//最大值数量 $chartdata['yAxis']['maxprice'] = 0;//最大值金额 foreach ($cycle_list as $k => $v) { $data['day'][] = $v['day']; $data['count'][] = $v['count']; $data['price'][] = round($v['price'], 2); if ($chartdata['yAxis']['maxnum'] < $v['count']) $chartdata['yAxis']['maxnum'] = $v['count'];//日最大订单数 if ($chartdata['yAxis']['maxprice'] < $v['price']) $chartdata['yAxis']['maxprice'] = $v['price'];//日最大金额 } $chartdata['legend'] = ['订单金额', '订单数'];//分类 $chartdata['xAxis'] = $data['day'];//X轴值 //,'itemStyle'=>$series // $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]]; $series1 = ['normal' => ['color' => [ 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1, 'colorStops' => [ [ 'offset' => 0, 'color' => '#69cdff' ], [ 'offset' => 0.5, 'color' => '#3eb3f7' ], [ 'offset' => 1, 'color' => '#1495eb' ] ] ]] ]; $series2 = ['normal' => ['color' => [ 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1, 'colorStops' => [ [ 'offset' => 0, 'color' => '#6fdeab' ], [ 'offset' => 0.5, 'color' => '#44d693' ], [ 'offset' => 1, 'color' => '#2cc981' ] ] ]] ]; $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['price']];//分类1值 $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series2, 'data' => $data['count']];//分类2值 //统计总数上期 $pre_total = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$pre_datebefor, $pre_dateafter]) ->field("count(*) as count,sum(pay_price) as price") ->find(); if ($pre_total) { $chartdata['pre_cycle']['count'] = [ 'data' => $pre_total['count'] ?: 0 ]; $chartdata['pre_cycle']['price'] = [ 'data' => $pre_total['price'] ?: 0 ]; } //统计总数 $total = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$datebefor, $dateafter]) ->field("count(*) as count,sum(pay_price) as price") ->find(); if ($total) { $cha_count = intval($pre_total['count']) - intval($total['count']); $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count']; $chartdata['cycle']['count'] = [ 'data' => $total['count'] ?: 0, 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2), 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1) ]; $cha_price = round($pre_total['price'], 2) - round($total['price'], 2); $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price']; $chartdata['cycle']['price'] = [ 'data' => $total['price'] ?: 0, 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2), 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1) ]; } return $this->success($chartdata); break; case 'week': $weekarray = array(['周日'], ['周一'], ['周二'], ['周三'], ['周四'], ['周五'], ['周六']); $datebefor = date('Y-m-d', strtotime('-1 week Monday')); $dateafter = date('Y-m-d', strtotime('-1 week Sunday')); $order_list = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$datebefor, $dateafter]) ->field("FROM_UNIXTIME(add_time,'%w') as day,count(*) as count,sum(pay_price) as price") ->group("FROM_UNIXTIME(add_time, '%Y%m%e')") ->order('add_time asc') ->select()->toArray(); //数据查询重新处理 $new_order_list = []; foreach ($order_list as $k => $v) { $new_order_list[$v['day']] = $v; } $now_datebefor = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)); $now_dateafter = date('Y-m-d', strtotime("+1 day")); $now_order_list = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$now_datebefor, $now_dateafter]) ->field("FROM_UNIXTIME(add_time,'%w') as day,count(*) as count,sum(pay_price) as price") ->group("FROM_UNIXTIME(add_time, '%Y%m%e')") ->order('add_time asc') ->select()->toArray(); //数据查询重新处理 key 变为当前值 $new_now_order_list = []; foreach ($now_order_list as $k => $v) { $new_now_order_list[$v['day']] = $v; } foreach ($weekarray as $dk => $dd) { if (!empty($new_order_list[$dk])) { $weekarray[$dk]['pre'] = $new_order_list[$dk]; } else { $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0']; } if (!empty($new_now_order_list[$dk])) { $weekarray[$dk]['now'] = $new_now_order_list[$dk]; } else { $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0']; } } $chartdata = []; $data = [];//临时 $chartdata['yAxis']['maxnum'] = 0;//最大值数量 $chartdata['yAxis']['maxprice'] = 0;//最大值金额 foreach ($weekarray as $k => $v) { $data['day'][] = $v[0]; $data['pre']['count'][] = $v['pre']['count']; $data['pre']['price'][] = round($v['pre']['price'], 2); $data['now']['count'][] = $v['now']['count']; $data['now']['price'][] = round($v['now']['price'], 2); if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) { $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数 } if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) { $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额 } } $chartdata['legend'] = ['上周金额', '本周金额', '上周订单数', '本周订单数'];//分类 $chartdata['xAxis'] = $data['day'];//X轴值 //,'itemStyle'=>$series // $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]]; $series1 = ['normal' => ['color' => [ 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1, 'colorStops' => [ [ 'offset' => 0, 'color' => '#69cdff' ], [ 'offset' => 0.5, 'color' => '#3eb3f7' ], [ 'offset' => 1, 'color' => '#1495eb' ] ] ]] ]; $series2 = ['normal' => ['color' => [ 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1, 'colorStops' => [ [ 'offset' => 0, 'color' => '#6fdeab' ], [ 'offset' => 0.5, 'color' => '#44d693' ], [ 'offset' => 1, 'color' => '#2cc981' ] ] ]] ]; $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值 $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['now']['price']];//分类1值 $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['pre']['count']];//分类2值 $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['now']['count']];//分类2值 //统计总数上期 $pre_total = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$datebefor, $dateafter]) ->field("count(*) as count,sum(pay_price) as price") ->find(); if ($pre_total) { $chartdata['pre_cycle']['count'] = [ 'data' => $pre_total['count'] ?: 0 ]; $chartdata['pre_cycle']['price'] = [ 'data' => $pre_total['price'] ?: 0 ]; } //统计总数 $total = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$now_datebefor, $now_dateafter]) ->field("count(*) as count,sum(pay_price) as price") ->find(); if ($total) { $cha_count = intval($pre_total['count']) - intval($total['count']); $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count']; $chartdata['cycle']['count'] = [ 'data' => $total['count'] ?: 0, 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2), 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1) ]; $cha_price = round($pre_total['price'], 2) - round($total['price'], 2); $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price']; $chartdata['cycle']['price'] = [ 'data' => $total['price'] ?: 0, 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2), 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1) ]; } return $this->success($chartdata); break; case 'month': $weekarray = array('01' => ['1'], '02' => ['2'], '03' => ['3'], '04' => ['4'], '05' => ['5'], '06' => ['6'], '07' => ['7'], '08' => ['8'], '09' => ['9'], '10' => ['10'], '11' => ['11'], '12' => ['12'], '13' => ['13'], '14' => ['14'], '15' => ['15'], '16' => ['16'], '17' => ['17'], '18' => ['18'], '19' => ['19'], '20' => ['20'], '21' => ['21'], '22' => ['22'], '23' => ['23'], '24' => ['24'], '25' => ['25'], '26' => ['26'], '27' => ['27'], '28' => ['28'], '29' => ['29'], '30' => ['30'], '31' => ['31']); $datebefor = date('Y-m-01', strtotime('-1 month')); $dateafter = date('Y-m-d', strtotime(date('Y-m-01'))); $order_list = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$datebefor, $dateafter]) ->field("FROM_UNIXTIME(add_time,'%d') as day,count(*) as count,sum(pay_price) as price") ->group("FROM_UNIXTIME(add_time, '%Y%m%e')") ->order('add_time asc') ->select()->toArray(); //数据查询重新处理 $new_order_list = []; foreach ($order_list as $k => $v) { $new_order_list[$v['day']] = $v; } $now_datebefor = date('Y-m-01'); $now_dateafter = date('Y-m-d', strtotime("+1 day")); $now_order_list = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$now_datebefor, $now_dateafter]) ->field("FROM_UNIXTIME(add_time,'%d') as day,count(*) as count,sum(pay_price) as price") ->group("FROM_UNIXTIME(add_time, '%Y%m%e')") ->order('add_time asc') ->select()->toArray(); //数据查询重新处理 key 变为当前值 $new_now_order_list = []; foreach ($now_order_list as $k => $v) { $new_now_order_list[$v['day']] = $v; } foreach ($weekarray as $dk => $dd) { if (!empty($new_order_list[$dk])) { $weekarray[$dk]['pre'] = $new_order_list[$dk]; } else { $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0']; } if (!empty($new_now_order_list[$dk])) { $weekarray[$dk]['now'] = $new_now_order_list[$dk]; } else { $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0']; } } $chartdata = []; $data = [];//临时 $chartdata['yAxis']['maxnum'] = 0;//最大值数量 $chartdata['yAxis']['maxprice'] = 0;//最大值金额 foreach ($weekarray as $k => $v) { $data['day'][] = $v[0]; $data['pre']['count'][] = $v['pre']['count']; $data['pre']['price'][] = round($v['pre']['price'], 2); $data['now']['count'][] = $v['now']['count']; $data['now']['price'][] = round($v['now']['price'], 2); if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) { $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数 } if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) { $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额 } } $chartdata['legend'] = ['上月金额', '本月金额', '上月订单数', '本月订单数'];//分类 $chartdata['xAxis'] = $data['day'];//X轴值 //,'itemStyle'=>$series // $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]]; $series1 = ['normal' => ['color' => [ 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1, 'colorStops' => [ [ 'offset' => 0, 'color' => '#69cdff' ], [ 'offset' => 0.5, 'color' => '#3eb3f7' ], [ 'offset' => 1, 'color' => '#1495eb' ] ] ]] ]; $series2 = ['normal' => ['color' => [ 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1, 'colorStops' => [ [ 'offset' => 0, 'color' => '#6fdeab' ], [ 'offset' => 0.5, 'color' => '#44d693' ], [ 'offset' => 1, 'color' => '#2cc981' ] ] ]] ]; $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值 $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['now']['price']];//分类1值 $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['pre']['count']];//分类2值 $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['now']['count']];//分类2值 //统计总数上期 $pre_total = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$datebefor, $dateafter]) ->field("count(*) as count,sum(pay_price) as price") ->find(); if ($pre_total) { $chartdata['pre_cycle']['count'] = [ 'data' => $pre_total['count'] ?: 0 ]; $chartdata['pre_cycle']['price'] = [ 'data' => $pre_total['price'] ?: 0 ]; } //统计总数 $total = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$now_datebefor, $now_dateafter]) ->field("count(*) as count,sum(pay_price) as price") ->find(); if ($total) { $cha_count = intval($pre_total['count']) - intval($total['count']); $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count']; $chartdata['cycle']['count'] = [ 'data' => $total['count'] ?: 0, 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2), 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1) ]; $cha_price = round($pre_total['price'], 2) - round($total['price'], 2); $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price']; $chartdata['cycle']['price'] = [ 'data' => $total['price'] ?: 0, 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2), 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1) ]; } return $this->success($chartdata); break; case 'year': $weekarray = array('01' => ['一月'], '02' => ['二月'], '03' => ['三月'], '04' => ['四月'], '05' => ['五月'], '06' => ['六月'], '07' => ['七月'], '08' => ['八月'], '09' => ['九月'], '10' => ['十月'], '11' => ['十一月'], '12' => ['十二月']); $datebefor = date('Y-01-01', strtotime('-1 year')); $dateafter = date('Y-12-31', strtotime('-1 year')); $order_list = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$datebefor, $dateafter]) ->field("FROM_UNIXTIME(add_time,'%m') as day,count(*) as count,sum(pay_price) as price") ->group("FROM_UNIXTIME(add_time, '%Y%m')") ->order('add_time asc') ->select()->toArray(); //数据查询重新处理 $new_order_list = []; foreach ($order_list as $k => $v) { $new_order_list[$v['day']] = $v; } $now_datebefor = date('Y-01-01'); $now_dateafter = date('Y-m-d'); $now_order_list = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$now_datebefor, $now_dateafter]) ->field("FROM_UNIXTIME(add_time,'%m') as day,count(*) as count,sum(pay_price) as price") ->group("FROM_UNIXTIME(add_time, '%Y%m')") ->order('add_time asc') ->select()->toArray(); //数据查询重新处理 key 变为当前值 $new_now_order_list = []; foreach ($now_order_list as $k => $v) { $new_now_order_list[$v['day']] = $v; } foreach ($weekarray as $dk => $dd) { if (!empty($new_order_list[$dk])) { $weekarray[$dk]['pre'] = $new_order_list[$dk]; } else { $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0']; } if (!empty($new_now_order_list[$dk])) { $weekarray[$dk]['now'] = $new_now_order_list[$dk]; } else { $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0']; } } $chartdata = []; $data = [];//临时 $chartdata['yAxis']['maxnum'] = 0;//最大值数量 $chartdata['yAxis']['maxprice'] = 0;//最大值金额 foreach ($weekarray as $k => $v) { $data['day'][] = $v[0]; $data['pre']['count'][] = $v['pre']['count']; $data['pre']['price'][] = round($v['pre']['price'], 2); $data['now']['count'][] = $v['now']['count']; $data['now']['price'][] = round($v['now']['price'], 2); if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) { $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数 } if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) { $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额 } } $chartdata['legend'] = ['去年金额', '今年金额', '去年订单数', '今年订单数'];//分类 $chartdata['xAxis'] = $data['day'];//X轴值 //,'itemStyle'=>$series // $series = ['normal' => ['label' => ['show' => true, 'position' => 'top']]]; $series1 = ['normal' => ['color' => [ 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1, 'colorStops' => [ [ 'offset' => 0, 'color' => '#69cdff' ], [ 'offset' => 0.5, 'color' => '#3eb3f7' ], [ 'offset' => 1, 'color' => '#1495eb' ] ] ]] ]; $series2 = ['normal' => ['color' => [ 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1, 'colorStops' => [ [ 'offset' => 0, 'color' => '#6fdeab' ], [ 'offset' => 0.5, 'color' => '#44d693' ], [ 'offset' => 1, 'color' => '#2cc981' ] ] ]] ]; $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值 $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['now']['price']];//分类1值 $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['pre']['count']];//分类2值 $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['now']['count']];//分类2值 //统计总数上期 $pre_total = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$datebefor, $dateafter]) ->field("count(*) as count,sum(pay_price) as price") ->find(); if ($pre_total) { $chartdata['pre_cycle']['count'] = [ 'data' => $pre_total['count'] ?: 0 ]; $chartdata['pre_cycle']['price'] = [ 'data' => $pre_total['price'] ?: 0 ]; } //统计总数 $total = StoreOrder::merSet($mer_id)->where('add_time', 'between time', [$now_datebefor, $now_dateafter]) ->field("count(*) as count,sum(pay_price) as price") ->find(); if ($total) { $cha_count = intval($pre_total['count']) - intval($total['count']); $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count']; $chartdata['cycle']['count'] = [ 'data' => $total['count'] ?: 0, 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2), 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1) ]; $cha_price = round($pre_total['price'], 2) - round($total['price'], 2); $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price']; $chartdata['cycle']['price'] = [ 'data' => $total['price'] ?: 0, 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2), 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1) ]; } return $this->success($chartdata); break; default: break; } } /** * 用户图表 */ public function userChart($mer_id) { $starday = date('Y-m-d', strtotime('-30 day')); $yesterday = date('Y-m-d'); $user_list = User::merSet($mer_id)->where('add_time', 'between time', [$starday, $yesterday]) ->field("FROM_UNIXTIME(add_time,'%m-%e') as day,count(*) as count") ->group("FROM_UNIXTIME(add_time, '%Y%m%e')") ->order('add_time asc') ->select()->toArray(); $chartdata = []; $data = []; $chartdata['legend'] = ['用户数'];//分类 $chartdata['yAxis']['maxnum'] = 0;//最大值数量 $chartdata['xAxis'] = [date('m-d')];//X轴值 $chartdata['series'] = [0];//分类1值 if (!empty($user_list)) { foreach ($user_list as $k => $v) { $data['day'][] = $v['day']; $data['count'][] = $v['count']; if ($chartdata['yAxis']['maxnum'] < $v['count']) $chartdata['yAxis']['maxnum'] = $v['count']; } $chartdata['xAxis'] = $data['day'];//X轴值 $chartdata['series'] = $data['count'];//分类1值 } $chartdata['bing_xdata'] = ['未消费用户', '消费一次用户', '留存客户', '回流客户']; $color = ['#5cadff', '#b37feb', '#19be6b', '#ff9900']; $pay[0] = User::merSet($mer_id)->where('pay_count', 0)->count(); $pay[1] = User::merSet($mer_id)->where('pay_count', 1)->count(); $pay[2] = User::merSet($mer_id)->where('pay_count', '>', 0)->where('pay_count', '<', 4)->count(); $pay[3] = User::merSet($mer_id)->where('pay_count', '>', 4)->count(); foreach ($pay as $key => $item) { $bing_data[] = ['name' => $chartdata['bing_xdata'][$key], 'value' => $pay[$key], 'itemStyle' => ['color' => $color[$key]]]; } $chartdata['bing_data'] = $bing_data; return $this->success($chartdata); } /** * 交易额排行 * @return mixed */ public function purchaseRanking($mer_id) { $dlist = StoreProductAttrValue::alias('v') ->where('p.mer_id', $mer_id) ->join('store_product p', 'v.product_id=p.id') ->field('v.product_id,p.store_name,sum(v.sales * v.price) as val')->group('v.product_id')->order('val', 'desc')->limit(20)->select()->toArray(); $slist = StoreProduct::merSet($mer_id)->field('id as product_id,store_name,sales * price as val')->where('is_del', 0)->order('val', 'desc')->limit(20)->select()->toArray(); $data = array_merge($dlist, $slist); $last_names = array_column($data, 'val'); array_multisort($last_names, SORT_DESC, $data); $list = array_splice($data, 0, 20); return $this->success(compact('list')); } }