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

171 lines
6.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Admin\Common;
  3. use App\Models\ImsCjdcOrderMain;
  4. use App\Models\LanzuUserRelationBind;
  5. class Order
  6. {
  7. /**
  8. * 查询结果
  9. * @param $params
  10. * @param $option
  11. * @return int
  12. */
  13. public static function getOrderData($params,$option)
  14. {
  15. //如果市场id存在
  16. $buildData = self::buildData();
  17. if ($params['market_id']) {//是否存在市场id
  18. if (is_array($params['market_id'])) {
  19. $buildData = $buildData->whereIn('lanzu_order_main.market_id',$params['market_id']);
  20. } else {
  21. $buildData = $buildData->where('lanzu_order_main.market_id',$params['market_id']);
  22. }
  23. }
  24. if ($params['user_type']=='lanzu_cs'||$params['user_type']=='lanzu_biz'){//社区角色
  25. if ($params['condition']==1){//获取时间范围
  26. list($beginTime,$endTime) = self::beginAndEnd($option);
  27. //>>2.构建数据模型
  28. if ($params['user_ids']){
  29. $buildData = $buildData->whereIn('lanzu_order_main.user_id',$params['user_ids']);
  30. }else{
  31. return 0;
  32. }
  33. $buildData = $buildData
  34. ->leftJoin('lanzu_user_relation_bind','lanzu_user_relation_bind.user_id','lanzu_order_main.user_id')
  35. ->whereRaw('lanzu_order_main.created_at > lanzu_user_relation_bind.created_at')
  36. ->whereBetween('lanzu_order_main.created_at',[$beginTime,$endTime]);
  37. }
  38. }elseif ($params['user_type']=='market_service'||$params['user_type']=='lanzu_mp'){//市场服务站角色 及服务商
  39. if ($params['condition']==1){//获取时间范围
  40. list($beginTime,$endTime) = self::beginAndEnd($option);
  41. //>>2.构建数据模型
  42. $buildData = $buildData->whereBetween('created_at',[$beginTime,$endTime]);
  43. }
  44. }
  45. if ($params['data_type']=='count_user'){//用户数
  46. $result = $buildData
  47. ->groupBy('lanzu_order_main.user_id')
  48. ->pluck('lanzu_order_main.user_id')
  49. ->toArray();
  50. $result = count($result);
  51. }elseif ($params['data_type']=='number'){//订单数
  52. $result = $buildData->count();
  53. }elseif ($params['data_type']=='amount'){//订单金额
  54. $result = $buildData->sum('money');
  55. }elseif ($params['data_type']=='shipping'){//配送方式
  56. $build1 = clone $buildData;
  57. $build3 = clone $buildData;
  58. $s1 = $build1->where('shipping_type',1)->count();
  59. $s3 = $build3->where('shipping_type',3)->count();
  60. $result = [$s1,$s3];
  61. }elseif ($params['data_type']=='distance'){
  62. $build1 = clone $buildData;
  63. $build2 = clone $buildData;
  64. $build3 = clone $buildData;
  65. $d1 = $build1->where('delivery_distance','<',1000)->count();
  66. $d2 = $build2->where('delivery_distance','>',[1000,3000])->count();
  67. $d3 = $build3->where('delivery_distance','>',3000)->count();
  68. $result = [$d1,$d2,$d3];
  69. }
  70. return $result;
  71. }
  72. /**
  73. * 查询时间范围
  74. * @param $option
  75. * @return array
  76. */
  77. public static function beginAndEnd($option)
  78. {
  79. switch ($option) {
  80. case '1':
  81. $beginTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  82. $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
  83. break;
  84. case '-1':
  85. $beginTime = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
  86. $endTime = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
  87. break;
  88. case '7':
  89. $beginTime = mktime(0, 0, 0, date('m'), date('d') - 7, date('Y'));
  90. $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
  91. break;
  92. case 'w':
  93. //$beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"));
  94. //$endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"));
  95. //当前日期
  96. $sdefaultDate = date("Y-m-d");
  97. //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
  98. $first=1;
  99. //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
  100. $w=date('w',strtotime($sdefaultDate));
  101. //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
  102. $beginTime=strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days');
  103. //本周结束日期
  104. $start = date('Y-m-d',$beginTime);
  105. $endTime=strtotime("$start +6 days")+86399;
  106. break;
  107. case 'lw':
  108. $beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y"));
  109. $endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y"));
  110. break;
  111. case '30':
  112. $beginTime = mktime(0, 0, 0, date('m'), 1, date('Y'));
  113. $endTime = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
  114. break;
  115. case '-30':
  116. $beginTime = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
  117. $endTime = strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y"))));
  118. break;
  119. default:
  120. $searchTime = request()->input('created_at');
  121. if ($searchTime){
  122. $beginTime = strtotime($searchTime['start']);
  123. $endTime = strtotime($searchTime['end']);
  124. }else{
  125. $beginTime = 0;
  126. $endTime = 999999999999;
  127. }
  128. break;
  129. }
  130. return [$beginTime,$endTime];
  131. }
  132. /**
  133. * 构建数据模型
  134. * @param $condition
  135. * @return mixed
  136. */
  137. public static function buildData()
  138. {
  139. $build = ImsCjdcOrderMain
  140. ::whereIn('state', [3, 4, 5, 10,11])
  141. ->where('type',1);
  142. return $build;
  143. }
  144. /**
  145. * 查询用户绑定后下过单的用户id
  146. * @param $adminUserId
  147. * @return mixed
  148. */
  149. public static function getUserOrder($adminUserId)
  150. {
  151. $uid = LanzuUserRelationBind::where('source_id',$adminUserId)
  152. ->leftJoin('lanzu_order_main','lanzu_order_main.user_id','lanzu_user_relation_bind.user_id')
  153. ->whereRaw('lanzu_order_main.created_at > lanzu_user_relation_bind.created_at')
  154. ->groupBy('lanzu_order_main.user_id')
  155. ->pluck('lanzu_order_main.user_id')
  156. ->toArray();//获取绑定社区的所有用户
  157. return $uid;
  158. }
  159. }