DataController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace App\Http\Controllers\Api\mini;
  3. use App\Models\Device;
  4. use App\Models\Order;
  5. use App\Models\OrderDevice;
  6. use App\Models\Project;
  7. use Carbon\Carbon;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Log;
  10. class DataController extends BaseController
  11. {
  12. protected $order;
  13. protected $order_device;
  14. protected $order_ids = [];
  15. protected $project_ids = [];
  16. protected $start_at;
  17. protected $end_at;
  18. public function __construct()
  19. {
  20. $this->order = new Order();
  21. $this->order_device = new OrderDevice();
  22. }
  23. public function getYearsAndMonths(Request $request)
  24. {
  25. $years = [
  26. ['text' => '所有年', 'value' => '']
  27. ];
  28. $this_year = Carbon::now()->year;
  29. for($i = 5; $i >= 0; $i--) {
  30. array_push($years, [
  31. 'text' => $this_year - $i,
  32. 'value' => $this_year - $i
  33. ]);
  34. }
  35. $months = [];
  36. for($i = 1; $i < 13; ++$i) {
  37. array_push($months, [
  38. 'text' => $i . '月',
  39. 'value' => $i
  40. ]);
  41. }
  42. $month = Carbon::now()->month;
  43. $now = Carbon::now()->toDateString();
  44. return $this->success(['data' => [
  45. ['key' => 'year', 'values' => $years, 'defaultIndex' => count($years) - 1],
  46. ['key' => 'month', 'values' => $months, 'defaultIndex' => $month]
  47. ], 'now' => $now]);
  48. }
  49. public function getDateInfo()
  50. {
  51. $max_date = Carbon::now()->toDateString();
  52. // $order = $this->order->orderBy('created_at', 'desc')->first();
  53. // $min_date = $order ? substr($order->created_at, 0, 10) : Carbon::now()->subYear()->toDateString();
  54. $min_date = Carbon::now()->subYears(10)->toDateString();
  55. $date = substr($max_date, 0, 7);
  56. $start_date = $max_date;
  57. $end_date = Carbon::now()->addMonth()->toDateString();
  58. $start_date = substr($start_date, 0, 7);
  59. $end_date = substr($end_date, 0, 7);
  60. return $this->success(['data' => compact('date', 'min_date', 'max_date', 'start_date', 'end_date')]);
  61. }
  62. public function getStartAt($date)
  63. {
  64. return strlen($date) <= 7 ? $date . '-01 00:00:00' : $date . ' 00:00:00';
  65. }
  66. public function getEndAt($date)
  67. {
  68. return strlen($date) <= 7 ? Carbon::createFromTimeString($date . '-01 00:00:00')->addMonth(1)->toDateTimeString() : Carbon::createFromTimeString($date . ' 00:00:00')->addDay(1)->toDateTimeString();
  69. }
  70. public function getOrders(Request $request)
  71. {
  72. $order_ids = $this->getOrderIds($request);
  73. $start_at = $this->getStartAt($request->input('start_date'));
  74. $end_at = $this->getEndAt($request->input('end_date'));
  75. return $this->order->whereIn('id', $order_ids)->where([
  76. ['type', '=', 1],
  77. ['created_at', '>=', $start_at],
  78. ['created_at', '<', $end_at]
  79. ])->get();
  80. }
  81. public function getOrderIds(Request $request)
  82. {
  83. $ids = $request->input('project_ids');
  84. if(!$ids || count($ids) < 0) return false;
  85. $this->project_ids = $ids;
  86. $order_devices = $this->order_device->whereIn('project_id', $ids);
  87. $in_items = ['device_ids', 'device_name_ids', 'spec_ids', 'rent_type_ids'];
  88. $key_items = ['device_id', 'device_name_id', 'spec_id', 'rent_type_id'];
  89. foreach ($in_items as $key => $item) {
  90. if($request->input($item)) {
  91. $item_ids = collect($request->input($item))->filter(function($id) {
  92. return $id;
  93. });
  94. if($item_ids && $item_ids->count() > 0) {
  95. $order_devices = $order_devices->whereIn($key_items[$key], $item_ids);
  96. }
  97. }
  98. }
  99. return $order_devices->pluck('order_id')->unique();
  100. }
  101. public function getStat(Request $request)
  102. {
  103. if(!$request->input('project_ids')) return $this->error(['msg' => '']);
  104. if($request->input('chart_type') == 'pie') return $this->getPieData($request);
  105. else if($request->input('chart_type') == 'radar') return $this->getRadarData($request);
  106. $orders = $this->getOrders($request);
  107. if(!$orders) return $this->error(['msg' => '']);
  108. $names = [];
  109. $values = [];
  110. // year|month
  111. $type = $request->input('type') ? $request->input('type') : 'year';
  112. $cnt = 0;
  113. $projects = Project::whereIn('id', $this->project_ids)->get();
  114. foreach($orders as $item) {
  115. if($this->inDate($names, $item->created_at, $type)) {
  116. foreach ($projects as $key => $project) {
  117. $values[$key][$cnt-1] = $project->id == $item->project_id ? $values[$key][$cnt-1] + ($item->money / 100) : $values[$key][$cnt-1];
  118. }
  119. } else {
  120. $names[$cnt] = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
  121. foreach ($projects as $key => $project) {
  122. $values[$key][$cnt] = $project->id == $item->project_id ? ($item->money / 100) : 0;
  123. }
  124. $cnt = $cnt + 1;
  125. }
  126. }
  127. $legends = $projects->pluck('name');
  128. return $this->success(['data' => compact('values', 'names', 'legends')]);
  129. }
  130. public function getRadarData(Request $request)
  131. {
  132. $orders = $this->getOrders($request);
  133. if(!$orders) return $this->error(['msg' => '']);
  134. $data = [];
  135. $projects = Project::whereIn('id', $request->input('project_ids'))->get();
  136. $indicator = [
  137. ['name' => '累计租赁花费', 'max' => 0],
  138. ['name' => '累计租赁数量', 'max' => 0],
  139. ['name' => '平均月消费', 'max' => 0],
  140. ['name' => '租赁次数', 'max' => 0],
  141. ['name' => '租赁天数', 'max' => 0]
  142. ];
  143. $legends = [];
  144. foreach($projects as $key => $project) {
  145. $tmp_orders = $orders->where('project_id', $project->id);
  146. $total_money = $tmp_orders->sum('money') / 100;
  147. $total_num = OrderDevice::whereIn('order_id', $orders->where('project_id', $project->id)->pluck('id'))->count();
  148. $months = $this->getMonths($tmp_orders);
  149. $mean_month = $total_money / $months;
  150. $rent_times = $tmp_orders->count();
  151. $rent_days = $this->getRentDays($tmp_orders);
  152. $indicator[0]['max'] = max($indicator[0]['max'], $total_money);
  153. $indicator[1]['max'] = max($indicator[1]['max'], $total_num);
  154. $indicator[2]['max'] = max($indicator[2]['max'], $mean_month);
  155. $indicator[3]['max'] = max($indicator[3]['max'], $rent_times);
  156. $indicator[4]['max'] = max($indicator[4]['max'], $rent_days);
  157. array_push($data, [
  158. 'name' => $project->name,
  159. 'value' => [$total_money, $total_num, $mean_month, $rent_times, $rent_days]
  160. ]);
  161. array_push($legends, $project->name);
  162. }
  163. return $this->success(['data' => compact('data', 'legends', 'indicator')]);
  164. }
  165. public function getMonths($orders)
  166. {
  167. $min_at = $orders->min('created_at');
  168. $max_at = $orders->max('updated_at');
  169. if($max_at && $max_at) $months = $min_at->diffInMonths($max_at);
  170. else $months = 1;
  171. return $months ? $months : 1;
  172. }
  173. public function getRentDays($orders)
  174. {
  175. $order_ids = $orders->pluck('id');
  176. $order_devices = OrderDevice::whereId('order_id', $order_ids)->get();
  177. $days = 0;
  178. foreach($order_devices as $order_device) {
  179. if($order_device->start_date && $order_device->end_date) {
  180. $days = $days + Carbon::createFromTimeString($order_device->start_date . ' 00:00:00')->diffInDays($order_device->end_date);
  181. }
  182. }
  183. return $days;
  184. }
  185. public function getPieData(Request $request)
  186. {
  187. $order_ids = $this->getOrderIds($request);
  188. if(!$order_ids) return $this->error(['msg' => '']);
  189. $total = OrderDevice::whereIn('order_id', $order_ids)->count();
  190. $devices = Device::all();
  191. $data = [];
  192. foreach($devices as $device) {
  193. $tmp = OrderDevice::whereIn('order_id', $order_ids)->where('device_id', $device->id)->count();
  194. $percent = $total > 0 ? round(($tmp / $total * 10000)) / 100 : '0';
  195. array_push($data, ['name' => $device->name, 'value' => $percent]);
  196. }
  197. $legends = $devices->pluck('name');
  198. return $this->success(['data' => compact('data', 'legends')]);
  199. }
  200. public function getDetailData(Request $request)
  201. {
  202. $order_ids = $this->getOrderIds($request);
  203. if(!$order_ids) return $this->error(['msg' => '']);
  204. $order_devices = OrderDevice::whereIn('order_id', $order_ids)->orderBy('created_at')->get();
  205. $devices = Device::all();
  206. $dates = [];
  207. // year|month
  208. $type = $request->input('type') ? $request->input('type') : 'year';
  209. $columns = [];
  210. $date = '';
  211. $values = [
  212. ['date' => '日期', 'total' => '总金额', 'data' => $devices->pluck('name')]
  213. ];
  214. foreach($order_devices as $item) {
  215. if($this->inDate($dates, $item->created_at, $type)) {
  216. foreach($devices as $key => $device) {
  217. $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
  218. $columns[$key] = $columns[$key] + $money;
  219. }
  220. } else {
  221. $date = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
  222. array_push($dates, $date);
  223. if(count($columns) > 0) {
  224. array_push($values, [
  225. 'date' => $date,
  226. 'total' => collect($columns)->sum() / 100,
  227. 'data' => $columns
  228. ]);
  229. }
  230. $columns = [];
  231. foreach($devices as $device) {
  232. $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
  233. array_push($columns, $money);
  234. }
  235. }
  236. }
  237. if(count($columns) > 0) {
  238. array_push($values, [
  239. 'date' => $date,
  240. 'total' => collect($columns)->sum(),
  241. 'data' => $columns
  242. ]);
  243. }
  244. return $this->success(['data' => $values]);
  245. }
  246. public function getTotalInfo(Request $request)
  247. {
  248. $start_at = $this->getStartAt($request->input('date'));
  249. $end_at = $this->getStartAt($request->input('date'));
  250. $total_money = $this->order->whereIn('project_id', $request->input('project_ids'))->sum('money') / 100;
  251. $month_money = $this->order->whereIn('project_id', $request->input('project_ids'))->where([
  252. ['created_at', '>=', $start_at],
  253. ['created_at', '<', $end_at]
  254. ])->sum('money') / 100;
  255. return $this->success(['data' => compact('total_money', 'month_money')]);
  256. }
  257. public function inDate($dates, $created_at, $type)
  258. {
  259. $date = $type == 'year' ? substr($created_at, 0, 7) : substr($created_at, 0, 10);
  260. return in_array($date, $dates);
  261. }
  262. public function parseDate($date)
  263. {
  264. $now = Carbon::now();
  265. if(!$date) return ['year' => $now->year, 'month' => $now->month, 'day' => $now->day];
  266. $items = explode('-', $date);
  267. $year = $now->year;
  268. $month = $now->month;
  269. $day = $now->day;
  270. if(isset($items[0])) {
  271. $year = (int)$items[0];
  272. $year = $year > 2010 && $year < 2050 ? $year : $now->year;
  273. }
  274. if(isset($items[1])) {
  275. $month = (int)$items[1];
  276. $month = $month > 0 && $month < 13 ? $month : $now->month;
  277. }
  278. if(isset($item[2])) {
  279. $day = (int)$items[2];
  280. $day = $day > 0 && $day < 32 ? $date : 1;
  281. }
  282. return compact('year', 'month', 'day');
  283. }
  284. }