DataController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. $orders = $this->getOrders($request);
  106. if(!$orders) return $this->error(['msg' => '']);
  107. $names = [];
  108. $values = [];
  109. // year|month
  110. $type = $request->input('type') ? $request->input('type') : 'year';
  111. $cnt = 0;
  112. $projects = Project::whereIn('id', $this->project_ids)->get();
  113. foreach($orders as $item) {
  114. if($this->inDate($names, $item->created_at, $type)) {
  115. foreach ($projects as $key => $project) {
  116. $values[$key][$cnt-1] = $project->id == $item->project_id ? $values[$key][$cnt-1] + ($item->money / 100) : $values[$key][$cnt-1];
  117. }
  118. } else {
  119. $names[$cnt] = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
  120. foreach ($projects as $key => $project) {
  121. $values[$key][$cnt] = $project->id == $item->project_id ? ($item->money / 100) : 0;
  122. }
  123. $cnt = $cnt + 1;
  124. }
  125. }
  126. $legends = $projects->pluck('name');
  127. return $this->success(['data' => compact('values', 'names', 'legends')]);
  128. }
  129. public function getPieData(Request $request)
  130. {
  131. $order_ids = $this->getOrderIds($request);
  132. if(!$order_ids) return $this->error(['msg' => '']);
  133. $total = OrderDevice::whereIn('order_id', $order_ids)->count();
  134. $devices = Device::all();
  135. $data = [];
  136. foreach($devices as $device) {
  137. $tmp = OrderDevice::whereIn('order_id', $order_ids)->where('device_id', $device->id)->count();
  138. $percent = $total > 0 ? round(($tmp / $total * 10000)) / 100 : '0';
  139. array_push($data, ['name' => $device->name, 'value' => $percent]);
  140. }
  141. $legends = $devices->pluck('name');
  142. return $this->success(['data' => compact('data', 'legends')]);
  143. }
  144. public function getDetailData(Request $request)
  145. {
  146. $order_ids = $this->getOrderIds($request);
  147. if(!$order_ids) return $this->error(['msg' => '']);
  148. $order_devices = OrderDevice::whereIn('order_id', $order_ids)->orderBy('created_at')->get();
  149. $devices = Device::all();
  150. $dates = [];
  151. // year|month
  152. $type = $request->input('type') ? $request->input('type') : 'year';
  153. $columns = [];
  154. $date = '';
  155. $values = [
  156. ['date' => '日期', 'total' => '总金额', 'data' => $devices->pluck('name')]
  157. ];
  158. foreach($order_devices as $item) {
  159. if($this->inDate($dates, $item->created_at, $type)) {
  160. foreach($devices as $key => $device) {
  161. $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
  162. $columns[$key] = $columns[$key] + $money;
  163. }
  164. } else {
  165. $date = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
  166. array_push($dates, $date);
  167. if(count($columns) > 0) {
  168. array_push($values, [
  169. 'date' => $date,
  170. 'total' => collect($columns)->sum() / 100,
  171. 'data' => $columns
  172. ]);
  173. }
  174. $columns = [];
  175. foreach($devices as $device) {
  176. $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
  177. array_push($columns, $money);
  178. }
  179. }
  180. }
  181. if(count($columns) > 0) {
  182. array_push($values, [
  183. 'date' => $date,
  184. 'total' => collect($columns)->sum(),
  185. 'data' => $columns
  186. ]);
  187. }
  188. return $this->success(['data' => $values]);
  189. }
  190. public function getTotalInfo(Request $request)
  191. {
  192. $start_at = $this->getStartAt($request->input('date'));
  193. $end_at = $this->getStartAt($request->input('date'));
  194. $total_money = $this->order->whereIn('project_id', $request->input('project_ids'))->sum('money') / 100;
  195. $month_money = $this->order->whereIn('project_id', $request->input('project_ids'))->where([
  196. ['created_at', '>=', $start_at],
  197. ['created_at', '<', $end_at]
  198. ])->sum('money') / 100;
  199. return $this->success(['data' => compact('total_money', 'month_money')]);
  200. }
  201. public function inDate($dates, $created_at, $type)
  202. {
  203. $date = $type == 'year' ? substr($created_at, 0, 7) : substr($created_at, 0, 10);
  204. return in_array($date, $dates);
  205. }
  206. public function parseDate($date)
  207. {
  208. $now = Carbon::now();
  209. if(!$date) return ['year' => $now->year, 'month' => $now->month, 'day' => $now->day];
  210. $items = explode('-', $date);
  211. $year = $now->year;
  212. $month = $now->month;
  213. $day = $now->day;
  214. if(isset($items[0])) {
  215. $year = (int)$items[0];
  216. $year = $year > 2010 && $year < 2050 ? $year : $now->year;
  217. }
  218. if(isset($items[1])) {
  219. $month = (int)$items[1];
  220. $month = $month > 0 && $month < 13 ? $month : $now->month;
  221. }
  222. if(isset($item[2])) {
  223. $day = (int)$items[2];
  224. $day = $day > 0 && $day < 32 ? $date : 1;
  225. }
  226. return compact('year', 'month', 'day');
  227. }
  228. }