ProjectController.php 9.5 KB

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