ProjectController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\AdminRoleModel;
  4. use App\Models\Department;
  5. use App\Models\Notification;
  6. use App\Models\Order;
  7. use App\Models\Project;
  8. use App\Models\Road;
  9. use App\Models\Role;
  10. use Illuminate\Http\Request;
  11. class ProjectController extends BaseController
  12. {
  13. protected $model;
  14. protected $department;
  15. protected $model_name = '项目';
  16. protected $pre_uri = '/admin/Project/';
  17. protected $view_path = 'admin.projects.';
  18. protected $redirect_index = '/admin/Project/index';
  19. public function __construct()
  20. {
  21. $this->model = new Project();
  22. }
  23. public function index()
  24. {
  25. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  26. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri'));
  27. }
  28. public function applyIndex()
  29. {
  30. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  31. return view($this->view_path . 'apply-index', compact('model', 'model_name','pre_uri'));
  32. }
  33. public function get(Request $request)
  34. {
  35. $items = $this->model->orderBy('created_at', 'desc');
  36. $tmp_items = collect(['name']);
  37. foreach($tmp_items as $tmp_item) {
  38. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  39. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  40. }
  41. }
  42. $select_items = collect([]);
  43. foreach($select_items as $select_item) {
  44. if($request->has($select_item) && !empty($request->input($select_item))) {
  45. $items = $items->where($select_item, '=', $request->input($select_item));
  46. }
  47. }
  48. if($request->input('type') == 'apply') {
  49. $items = $items->whereNotNull('user_id');
  50. }
  51. $items = $items->paginate();
  52. foreach($items as $item) {
  53. $item->active_label = $item->getNameOrLabel('active', 'label');
  54. $item->manager = $item->getManager();
  55. $item->manager_name = $item->manager ? $item->manager->name : '';
  56. $item->manager_phone = $item->manager ? $item->manager->phone : '';
  57. $item->user_name = $item->user ? $item->user->name : '';
  58. $item->user_avatar = $item->user ? $item->user->getAvatar() : '';
  59. $item->user_phone = $item->user ? $item->user->phone : '';
  60. }
  61. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  62. }
  63. public function create()
  64. {
  65. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  66. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  67. }
  68. public function store(Request $request)
  69. {
  70. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  71. $validator = $this->model->getValidator($request, 'store');
  72. if($validator->fails()) {
  73. return back()->withErrors($validator)->withInput();
  74. }
  75. $data = $request->input('data');
  76. $res = $this->model->create($data);
  77. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  78. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  79. }
  80. public function edit(Request $request)
  81. {
  82. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  83. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  84. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  85. }
  86. public function update(Request $request)
  87. {
  88. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  89. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  90. $validator = $this->model->getValidator($request, 'update');
  91. if($validator->fails()) {
  92. return back()->withErrors($validator)->withInput();
  93. }
  94. $data = $request->input('data');
  95. $res = $this->model->where('id', $request->input('id'))->update($data);
  96. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  97. return back()->with(['sg_success_info' => '编辑成功']);
  98. }
  99. public function delete(Request $request)
  100. {
  101. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  102. $res = $item->delete();
  103. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  104. return response()->json(['status' => 'success', 'info' => '操作成功']);
  105. }
  106. public function change(Request $request)
  107. {
  108. $this->model->where('id', $request->input('id'))->update(['active' => $request->input('active')]);
  109. Notification::sendProjectInfo($request->input('id'));
  110. return response()->json(['status' => 'success', 'info' => '操作成功']);
  111. }
  112. public function active(Request $request)
  113. {
  114. $this->model->where('id', $request->input('id'))->update(['active' => 1]);
  115. return response()->json(['status' => 'success', 'info' => '操作成功']);
  116. }
  117. public function getStat(Request $request)
  118. {
  119. $projects = $this->model->get();
  120. $names = $projects->pluck('name');
  121. $data = [[], []];
  122. $max = [0, 0];
  123. foreach($projects as $project) {
  124. $total = Order::where('project_id', $project->id)->count();
  125. $total_money = Order::where('project_id', $project->id)->sum('money') / 100;
  126. array_push($data[0], $total);
  127. array_push($data[1], $total_money);
  128. $max[0] = max($max[0], $total);
  129. $max[1] = max($max[1], $total_money);
  130. }
  131. return response()->json(['status' => 'success', 'data' => compact('data', 'names', 'max')]);
  132. }
  133. }