DataController.php 26 KB

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