ProjectController.php 8.9 KB

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