链街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.

174 lines
6.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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 $params;
  11. public function __construct($title=null,$data=[])
  12. {
  13. $this->params = $data;
  14. parent::__construct($title, null);
  15. }
  16. public function init()
  17. {
  18. parent::init(); // TODO: Change the autogenerated stub
  19. $this->dropdown([
  20. '1' => '今日',
  21. '-1' => '昨日',
  22. '7' => '最近7天',
  23. 'w' => '本周',
  24. 'lw' => '上周',
  25. '30' => '本月',
  26. '-30' => '上月'
  27. ]);
  28. }
  29. public function handle(Request $request)
  30. {
  31. $params = $request->get('params');//参数选项
  32. $option = $request->get('option');//数据选项
  33. //获取订单数据
  34. $result = $this->getOrderData($params,$option);
  35. $this->content($result);
  36. }
  37. /**
  38. * 查询结果
  39. * @param $params
  40. * @param $option
  41. * @return int
  42. */
  43. public function getOrderData($params,$option)
  44. {
  45. //如果市场id存在
  46. $buildData = $this->buildData();
  47. if ($params['market_id']) {//是否存在市场id
  48. if (is_array($params['market_id'])) {
  49. $buildData = $buildData->whereIn('lanzu_order_main.market_id',$params['market_id']);
  50. } else {
  51. $buildData = $buildData->where('lanzu_order_main.market_id',$params['market_id']);
  52. }
  53. }
  54. if ($params['user_type']=='lanzu_cs'||$params['user_type']=='lanzu_biz'){//社区角色
  55. if ($params['condition']==1){//获取时间范围
  56. list($beginTime,$endTime) = $this->beginAndEnd($option);
  57. //>>2.构建数据模型
  58. if ($params['user_ids']){
  59. $buildData = $buildData->whereIn('lanzu_order_main.user_id',$params['user_ids']);
  60. }else{
  61. return 0;
  62. }
  63. $buildData = $buildData
  64. ->leftJoin('lanzu_user_relation_bind','lanzu_user_relation_bind.user_id','lanzu_order_main.user_id')
  65. ->whereRaw('lanzu_order_main.created_at > lanzu_user_relation_bind.created_at')
  66. ->whereBetween('lanzu_order_main.created_at',[$beginTime,$endTime]);
  67. }
  68. }elseif ($params['user_type']=='market_service'||$params['user_type']=='lanzu_mp'){//市场服务站角色 及服务商
  69. if ($params['condition']==1){//获取时间范围
  70. list($beginTime,$endTime) = $this->beginAndEnd($option);
  71. //>>2.构建数据模型
  72. $buildData = $buildData->whereBetween('created_at',[$beginTime,$endTime]);
  73. }
  74. }
  75. if ($params['data_type']=='count_user'){
  76. $result = $buildData->pluck('lanzu_order_main.user_id')
  77. ->groupBy('lanzu_order_main.user_id')
  78. ->toArray();
  79. $result = count($result);
  80. }elseif ($params['data_type']=='number'){
  81. $result = $buildData->count();
  82. }elseif ($params['data_type']=='amount'){
  83. $result = $buildData->sum('money');
  84. }
  85. return $result;
  86. }
  87. /**
  88. * 查询时间范围
  89. * @param $option
  90. * @return array
  91. */
  92. public function beginAndEnd($option)
  93. {
  94. switch ($option) {
  95. case '1':
  96. $beginTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  97. $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
  98. break;
  99. case '-1':
  100. $beginTime = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
  101. $endTime = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
  102. break;
  103. case '7':
  104. $beginTime = mktime(0, 0, 0, date('m'), date('d') - 7, date('Y'));
  105. $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
  106. break;
  107. case 'w':
  108. $beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"));
  109. $endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"));
  110. break;
  111. case 'lw':
  112. $beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y"));
  113. $endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y"));
  114. break;
  115. case '30':
  116. $beginTime = mktime(0, 0, 0, date('m'), 1, date('Y'));
  117. $endTime = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
  118. break;
  119. case '-30':
  120. $beginTime = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
  121. $endTime = strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y"))));
  122. break;
  123. default:
  124. $beginTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  125. $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
  126. break;
  127. }
  128. return [$beginTime,$endTime];
  129. }
  130. /**
  131. * 构建数据模型
  132. * @param $condition
  133. * @return mixed
  134. */
  135. public function buildData()
  136. {
  137. $build = ImsCjdcOrderMain
  138. ::whereIn('state', [3, 4, 5, 10])
  139. ->where('type',1);
  140. return $build;
  141. }
  142. // 传递自定义参数到 handle 方法
  143. public function parameters(): array
  144. {
  145. return [
  146. 'params'=>$this->params
  147. ];
  148. }
  149. /**
  150. * 渲染卡片内容
  151. * @return string
  152. */
  153. public function renderContent()
  154. {
  155. $content = parent::renderContent();
  156. return <<<HTML
  157. <div class="d-flex justify-content-between bg-primary align-items-center mt-1" style="margin-bottom: 2px">
  158. <h2 class="ml-1 font-large-1">{$content}</h2>
  159. </div>
  160. HTML;
  161. }
  162. }