PersonalPointsController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace App\Admin\Controllers\Person;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\MembersHandsel;
  5. use App\Models\MembersPoint;
  6. use Encore\Admin\Controllers\HasResourceActions;
  7. use Encore\Admin\Facades\Admin;
  8. use Encore\Admin\Form;
  9. use Encore\Admin\Grid;
  10. use Encore\Admin\Layout\Content;
  11. use Encore\Admin\Show;
  12. class PersonalPointsController extends Controller
  13. {
  14. use HasResourceActions;
  15. /**
  16. * Index interface.
  17. *
  18. * @param Content $content
  19. * @return Content
  20. */
  21. public function index(Content $content)
  22. {
  23. return $content
  24. ->header('个人积分')
  25. ->description('列表')
  26. ->body($this->grid());
  27. }
  28. /**
  29. * Show interface.
  30. *
  31. * @param mixed $id
  32. * @param Content $content
  33. * @return Content
  34. */
  35. public function show($id, Content $content)
  36. {
  37. return $content
  38. ->header('个人积分')
  39. ->description('详细')
  40. ->body($this->detail($id));
  41. }
  42. /**
  43. * Edit interface.
  44. *
  45. * @param mixed $id
  46. * @param Content $content
  47. * @return Content
  48. */
  49. public function edit($id, Content $content)
  50. {
  51. return $content
  52. ->header('个人积分')
  53. ->description('编辑')
  54. ->body($this->editForm($id)->edit($id));
  55. }
  56. /**
  57. * Create interface.
  58. *
  59. * @param Content $content
  60. * @return Content
  61. */
  62. public function create(Content $content)
  63. {
  64. return $content
  65. ->header('个人积分')
  66. ->description('创建')
  67. ->body($this->createForm());
  68. }
  69. /**
  70. * Make a grid builder.
  71. *
  72. * @return Grid
  73. */
  74. protected function grid()
  75. {
  76. $grid = new Grid(new MembersHandsel);
  77. $grid->model()->when(get_subsite_id()>0, function ($query) {
  78. $query->whereHas('members',function ($query){
  79. $query->where('subsite_id', get_subsite_id());
  80. });
  81. })->where('utype', 2)->orderBy('id', 'desc');
  82. $grid->id('ID');
  83. $grid->uid('会员ID');
  84. $grid->utype('会员类型')->display(function ($utype) {
  85. if ($utype==1) {
  86. return '企业';
  87. } else {
  88. return '个人';
  89. }
  90. })->width(100);
  91. $grid->points('操作积分')->display(function ($points) {
  92. if ($this->operate==1) {
  93. return "<span style='color:green;'>+".$points."</span>";
  94. } else {
  95. return "<span style='color:red;'>-".$points."</span>";
  96. }
  97. })->width(100);
  98. $grid->htype_cn('操作说明')->width(150);
  99. $grid->created_at('添加时间')->sortable();
  100. $grid->updated_at('更新时间')->sortable();
  101. $grid->disableCreateButton();
  102. $grid->disableExport();
  103. $grid->actions(function ($actions) use ($grid) {
  104. if (Admin::user()->can('person_point_delete')) {
  105. $actions->disableDelete(false);
  106. }
  107. if (Admin::user()->can('person_point_edit')) {
  108. $actions->disableEdit(false);
  109. }
  110. });
  111. if (Admin::user()->can('person_point_delete')) {
  112. $grid->tools(function ($tools) {
  113. $tools->batch(function ($batch) {
  114. $batch->disableDelete(false);
  115. });
  116. });
  117. }
  118. $grid->filter(function ($filter) {
  119. $filter->disableIdFilter();
  120. $filter->equal('uid', '会员ID');
  121. $filter->like('htype_cn', '操作说明');
  122. });
  123. return $grid;
  124. }
  125. /**
  126. * Make a show builder.
  127. *
  128. * @param mixed $id
  129. * @return Show
  130. */
  131. protected function detail($id)
  132. {
  133. $show = new Show(MembersHandsel::findOrFail($id));
  134. $show->id('ID');
  135. $show->uid('会员ID');
  136. $show->utype('会员类型')->as(function ($utype) {
  137. if ($utype==1) {
  138. return '企业';
  139. } else {
  140. return '个人';
  141. }
  142. });
  143. $show->points('操作积分')->as(function ($points) {
  144. if ($this->operate == 1) {
  145. return '+'.$points;
  146. } else {
  147. return '-'.$points;
  148. }
  149. });
  150. $show->created_at('添加时间');
  151. $show->updated_at('更新时间');
  152. return $show;
  153. }
  154. /**
  155. * Make a form builder.
  156. *
  157. * @return Form
  158. */
  159. protected function form()
  160. {
  161. $form = new Form(new MembersHandsel);
  162. $form->display('id');
  163. $form->display('created_at');
  164. $form->display('updated_at');
  165. return $form;
  166. }
  167. /**
  168. * Make a form builder.
  169. *
  170. * @return Form
  171. */
  172. protected function editForm($id)
  173. {
  174. $form = new Form(new MembersHandsel);
  175. $form->setAction(route('admin.personal.addPoints'));
  176. $MembersHandsel = MembersHandsel::findOrFail($id);
  177. $utype = $MembersHandsel->utype;
  178. $points = MembersHandsel::find($id)->membersPoints()->where('utype', $utype)->first();
  179. $form->display('points_num', '会员积分')->setWidth(2)->with(function () use ($points) {
  180. return $points->points;
  181. });
  182. $form->radio('points_type', '操作积分')->options([1 => '增加', 2=> '减少'])->default(1);
  183. $form->switch('ismany', '是否收费?');
  184. $form->number('many_point', '增减积分')->default(0)->min(0);
  185. $form->number('amount', '收费金额')->default(0)->min(0);
  186. $form->text('explain', '操作说明')->setWidth(8);
  187. $form->hidden('points_id')->default($points->id);
  188. return $form;
  189. }
  190. protected function createForm()
  191. {
  192. $form = new Form(new MembersHandsel);
  193. $form->display('id');
  194. $form->display('created_at');
  195. $form->display('updated_at');
  196. return $form;
  197. }
  198. /**
  199. * Store a newly created resource in storage.
  200. *
  201. * @return mixed
  202. */
  203. public function store()
  204. {
  205. return $this->createForm()->store();
  206. }
  207. /**
  208. * Update the specified resource in storage.
  209. *
  210. * @param int $id
  211. *
  212. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  213. */
  214. public function update($id)
  215. {
  216. return $this->editForm()->update($id);
  217. }
  218. public function addPoints()
  219. {
  220. $id = request()->points_id;
  221. if (!$id) {
  222. admin_toastr('积分ID不存在!', 'error');
  223. }
  224. $type = request()->points_type;
  225. $points = request()->many_point;
  226. if ($type==1) {
  227. $res = MembersPoint::where('id', $id)->increment('points', $points);
  228. } else {
  229. $res = MembersPoint::where('id', $id)->decrement('points', $points);
  230. }
  231. if ($res) {
  232. admin_toastr('操作成功!', 'success');
  233. } else {
  234. admin_toastr('操作失败!', 'error');
  235. }
  236. return back();
  237. }
  238. }