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

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