CompanyImgController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace App\Admin\Controllers\Company;
  3. use App\Admin\Extensions\Tools\DialogTool;
  4. use App\Admin\Extensions\Tools\GridView;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Admin\AdminRole;
  7. use App\Models\AuditReason;
  8. use App\Models\Company;
  9. use App\Models\CompanyImg;
  10. use App\Models\Subsite;
  11. use App\Repositories\CompanyImgRepository;
  12. use App\Services\Common\PmsService;
  13. use Encore\Admin\Auth\Permission;
  14. use Encore\Admin\Controllers\HasResourceActions;
  15. use Encore\Admin\Facades\Admin;
  16. use Encore\Admin\Form;
  17. use Encore\Admin\Grid;
  18. use Encore\Admin\Layout\Content;
  19. use Encore\Admin\Show;
  20. use Illuminate\Http\Request as Req;
  21. use Illuminate\Support\Facades\Request;
  22. class CompanyImgController extends Controller
  23. {
  24. use HasResourceActions;
  25. protected $companyImgRepository;
  26. protected $pmsService;
  27. /**
  28. * CompanyImgController constructor.
  29. * @param $companyImgRepository
  30. * @param $pmsService
  31. */
  32. public function __construct(CompanyImgRepository $companyImgRepository, PmsService $pmsService)
  33. {
  34. $this->companyImgRepository = $companyImgRepository;
  35. $this->pmsService = $pmsService;
  36. }
  37. /**
  38. * Index interface.
  39. *
  40. * @param Content $content
  41. * @return Content
  42. */
  43. public function index(Content $content)
  44. {
  45. Permission::check('companyImg_manager');
  46. return $content
  47. ->header('企业风采')
  48. ->description('列表')
  49. ->body($this->grid());
  50. }
  51. /**
  52. * Show interface.
  53. *
  54. * @param mixed $id
  55. * @param Content $content
  56. * @return Content
  57. */
  58. public function show($id, Content $content)
  59. {
  60. return $content
  61. ->header('企业风采')
  62. ->description('详情')
  63. ->body($this->detail($id));
  64. }
  65. /**
  66. * Edit interface.
  67. *
  68. * @param mixed $id
  69. * @param Content $content
  70. * @return Content
  71. */
  72. public function edit($id, Content $content)
  73. {
  74. Permission::check('companyImg_manager_edit');
  75. return $content
  76. ->header('企业风采')
  77. ->description('编辑')
  78. ->body($this->editForm()->edit($id));
  79. }
  80. /**
  81. * Create interface.
  82. *
  83. * @param Content $content
  84. * @return Content
  85. */
  86. public function create(Content $content)
  87. {
  88. return $content
  89. ->header('企业风采')
  90. ->description('新增')
  91. ->body($this->form());
  92. }
  93. /**
  94. * Make a grid builder.
  95. *
  96. * @return Grid
  97. */
  98. protected function grid()
  99. {
  100. $grid = new Grid(new CompanyImg);
  101. $grid->model()->when(get_subsite_id()>0, function ($query) {
  102. $query->where('subsite_id', get_subsite_id());
  103. })->whereHas('companys')->when(Admin::user()->isRole(AdminRole::$consultantSlug), function ($query) {
  104. $query->whereHas('companyConsultant',function ($query){
  105. $query->where('consultant_id', isset(Admin::user()->consultant->id) ? Admin::user()->consultant->id : -1);
  106. });
  107. })->orderByRaw('field(audit,0,2,1,3)')->orderBy('created_at', 'desc');
  108. $grid->id('ID');
  109. $grid->image()->image();
  110. $grid->company_id('企业名称')->display(function ($company_id) {
  111. $companyname = Company::where('id', $company_id)->select(['companyname'])->first();
  112. return $companyname['companyname'];
  113. })->width(200);
  114. $grid->audit('审核状态')->display(function ($audit) {
  115. switch ($audit) {
  116. case 1:
  117. return '<span style="color:#009900">审核通过</span>';
  118. break;
  119. case 2:
  120. return '<span style="color:#FF6600">审核中</span>';
  121. break;
  122. case 3:
  123. return '<span style="color:#FF0000">审核未通过</span>';
  124. break;
  125. case 0:
  126. return '<span style="color:#FF6600">未审核</span>';
  127. break;
  128. }
  129. });
  130. $grid->subsite_id('所属分站')->display(function ($subsite_id) {
  131. if ($subsite_id == 0) {
  132. return "总站";
  133. } else {
  134. $subsite = Subsite::findOrFail($subsite_id);
  135. return $subsite->sitename;
  136. }
  137. });
  138. $grid->created_at('添加时间');
  139. $grid->tools(function (Grid\Tools $tools) {
  140. if (Admin::user()->can('companyImg_manager_audit')) {
  141. $form = new \Encore\Admin\Widgets\Form();
  142. $form->action(route('companyimg.audit'));
  143. $form->radio('audit1', '审核状态')->options([1=>'审核通过', 2=>'审核中', 3=>'审核未通过'])->default(1);
  144. $states = [
  145. 'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
  146. 'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
  147. ];
  148. $form->textarea('reason', '备注');
  149. $form->html("<input type='checkbox' name='is_send' checked value='1'/>站内信通知");
  150. $config = array(
  151. 'button' => "审核",//添加的按钮文字
  152. 'title' => "将所选企业",//弹窗标题
  153. 'dialog_cancel' => "取消",//取消文字
  154. 'dialog_ok' => "确认",//确认文字
  155. 'is_batch' => true//是否批量操作,如果是批量操作则提交表单时会带上ids参数
  156. );
  157. $tools->append(new DialogTool($form, $config));
  158. }
  159. $tools->append(new GridView());
  160. });
  161. if (Request::get('view') !== 'table') {
  162. $grid->setView('admin.grid.card');
  163. }
  164. $grid->actions(function (Grid\Displayers\Actions $actions) {
  165. if(Request::get('view') !== 'table') {
  166. if (Admin::user()->can('companyImg_manager_audit')) {
  167. $actions->append("<button class='btn btn-primary btn-xs jobaudit' data-code=".$actions->row['id'].">审核</button>");
  168. }
  169. }
  170. if (Admin::user()->can('companyImg_manager_edit')) {
  171. $actions->disableEdit(false);
  172. }
  173. if (Admin::user()->can('companyImg_manager_delete')) {
  174. $actions->disableDelete(false);
  175. }
  176. });
  177. $grid->filter(function ($filter) {
  178. $filter->where(function ($query) {
  179. $query->whereHas('companys', function ($query) {
  180. $query->where('companyname', 'like', "%{$this->input}%");
  181. });
  182. }, '公司名称');
  183. $filter->equal('company_id', '企业ID');
  184. $filter->column(1/2, function ($filter) {
  185. $filter->equal('audit', '审核状态')->select([0=>'未审核',1=>'审核通过',2=>'审核中',3=>'审核未通过']);
  186. });
  187. $filter->column(1/2, function ($filter) {
  188. $subsite = Subsite::where('effective', 1)->select('id', 'sitename')->get();
  189. $subsiteArr=[];
  190. if ($subsite) {
  191. $subsiteArr = $subsite->toArray();
  192. $subsiteArr = array_column($subsiteArr, 'sitename', 'id');
  193. }
  194. $subsiteArr[0] = "总站";
  195. ksort($subsiteArr);
  196. if (get_subsite_id()== 0) {
  197. $filter->equal('subsite_id', '所属分站')->select($subsiteArr);
  198. }
  199. $filter->between('created_at', '添加时间')->datetime();
  200. });
  201. });
  202. return $grid;
  203. }
  204. /**
  205. * Make a show builder.
  206. *
  207. * @param mixed $id
  208. * @return Show
  209. */
  210. protected function detail($id)
  211. {
  212. $show = new Show(CompanyImg::findOrFail($id));
  213. $show->id('ID');
  214. $show->title('标题');
  215. $show->image('图片')->image();
  216. $show->audit('审核状态')->as(function ($audit) {
  217. switch ($audit) {
  218. case 0:
  219. return '未审核';
  220. break;
  221. case 1:
  222. return '审核通过';
  223. break;
  224. case 2:
  225. return '审核中';
  226. break;
  227. case 3:
  228. return '审核未通过';
  229. break;
  230. }
  231. });
  232. $show->created_at('创建时间');
  233. return $show;
  234. }
  235. /**
  236. * Make a form builder.
  237. *
  238. * @return Form
  239. */
  240. protected function form()
  241. {
  242. $form = new Form(new CompanyImg);
  243. $form->display('id');
  244. $form->text('title', '标题');
  245. $form->image('image', '图片')->rules('required');
  246. $form->radio('audit', '审核状态')->options([0=>'未审核',1=>'审核通过',2=>'审核中',3=>'审核未通过',]);
  247. $form->display('created_at', '上传时间');
  248. return $form;
  249. }
  250. public function editForm()
  251. {
  252. $form = new Form(new CompanyImg);
  253. $form->display('id');
  254. $form->text('title', '标题');
  255. $form->image('image', '图片')->rules('required');
  256. $form->radio('audit', '审核状态')->options([0=>'未审核',1=>'审核通过',2=>'审核中',3=>'审核未通过',]);
  257. $form->display('created_at', '上传时间');
  258. return $form;
  259. }
  260. public function update($id)
  261. {
  262. if (request()->has(Form\Field::FILE_DELETE_FLAG)) {
  263. return $this->editForm($id)->update($id);
  264. }
  265. return $this->editForm($id)->update($id);
  266. }
  267. public function companyImgAudit(Req $request)
  268. {
  269. $ids = $request->ids;
  270. if (!$ids) {
  271. return admin_toastr('请选择要审核的企业', 'error');
  272. }
  273. $id = explode(',', $ids);
  274. $img_com_ids =$this->companyImgRepository->findWhereIn('id', $id, ['company_id','id'])->toArray();
  275. $reason = $request->reason;
  276. $data = ['audit'=>$request->audit1];
  277. if (CompanyImg::whereIn('id', $id)->update($data)) {
  278. //审核日志$reason
  279. $auditData= [];
  280. $auditData['ids'] = $id;
  281. $auditData['status'] = $request->audit1;
  282. $auditData['type'] = 9;
  283. $auditData['reason'] = $reason;
  284. AuditReason::addData($auditData);
  285. if ($request->is_send) {
  286. //站内信
  287. $insertData = [];
  288. switch ($request->audit1) {
  289. case 1:
  290. $html = "通过审核";
  291. break;
  292. case 2:
  293. $html = "审核中";
  294. break;
  295. case 3:
  296. $html = "未通过审核";
  297. break;
  298. }
  299. foreach ($img_com_ids as $key => $val) {
  300. $insertData[$key] = array(
  301. 'utype' => 1,
  302. 'msgtype' => 1,
  303. 'msgfromuid' => admin::user()->id,
  304. 'msgfrom' => admin::user()->username,
  305. 'msgtoname' =>getComUserName($val['company_id']),
  306. 'msgtouid' => $val['company_id'],
  307. 'message' => '企业风采'.$val['id'].$html.'【备注】'.$reason,
  308. 'new' => 1,
  309. 'created_at' =>date('Y-m-d H:i:s', time()),
  310. 'updated_at' =>date('Y-m-d H:i:s', time()),
  311. );
  312. }
  313. $this->pmsService->addBatchPms($insertData);
  314. }
  315. if($request->type) {
  316. admin_toastr('企业风采审核成功', 'success');
  317. return back();
  318. } else{
  319. return admin_toastr('企业风采审核成功', 'success');
  320. }
  321. } else {
  322. if($request->type) {
  323. admin_toastr('企业风采审核失败', 'error');
  324. return back();
  325. } else{
  326. return admin_toastr('企业风采审核失败', 'error');
  327. }
  328. }
  329. }
  330. }