链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.7 KiB

  1. <?php
  2. namespace App\Admin\Metrics\Examples\Order;
  3. use App\Models\ImsCjdcOrder;
  4. use App\Models\ImsCjdcOrderMain;
  5. use Dcat\Admin\Widgets\Metrics\Card;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\DB;
  8. class OrderData extends Card
  9. {
  10. protected $title;
  11. protected $data_type;
  12. protected $market_id;
  13. public function __construct($data_type=null,$market_id=null,$title = null, $icon = null)
  14. {
  15. $this->title = $title;
  16. $this->data_type = $data_type;
  17. $this->market_id = $market_id;
  18. parent::__construct($title, $icon);
  19. }
  20. public function init()
  21. {
  22. parent::init(); // TODO: Change the autogenerated stub
  23. $this->dropdown([
  24. '1'=>'今日',
  25. '-1'=>'昨日',
  26. '7'=>'过去一周',
  27. '30'=>'本月',
  28. '-30'=>'上月'
  29. ]);
  30. }
  31. public function handle(Request $request)
  32. {
  33. $type = $request->get('option');//数据选项
  34. $data_type = $request->get('data_type');//数据类型
  35. $market_id = $request->get('market_id');//市场id
  36. $this->content($this->orderNum($data_type,$type,$market_id));
  37. }
  38. public function orderDataBuild($type=null,$marketId=null)
  39. {
  40. $where = [];
  41. $where['type'] = 1;
  42. if ($marketId){
  43. if (is_array($marketId)){
  44. $where[] = ['in'=>['market_id'=>$marketId]];
  45. }else{
  46. $where['market_id'] = $marketId;
  47. }
  48. }
  49. switch ($type){
  50. case '1':
  51. $beginTime=mktime(0,0,0,date('m'),date('d'),date('Y'));
  52. $endTime=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
  53. break;
  54. case '-1':
  55. $beginTime=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
  56. $endTime=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
  57. break;
  58. case '7':
  59. $beginTime=mktime(0,0,0,date('m'),date('d')-7,date('Y'));
  60. $endTime=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
  61. break;
  62. case '30':
  63. $beginTime=mktime(0,0,0,date('m'),1,date('Y'));
  64. $endTime=mktime(23,59,59,date('m'),date('t'),date('Y'));
  65. break;
  66. case '-30':
  67. $beginTime=mktime(0,0,0,date('m')-1,1,date('Y'));
  68. $endTime=strtotime(date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))));
  69. break;
  70. default:
  71. $beginTime=mktime(0,0,0,date('m'),date('d'),date('Y'));
  72. $endTime=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
  73. break;
  74. }
  75. $build = ImsCjdcOrderMain
  76. ::whereBetween('created_at',[$beginTime,$endTime])
  77. ->whereIn('state',[3,4,5,10])
  78. ->where($where);
  79. return $build;
  80. }
  81. public function orderNum($data_type,$type,$marketId)
  82. {
  83. $build = $this->orderDataBuild($type,$marketId);
  84. if ($data_type=='number'){
  85. $result = $build->count();
  86. }elseif ($data_type=='amount'){
  87. $result = $build->sum('money');
  88. }else{
  89. $result = 0;
  90. }
  91. return $result;
  92. }
  93. // 传递自定义参数到 handle 方法
  94. public function parameters() : array
  95. {
  96. return [
  97. 'data_type'=>$this->data_type,
  98. 'market_id'=>$this->market_id,
  99. ];
  100. }
  101. /**
  102. * 渲染卡片内容
  103. * @return string
  104. */
  105. public function renderContent()
  106. {
  107. $content = parent::renderContent();
  108. return <<<HTML
  109. <div class="d-flex justify-content-between bg-primary align-items-center mt-1" style="margin-bottom: 2px">
  110. <h2 class="ml-1 font-large-1">{$content}</h2>
  111. </div>
  112. HTML;
  113. }
  114. }