DataController.php 27 KB

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