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

203 lines
8.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. $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. }elseif ($params['data_type']=='market'){
  81. $data = $buildData
  82. ->selectRaw("count(*) as num,case market_id when 2 then 'dg' when 3 then 'tg' when 4 then 'hy' when 5 then 'px' end 'name'")
  83. ->groupBy('market_id')
  84. ->orderBy('market_id')
  85. ->get()->toArray();
  86. $arr = ['dg','tg','hy','px'];
  87. $result = [];
  88. foreach ($arr as $key=>$item){
  89. foreach ($data as $val){
  90. if ($item==$val['name']){
  91. $result[$key] = $val['num'];
  92. break;
  93. }
  94. }
  95. if (!isset($result[$key])){
  96. $result[$key] = 0;
  97. }
  98. }
  99. }
  100. return $result;
  101. }
  102. /**
  103. * 查询时间范围
  104. * @param $option
  105. * @return array
  106. */
  107. public static function beginAndEnd($option)
  108. {
  109. switch ($option) {
  110. case '1':
  111. $beginTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  112. $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
  113. break;
  114. case '-1':
  115. $beginTime = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
  116. $endTime = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
  117. break;
  118. case '7':
  119. $beginTime = mktime(0, 0, 0, date('m'), date('d') - 7, date('Y'));
  120. $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
  121. break;
  122. case 'w':
  123. //$beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"));
  124. //$endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"));
  125. //当前日期
  126. $sdefaultDate = date("Y-m-d");
  127. //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
  128. $first=1;
  129. //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
  130. $w=date('w',strtotime($sdefaultDate));
  131. //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
  132. $beginTime=strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days');
  133. //本周结束日期
  134. $start = date('Y-m-d',$beginTime);
  135. $endTime=strtotime("$start +6 days")+86399;
  136. break;
  137. case 'lw':
  138. $beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y"));
  139. $endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y"));
  140. break;
  141. case '30':
  142. $beginTime = mktime(0, 0, 0, date('m'), 1, date('Y'));
  143. $endTime = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
  144. break;
  145. case '-30':
  146. $beginTime = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
  147. $endTime = strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y"))));
  148. break;
  149. default:
  150. $searchTime = request()->input('created_at');
  151. if ($searchTime){
  152. $beginTime = strtotime($searchTime['start']);
  153. $endTime = strtotime($searchTime['end']);
  154. }else{
  155. $beginTime = 0;
  156. $endTime = 999999999999;
  157. }
  158. break;
  159. }
  160. return [$beginTime,$endTime];
  161. }
  162. /**
  163. * 构建数据模型
  164. * @param $condition
  165. * @return mixed
  166. */
  167. public static function buildData()
  168. {
  169. $build = ImsCjdcOrderMain
  170. ::whereIn('state', [3, 4, 5, 10,11])
  171. ->where('type',1);
  172. return $build;
  173. }
  174. /**
  175. * 查询用户绑定后下过单的用户id
  176. * @param $adminUserId
  177. * @return mixed
  178. */
  179. public static function getUserOrder($adminUserId)
  180. {
  181. $uid = LanzuUserRelationBind::where('source_id',$adminUserId)
  182. ->leftJoin('lanzu_order_main','lanzu_order_main.user_id','lanzu_user_relation_bind.user_id')
  183. ->whereRaw('lanzu_order_main.created_at > lanzu_user_relation_bind.created_at')
  184. ->groupBy('lanzu_order_main.user_id')
  185. ->pluck('lanzu_order_main.user_id')
  186. ->toArray();//获取绑定社区的所有用户
  187. return $uid;
  188. }
  189. }