DataController.php 21 KB

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