ControlPanelController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\AsyncResponse;
  4. use App\OrderPackageCountingRecord;
  5. use App\Owner;
  6. use App\Services\CacheService;
  7. use App\Services\CheckActiveMenuService;
  8. use App\Services\LaborReportsCountingRecordService;
  9. use App\Services\NewOrderCountingRecordService;
  10. use App\Services\RealtimePendingOrdersService;
  11. use App\Services\UserService;
  12. use App\User;
  13. use Carbon\Carbon;
  14. use Carbon\CarbonPeriod;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Cache;
  17. use Illuminate\Support\Facades\DB;
  18. class ControlPanelController extends Controller
  19. {
  20. use AsyncResponse;
  21. /**
  22. * ControlPanelController constructor.
  23. */
  24. public function __construct()
  25. {
  26. $this->middleware('auth');
  27. }
  28. public function index()
  29. {
  30. /**
  31. * @var $orderCountingRecordService NewOrderCountingRecordService
  32. * @var $laborReportsCountingRecordService LaborReportsCountingRecordService
  33. */
  34. $checkActiveMenuService = app(CheckActiveMenuService::class);
  35. $menus = $checkActiveMenuService->activeMenus();
  36. $realtimePendingOrdersService = app(RealtimePendingOrdersService::class);
  37. $warehousesOrders = $realtimePendingOrdersService->warehousesOrders();
  38. $orderCountingRecordService = app(NewOrderCountingRecordService::class);
  39. //默认查询一个月的数据
  40. $start = (new Carbon())->subMonth()->toDateString();
  41. $end = (new Carbon())->toDateString();
  42. $ownerIds = $this->getCountingOwnerIds(null);
  43. $owners=Owner::query()->whereIn('id',$ownerIds)->get();
  44. $unit = '日';
  45. $orderCountingRecords = $orderCountingRecordService->orderCountingRecordsFromCache($start, $end, $unit, $ownerIds);
  46. $logisticsCountingRecords = $orderCountingRecordService->logisticsCountingRecords($start, $end, $ownerIds);
  47. $warehouseCountingRecords = $orderCountingRecordService->warehouseCountingRecords($start, $end, $ownerIds);
  48. $laborReportsCountingRecordService = app(LaborReportsCountingRecordService::class);
  49. $laborReportsCountingRecords = $laborReportsCountingRecordService->get($start, $end, $unit);
  50. $laborReportsUserGroupsCount = $laborReportsCountingRecordService->userGroupsCount($start, $end);
  51. return view('control.panel', compact('owners','menus', 'warehousesOrders', 'orderCountingRecords', 'logisticsCountingRecords', 'warehouseCountingRecords', 'laborReportsCountingRecords', 'laborReportsUserGroupsCount'));
  52. }
  53. public function orderCountingRecordsApi(Request $request)
  54. {
  55. /**
  56. * @var $orderCountingRecordService NewOrderCountingRecordService
  57. */
  58. $orderCountingRecordService = app(NewOrderCountingRecordService::class);
  59. $start = Carbon::parse($request->start)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->start;
  60. $end = Carbon::parse($request->end)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->end;
  61. $ownerIds=$request->owner_ids;
  62. if (!$ownerIds || in_array('all',$ownerIds)) $ownerIds = $this->getCountingOwnerIds(null);
  63. $orderCountingRecords = $orderCountingRecordService->orderCountingRecords($start, $end, $request->unit, $ownerIds);
  64. return compact('orderCountingRecords');
  65. }
  66. public function logisticsCountingRecordsApi(Request $request)
  67. {
  68. /**
  69. * @var $orderCountingRecordService NewOrderCountingRecordService
  70. */
  71. $orderCountingRecordService = app(NewOrderCountingRecordService::class);
  72. $start = Carbon::parse($request->start)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->start;
  73. $end = Carbon::parse($request->end)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->end;
  74. $ownerIds=$request->input('owner_ids');
  75. if (!$ownerIds || in_array('all',$ownerIds)) $ownerIds = $this->getCountingOwnerIds(null);
  76. $logisticsCountingRecords = $orderCountingRecordService->logisticsCountingRecords($start, $end, $ownerIds);
  77. return compact('logisticsCountingRecords');
  78. }
  79. public function warehouseCountingRecordsApi(Request $request)
  80. {
  81. /**
  82. * @var $orderCountingRecordService NewOrderCountingRecordService
  83. */
  84. $orderCountingRecordService = app(NewOrderCountingRecordService::class);
  85. $start = Carbon::parse($request->start)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->start;
  86. $end = Carbon::parse($request->end)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->end;
  87. $ownerIds = $this->getCountingOwnerIds(null);
  88. $warehouseCountingRecords = $orderCountingRecordService->warehouseCountingRecords($start, $end, $ownerIds);
  89. return compact('warehouseCountingRecords');
  90. }
  91. public function laborReportsCountingRecordApi(Request $request)
  92. {
  93. $laborReportsCountingRecordService = app(LaborReportsCountingRecordService::class);
  94. $start = Carbon::parse($request->start)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->start;
  95. $end = Carbon::parse($request->end)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->end;
  96. $unit = $request->unit;
  97. $laborReportsCountingRecords = $laborReportsCountingRecordService->get($start, $end, $unit);
  98. return compact('laborReportsCountingRecords');
  99. }
  100. public function laborReportsUserGroupsCountApi(Request $request)
  101. {
  102. $laborReportsCountingRecordService = app(LaborReportsCountingRecordService::class);
  103. $start = Carbon::parse($request->start)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->start;
  104. $end = Carbon::parse($request->end)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->end;
  105. $laborReportsUserGroupsCount = $laborReportsCountingRecordService->userGroupsCount($start, $end);
  106. return compact('laborReportsUserGroupsCount');
  107. }
  108. public function getCountingOwnerIds($ownerIds)
  109. {
  110. $user = auth()->user();
  111. /** @var UserService $userService */
  112. $userService = app('UserService');
  113. $permittingOwnerIds = $userService->getPermittingOwnerIds($user);
  114. if (!$ownerIds) {
  115. return $permittingOwnerIds;
  116. }
  117. return Cache::remember(
  118. 'PermittingOwnerIds' . '_' . auth()->user()->id . '_' . implode('_', $ownerIds),
  119. 600, function () use ($ownerIds, $permittingOwnerIds) {
  120. /** @var User $user */
  121. return array_intersect($ownerIds, $permittingOwnerIds);
  122. });
  123. }
  124. /**
  125. * 获取称重统计数据API
  126. */
  127. public function weightApi()
  128. {
  129. //转化为Carbon
  130. $start = Carbon::parse(request("start"));
  131. $end = Carbon::parse(request("end"));
  132. //定义三个数组 空间换时间 避免结果集二次转换
  133. $title = []; //标题
  134. $data = []; //核心数据,二维数组
  135. foreach (CarbonPeriod::create($start,$end) as $date){
  136. /** @var $date Carbon */
  137. $str = $date->format("Y-m-d");
  138. $data[] = $this->getTargetData($str);
  139. $title[] = $str;
  140. }
  141. //大于31天转换为月份显示
  142. if ($end->diffInDays($start) > 31){
  143. $title = [];
  144. $sign = []; //标记是否已被插入
  145. $dataTemp = []; //临时存储
  146. foreach ($data as $datum){
  147. $month = substr($datum["date"],0,7);
  148. if (!isset($sign[$month])){
  149. $dataTemp[] = ["date"=>$month,"total"=>$datum["total"],"count"=>$datum["count"],"value"=>$datum["value"]];
  150. $title[] = $month;
  151. $sign[$month] = count($dataTemp)-1;
  152. }else{
  153. $dataTemp[$sign[$month]]["total"] += $datum["total"];
  154. $dataTemp[$sign[$month]]["count"] += $datum["count"];
  155. $dataTemp[$sign[$month]]["value"] = (string)($dataTemp[$sign[$month]]["total"] ? intval(($dataTemp[$sign[$month]]["count"]/$dataTemp[$sign[$month]]["total"])*100) : 0);
  156. }
  157. }
  158. $data = $dataTemp;
  159. }
  160. $this->success(["title"=>$title,"data"=>$data]);
  161. }
  162. /**
  163. * 根据指定日期获取目标统计数据
  164. *
  165. * @param string $date
  166. * @return array|null
  167. */
  168. private function getTargetData(string $date)
  169. {
  170. if ($date == date("Y-m-d")){
  171. $sql = <<<sql
  172. SELECT DATE_FORMAT(created_at,'%Y-%m-%d') date,
  173. SUM(CASE WHEN weighed_at IS NOT NULL THEN 1 ELSE 0 END) AS count,
  174. COUNT(1) total FROM order_packages WHERE created_at >= '{$date} 00:00:00' GROUP BY date
  175. sql;
  176. $pack = DB::selectOne(DB::raw($sql));
  177. if (!$pack)return ["date"=>$date,"total"=>0,"count"=>0,"value"=>0];
  178. return ["date"=>$pack->date,"total"=>$pack->total,"count"=>$pack->count,"value"=>(string)($pack->total ? intval(($pack->count/$pack->total)*100) : 0)];
  179. }
  180. return app(CacheService::class)->getOrExecute("weight.".$date,function ()use($date){
  181. $count = OrderPackageCountingRecord::query()->where("targeted_at",$date)->first();
  182. if (!$count)return ["date"=>$date,"total"=>0,"count"=>0,"value"=>0];
  183. return ["date"=>$count->targeted_at,"total"=>$count->total_count,"count"=>$count->un_weigh_count,"value"=>(string)($count->total_count ? intval(($count->un_weigh_count/$count->total_count)*100) : 0)];
  184. },config("cache.expirations.forever"));
  185. }
  186. }