PromotionController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace App\Admin\Controllers\Company;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Company;
  5. use App\Models\Jobs;
  6. use App\Models\Promotion;
  7. use Encore\Admin\Auth\Permission;
  8. use Encore\Admin\Controllers\HasResourceActions;
  9. use Encore\Admin\Facades\Admin;
  10. use Encore\Admin\Form;
  11. use Encore\Admin\Grid;
  12. use Encore\Admin\Layout\Content;
  13. use Encore\Admin\Show;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Support\Facades\Input;
  16. class PromotionController extends Controller
  17. {
  18. use HasResourceActions;
  19. /**
  20. * Index interface.
  21. *
  22. * @param Content $content
  23. * @return Content
  24. */
  25. public function index(Content $content)
  26. {
  27. return $content
  28. ->header('企业推广')
  29. ->description('列表')
  30. ->body($this->grid());
  31. }
  32. /**
  33. * Show interface.
  34. *
  35. * @param mixed $id
  36. * @param Content $content
  37. * @return Content
  38. */
  39. public function show($id, Content $content)
  40. {
  41. return $content
  42. ->header('Detail')
  43. ->description('description')
  44. ->body($this->detail($id));
  45. }
  46. /**
  47. * Edit interface.
  48. *
  49. * @param mixed $id
  50. * @param Content $content
  51. * @return Content
  52. */
  53. public function edit($id, Content $content)
  54. {
  55. return $content
  56. ->header('企业推广')
  57. ->description('修改')
  58. ->body($this->editForm()->edit($id));
  59. }
  60. /**
  61. * Create interface.
  62. *
  63. * @param Content $content
  64. * @return Content
  65. */
  66. public function create(Content $content)
  67. {
  68. return $content
  69. ->header('企业推广')
  70. ->description('添加')
  71. ->body(view('admin.promotion.add')->with('grid', $this->jobGrid()->render()));
  72. }
  73. protected function jobGrid()
  74. {
  75. $form = new \Encore\Admin\Widgets\Form();
  76. $form->text('id', '职位ID');
  77. $form->text('jobs_name', '职位名称');
  78. $form->text('company_name', '企业名称');
  79. $form->text('company_id', '企业ID');
  80. $form->disableReset();
  81. $form->disableSubmit();
  82. return $form;
  83. }
  84. public function adds(Request $request)
  85. {
  86. $jid = $request->jid;
  87. $type = $request->ptype;
  88. $cid = $request->cid;
  89. $days = $request->days;
  90. $where = [
  91. ['jobid',$jid],
  92. ['ptype',$type]
  93. ];
  94. if (Promotion::get($where)) {
  95. admin_toastr('该职位推广已存在', 'error');
  96. return back();
  97. }
  98. if (!$jid || !$cid) {
  99. admin_toastr('请选择要推广的企业的职位', 'error');
  100. return back();
  101. }
  102. if (!$days) {
  103. admin_toastr('请填写推广天数', 'error');
  104. return back();
  105. }
  106. switch ($type) {
  107. case 1:
  108. $flied = 'stick';
  109. break;
  110. case 2:
  111. $flied = 'emergency';
  112. }
  113. $data['companyid'] = $cid;
  114. $data['jobid'] = $jid;
  115. $data['days'] = $days;
  116. $data['ptype'] = $type;
  117. $data['starttime'] = time();
  118. $data['endtime'] = time()+$days*86400;
  119. if (!Promotion::create($data)) {
  120. admin_toastr('保存失败', 'error');
  121. }
  122. $set_data = [$flied=>1];
  123. if ($type==1) {
  124. $set_data['stime']=$data['endtime'];
  125. }
  126. Jobs::where('id', $jid)->update($set_data);
  127. event_search_update(Jobs::class, $jid, 'update');
  128. admin_toastr('保存成功', 'success');
  129. return redirect(route('promotion.index'));
  130. }
  131. /**
  132. * Make a grid builder.
  133. *
  134. * @return Grid
  135. */
  136. protected function grid()
  137. {
  138. Permission::check('promotion_manager');
  139. $grid = new Grid(new Promotion);
  140. $grid->model()->when(get_subsite_id()>0, function ($querys) {
  141. $querys->whereHas('job', function ($query) {
  142. $query->where('subsite_id', get_subsite_id());
  143. });
  144. })->whereHas('company')->whereHas('job')->orderBy('id', 'desc');
  145. $grid->column('job.jobs_name', '推广职位');
  146. $grid->column('company.companyname', '企业名称')->width(200);
  147. $grid->ptype('推广类型')->display(function ($ptype) {
  148. return $ptype==1?"置顶":"紧急";
  149. });
  150. $grid->starttime('开始时间')->display(function ($starttime) {
  151. return date('Y-m-d', $starttime);
  152. });
  153. $grid->endtime('结束时间')->display(function ($endtime) {
  154. return date('Y-m-d', $endtime);
  155. });
  156. $grid->actions(function ($actions) {
  157. if (Admin::user()->can('promotion_manager_edit')) {
  158. $actions->disableEdit(false);
  159. }
  160. if (Admin::user()->can('promotion_manager_delete')) {
  161. $actions->disableDelete(false);
  162. }
  163. });
  164. if (Admin::user()->can('promotion_manager_add')) {
  165. $grid->disableCreateButton(false);
  166. }
  167. $grid->filter(function ($filter) {
  168. $filter->like('company.companyname', '企业名称');
  169. $filter->like('company.id', '企业ID');
  170. $filter->like('job.jobs_name', '职位名称');
  171. $filter->like('job.id', '职位ID');
  172. $filter->equal('ptype', '推广类型')->select([1=>'置顶', 2=>'紧急']);
  173. });
  174. return $grid;
  175. }
  176. /**
  177. * Make a show builder.
  178. *
  179. * @param mixed $id
  180. * @return Show
  181. */
  182. protected function detail($id)
  183. {
  184. $show = new Show(Promotion::findOrFail($id));
  185. $show->companyid('企业名称')->as(function ($companyid) {
  186. $comInfo = Company::select('companyname')->find($companyid);
  187. if ($comInfo) {
  188. return $comInfo->companyname;
  189. }
  190. return '';
  191. });
  192. $show->ptype('推广类型')->as(function ($ptype) {
  193. switch ($ptype) {
  194. case 1:
  195. return '职位置顶';
  196. break;
  197. case 2:
  198. return '职位紧急';
  199. break;
  200. }
  201. });
  202. $show->jobid('职位名称')->as(function ($jobid) {
  203. $jobInfo = Jobs::select('jobs_name')->find($jobid);
  204. if ($jobInfo) {
  205. return $jobInfo->jobs_name;
  206. }
  207. return '';
  208. });
  209. $show->days('推广天数');
  210. $show->starttime('开始时间')->as(function ($starttime) {
  211. return date('Y-m-d H:i:s', $starttime);
  212. });
  213. $show->endtime('开始时间')->as(function ($endtime) {
  214. return date('Y-m-d H:i:s', $endtime);
  215. });
  216. return $show;
  217. }
  218. /**
  219. * Make a form builder.
  220. *
  221. * @return Form
  222. */
  223. protected function editForm()
  224. {
  225. Permission::check('promotion_manager_edit');
  226. $form = new Form(new Promotion);
  227. $form->display('id');
  228. $form->display('company.companyname', '企业名称');
  229. $form->display('job.jobs_name', '职位名称');
  230. $form->display('ptype', '推广方案')->with(function ($ptype) {
  231. return $ptype==1?'置顶':'紧急';
  232. });
  233. $form->display('days', '推广天数');
  234. $form->display('dead_line', '推广期限');
  235. $form->hidden('endtime', '结束时间');
  236. $form->text('day', '延迟推广天数')->rules('required', ['required'=>'请输入延迟推广天数']);
  237. return $form;
  238. }
  239. /**
  240. * Make a form builder.
  241. *
  242. * @return Form
  243. */
  244. protected function form()
  245. {
  246. $form = new Form(new Promotion);
  247. return $form;
  248. }
  249. /**
  250. * @param $id推广ID
  251. * @return \Symfony\Component\HttpFoundation\Response
  252. */
  253. public function update($id)
  254. {
  255. if (!Input::get('day')) {
  256. admin_toastr('请输入延迟推广天数!', 'error');
  257. return back();
  258. }
  259. $data = [
  260. 'endtime' =>Input::get('endtime')+Input::get('day')*86400,
  261. 'days' =>Input::get('day'),
  262. ];
  263. return $this->editForm()->update($id, $data);
  264. }
  265. public function destroy($id)
  266. {
  267. Permission::check('promotion_manager_delete');
  268. $model = Promotion::where('id', $id)->first();
  269. switch ($model->ptype) {
  270. case 1:
  271. $type = 'stick';
  272. $html = "置顶";
  273. $data['stime'] = 0;
  274. break;
  275. case 2:
  276. $type = 'emergency';
  277. $html = "紧急";
  278. break;
  279. }
  280. if (Promotion::destroy($id)) {
  281. if (Jobs::where('id', $model->jobid)->first()) {
  282. $data[$type] = 0;
  283. Jobs::where('id', $model->jobid)->update($data);
  284. }
  285. return admin_toastr('职位'.$html.'删除成功', 'success');
  286. }
  287. return admin_toastr('职位'.$html.'删除失败', 'error');
  288. }
  289. }