FinancialRechargeChart.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 陈武杰
  5. * Date: 2021/1/15
  6. * Time: 10:31
  7. */
  8. namespace App\Admin\Controllers\DataCenter;
  9. use Encore\Admin\Widgets\Box;
  10. use Encore\Admin\Widgets\Echarts\Echarts;
  11. use Encore\Admin\Widgets\Form;
  12. use Encore\Admin\Layout\Row;
  13. use Encore\Admin\Layout\Column;
  14. use Encore\Admin\Widgets\Collapse;
  15. use App\Models\Order;
  16. class FinancialRechargeChart
  17. {
  18. public function chart(Row $row)
  19. {
  20. $row->column(12,function (Column $column){
  21. $form = new Form();
  22. $form->disableReset();
  23. $form->disableSubmit();
  24. $ry = request('year');
  25. $rm = request('month');
  26. //是否是初始化
  27. $isb = request('b');
  28. // dd($isb);
  29. $year = range(2000,2099);
  30. $month = range(1,12);
  31. $nowYear = date('Y');
  32. $nowMonth = date('m');
  33. //默认数据获取方式 1默认选择当前月份 2根据时间
  34. $type = 2;
  35. if(empty($ry) && empty($rm)){
  36. $type = 1;
  37. }
  38. //判断是否是初始化
  39. if(empty($isb)){
  40. $ry = $nowYear;
  41. $rm = $nowMonth;
  42. } else {
  43. $type = 2;
  44. }
  45. //生成对应年月的键名的数组
  46. foreach ($year as $v){
  47. $realYear[$v]=$v;
  48. }
  49. foreach ($month as $v){
  50. $realMonth[$v]=$v;
  51. }
  52. //获取数据 传入参数 真实的年、月、和类型 1默认选择当前月份 2根据时间
  53. $chartData =self::getData($ry,$rm,$type);
  54. $filter_content = new Row(function (Column $col)use ($form,$type,$realYear,$ry,$rm,$realMonth){
  55. $col->row(function (Row $row)use ($form,$type,$realYear,$ry,$rm,$realMonth){
  56. $form->method('get');
  57. $form->select('year','年份')->options($realYear)->default($ry);
  58. $form->select('month','月份')->options($realMonth)->default($rm);
  59. $form->hidden('b')->default(1);
  60. $form->html("<button type='submit' class='btn btn-success'>筛选</button><a style='margin-left: 20px;' href='/admin/financial_recharge_overview' class='btn btn-danger'>重置</a>");
  61. $form->action('/admin/financial_recharge_overview');
  62. $row->column(6,$form->render());
  63. });
  64. });
  65. $col = new Collapse();
  66. $col->add('日期筛选',$filter_content->render());
  67. $column->append($col->render());
  68. $column->row(function (Row $row)use ($chartData){
  69. $row->column(12,function (Column $column)use ($chartData){
  70. $echarts = (new Echarts('充值走势图', '','user_change'))
  71. ->setData($chartData['d'])
  72. ->bindLegend($chartData['h'])->setShowToolbox(true);
  73. return $column->append(new Box('',$echarts));
  74. });
  75. });
  76. });
  77. }
  78. public function getData($year,$month,$type)
  79. {
  80. //type 1默认选择当前月份 2根据时间+
  81. $month >10??$month = '0'.$month;
  82. if($type == 1){
  83. //查找天
  84. $data = Order::query()
  85. ->selectRaw('DATE_FORMAT(created_at,"%m-%d") as day')
  86. ->selectRaw('SUM(payment_amount) as sum')
  87. ->where('product_type',7)
  88. ->where('payment_status',2)
  89. ->where('created_at', '>=', $year.'-'.$month)
  90. ->where('created_at', '<', $year.'-'.($month+1))
  91. ->groupBy('day')
  92. ->orderBy('day')
  93. ->get()->toArray();
  94. // dd($data);
  95. $data = self::getFomateData($data,3);
  96. return $data;
  97. }
  98. if(empty($year) && empty($month)){
  99. //查找年
  100. $data = Order::query()
  101. ->selectRaw('DATE_FORMAT(created_at,"%Y") as day')
  102. ->selectRaw('SUM(payment_amount) as sum')
  103. ->where('product_type',7)
  104. ->where('payment_status',2)
  105. ->where('created_at', '>', '2000-')
  106. ->where('created_at', '<', '2099-')
  107. ->groupBy('day')
  108. ->orderBy('day')
  109. ->get()->toArray();
  110. $data = self::getFomateData($data,1);
  111. return $data;
  112. }
  113. if($year && $month){
  114. //查找天
  115. $data = Order::query()
  116. ->selectRaw('DATE_FORMAT(created_at,"%m-%d") as day')
  117. ->selectRaw('SUM(payment_amount) as sum')
  118. ->where('product_type',7)
  119. ->where('payment_status',2)
  120. ->where('created_at', '>', $year.'-'.$month)
  121. ->where('created_at', '<', $year.'-'.($month+1))
  122. ->groupBy('day')
  123. ->orderBy('day')
  124. ->get()->toArray();
  125. $data = self::getFomateData($data,3);
  126. return $data;
  127. }
  128. if($year){
  129. //查找月
  130. $data = Order::query()
  131. ->selectRaw('DATE_FORMAT(created_at,"%m") as day')
  132. ->selectRaw('SUM(payment_amount) as sum')
  133. ->where('product_type',7)
  134. ->where('payment_status',2)
  135. ->where('created_at', '>=', $year.'-')
  136. ->where('created_at', '<', ($year+1).'-')
  137. ->groupBy('day')
  138. ->orderBy('day')
  139. ->get()->toArray();
  140. $data = self::getFomateData($data,2);
  141. return $data;
  142. }
  143. return self::getFomateData([],3);
  144. }
  145. public static function getFomateData($data,$type)
  146. {
  147. $typeArr = [1=>'年',2=>'月',3=>'日'];
  148. $chartData['d'] = [];
  149. foreach ($data as $v){
  150. $chartData['d'][] = ['day'=>$v['day'].$typeArr[$type],'sum'=>$v['sum']/100];
  151. }
  152. $chartData['h'] = ['day'=>$typeArr[$type],'sum'=>'充值金额'];
  153. return $chartData;
  154. }
  155. }