ProjectController.php 8.1 KB

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