ProjectController.php 9.2 KB

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