DataController.php 25 KB

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