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

185 lines
7.8 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. $data = $buildData->selectRaw("count(*) as shipping,shipping_type")->whereIn('shipping_type',[1,3])->groupBy('shipping_type')->orderBy('shipping_type')->get()->toArray();
  58. $result[0] = isset($data[0]['shipping'])&&($data[0]['shipping_type']=='1')?$data[0]['shipping']:0;
  59. $result[1] = isset($data[1]['shipping'])&&($data[1]['shipping_type']=='3')?$data[1]['shipping']:0;
  60. }elseif ($params['data_type']=='distance'){
  61. $data = $buildData->selectRaw(
  62. "ELT(INTERVAL(lanzu_order_main.delivery_distance,0,1000,3000,5000),'d1','d2','d3','d4') as delivery,count(lanzu_order_main.id) as num"
  63. )->groupBy('delivery')->orderBy('delivery')->get()->toArray();
  64. $result[0] = isset($data[0]['delivery'])&&($data[0]['delivery']=='d1')?$data[0]['num']:0;
  65. $result[1] = isset($data[1]['delivery'])&&($data[1]['delivery']=='d2')?$data[1]['num']:0;
  66. $result[2] = isset($data[2]['delivery'])&&($data[2]['delivery']=='d3')?$data[2]['num']:0;
  67. $result[3] = isset($data[3]['delivery'])&&($data[3]['delivery']=='d4')?$data[3]['num']:0;
  68. }elseif ($params['data_type']=='order_count_user'){
  69. $result = DB::table('lanzu_order_main')
  70. ->whereIn('state', [3, 4, 5, 10,11])
  71. ->where('type',1)
  72. ->pluck('user_id')
  73. ->toArray();
  74. $result = count(array_unique($result));
  75. }elseif ($params['data_type']=='num'){
  76. $result = $buildData->pluck('user_id');
  77. if ($result){
  78. $result = $result->toArray();
  79. }
  80. }
  81. return $result;
  82. }
  83. /**
  84. * 查询时间范围
  85. * @param $option
  86. * @return array
  87. */
  88. public static function beginAndEnd($option)
  89. {
  90. switch ($option) {
  91. case '1':
  92. $beginTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  93. $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
  94. break;
  95. case '-1':
  96. $beginTime = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
  97. $endTime = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
  98. break;
  99. case '7':
  100. $beginTime = mktime(0, 0, 0, date('m'), date('d') - 7, date('Y'));
  101. $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
  102. break;
  103. case 'w':
  104. //$beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"));
  105. //$endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"));
  106. //当前日期
  107. $sdefaultDate = date("Y-m-d");
  108. //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
  109. $first=1;
  110. //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
  111. $w=date('w',strtotime($sdefaultDate));
  112. //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
  113. $beginTime=strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days');
  114. //本周结束日期
  115. $start = date('Y-m-d',$beginTime);
  116. $endTime=strtotime("$start +6 days")+86399;
  117. break;
  118. case 'lw':
  119. $beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y"));
  120. $endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y"));
  121. break;
  122. case '30':
  123. $beginTime = mktime(0, 0, 0, date('m'), 1, date('Y'));
  124. $endTime = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
  125. break;
  126. case '-30':
  127. $beginTime = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
  128. $endTime = strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y"))));
  129. break;
  130. default:
  131. $searchTime = request()->input('created_at');
  132. if ($searchTime){
  133. $beginTime = strtotime($searchTime['start']);
  134. $endTime = strtotime($searchTime['end']);
  135. }else{
  136. $beginTime = 0;
  137. $endTime = 999999999999;
  138. }
  139. break;
  140. }
  141. return [$beginTime,$endTime];
  142. }
  143. /**
  144. * 构建数据模型
  145. * @param $condition
  146. * @return mixed
  147. */
  148. public static function buildData()
  149. {
  150. $build = ImsCjdcOrderMain
  151. ::whereIn('state', [3, 4, 5, 10,11])
  152. ->where('type',1);
  153. return $build;
  154. }
  155. /**
  156. * 查询用户绑定后下过单的用户id
  157. * @param $adminUserId
  158. * @return mixed
  159. */
  160. public static function getUserOrder($adminUserId)
  161. {
  162. $uid = LanzuUserRelationBind::where('source_id',$adminUserId)
  163. ->leftJoin('lanzu_order_main','lanzu_order_main.user_id','lanzu_user_relation_bind.user_id')
  164. ->whereRaw('lanzu_order_main.created_at > lanzu_user_relation_bind.created_at')
  165. ->groupBy('lanzu_order_main.user_id')
  166. ->pluck('lanzu_order_main.user_id')
  167. ->toArray();//获取绑定社区的所有用户
  168. return $uid;
  169. }
  170. }