DataController.php 23 KB

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