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

187 lines
7.5 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
  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. ->pluck('user_id')
  77. ->toArray();
  78. $result = count(array_unique($result));
  79. }elseif ($params['data_type']=='num'){
  80. $result = $buildData->pluck('user_id');
  81. if ($result){
  82. $result = $result->toArray();
  83. }
  84. }
  85. return $result;
  86. }
  87. /**
  88. * 查询时间范围
  89. * @param $option
  90. * @return array
  91. */
  92. public static 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. //当前日期
  111. $sdefaultDate = date("Y-m-d");
  112. //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
  113. $first=1;
  114. //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
  115. $w=date('w',strtotime($sdefaultDate));
  116. //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
  117. $beginTime=strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days');
  118. //本周结束日期
  119. $start = date('Y-m-d',$beginTime);
  120. $endTime=strtotime("$start +6 days")+86399;
  121. break;
  122. case 'lw':
  123. $beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y"));
  124. $endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y"));
  125. break;
  126. case '30':
  127. $beginTime = mktime(0, 0, 0, date('m'), 1, date('Y'));
  128. $endTime = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
  129. break;
  130. case '-30':
  131. $beginTime = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
  132. $endTime = strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y"))));
  133. break;
  134. default:
  135. $searchTime = request()->input('created_at');
  136. if ($searchTime){
  137. $beginTime = strtotime($searchTime['start']);
  138. $endTime = strtotime($searchTime['end']);
  139. }else{
  140. $beginTime = 0;
  141. $endTime = 999999999999;
  142. }
  143. break;
  144. }
  145. return [$beginTime,$endTime];
  146. }
  147. /**
  148. * 构建数据模型
  149. * @param $condition
  150. * @return mixed
  151. */
  152. public static function buildData()
  153. {
  154. $build = ImsCjdcOrderMain
  155. ::whereIn('state', [3, 4, 5, 10,11])
  156. ->where('type',1);
  157. return $build;
  158. }
  159. /**
  160. * 查询用户绑定后下过单的用户id
  161. * @param $adminUserId
  162. * @return mixed
  163. */
  164. public static function getUserOrder($adminUserId)
  165. {
  166. $uid = LanzuUserRelationBind::where('source_id',$adminUserId)
  167. ->leftJoin('lanzu_order_main','lanzu_order_main.user_id','lanzu_user_relation_bind.user_id')
  168. ->whereRaw('lanzu_order_main.created_at > lanzu_user_relation_bind.created_at')
  169. ->groupBy('lanzu_order_main.user_id')
  170. ->pluck('lanzu_order_main.user_id')
  171. ->toArray();//获取绑定社区的所有用户
  172. return $uid;
  173. }
  174. }