DataController.php 20 KB

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