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

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