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

176 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
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
  77. ->groupBy('lanzu_order_main.user_id')
  78. ->pluck('lanzu_order_main.user_id')
  79. ->toArray();
  80. $result = count($result);
  81. }elseif ($params['data_type']=='number'){
  82. $result = $buildData->count();
  83. }elseif ($params['data_type']=='amount'){
  84. $result = $buildData->sum('money');
  85. }
  86. return $result;
  87. }
  88. /**
  89. * 查询时间范围
  90. * @param $option
  91. * @return array
  92. */
  93. public function beginAndEnd($option)
  94. {
  95. switch ($option) {
  96. case '1':
  97. $beginTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  98. $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
  99. break;
  100. case '-1':
  101. $beginTime = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
  102. $endTime = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
  103. break;
  104. case '7':
  105. $beginTime = mktime(0, 0, 0, date('m'), date('d') - 7, date('Y'));
  106. $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
  107. break;
  108. case 'w':
  109. $beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"));
  110. $endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"));
  111. break;
  112. case 'lw':
  113. $beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y"));
  114. $endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y"));
  115. break;
  116. case '30':
  117. $beginTime = mktime(0, 0, 0, date('m'), 1, date('Y'));
  118. $endTime = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
  119. break;
  120. case '-30':
  121. $beginTime = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
  122. $endTime = strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y"))));
  123. break;
  124. default:
  125. $beginTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  126. $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
  127. break;
  128. }
  129. return [$beginTime,$endTime];
  130. }
  131. /**
  132. * 构建数据模型
  133. * @param $condition
  134. * @return mixed
  135. */
  136. public function buildData()
  137. {
  138. $build = ImsCjdcOrderMain
  139. ::whereIn('state', [3, 4, 5, 10])
  140. ->where('type',1);
  141. return $build;
  142. }
  143. // 传递自定义参数到 handle 方法
  144. public function parameters(): array
  145. {
  146. return [
  147. 'params'=>$this->params
  148. ];
  149. }
  150. /**
  151. * 渲染卡片内容
  152. * @return string
  153. */
  154. public function renderContent()
  155. {
  156. $content = parent::renderContent();
  157. return <<<HTML
  158. <div class="d-flex justify-content-between bg-primary align-items-center mt-1" style="margin-bottom: 2px">
  159. <h2 class="ml-1 font-large-1">{$content}</h2>
  160. </div>
  161. HTML;
  162. }
  163. }