DataController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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\DB;
  14. use Illuminate\Support\Facades\Log;
  15. use phpDocumentor\Reflection\DocBlock;
  16. class DataController extends BaseController
  17. {
  18. protected $order;
  19. protected $order_device;
  20. protected $order_ids = [];
  21. protected $project_ids = [];
  22. protected $start_at;
  23. protected $end_at;
  24. public function __construct()
  25. {
  26. $this->order = new Order();
  27. $this->order_device = new OrderDevice();
  28. }
  29. public function getYearsAndMonths(Request $request)
  30. {
  31. $years = [];
  32. $this_year = Carbon::now()->year;
  33. for($i = 5; $i >= 0; $i--) {
  34. array_push($years, [
  35. 'name' => $this_year - $i,
  36. 'id' => $this_year - $i
  37. ]);
  38. }
  39. array_push($years, ['name' => '所有年', 'id' => '']);
  40. $months = [];
  41. for($i = 1; $i < 13; ++$i) {
  42. array_push($months, [
  43. 'name' => $i . '月',
  44. 'id' => $i
  45. ]);
  46. }
  47. $month = Carbon::now()->month;
  48. return $this->success(['data' => [
  49. 'array' => [$years, $months],
  50. 'index' => [count($years) - 2, $month - 1]
  51. ]]);
  52. }
  53. public function getDateInfo()
  54. {
  55. $max_date = Carbon::now()->toDateString();
  56. // $order = $this->order->orderBy('created_at', 'desc')->first();
  57. // $min_date = $order ? substr($order->created_at, 0, 10) : Carbon::now()->subYear()->toDateString();
  58. $min_date = Carbon::now()->subYears(10)->toDateString();
  59. $date = substr($max_date, 0, 7);
  60. $start_date = $max_date;
  61. $end_date = Carbon::now()->addMonth()->toDateString();
  62. $start_date = substr($start_date, 0, 7);
  63. $end_date = substr($end_date, 0, 7);
  64. return $this->success(['data' => compact('date', 'min_date', 'max_date', 'start_date', 'end_date')]);
  65. }
  66. public function getStartAt($date)
  67. {
  68. return strlen($date) <= 7 ? $date . '-01 00:00:00' : $date . ' 00:00:00';
  69. }
  70. public function getEndAt($date)
  71. {
  72. return strlen($date) <= 7 ? Carbon::createFromTimeString($date . '-01 00:00:00')->addMonth(1)->toDateTimeString() : Carbon::createFromTimeString($date . ' 00:00:00')->addDay(1)->toDateTimeString();
  73. }
  74. public function getYearEnd($date,$is_end=true)
  75. {
  76. return $is_end ? Carbon::createFromTimeString(substr($date,0,4) . '-01-01 00:00:00')->addYear(1)->toDateTimeString() : Carbon::createFromTimeString(substr($date,0,4) . '-01-01 00:00:00')->toDateTimeString() ;
  77. }
  78. public function getOrders(Request $request)
  79. {
  80. $order_ids = $this->getOrderIds($request);
  81. $start_at = $this->getStartAt($request->input('start_date'));
  82. $end_at = $this->getEndAt($request->input('end_date'));
  83. return $this->order->whereIn('id', $order_ids)->where([
  84. ['type', '=', 1],
  85. ['created_at', '>=', $start_at],
  86. ['created_at', '<', $end_at]
  87. ])->get();
  88. }
  89. public function getOrderIds(Request $request)
  90. {
  91. $ids = $request->input('project_ids');
  92. if(!$ids || count($ids) < 0) return false;
  93. $this->project_ids = $ids;
  94. $order_devices = $this->order_device->whereIn('project_id', $ids);
  95. $in_items = ['device_ids', 'device_name_ids', 'spec_ids', 'rent_type_ids'];
  96. $key_items = ['device_id', 'device_name_id', 'spec_id', 'rent_type_id'];
  97. foreach ($in_items as $key => $item) {
  98. if($request->input($item)) {
  99. $item_ids = collect($request->input($item))->filter(function($id) {
  100. return $id;
  101. });
  102. if($item_ids && $item_ids->count() > 0) {
  103. $order_devices = $order_devices->whereIn($key_items[$key], $item_ids);
  104. }
  105. }
  106. }
  107. return $order_devices->pluck('order_id')->unique();
  108. }
  109. public function getStat(Request $request)
  110. {
  111. $info = [];
  112. $device_arr = $request->input('device_ids');
  113. $device_name_arr = $request->input('device_name_ids');
  114. $spec_arr = $request->input('spec_ids');
  115. if (array_sum($device_arr) != 0)
  116. {
  117. array_push($info,Device::whereIn('id',$device_arr)->pluck('name')->toArray()) ;
  118. array_push($info,DeviceName::whereIn('id',$device_name_arr)->pluck('name')->toArray()) ;
  119. array_push($info,Spec::whereIn('device_id',$device_arr)
  120. ->whereIn('device_name_id',$device_name_arr)
  121. ->whereIn('id',$spec_arr)
  122. ->pluck('name')->toArray());
  123. array_push($info,RentType::where('id',$request->input('rent_type_ids'))->value('name'));
  124. }
  125. if(!$request->input('project_ids')) return $this->error(['msg' => '']);
  126. if($request->input('chart_type') == 'pie') return $this->getPieData($request);
  127. if($request->input('chart_type') == 'line' || $request->input('chart_type') == 'bar' ) return $this->getBarData($request);
  128. else if($request->input('chart_type') == 'radar') return $this->getRadarData($request);
  129. $orders = $this->getOrders($request);
  130. $names = [];
  131. $values = [];
  132. // year|month
  133. $type = $request->input('type') ? $request->input('type') : 'year';
  134. $cnt = 0;
  135. $projects = Project::whereIn('id', $this->project_ids)->where('id','!=',1)->get();
  136. if(empty($orders->toArray())){
  137. foreach ($projects as $p){
  138. $values[] = 0;
  139. $names[] = $p->name;
  140. }
  141. }
  142. foreach($orders as $item) {
  143. if($this->inDate($names, $item->created_at, $type)) {
  144. foreach ($projects as $key => $project) {
  145. $values[$key][$cnt-1] = $project->id == $item->project_id ? $values[$key][$cnt-1] + ($item->money / 100) : $values[$key][$cnt-1];
  146. }
  147. } else {
  148. $names[$cnt] = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
  149. foreach ($projects as $key => $project) {
  150. $values[$key][$cnt] = $project->id == $item->project_id ? ($item->money / 100) : 0;
  151. }
  152. $cnt = $cnt + 1;
  153. }
  154. }
  155. $legends = $projects->pluck('name');
  156. return $this->success(['data' => compact('values', 'names', 'legends','info')]);
  157. }
  158. /**
  159. * @获取折现图,柱状图订单总金额
  160. * @param $request
  161. * @return \Illuminate\Http\JsonResponse
  162. * User: HaiJun.Wang
  163. * Date: 2021/4/29 14:48
  164. */
  165. public function getBarData($request)
  166. {
  167. $project_ids = $request->input('project_ids');
  168. $projects = Project::whereIn('id', $project_ids)->where('id','!=',1)->get();
  169. $values = [];
  170. $info = [];
  171. $names = [];
  172. $start_at = $this->getStartAt($request->input('date'));
  173. $end_at = $this->getEndAt($request->input('date'));
  174. foreach ($projects as $p){
  175. $order_total= OrderDevice::where(['project_id'=>$p->id])
  176. ->where([['created_at', '>=', $start_at], ['created_at', '<', $end_at]])
  177. ->select(DB::raw('SUM(price * quantity) money'))->first()->toArray();
  178. if(!empty($order_total)){
  179. $values[] = ($order_total['money'] /100);
  180. } else {
  181. $values[] = 0;
  182. }
  183. $names[] = $p->name;
  184. }
  185. $legends = $projects->pluck('name');
  186. return $this->success(['data' => compact('values', 'names', 'legends','info')]);
  187. }
  188. public function getRadarData(Request $request)
  189. {
  190. $orders = $this->getOrders($request);
  191. if(!$orders) return $this->error(['msg' => '']);
  192. $data = [];
  193. $projects = Project::whereIn('id', $request->input('project_ids'))->get();
  194. $indicator = [
  195. ['name' => '累计租赁花费', 'max' => 0],
  196. ['name' => '累计租赁数量', 'max' => 0],
  197. ['name' => '平均月消费', 'max' => 0],
  198. ['name' => '租赁次数', 'max' => 0],
  199. ['name' => '租赁天数', 'max' => 0]
  200. ];
  201. $legends = [];
  202. foreach($projects as $key => $project) {
  203. $tmp_orders = $orders->where('project_id', $project->id);
  204. $total_money = $tmp_orders->sum('money') / 100;
  205. $total_num = OrderDevice::whereIn('order_id', $orders->where('project_id', $project->id)->pluck('id'))->count();
  206. $months = $this->getMonths($tmp_orders);
  207. $mean_month = $total_money / $months;
  208. $rent_times = $tmp_orders->count();
  209. $rent_days = $this->getRentDays($tmp_orders);
  210. $indicator[0]['max'] = max($indicator[0]['max'], $total_money);
  211. $indicator[1]['max'] = max($indicator[1]['max'], $total_num);
  212. $indicator[2]['max'] = max($indicator[2]['max'], $mean_month);
  213. $indicator[3]['max'] = max($indicator[3]['max'], $rent_times);
  214. $indicator[4]['max'] = max($indicator[4]['max'], $rent_days);
  215. array_push($data, [
  216. 'name' => $project->name,
  217. 'value' => [$total_money, $total_num, $mean_month, $rent_times, $rent_days]
  218. ]);
  219. array_push($legends, $project->name);
  220. }
  221. return $this->success(['data' => compact('data', 'legends', 'indicator')]);
  222. }
  223. public function getMonths($orders)
  224. {
  225. $min_at = $orders->min('created_at');
  226. $max_at = $orders->max('updated_at');
  227. if($max_at && $max_at) $months = $min_at->diffInMonths($max_at);
  228. else $months = 1;
  229. return $months ? $months : 1;
  230. }
  231. public function getRentDays($orders)
  232. {
  233. $order_ids = $orders->pluck('id');
  234. $order_devices = OrderDevice::whereId('order_id', $order_ids)->get();
  235. $days = 0;
  236. foreach($order_devices as $order_device) {
  237. if($order_device->start_date && $order_device->end_date) {
  238. $days = $days + Carbon::createFromTimeString($order_device->start_date . ' 00:00:00')->diffInDays($order_device->end_date);
  239. }
  240. }
  241. return $days;
  242. }
  243. public function getPieData(Request $request)
  244. {
  245. $order_ids = $this->getOrderIds($request);
  246. if(!$order_ids) return $this->error(['msg' => '']);
  247. $total = OrderDevice::whereIn('order_id', $order_ids)->count();
  248. $devices = Device::all();
  249. $data = [];
  250. foreach($devices as $device) {
  251. $tmp = OrderDevice::whereIn('order_id', $order_ids)->where('device_id', $device->id)->count();
  252. $percent = $total > 0 ? round(($tmp / $total * 10000)) / 100 : '0';
  253. array_push($data, ['name' => $device->name, 'value' => $percent]);
  254. }
  255. $legends = $devices->pluck('name');
  256. return $this->success(['data' => compact('data', 'legends')]);
  257. }
  258. public function getDetailData(Request $request)
  259. {
  260. // $order_ids = $this->getOrderIds($request);
  261. // if(!$order_ids) return $this->error(['msg' => '']);
  262. // $order_devices = OrderDevice::whereIn('order_id', $order_ids)->orderBy('created_at')->get();
  263. // $devices = Device::all();
  264. //
  265. // $dates = [];
  266. // // year|month
  267. // $type = $request->input('type') ? $request->input('type') : 'year';
  268. // $columns = [];
  269. // $date = '';
  270. // $values = [
  271. // ['date' => '日期', 'total' => '总金额', 'data' => $devices->pluck('name')]
  272. // ];
  273. // foreach($order_devices as $item) {
  274. // if($this->inDate($dates, $item->created_at, $type)) {
  275. // foreach($devices as $key => $device) {
  276. // $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
  277. // $columns[$key] = $columns[$key] + $money;
  278. // }
  279. // } else {
  280. // $date = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
  281. // array_push($dates, $date);
  282. // if(count($columns) > 0) {
  283. // array_push($values, [
  284. // 'date' => $date,
  285. // 'total' => collect($columns)->sum() / 100,
  286. // 'data' => $columns
  287. // ]);
  288. // }
  289. // $columns = [];
  290. // foreach($devices as $device) {
  291. // $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
  292. // array_push($columns, $money);
  293. // }
  294. // }
  295. // }
  296. // if(count($columns) > 0) {
  297. // array_push($values, [
  298. // 'date' => $date,
  299. // 'total' => collect($columns)->sum(),
  300. // 'data' => $columns
  301. // ]);
  302. // }
  303. // return $this->success(['data' => $values]);
  304. $start_date = $this->getStartAt($request->input('start_date'));
  305. $end_date = $this->getYearEnd($request->input('end_date'));
  306. $project_id = $request->input('project_ids');
  307. $project_id = $project_id[0];
  308. if($request->input('type') == 'year'){
  309. $year = date('Y',strtotime($start_date));
  310. $items = OrderOverviewModel::whereYear('date',$year)->where('project_id',$project_id)->get()->toArray();
  311. $device_ids = OrderDevice::where('project_id',$project_id)->whereYear('created_at',$year)->where('device_id','>',0)->distinct('device_id')->pluck('device_id')->toArray();
  312. } else {
  313. $items = OrderOverviewModel::whereBetween('date',[$start_date,$end_date])->where('project_id',$project_id)->get()->toArray();
  314. $device_ids = OrderDevice::where('project_id',$project_id)->whereBetween('created_at',[$start_date,$end_date])>where('device_id','>',0)->distinct('device_id')->pluck('device_id')->toArray();
  315. }
  316. $base_name = [];
  317. if(!empty($device_ids)){
  318. //设备基础金额 默认为0
  319. $base_data = array_fill(0,count($device_ids),0);
  320. $base_name = Device::whereIn('id',$device_ids)->pluck('name')->toArray();
  321. }
  322. //头部标题
  323. $data = [
  324. ['date' => '日期', 'total' => '总金额', 'data' => $base_name]
  325. ];
  326. foreach ($items as &$item)
  327. {
  328. $arr = [
  329. 'data'=>$base_data
  330. ];
  331. $arr['total'] = $item['total_price']/100;
  332. $arr['date'] = date('Y-m',strtotime($item['date']));
  333. //获取到月份的开始时间和结束时间
  334. $start_at = $item['date'];
  335. $end_at = $this->getEndAt(date('Y-m',strtotime($item['date'])));
  336. //获取到订单数组
  337. $order_arr = Order::
  338. whereBetween('updated_at',[$start_at,$end_at])
  339. ->where('project_id',$project_id)
  340. ->where('status',3)
  341. ->where('type',1)
  342. ->pluck('id')->toArray();
  343. if(!empty($order_arr)){
  344. //获取对应设备的总金额
  345. foreach ($device_ids as $k=>$id){
  346. $device_total = OrderDevice::whereIn('order_id',$order_arr)->where('device_id',$id)->select('quantity','price','device_id',DB::raw('SUM(price * quantity) AS total_price'))->first()->toArray();
  347. if(!empty($device_total['total_price'])){
  348. $base_data[$k] = $device_total['total_price']/100;
  349. }
  350. }
  351. }
  352. $arr['data'] = $base_data;
  353. array_push($data,$arr);
  354. }
  355. return $this->success(['data' => $data]);
  356. }
  357. public function getTotalInfo(Request $request)
  358. {
  359. if (!$request->input('project_ids'))
  360. {
  361. return $this->error(['msg' => '暂未选择项目']);
  362. }
  363. $start_at = $this->getStartAt($request->input('date'));
  364. $end_at = $this->getEndAt($request->input('date'));
  365. $total_money = $this->order->whereIn('project_id', $request->input('project_ids'))->sum('money') / 100;
  366. $month_money = $this->order->whereIn('project_id', $request->input('project_ids'))->where([
  367. ['created_at', '>=', $start_at],
  368. ['created_at', '<', $end_at]
  369. ])->sum('money') / 100;
  370. return $this->success(['data' => compact('total_money', 'month_money')]);
  371. }
  372. public function inDate($dates, $created_at, $type)
  373. {
  374. $date = $type == 'year' ? substr($created_at, 0, 7) : substr($created_at, 0, 10);
  375. return in_array($date, $dates);
  376. }
  377. public function parseDate($date)
  378. {
  379. $now = Carbon::now();
  380. if(!$date) return ['year' => $now->year, 'month' => $now->month, 'day' => $now->day];
  381. $items = explode('-', $date);
  382. $year = $now->year;
  383. $month = $now->month;
  384. $day = $now->day;
  385. if(isset($items[0])) {
  386. $year = (int)$items[0];
  387. $year = $year > 2010 && $year < 2050 ? $year : $now->year;
  388. }
  389. if(isset($items[1])) {
  390. $month = (int)$items[1];
  391. $month = $month > 0 && $month < 13 ? $month : $now->month;
  392. }
  393. if(isset($item[2])) {
  394. $day = (int)$items[2];
  395. $day = $day > 0 && $day < 32 ? $date : 1;
  396. }
  397. return compact('year', 'month', 'day');
  398. }
  399. public function getYearAndMonthMoney(Request $request)
  400. {
  401. $date = $request->input('date');
  402. if(!$date) {
  403. $year_money = Order::where('type', 1)->sum('money') / 100;
  404. $month_money = $year_money;
  405. } else {
  406. $start_year = Carbon::createFromTimeString(substr($date, 0, 4) . '-01-01 00:00:00')->toDateTimeString();
  407. $end_year = Carbon::createFromTimeString(substr($date, 0, 4) . '-01-01 00:00:00')->addYear()->toDateTimeString();
  408. $start_month = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  409. $end_month = Carbon::createFromTimeString($date . ' 00:00:00')->addMonth()->toDateTimeString();
  410. $year_money = Order::where([
  411. ['type', 1],
  412. ['created_at', '>=', $start_year],
  413. ['created_at', '<', $end_year]
  414. ])->sum('money') / 100;
  415. $month_money = Order::where([
  416. ['type', 1],
  417. ['created_at', '>=', $start_month],
  418. ['created_at', '<', $end_month]
  419. ])->sum('money') / 100;
  420. }
  421. return $this->success(['data' => compact('year_money', 'month_money')]);
  422. }
  423. public function getProjectYears()
  424. {
  425. $start=2020;
  426. $now = date('Y',time());
  427. $years_arr = [[
  428. 'text' => '请选择年份',
  429. 'value' => ''
  430. ]];
  431. for ($i = 0;$i<=($now-$start);$i++)
  432. {
  433. $years_arr[$i+1]['text'] = ($start+$i).'年';
  434. $years_arr[$i+1]['value'] = $i+1;
  435. }
  436. return $this->success(['msg' => '操作成功', 'data' => $years_arr]);
  437. }
  438. public function getSingleStat(Request $request)
  439. {
  440. //如果初始进来不传筛选的年份,那就默认选择当前的年份
  441. $project_year = $request->input('project_year')? $request->input('project_year'):date('Y',time());
  442. //如果有项目id,那就用项目id,如果没有就默认选择第一个项目
  443. if ($request->input('project_id')!=0)
  444. {
  445. $all_project = 0;
  446. $project_id = $request->input('project_id') ? $request->input('project_id') : Project::where('id','>',1)->first()->id;
  447. $project = Project::find($project_id);
  448. }else
  449. {
  450. $all_project = 1;
  451. }
  452. $sort_type = $request->input('sort_type') == 'year' ? 'year' : 'month';
  453. $values = [];
  454. $names = [];
  455. $name = $sort_type == 'year' ? '每年租赁金额' : '每月租赁金额';
  456. if($sort_type == 'month') {
  457. $start_at = $project_year.'-01-01 00:00:00';
  458. $end_at = ($project_year+1).'-01-01 00:00:00';
  459. for($i = 1; $i < 13; $i++) {
  460. if ($i>9&&$i!=12)
  461. {
  462. $start_month_at = $project_year.'-'.$i.'-01 00:00:00';
  463. }else
  464. {
  465. $start_month_at = $project_year.'-0'.$i.'-01 00:00:00';
  466. }
  467. if ($i>=9&&$i!=12)
  468. {
  469. $end_month_at = $project_year.'-'.($i+1).'-01 00:00:00';
  470. }else
  471. {
  472. $end_month_at = $project_year.'-0'.($i+1).'-01 00:00:00';
  473. }
  474. if ($i==12)
  475. {
  476. $start_month_at = $project_year.'-'.$i.'-01 00:00:00';
  477. $end_month_at = ($project_year+1).'-01-01 00:00:00';
  478. }
  479. if ($all_project == 0)
  480. {
  481. $value = Order::where('type',1)
  482. ->where('project_id', $project->id)
  483. ->whereBetween('created_at',[$start_at,$end_at])
  484. ->whereBetween('created_at',[$start_month_at,$end_month_at])
  485. ->sum('money');
  486. $value = $value/100;
  487. }else
  488. {
  489. $value = Order::where('type',1)
  490. ->whereBetween('created_at',[$start_at,$end_at])
  491. ->whereBetween('created_at',[$start_month_at,$end_month_at])
  492. ->sum('money');
  493. $value = $value/100;
  494. }
  495. array_push($values, $value);
  496. array_push($names, $i . '月');
  497. }
  498. } else {
  499. $start=2020;
  500. $now = date('Y',time());
  501. $year = [];
  502. for ($i = 0;$i<=($now-$start);$i++)
  503. {
  504. array_push($year,($start+$i));
  505. array_push($names,($start+$i).'年');
  506. }
  507. foreach ($year as $v)
  508. {
  509. $start_year_at = $v.'-01-01 00:00:00';
  510. $end_year_at = ($v+1).'-01-01 00:00:00';
  511. if ($all_project == 0)
  512. {
  513. $value = Order::where('type',1)
  514. ->where('project_id', $project->id)
  515. ->whereBetween('created_at',[$start_year_at,$end_year_at])
  516. ->sum('money');
  517. $value = $value/100;
  518. }else{
  519. $value = Order::where('type',1)
  520. ->whereBetween('created_at',[$start_year_at,$end_year_at])
  521. ->sum('money');
  522. $value = $value/100;
  523. }
  524. array_push($values, $value);
  525. }
  526. }
  527. if ($all_project == 0)
  528. {
  529. return $this->success(['data' => compact('values', 'names', 'project', 'name')]);
  530. }else
  531. {
  532. return $this->success(['data' => compact('values', 'names', 'name')]);
  533. }
  534. }
  535. //租赁单价统计
  536. public function getMaxStat(Request $request)
  537. {
  538. $info = [];
  539. if ($request->input('devices')!=null)
  540. {
  541. array_push($info,Device::where('id',$request->input('device_ids'))->value('name')) ;
  542. array_push($info,DeviceName::where('id',$request->input('device_name_ids'))->value('name')) ;
  543. 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'));
  544. array_push($info,RentType::where('id',$request->input('rent_type_ids'))->value('name'));
  545. }
  546. $date = $request->input('date');
  547. $date = $date ? $date : Carbon::now()->toDateString();
  548. $start_at = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  549. $end_at = Carbon::now()->toDateTimeString();
  550. $items = OrderDevice::where([
  551. ['created_at', '>=', $start_at],
  552. ['created_at', '<', $end_at]
  553. ]);
  554. $in_items = ['device_ids', 'device_name_ids', 'spec_ids', 'rent_type_ids'];
  555. $key_items = ['device_id', 'device_name_id', 'spec_id', 'rent_type_id'];
  556. foreach ($in_items as $key => $item) {
  557. if($request->input($item)) {
  558. $item_ids = collect($request->input($item))->filter(function($id) {
  559. return $id;
  560. });
  561. if($item_ids && $item_ids->count() > 0) {
  562. $items = $items->whereIn($key_items[$key], $item_ids);
  563. }
  564. }
  565. }
  566. $items = $items->get();
  567. $projects = Project::where('id','!=',1)->get();
  568. foreach ($projects as $project) {
  569. $project->max_price = $items->where('project_id', $project->id)->max('price') / 100;
  570. }
  571. $orderBy = $request->input('orderBy');
  572. if($orderBy == 'asc') $projects = $projects->sortBy('max_price');
  573. else $projects = $projects->sortByDesc('max_price');
  574. if ($info == null)
  575. {
  576. $values = $projects->pluck('max_price')->toArray();
  577. // foreach ($values as &$value)
  578. // {
  579. // $value = 0;
  580. // }
  581. }else
  582. {
  583. $values = $projects->pluck('max_price');
  584. }
  585. $names = $projects->pluck('name');
  586. return $this->success(['data' => compact('values', 'names','info')]);
  587. }
  588. public function projectStat(Request $request)
  589. {
  590. if($request->input('chartIndex') == 0) {
  591. return $this->getSingleStat($request);
  592. } else if($request->input('chartIndex') == 2) {
  593. return $this->getMaxStat($request);
  594. }
  595. $items = Order::where('type', 1);
  596. $date = $request->input('date');
  597. if($date) {
  598. $start_month = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  599. $end_month = Carbon::createFromTimeString($date . ' 00:00:00')->addMonth()->toDateTimeString();
  600. $items= $items->where([
  601. ['created_at', '>=', $start_month],
  602. ['created_at', '<', $end_month]
  603. ]);
  604. }
  605. $orderBy = $request->input('orderBy');
  606. $items = $items->groupBy('project_id')->selectRaw('sum(money) as sum, project_id')->pluck('sum', 'project_id');
  607. $projects = Project::where('id','!=',1)->get();
  608. foreach ($projects as $project) {
  609. $project->money = 0;
  610. foreach ($items as $key => $val) {
  611. if($key == $project->id) {
  612. $project->money = $val / 100;
  613. break;
  614. }
  615. }
  616. }
  617. if($orderBy == 'asc') $projects = $projects->sortBy('money');
  618. else $projects = $projects->sortByDesc('money');
  619. $values = $projects->pluck('money');
  620. $names = $projects->pluck('name');
  621. return $this->success(['data' => compact('values', 'names')]);
  622. }
  623. }