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. $base_data = [];
  318. if(!empty($device_ids)){
  319. //设备基础金额 默认为0
  320. $base_data = array_fill(0,count($device_ids),0);
  321. $base_name = Device::whereIn('id',$device_ids)->pluck('name')->toArray();
  322. }
  323. //头部标题
  324. $data = [
  325. ['date' => '日期', 'total' => '总金额', 'data' => $base_name]
  326. ];
  327. foreach ($items as &$item)
  328. {
  329. $arr = [
  330. 'data'=>$base_data
  331. ];
  332. $arr['total'] = $item['total_price']/100;
  333. $arr['date'] = date('Y-m',strtotime($item['date']));
  334. //获取到月份的开始时间和结束时间
  335. $start_at = $item['date'];
  336. $end_at = $this->getEndAt(date('Y-m',strtotime($item['date'])));
  337. //获取到订单数组
  338. $order_arr = Order::
  339. whereBetween('updated_at',[$start_at,$end_at])
  340. ->where('project_id',$project_id)
  341. ->where('status',3)
  342. ->where('type',1)
  343. ->pluck('id')->toArray();
  344. if(!empty($order_arr)){
  345. //获取对应设备的总金额
  346. foreach ($device_ids as $k=>$id){
  347. $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();
  348. if(!empty($device_total['total_price'])){
  349. $base_data[$k] = $device_total['total_price']/100;
  350. }
  351. }
  352. }
  353. $arr['data'] = $base_data;
  354. array_push($data,$arr);
  355. }
  356. return $this->success(['data' => $data]);
  357. }
  358. public function getTotalInfo(Request $request)
  359. {
  360. if (!$request->input('project_ids'))
  361. {
  362. return $this->error(['msg' => '暂未选择项目']);
  363. }
  364. $start_at = $this->getStartAt($request->input('date'));
  365. $end_at = $this->getEndAt($request->input('date'));
  366. $total_money = $this->order->whereIn('project_id', $request->input('project_ids'))->sum('money') / 100;
  367. $month_money = $this->order->whereIn('project_id', $request->input('project_ids'))->where([
  368. ['created_at', '>=', $start_at],
  369. ['created_at', '<', $end_at]
  370. ])->sum('money') / 100;
  371. return $this->success(['data' => compact('total_money', 'month_money')]);
  372. }
  373. public function inDate($dates, $created_at, $type)
  374. {
  375. $date = $type == 'year' ? substr($created_at, 0, 7) : substr($created_at, 0, 10);
  376. return in_array($date, $dates);
  377. }
  378. public function parseDate($date)
  379. {
  380. $now = Carbon::now();
  381. if(!$date) return ['year' => $now->year, 'month' => $now->month, 'day' => $now->day];
  382. $items = explode('-', $date);
  383. $year = $now->year;
  384. $month = $now->month;
  385. $day = $now->day;
  386. if(isset($items[0])) {
  387. $year = (int)$items[0];
  388. $year = $year > 2010 && $year < 2050 ? $year : $now->year;
  389. }
  390. if(isset($items[1])) {
  391. $month = (int)$items[1];
  392. $month = $month > 0 && $month < 13 ? $month : $now->month;
  393. }
  394. if(isset($item[2])) {
  395. $day = (int)$items[2];
  396. $day = $day > 0 && $day < 32 ? $date : 1;
  397. }
  398. return compact('year', 'month', 'day');
  399. }
  400. public function getYearAndMonthMoney(Request $request)
  401. {
  402. $date = $request->input('date');
  403. if(!$date) {
  404. $year_money = Order::where('type', 1)->sum('money') / 100;
  405. $month_money = $year_money;
  406. } else {
  407. $start_year = Carbon::createFromTimeString(substr($date, 0, 4) . '-01-01 00:00:00')->toDateTimeString();
  408. $end_year = Carbon::createFromTimeString(substr($date, 0, 4) . '-01-01 00:00:00')->addYear()->toDateTimeString();
  409. $start_month = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  410. $end_month = Carbon::createFromTimeString($date . ' 00:00:00')->addMonth()->toDateTimeString();
  411. $year_money = Order::where([
  412. ['type', 1],
  413. ['created_at', '>=', $start_year],
  414. ['created_at', '<', $end_year]
  415. ])->sum('money') / 100;
  416. $month_money = Order::where([
  417. ['type', 1],
  418. ['created_at', '>=', $start_month],
  419. ['created_at', '<', $end_month]
  420. ])->sum('money') / 100;
  421. }
  422. return $this->success(['data' => compact('year_money', 'month_money')]);
  423. }
  424. public function getProjectYears()
  425. {
  426. $start=2020;
  427. $now = date('Y',time());
  428. $years_arr = [[
  429. 'text' => '请选择年份',
  430. 'value' => ''
  431. ]];
  432. for ($i = 0;$i<=($now-$start);$i++)
  433. {
  434. $years_arr[$i+1]['text'] = ($start+$i).'年';
  435. $years_arr[$i+1]['value'] = $i+1;
  436. }
  437. return $this->success(['msg' => '操作成功', 'data' => $years_arr]);
  438. }
  439. public function getSingleStat(Request $request)
  440. {
  441. //如果初始进来不传筛选的年份,那就默认选择当前的年份
  442. $project_year = $request->input('project_year')? $request->input('project_year'):date('Y',time());
  443. //如果有项目id,那就用项目id,如果没有就默认选择第一个项目
  444. if ($request->input('project_id')!=0)
  445. {
  446. $all_project = 0;
  447. $project_id = $request->input('project_id') ? $request->input('project_id') : Project::where('id','>',1)->first()->id;
  448. $project = Project::find($project_id);
  449. }else
  450. {
  451. $all_project = 1;
  452. }
  453. $sort_type = $request->input('sort_type') == 'year' ? 'year' : 'month';
  454. $values = [];
  455. $names = [];
  456. $name = $sort_type == 'year' ? '每年租赁金额' : '每月租赁金额';
  457. if($sort_type == 'month') {
  458. $start_at = $project_year.'-01-01 00:00:00';
  459. $end_at = ($project_year+1).'-01-01 00:00:00';
  460. for($i = 1; $i < 13; $i++) {
  461. if ($i>9&&$i!=12)
  462. {
  463. $start_month_at = $project_year.'-'.$i.'-01 00:00:00';
  464. }else
  465. {
  466. $start_month_at = $project_year.'-0'.$i.'-01 00:00:00';
  467. }
  468. if ($i>=9&&$i!=12)
  469. {
  470. $end_month_at = $project_year.'-'.($i+1).'-01 00:00:00';
  471. }else
  472. {
  473. $end_month_at = $project_year.'-0'.($i+1).'-01 00:00:00';
  474. }
  475. if ($i==12)
  476. {
  477. $start_month_at = $project_year.'-'.$i.'-01 00:00:00';
  478. $end_month_at = ($project_year+1).'-01-01 00:00:00';
  479. }
  480. if ($all_project == 0)
  481. {
  482. $value = Order::where('type',1)
  483. ->where('project_id', $project->id)
  484. ->whereBetween('created_at',[$start_at,$end_at])
  485. ->whereBetween('created_at',[$start_month_at,$end_month_at])
  486. ->sum('money');
  487. $value = $value/100;
  488. }else
  489. {
  490. $value = Order::where('type',1)
  491. ->whereBetween('created_at',[$start_at,$end_at])
  492. ->whereBetween('created_at',[$start_month_at,$end_month_at])
  493. ->sum('money');
  494. $value = $value/100;
  495. }
  496. array_push($values, $value);
  497. array_push($names, $i . '月');
  498. }
  499. } else {
  500. $start=2020;
  501. $now = date('Y',time());
  502. $year = [];
  503. for ($i = 0;$i<=($now-$start);$i++)
  504. {
  505. array_push($year,($start+$i));
  506. array_push($names,($start+$i).'年');
  507. }
  508. foreach ($year as $v)
  509. {
  510. $start_year_at = $v.'-01-01 00:00:00';
  511. $end_year_at = ($v+1).'-01-01 00:00:00';
  512. if ($all_project == 0)
  513. {
  514. $value = Order::where('type',1)
  515. ->where('project_id', $project->id)
  516. ->whereBetween('created_at',[$start_year_at,$end_year_at])
  517. ->sum('money');
  518. $value = $value/100;
  519. }else{
  520. $value = Order::where('type',1)
  521. ->whereBetween('created_at',[$start_year_at,$end_year_at])
  522. ->sum('money');
  523. $value = $value/100;
  524. }
  525. array_push($values, $value);
  526. }
  527. }
  528. if ($all_project == 0)
  529. {
  530. return $this->success(['data' => compact('values', 'names', 'project', 'name')]);
  531. }else
  532. {
  533. return $this->success(['data' => compact('values', 'names', 'name')]);
  534. }
  535. }
  536. //租赁单价统计
  537. public function getMaxStat(Request $request)
  538. {
  539. $info = [];
  540. if ($request->input('devices')!=null)
  541. {
  542. array_push($info,Device::where('id',$request->input('device_ids'))->value('name')) ;
  543. array_push($info,DeviceName::where('id',$request->input('device_name_ids'))->value('name')) ;
  544. 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'));
  545. array_push($info,RentType::where('id',$request->input('rent_type_ids'))->value('name'));
  546. }
  547. $date = $request->input('date');
  548. $date = $date ? $date : Carbon::now()->toDateString();
  549. $start_at = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  550. $end_at = Carbon::now()->toDateTimeString();
  551. $items = OrderDevice::where([
  552. ['created_at', '>=', $start_at],
  553. ['created_at', '<', $end_at]
  554. ]);
  555. $in_items = ['device_ids', 'device_name_ids', 'spec_ids', 'rent_type_ids'];
  556. $key_items = ['device_id', 'device_name_id', 'spec_id', 'rent_type_id'];
  557. foreach ($in_items as $key => $item) {
  558. if($request->input($item)) {
  559. $item_ids = collect($request->input($item))->filter(function($id) {
  560. return $id;
  561. });
  562. if($item_ids && $item_ids->count() > 0) {
  563. $items = $items->whereIn($key_items[$key], $item_ids);
  564. }
  565. }
  566. }
  567. $items = $items->get();
  568. $projects = Project::where('id','!=',1)->get();
  569. foreach ($projects as $project) {
  570. $project->max_price = $items->where('project_id', $project->id)->max('price') / 100;
  571. }
  572. $orderBy = $request->input('orderBy');
  573. if($orderBy == 'asc') $projects = $projects->sortBy('max_price');
  574. else $projects = $projects->sortByDesc('max_price');
  575. if ($info == null)
  576. {
  577. $values = $projects->pluck('max_price')->toArray();
  578. // foreach ($values as &$value)
  579. // {
  580. // $value = 0;
  581. // }
  582. }else
  583. {
  584. $values = $projects->pluck('max_price');
  585. }
  586. $names = $projects->pluck('name');
  587. return $this->success(['data' => compact('values', 'names','info')]);
  588. }
  589. public function projectStat(Request $request)
  590. {
  591. if($request->input('chartIndex') == 0) {
  592. return $this->getSingleStat($request);
  593. } else if($request->input('chartIndex') == 2) {
  594. return $this->getMaxStat($request);
  595. }
  596. $items = Order::where('type', 1);
  597. $date = $request->input('date');
  598. if($date) {
  599. $start_month = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  600. $end_month = Carbon::createFromTimeString($date . ' 00:00:00')->addMonth()->toDateTimeString();
  601. $items= $items->where([
  602. ['created_at', '>=', $start_month],
  603. ['created_at', '<', $end_month]
  604. ]);
  605. }
  606. $orderBy = $request->input('orderBy');
  607. $items = $items->groupBy('project_id')->selectRaw('sum(money) as sum, project_id')->pluck('sum', 'project_id');
  608. $projects = Project::where('id','!=',1)->get();
  609. foreach ($projects as $project) {
  610. $project->money = 0;
  611. foreach ($items as $key => $val) {
  612. if($key == $project->id) {
  613. $project->money = $val / 100;
  614. break;
  615. }
  616. }
  617. }
  618. if($orderBy == 'asc') $projects = $projects->sortBy('money');
  619. else $projects = $projects->sortByDesc('money');
  620. $values = $projects->pluck('money');
  621. $names = $projects->pluck('name');
  622. return $this->success(['data' => compact('values', 'names')]);
  623. }
  624. }