DataController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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)->where('id','!=',1)->get();
  112. if(empty($orders->toArray())){
  113. foreach ($projects as $p){
  114. $values[] = 0;
  115. $names[] = $p->name;
  116. }
  117. }
  118. foreach($orders as $item) {
  119. if($this->inDate($names, $item->created_at, $type)) {
  120. foreach ($projects as $key => $project) {
  121. $values[$key][$cnt-1] = $project->id == $item->project_id ? $values[$key][$cnt-1] + ($item->money / 100) : $values[$key][$cnt-1];
  122. }
  123. } else {
  124. $names[$cnt] = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
  125. foreach ($projects as $key => $project) {
  126. $values[$key][$cnt] = $project->id == $item->project_id ? ($item->money / 100) : 0;
  127. }
  128. $cnt = $cnt + 1;
  129. }
  130. }
  131. $legends = $projects->pluck('name');
  132. return $this->success(['data' => compact('values', 'names', 'legends')]);
  133. }
  134. public function getRadarData(Request $request)
  135. {
  136. $orders = $this->getOrders($request);
  137. if(!$orders) return $this->error(['msg' => '']);
  138. $data = [];
  139. $projects = Project::whereIn('id', $request->input('project_ids'))->get();
  140. $indicator = [
  141. ['name' => '累计租赁花费', 'max' => 0],
  142. ['name' => '累计租赁数量', 'max' => 0],
  143. ['name' => '平均月消费', 'max' => 0],
  144. ['name' => '租赁次数', 'max' => 0],
  145. ['name' => '租赁天数', 'max' => 0]
  146. ];
  147. $legends = [];
  148. foreach($projects as $key => $project) {
  149. $tmp_orders = $orders->where('project_id', $project->id);
  150. $total_money = $tmp_orders->sum('money') / 100;
  151. $total_num = OrderDevice::whereIn('order_id', $orders->where('project_id', $project->id)->pluck('id'))->count();
  152. $months = $this->getMonths($tmp_orders);
  153. $mean_month = $total_money / $months;
  154. $rent_times = $tmp_orders->count();
  155. $rent_days = $this->getRentDays($tmp_orders);
  156. $indicator[0]['max'] = max($indicator[0]['max'], $total_money);
  157. $indicator[1]['max'] = max($indicator[1]['max'], $total_num);
  158. $indicator[2]['max'] = max($indicator[2]['max'], $mean_month);
  159. $indicator[3]['max'] = max($indicator[3]['max'], $rent_times);
  160. $indicator[4]['max'] = max($indicator[4]['max'], $rent_days);
  161. array_push($data, [
  162. 'name' => $project->name,
  163. 'value' => [$total_money, $total_num, $mean_month, $rent_times, $rent_days]
  164. ]);
  165. array_push($legends, $project->name);
  166. }
  167. return $this->success(['data' => compact('data', 'legends', 'indicator')]);
  168. }
  169. public function getMonths($orders)
  170. {
  171. $min_at = $orders->min('created_at');
  172. $max_at = $orders->max('updated_at');
  173. if($max_at && $max_at) $months = $min_at->diffInMonths($max_at);
  174. else $months = 1;
  175. return $months ? $months : 1;
  176. }
  177. public function getRentDays($orders)
  178. {
  179. $order_ids = $orders->pluck('id');
  180. $order_devices = OrderDevice::whereId('order_id', $order_ids)->get();
  181. $days = 0;
  182. foreach($order_devices as $order_device) {
  183. if($order_device->start_date && $order_device->end_date) {
  184. $days = $days + Carbon::createFromTimeString($order_device->start_date . ' 00:00:00')->diffInDays($order_device->end_date);
  185. }
  186. }
  187. return $days;
  188. }
  189. public function getPieData(Request $request)
  190. {
  191. $order_ids = $this->getOrderIds($request);
  192. if(!$order_ids) return $this->error(['msg' => '']);
  193. $total = OrderDevice::whereIn('order_id', $order_ids)->count();
  194. $devices = Device::all();
  195. $data = [];
  196. foreach($devices as $device) {
  197. $tmp = OrderDevice::whereIn('order_id', $order_ids)->where('device_id', $device->id)->count();
  198. $percent = $total > 0 ? round(($tmp / $total * 10000)) / 100 : '0';
  199. array_push($data, ['name' => $device->name, 'value' => $percent]);
  200. }
  201. $legends = $devices->pluck('name');
  202. return $this->success(['data' => compact('data', 'legends')]);
  203. }
  204. public function getDetailData(Request $request)
  205. {
  206. $order_ids = $this->getOrderIds($request);
  207. if(!$order_ids) return $this->error(['msg' => '']);
  208. $order_devices = OrderDevice::whereIn('order_id', $order_ids)->orderBy('created_at')->get();
  209. $devices = Device::all();
  210. $dates = [];
  211. // year|month
  212. $type = $request->input('type') ? $request->input('type') : 'year';
  213. $columns = [];
  214. $date = '';
  215. $values = [
  216. ['date' => '日期', 'total' => '总金额', 'data' => $devices->pluck('name')]
  217. ];
  218. foreach($order_devices as $item) {
  219. if($this->inDate($dates, $item->created_at, $type)) {
  220. foreach($devices as $key => $device) {
  221. $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
  222. $columns[$key] = $columns[$key] + $money;
  223. }
  224. } else {
  225. $date = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
  226. array_push($dates, $date);
  227. if(count($columns) > 0) {
  228. array_push($values, [
  229. 'date' => $date,
  230. 'total' => collect($columns)->sum() / 100,
  231. 'data' => $columns
  232. ]);
  233. }
  234. $columns = [];
  235. foreach($devices as $device) {
  236. $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
  237. array_push($columns, $money);
  238. }
  239. }
  240. }
  241. if(count($columns) > 0) {
  242. array_push($values, [
  243. 'date' => $date,
  244. 'total' => collect($columns)->sum(),
  245. 'data' => $columns
  246. ]);
  247. }
  248. return $this->success(['data' => $values]);
  249. }
  250. public function getTotalInfo(Request $request)
  251. {
  252. $start_at = $this->getStartAt($request->input('date'));
  253. $end_at = $this->getStartAt($request->input('date'));
  254. $total_money = $this->order->whereIn('project_id', $request->input('project_ids'))->sum('money') / 100;
  255. $month_money = $this->order->whereIn('project_id', $request->input('project_ids'))->where([
  256. ['created_at', '>=', $start_at],
  257. ['created_at', '<', $end_at]
  258. ])->sum('money') / 100;
  259. return $this->success(['data' => compact('total_money', 'month_money')]);
  260. }
  261. public function inDate($dates, $created_at, $type)
  262. {
  263. $date = $type == 'year' ? substr($created_at, 0, 7) : substr($created_at, 0, 10);
  264. return in_array($date, $dates);
  265. }
  266. public function parseDate($date)
  267. {
  268. $now = Carbon::now();
  269. if(!$date) return ['year' => $now->year, 'month' => $now->month, 'day' => $now->day];
  270. $items = explode('-', $date);
  271. $year = $now->year;
  272. $month = $now->month;
  273. $day = $now->day;
  274. if(isset($items[0])) {
  275. $year = (int)$items[0];
  276. $year = $year > 2010 && $year < 2050 ? $year : $now->year;
  277. }
  278. if(isset($items[1])) {
  279. $month = (int)$items[1];
  280. $month = $month > 0 && $month < 13 ? $month : $now->month;
  281. }
  282. if(isset($item[2])) {
  283. $day = (int)$items[2];
  284. $day = $day > 0 && $day < 32 ? $date : 1;
  285. }
  286. return compact('year', 'month', 'day');
  287. }
  288. public function getYearAndMonthMoney(Request $request)
  289. {
  290. $date = $request->input('date');
  291. if(!$date) {
  292. $year_money = Order::where('type', 1)->sum('money') / 100;
  293. $month_money = $year_money;
  294. } else {
  295. $start_year = Carbon::createFromTimeString(substr($date, 0, 4) . '-01-01 00:00:00')->toDateTimeString();
  296. $end_year = Carbon::createFromTimeString(substr($date, 0, 4) . '-01-01 00:00:00')->addYear()->toDateTimeString();
  297. $start_month = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  298. $end_month = Carbon::createFromTimeString($date . ' 00:00:00')->addMonth()->toDateTimeString();
  299. $year_money = Order::where([
  300. ['type', 1],
  301. ['created_at', '>=', $start_year],
  302. ['created_at', '<', $end_year]
  303. ])->sum('money') / 100;
  304. $month_money = Order::where([
  305. ['type', 1],
  306. ['created_at', '>=', $start_month],
  307. ['created_at', '<', $end_month]
  308. ])->sum('money') / 100;
  309. }
  310. return $this->success(['data' => compact('year_money', 'month_money')]);
  311. }
  312. public function getSingleStat(Request $request)
  313. {
  314. $items = Order::where('type', 1);
  315. $date = $request->input('date');
  316. $project_id = $request->input('project_id') ? $request->input('project_id') : Project::first()->id;
  317. $project = Project::find($project_id);
  318. $items = $items->where('project_id', $project->id);
  319. $sort_type = $request->input('sort_type') == 'year' ? 'year' : 'month';
  320. $date = $date ? $date : Carbon::now()->toDateString();
  321. $values = [];
  322. $names = [];
  323. $name = $sort_type == 'year' ? '每年租赁金额' : '每月租赁金额';
  324. if($sort_type == 'month') {
  325. $start_at = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  326. $end_at = Carbon::createFromTimeString($date . ' 00:00:00')->addYear()->toDateTimeString();
  327. $items = $items->where([
  328. ['created_at', '>=', $start_at],
  329. ['created_at', '<', $end_at]
  330. ]);
  331. $items = $items->get();
  332. for($i = 1; $i < 13; ++$i) {
  333. $start_at = Carbon::createFromTimeString($date . ' 00:00:00')->addMonths($i - 1)->toDateTimeString();
  334. $end_at = Carbon::createFromTimeString($date . ' 00:00:00')->addMonths($i)->toDateTimeString();
  335. $value = $items->where('created_at', '>=', $start_at)->where('created_at', '<', $end_at)->sum('money') / 100;
  336. array_push($values, $value);
  337. array_push($names, $i . '月');
  338. }
  339. } else {
  340. $start_at = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  341. $end_at = Carbon::now()->addYear(1)->toDateTimeString();
  342. $items = $items->where([
  343. ['created_at', '>=', $start_at],
  344. ['created_at', '<', $end_at]
  345. ]);
  346. $items = $items->get();
  347. $cnt = 1;
  348. $next_year = Carbon::now()->toDateTimeString();
  349. do {
  350. $start_at = Carbon::createFromTimeString($date . ' 00:00:00')->addYears($cnt - 1)->toDateTimeString();
  351. $end_at = Carbon::createFromTimeString($date . ' 00:00:00')->addYears($cnt)->toDateTimeString();
  352. $value = $items->where('created_at', '>=', $start_at)->where('created_at', '<', $end_at)->sum('money') / 100;
  353. array_push($values, $value);
  354. array_push($names, Carbon::createFromTimeString($date . ' 00:00:00')->addYear($cnt - 1)->year . '年');
  355. $cnt = $cnt + 1;
  356. } while($cnt < 10 && $end_at <= $next_year);
  357. }
  358. return $this->success(['data' => compact('values', 'names', 'project', 'name')]);
  359. }
  360. public function getMaxStat(Request $request)
  361. {
  362. $date = $request->input('date');
  363. $date = $date ? $date : Carbon::now()->toDateString();
  364. $start_at = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  365. $end_at = Carbon::now()->toDateTimeString();
  366. $items = OrderDevice::where([
  367. ['created_at', '>=', $start_at],
  368. ['created_at', '<', $end_at]
  369. ]);
  370. $in_items = ['device_ids', 'device_name_ids', 'spec_ids', 'rent_type_ids'];
  371. $key_items = ['device_id', 'device_name_id', 'spec_id', 'rent_type_id'];
  372. foreach ($in_items as $key => $item) {
  373. if($request->input($item)) {
  374. $item_ids = collect($request->input($item))->filter(function($id) {
  375. return $id;
  376. });
  377. if($item_ids && $item_ids->count() > 0) {
  378. $items = $items->whereIn($key_items[$key], $item_ids);
  379. }
  380. }
  381. }
  382. $items = $items->get();
  383. $projects = Project::where('id','!=',1)->get();
  384. foreach ($projects as $project) {
  385. $project->max_price = $items->where('project_id', $project->id)->max('price') / 100;
  386. }
  387. $orderBy = $request->input('orderBy');
  388. if($orderBy == 'asc') $projects = $projects->sortBy('max_price');
  389. else $projects = $projects->sortByDesc('max_price');
  390. $values = $projects->pluck('max_price');
  391. $names = $projects->pluck('name');
  392. return $this->success(['data' => compact('values', 'names')]);
  393. }
  394. public function projectStat(Request $request)
  395. {
  396. if($request->input('chartIndex') == 0) {
  397. return $this->getSingleStat($request);
  398. } else if($request->input('chartIndex') == 2) {
  399. return $this->getMaxStat($request);
  400. }
  401. $items = Order::where('type', 1);
  402. $date = $request->input('date');
  403. if($date) {
  404. $start_month = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  405. $end_month = Carbon::createFromTimeString($date . ' 00:00:00')->addMonth()->toDateTimeString();
  406. $items= $items->where([
  407. ['created_at', '>=', $start_month],
  408. ['created_at', '<', $end_month]
  409. ]);
  410. }
  411. $orderBy = $request->input('orderBy');
  412. $items = $items->groupBy('project_id')->selectRaw('sum(money) as sum, project_id')->pluck('sum', 'project_id');
  413. $projects = Project::where('id','!=',1)->get();
  414. foreach ($projects as $project) {
  415. $project->money = 0;
  416. foreach ($items as $key => $val) {
  417. if($key == $project->id) {
  418. $project->money = $val / 100;
  419. break;
  420. }
  421. }
  422. }
  423. if($orderBy == 'asc') $projects = $projects->sortBy('money');
  424. else $projects = $projects->sortByDesc('money');
  425. $values = $projects->pluck('money');
  426. $names = $projects->pluck('name');
  427. return $this->success(['data' => compact('values', 'names')]);
  428. }
  429. }