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

155 lines
6.0 KiB

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. }
  56. return $result;
  57. }
  58. /**
  59. * 查询时间范围
  60. * @param $option
  61. * @return array
  62. */
  63. public static function beginAndEnd($option)
  64. {
  65. switch ($option) {
  66. case '1':
  67. $beginTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  68. $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
  69. break;
  70. case '-1':
  71. $beginTime = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
  72. $endTime = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
  73. break;
  74. case '7':
  75. $beginTime = mktime(0, 0, 0, date('m'), date('d') - 7, date('Y'));
  76. $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
  77. break;
  78. case 'w':
  79. //$beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"));
  80. //$endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"));
  81. //当前日期
  82. $sdefaultDate = date("Y-m-d");
  83. //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
  84. $first=1;
  85. //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
  86. $w=date('w',strtotime($sdefaultDate));
  87. //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
  88. $beginTime=strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days');
  89. //本周结束日期
  90. $start = date('Y-m-d',$beginTime);
  91. $endTime=strtotime("$start +6 days")+86399;
  92. break;
  93. case 'lw':
  94. $beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y"));
  95. $endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y"));
  96. break;
  97. case '30':
  98. $beginTime = mktime(0, 0, 0, date('m'), 1, date('Y'));
  99. $endTime = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
  100. break;
  101. case '-30':
  102. $beginTime = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
  103. $endTime = strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y"))));
  104. break;
  105. default:
  106. $searchTime = request()->input('created_at');
  107. if ($searchTime){
  108. $beginTime = strtotime($searchTime['start']);
  109. $endTime = strtotime($searchTime['end']);
  110. }else{
  111. $beginTime = 0;
  112. $endTime = 999999999999;
  113. }
  114. break;
  115. }
  116. return [$beginTime,$endTime];
  117. }
  118. /**
  119. * 构建数据模型
  120. * @param $condition
  121. * @return mixed
  122. */
  123. public static function buildData()
  124. {
  125. $build = ImsCjdcOrderMain
  126. ::whereIn('state', [3, 4, 5, 10,11])
  127. ->where('type',1);
  128. return $build;
  129. }
  130. /**
  131. * 查询用户绑定后下过单的用户id
  132. * @param $adminUserId
  133. * @return mixed
  134. */
  135. public static function getUserOrder($adminUserId)
  136. {
  137. $uid = LanzuUserRelationBind::where('source_id',$adminUserId)
  138. ->leftJoin('lanzu_order_main','lanzu_order_main.user_id','lanzu_user_relation_bind.user_id')
  139. ->whereRaw('lanzu_order_main.created_at > lanzu_user_relation_bind.created_at')
  140. ->groupBy('lanzu_order_main.user_id')
  141. ->pluck('lanzu_order_main.user_id')
  142. ->toArray();//获取绑定社区的所有用户
  143. return $uid;
  144. }
  145. }