ProjectController.php 8.0 KB

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