ProjectController.php 7.5 KB

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