123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903 |
- <?php
- namespace app\badminapi\controller;
- use app\models\merchant\Merchant;
- use app\models\store\StoreOrder;
- use app\models\store\StoreProduct;
- use app\models\store\StoreProductAttrValue;
- use app\models\system\MerchantDailyReport;
- use app\models\system\SystemDailyReport;
- use app\models\user\User;
- use crmeb\basic\BaseModel;
- use crmeb\services\UtilService;
- use crmeb\traits\ModelTrait;
- use Exception;
- use think\db\exception\DbException;
- /**
- * 报表控制器
- * Class Report
- * @package app\adminapi\controller
- */
- class Report extends AuthController
- {
- public function initialize()
- {
- parent::initialize(); // TODO: Change the autogenerated stub
- }
- use ModelTrait;
- /**
- * 手动生成昨日报表
- * @return mixed
- */
- public function createYesterdayReport()
- {
- try {
- MerchantDailyReport::recordYesterdayReport();
- SystemDailyReport::recordYesterdayReport();
- BaseModel::commitTrans();
- return app('json')->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'));
- }
- }
|