DataController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. namespace App\Http\Controllers\Api\mini;
  3. use App\Models\Device;
  4. use App\Models\Order;
  5. use App\Models\OrderDevice;
  6. use App\Models\Project;
  7. use Carbon\Carbon;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Log;
  10. class DataController extends BaseController
  11. {
  12. protected $order;
  13. protected $order_device;
  14. protected $order_ids = [];
  15. protected $project_ids = [];
  16. protected $start_at;
  17. protected $end_at;
  18. public function __construct()
  19. {
  20. $this->order = new Order();
  21. $this->order_device = new OrderDevice();
  22. }
  23. public function getYearsAndMonths(Request $request)
  24. {
  25. $years = [];
  26. $this_year = Carbon::now()->year;
  27. for($i = 5; $i >= 0; $i--) {
  28. array_push($years, [
  29. 'name' => $this_year - $i,
  30. 'id' => $this_year - $i
  31. ]);
  32. }
  33. array_push($years, ['name' => '所有年', 'id' => '']);
  34. $months = [];
  35. for($i = 1; $i < 13; ++$i) {
  36. array_push($months, [
  37. 'name' => $i . '月',
  38. 'id' => $i
  39. ]);
  40. }
  41. $month = Carbon::now()->month;
  42. return $this->success(['data' => [
  43. 'array' => [$years, $months],
  44. 'index' => [count($years) - 2, $month - 1]
  45. ]]);
  46. }
  47. public function getDateInfo()
  48. {
  49. $max_date = Carbon::now()->toDateString();
  50. // $order = $this->order->orderBy('created_at', 'desc')->first();
  51. // $min_date = $order ? substr($order->created_at, 0, 10) : Carbon::now()->subYear()->toDateString();
  52. $min_date = Carbon::now()->subYears(10)->toDateString();
  53. $date = substr($max_date, 0, 7);
  54. $start_date = $max_date;
  55. $end_date = Carbon::now()->addMonth()->toDateString();
  56. $start_date = substr($start_date, 0, 7);
  57. $end_date = substr($end_date, 0, 7);
  58. return $this->success(['data' => compact('date', 'min_date', 'max_date', 'start_date', 'end_date')]);
  59. }
  60. public function getStartAt($date)
  61. {
  62. return strlen($date) <= 7 ? $date . '-01 00:00:00' : $date . ' 00:00:00';
  63. }
  64. public function getEndAt($date)
  65. {
  66. return strlen($date) <= 7 ? Carbon::createFromTimeString($date . '-01 00:00:00')->addMonth(1)->toDateTimeString() : Carbon::createFromTimeString($date . ' 00:00:00')->addDay(1)->toDateTimeString();
  67. }
  68. public function getOrders(Request $request)
  69. {
  70. $order_ids = $this->getOrderIds($request);
  71. $start_at = $this->getStartAt($request->input('start_date'));
  72. $end_at = $this->getEndAt($request->input('end_date'));
  73. return $this->order->whereIn('id', $order_ids)->where([
  74. ['type', '=', 1],
  75. ['created_at', '>=', $start_at],
  76. ['created_at', '<', $end_at]
  77. ])->get();
  78. }
  79. public function getOrderIds(Request $request)
  80. {
  81. $ids = $request->input('project_ids');
  82. if(!$ids || count($ids) < 0) return false;
  83. $this->project_ids = $ids;
  84. $order_devices = $this->order_device->whereIn('project_id', $ids);
  85. $in_items = ['device_ids', 'device_name_ids', 'spec_ids', 'rent_type_ids'];
  86. $key_items = ['device_id', 'device_name_id', 'spec_id', 'rent_type_id'];
  87. foreach ($in_items as $key => $item) {
  88. if($request->input($item)) {
  89. $item_ids = collect($request->input($item))->filter(function($id) {
  90. return $id;
  91. });
  92. if($item_ids && $item_ids->count() > 0) {
  93. $order_devices = $order_devices->whereIn($key_items[$key], $item_ids);
  94. }
  95. }
  96. }
  97. return $order_devices->pluck('order_id')->unique();
  98. }
  99. public function getStat(Request $request)
  100. {
  101. if(!$request->input('project_ids')) return $this->error(['msg' => '']);
  102. if($request->input('chart_type') == 'pie') return $this->getPieData($request);
  103. else if($request->input('chart_type') == 'radar') return $this->getRadarData($request);
  104. $orders = $this->getOrders($request);
  105. if(!$orders) return $this->error(['msg' => '']);
  106. $names = [];
  107. $values = [];
  108. // year|month
  109. $type = $request->input('type') ? $request->input('type') : 'year';
  110. $cnt = 0;
  111. $projects = Project::whereIn('id', $this->project_ids)->get();
  112. foreach($orders as $item) {
  113. if($this->inDate($names, $item->created_at, $type)) {
  114. foreach ($projects as $key => $project) {
  115. $values[$key][$cnt-1] = $project->id == $item->project_id ? $values[$key][$cnt-1] + ($item->money / 100) : $values[$key][$cnt-1];
  116. }
  117. } else {
  118. $names[$cnt] = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
  119. foreach ($projects as $key => $project) {
  120. $values[$key][$cnt] = $project->id == $item->project_id ? ($item->money / 100) : 0;
  121. }
  122. $cnt = $cnt + 1;
  123. }
  124. }
  125. $legends = $projects->pluck('name');
  126. return $this->success(['data' => compact('values', 'names', 'legends')]);
  127. }
  128. public function getRadarData(Request $request)
  129. {
  130. $orders = $this->getOrders($request);
  131. if(!$orders) return $this->error(['msg' => '']);
  132. $data = [];
  133. $projects = Project::whereIn('id', $request->input('project_ids'))->get();
  134. $indicator = [
  135. ['name' => '累计租赁花费', 'max' => 0],
  136. ['name' => '累计租赁数量', 'max' => 0],
  137. ['name' => '平均月消费', 'max' => 0],
  138. ['name' => '租赁次数', 'max' => 0],
  139. ['name' => '租赁天数', 'max' => 0]
  140. ];
  141. $legends = [];
  142. foreach($projects as $key => $project) {
  143. $tmp_orders = $orders->where('project_id', $project->id);
  144. $total_money = $tmp_orders->sum('money') / 100;
  145. $total_num = OrderDevice::whereIn('order_id', $orders->where('project_id', $project->id)->pluck('id'))->count();
  146. $months = $this->getMonths($tmp_orders);
  147. $mean_month = $total_money / $months;
  148. $rent_times = $tmp_orders->count();
  149. $rent_days = $this->getRentDays($tmp_orders);
  150. $indicator[0]['max'] = max($indicator[0]['max'], $total_money);
  151. $indicator[1]['max'] = max($indicator[1]['max'], $total_num);
  152. $indicator[2]['max'] = max($indicator[2]['max'], $mean_month);
  153. $indicator[3]['max'] = max($indicator[3]['max'], $rent_times);
  154. $indicator[4]['max'] = max($indicator[4]['max'], $rent_days);
  155. array_push($data, [
  156. 'name' => $project->name,
  157. 'value' => [$total_money, $total_num, $mean_month, $rent_times, $rent_days]
  158. ]);
  159. array_push($legends, $project->name);
  160. }
  161. return $this->success(['data' => compact('data', 'legends', 'indicator')]);
  162. }
  163. public function getMonths($orders)
  164. {
  165. $min_at = $orders->min('created_at');
  166. $max_at = $orders->max('updated_at');
  167. if($max_at && $max_at) $months = $min_at->diffInMonths($max_at);
  168. else $months = 1;
  169. return $months ? $months : 1;
  170. }
  171. public function getRentDays($orders)
  172. {
  173. $order_ids = $orders->pluck('id');
  174. $order_devices = OrderDevice::whereId('order_id', $order_ids)->get();
  175. $days = 0;
  176. foreach($order_devices as $order_device) {
  177. if($order_device->start_date && $order_device->end_date) {
  178. $days = $days + Carbon::createFromTimeString($order_device->start_date . ' 00:00:00')->diffInDays($order_device->end_date);
  179. }
  180. }
  181. return $days;
  182. }
  183. public function getPieData(Request $request)
  184. {
  185. $order_ids = $this->getOrderIds($request);
  186. if(!$order_ids) return $this->error(['msg' => '']);
  187. $total = OrderDevice::whereIn('order_id', $order_ids)->count();
  188. $devices = Device::all();
  189. $data = [];
  190. foreach($devices as $device) {
  191. $tmp = OrderDevice::whereIn('order_id', $order_ids)->where('device_id', $device->id)->count();
  192. $percent = $total > 0 ? round(($tmp / $total * 10000)) / 100 : '0';
  193. array_push($data, ['name' => $device->name, 'value' => $percent]);
  194. }
  195. $legends = $devices->pluck('name');
  196. return $this->success(['data' => compact('data', 'legends')]);
  197. }
  198. public function getDetailData(Request $request)
  199. {
  200. $order_ids = $this->getOrderIds($request);
  201. if(!$order_ids) return $this->error(['msg' => '']);
  202. $order_devices = OrderDevice::whereIn('order_id', $order_ids)->orderBy('created_at')->get();
  203. $devices = Device::all();
  204. $dates = [];
  205. // year|month
  206. $type = $request->input('type') ? $request->input('type') : 'year';
  207. $columns = [];
  208. $date = '';
  209. $values = [
  210. ['date' => '日期', 'total' => '总金额', 'data' => $devices->pluck('name')]
  211. ];
  212. foreach($order_devices as $item) {
  213. if($this->inDate($dates, $item->created_at, $type)) {
  214. foreach($devices as $key => $device) {
  215. $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
  216. $columns[$key] = $columns[$key] + $money;
  217. }
  218. } else {
  219. $date = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
  220. array_push($dates, $date);
  221. if(count($columns) > 0) {
  222. array_push($values, [
  223. 'date' => $date,
  224. 'total' => collect($columns)->sum() / 100,
  225. 'data' => $columns
  226. ]);
  227. }
  228. $columns = [];
  229. foreach($devices as $device) {
  230. $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
  231. array_push($columns, $money);
  232. }
  233. }
  234. }
  235. if(count($columns) > 0) {
  236. array_push($values, [
  237. 'date' => $date,
  238. 'total' => collect($columns)->sum(),
  239. 'data' => $columns
  240. ]);
  241. }
  242. return $this->success(['data' => $values]);
  243. }
  244. public function getTotalInfo(Request $request)
  245. {
  246. $start_at = $this->getStartAt($request->input('date'));
  247. $end_at = $this->getStartAt($request->input('date'));
  248. $total_money = $this->order->whereIn('project_id', $request->input('project_ids'))->sum('money') / 100;
  249. $month_money = $this->order->whereIn('project_id', $request->input('project_ids'))->where([
  250. ['created_at', '>=', $start_at],
  251. ['created_at', '<', $end_at]
  252. ])->sum('money') / 100;
  253. return $this->success(['data' => compact('total_money', 'month_money')]);
  254. }
  255. public function inDate($dates, $created_at, $type)
  256. {
  257. $date = $type == 'year' ? substr($created_at, 0, 7) : substr($created_at, 0, 10);
  258. return in_array($date, $dates);
  259. }
  260. public function parseDate($date)
  261. {
  262. $now = Carbon::now();
  263. if(!$date) return ['year' => $now->year, 'month' => $now->month, 'day' => $now->day];
  264. $items = explode('-', $date);
  265. $year = $now->year;
  266. $month = $now->month;
  267. $day = $now->day;
  268. if(isset($items[0])) {
  269. $year = (int)$items[0];
  270. $year = $year > 2010 && $year < 2050 ? $year : $now->year;
  271. }
  272. if(isset($items[1])) {
  273. $month = (int)$items[1];
  274. $month = $month > 0 && $month < 13 ? $month : $now->month;
  275. }
  276. if(isset($item[2])) {
  277. $day = (int)$items[2];
  278. $day = $day > 0 && $day < 32 ? $date : 1;
  279. }
  280. return compact('year', 'month', 'day');
  281. }
  282. public function getYearAndMonthMoney(Request $request)
  283. {
  284. $date = $request->input('date');
  285. if(!$date) {
  286. $year_money = Order::where('type', 1)->sum('money') / 100;
  287. $month_money = $year_money;
  288. } else {
  289. $start_year = Carbon::createFromTimeString(substr($date, 0, 4) . '-01-01 00:00:00')->toDateTimeString();
  290. $end_year = Carbon::createFromTimeString(substr($date, 0, 4) . '-01-01 00:00:00')->addYear()->toDateTimeString();
  291. $start_month = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  292. $end_month = Carbon::createFromTimeString($date . ' 00:00:00')->addMonth()->toDateTimeString();
  293. $year_money = Order::where([
  294. ['type', 1],
  295. ['created_at', '>=', $start_year],
  296. ['created_at', '<', $end_year]
  297. ])->sum('money') / 100;
  298. $month_money = Order::where([
  299. ['type', 1],
  300. ['created_at', '>=', $start_month],
  301. ['created_at', '<', $end_month]
  302. ])->sum('money') / 100;
  303. }
  304. return $this->success(['data' => compact('year_money', 'month_money')]);
  305. }
  306. public function getSingleStat(Request $request)
  307. {
  308. $items = Order::where('type', 1);
  309. $date = $request->input('date');
  310. $project_id = $request->input('project_id') ? $request->input('project_id') : Project::first()->id;
  311. $project = Project::find($project_id);
  312. $items = $items->where('project_id', $project->id);
  313. $sort_type = $request->input('sort_type') == 'year' ? 'year' : 'month';
  314. $date = $date ? $date : Carbon::now()->toDateString();
  315. $values = [];
  316. $names = [];
  317. $name = $sort_type == 'year' ? '每年租赁金额' : '每月租赁金额';
  318. if($sort_type == 'month') {
  319. $start_at = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  320. $end_at = Carbon::createFromTimeString($date . ' 00:00:00')->addYear()->toDateTimeString();
  321. $items = $items->where([
  322. ['created_at', '>=', $start_at],
  323. ['created_at', '<', $end_at]
  324. ]);
  325. $items = $items->get();
  326. for($i = 1; $i < 13; ++$i) {
  327. $start_at = Carbon::createFromTimeString($date . ' 00:00:00')->addMonths($i - 1)->toDateTimeString();
  328. $end_at = Carbon::createFromTimeString($date . ' 00:00:00')->addMonths($i)->toDateTimeString();
  329. $value = $items->where('created_at', '>=', $start_at)->where('created_at', '<', $end_at)->sum('money') / 100;
  330. array_push($values, $value);
  331. array_push($names, $i . '月');
  332. }
  333. } else {
  334. $start_at = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  335. $end_at = Carbon::now()->addYear(1)->toDateTimeString();
  336. $items = $items->where([
  337. ['created_at', '>=', $start_at],
  338. ['created_at', '<', $end_at]
  339. ]);
  340. $items = $items->get();
  341. $cnt = 1;
  342. $next_year = Carbon::now()->toDateTimeString();
  343. do {
  344. $start_at = Carbon::createFromTimeString($date . ' 00:00:00')->addYears($cnt - 1)->toDateTimeString();
  345. $end_at = Carbon::createFromTimeString($date . ' 00:00:00')->addYears($cnt)->toDateTimeString();
  346. $value = $items->where('created_at', '>=', $start_at)->where('created_at', '<', $end_at)->sum('money') / 100;
  347. array_push($values, $value);
  348. array_push($names, Carbon::createFromTimeString($date . ' 00:00:00')->addYear($cnt - 1)->year . '年');
  349. $cnt = $cnt + 1;
  350. } while($cnt < 10 && $end_at <= $next_year);
  351. }
  352. return $this->success(['data' => compact('values', 'names', 'project', 'name')]);
  353. }
  354. public function getMaxStat(Request $request)
  355. {
  356. $date = $request->input('date');
  357. $date = $date ? $date : Carbon::now()->toDateString();
  358. $start_at = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  359. $end_at = Carbon::now()->toDateTimeString();
  360. $items = OrderDevice::where([
  361. ['created_at', '>=', $start_at],
  362. ['created_at', '<', $end_at]
  363. ]);
  364. $in_items = ['device_ids', 'device_name_ids', 'spec_ids', 'rent_type_ids'];
  365. $key_items = ['device_id', 'device_name_id', 'spec_id', 'rent_type_id'];
  366. foreach ($in_items as $key => $item) {
  367. if($request->input($item)) {
  368. $item_ids = collect($request->input($item))->filter(function($id) {
  369. return $id;
  370. });
  371. if($item_ids && $item_ids->count() > 0) {
  372. $items = $items->whereIn($key_items[$key], $item_ids);
  373. }
  374. }
  375. }
  376. $items = $items->get();
  377. $projects = Project::all();
  378. foreach ($projects as $project) {
  379. $project->max_price = $items->where('project_id', $project->id)->max('price') / 100;
  380. }
  381. $orderBy = $request->input('orderBy');
  382. if($orderBy == 'asc') $projects = $projects->sortBy('max_price');
  383. else $projects = $projects->sortByDesc('max_price');
  384. $values = $projects->pluck('max_price');
  385. $names = $projects->pluck('name');
  386. return $this->success(['data' => compact('values', 'names')]);
  387. }
  388. public function projectStat(Request $request)
  389. {
  390. if($request->input('chartIndex') == 0) {
  391. return $this->getSingleStat($request);
  392. } else if($request->input('chartIndex') == 2) {
  393. return $this->getMaxStat($request);
  394. }
  395. $items = Order::where('type', 1);
  396. $date = $request->input('date');
  397. if($date) {
  398. $start_month = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  399. $end_month = Carbon::createFromTimeString($date . ' 00:00:00')->addMonth()->toDateTimeString();
  400. $items= $items->where([
  401. ['created_at', '>=', $start_month],
  402. ['created_at', '<', $end_month]
  403. ]);
  404. }
  405. $orderBy = $request->input('orderBy');
  406. $items = $items->groupBy('project_id')->selectRaw('sum(money) as sum, project_id')->pluck('sum', 'project_id');
  407. $projects = Project::all();
  408. foreach ($projects as $project) {
  409. $project->money = 0;
  410. foreach ($items as $key => $val) {
  411. if($key == $project->id) {
  412. $project->money = $val / 100;
  413. break;
  414. }
  415. }
  416. }
  417. if($orderBy == 'asc') $projects = $projects->sortBy('money');
  418. else $projects = $projects->sortByDesc('money');
  419. $values = $projects->pluck('money');
  420. $names = $projects->pluck('name');
  421. return $this->success(['data' => compact('values', 'names')]);
  422. }
  423. }