CourseFieldController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\Course;
  4. use App\Models\CourseField;
  5. use App\Models\CourseHole;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use Dcat\Admin\Http\Controllers\AdminController;
  10. use Illuminate\Support\Facades\App;
  11. use Illuminate\Support\Facades\Cache;
  12. class CourseFieldController extends AdminController
  13. {
  14. /**
  15. * Make a grid builder.
  16. *
  17. * @return Grid
  18. */
  19. protected function grid()
  20. {
  21. return Grid::make(CourseField::with('course'), function (Grid $grid) {
  22. $grid->tools(function (Grid\Tools $tools) {
  23. $tools->append('<a class="btn btn-md btn-primary" onClick="javascript :history.back(-1);" style="color:#586cb1;margin-left:10px">返回</a>');
  24. });
  25. $course_id = request()->input('course_id');
  26. Cache::put('course_id', $course_id); //设置缓存,添加场地是可用
  27. $grid->model()->where('course_id', '=', $course_id);
  28. $grid->model()->orderByDesc('id');
  29. $grid->column('id')->sortable();
  30. $grid->column('course_id')->display(function () {
  31. $str = "";
  32. if($this->course){
  33. $str .= "<div style='margin-right:10px;display: flex;align-items: center'>";
  34. $str .= '<img data-action="preview-img" src="' . $this->course->img . '" style="height:50px;width:50px;cursor:pointer;margin-right:10px;" class="img img-thumbnail">';
  35. $str .= '<p style="margin-bottom: 5px">' . $this->course->name . '</p>';
  36. $str .= "</div>";
  37. }
  38. return $str;
  39. });
  40. $grid->column('name');
  41. $grid->column('before9')->display(function ($res) {
  42. $str = "";
  43. if(!empty($res)){
  44. $res = json_decode($res, true);
  45. //总标杆
  46. $par_total = 0;
  47. foreach ($res as $key => $val) {
  48. $par_total += $val['par'];
  49. }
  50. //前九标杆
  51. $bf9_par_total = 0;
  52. foreach ($res as $key => $val) {
  53. if ($val['sort'] <= 9) {
  54. $bf9_par_total += $val['par'];
  55. }
  56. }
  57. //球洞
  58. $hole_con = '';
  59. foreach ($res as $key => $val) {
  60. $hole_con .= "<td>" . ($key + 1) . "</td>";
  61. if ($val['sort'] == 9) {
  62. $hole_con .= "<td>" . trans('course-field.fields.before9') . "</td>";
  63. }
  64. }
  65. //难度
  66. $diff_con = '';
  67. foreach ($res as $key => $val) {
  68. $diff_con .= "<td>" . $val['difficulty'] . "</td>";
  69. if ($val['sort'] == 9) {
  70. $diff_con .= "<td>" . '#' . "</td>";
  71. }
  72. }
  73. //标杆
  74. $par_con = '';
  75. foreach ($res as $key => $val) {
  76. $par_con .= "<td>" . $val['par'] . "</td>";
  77. if ($val['sort'] == 9) {
  78. $par_con .= "<td>" . $bf9_par_total . "</td>";
  79. }
  80. }
  81. $str = "<style>
  82. .scores_table td{
  83. border: 1px solid #ccc!important;
  84. width: 40px;
  85. }
  86. </style>
  87. <table class='scores_table' style='text-align: center'>
  88. <tr>
  89. <td>" . trans('game-user.fields.hole') . "</td>
  90. " . $hole_con . "
  91. </tr>
  92. <tr>
  93. <td>" . trans('game-user.fields.difficulty') . "</td>
  94. " . $diff_con . "
  95. </tr>
  96. <tr>
  97. <td>" . trans('game-user.fields.par') . "</td>
  98. " . $par_con . "
  99. </tr>
  100. </table>";
  101. }
  102. return $str;
  103. });
  104. $grid->column('after9')->display(function ($res) {
  105. $str = "";
  106. if(!empty($res)){
  107. $res = json_decode($res, true);
  108. //总标杆
  109. $par_total = 0;
  110. foreach ($res as $key => $val) {
  111. $par_total += $val['par'];
  112. }
  113. //后九标杆
  114. $af9_par_total = 0;
  115. foreach ($res as $key => $val) {
  116. if ($val['sort'] <= 9) {
  117. $af9_par_total += $val['par'];
  118. }
  119. }
  120. //球洞
  121. $hole_con = '';
  122. foreach ($res as $key => $val) {
  123. $hole_con .= "<td>" . ($key + 1) . "</td>";
  124. if ($val['sort'] == 9) {
  125. $hole_con .= "<td>" . trans('course-field.fields.after9') . "</td>";
  126. }
  127. }
  128. //难度
  129. $diff_con = '';
  130. foreach ($res as $key => $val) {
  131. $diff_con .= "<td>" . $val['difficulty'] . "</td>";
  132. if ($val['sort'] == 9) {
  133. $diff_con .= "<td>" . '#' . "</td>";
  134. }
  135. }
  136. //标杆
  137. $par_con = '';
  138. foreach ($res as $key => $val) {
  139. $par_con .= "<td>" . $val['par'] . "</td>";
  140. if ($val['sort'] == 9) {
  141. $par_con .= "<td>" . $af9_par_total . "</td>";
  142. }
  143. }
  144. $str = "<style>
  145. .scores_table td{
  146. border: 1px solid #ccc!important;
  147. width: 40px;
  148. }
  149. </style>
  150. <table class='scores_table' style='text-align: center'>
  151. <tr>
  152. <td>" . trans('game-user.fields.hole') . "</td>
  153. " . $hole_con . "
  154. </tr>
  155. <tr>
  156. <td>" . trans('game-user.fields.difficulty') . "</td>
  157. " . $diff_con . "
  158. </tr>
  159. <tr>
  160. <td>" . trans('game-user.fields.par') . "</td>
  161. " . $par_con . "
  162. </tr>
  163. </table>";
  164. }
  165. return $str;
  166. });
  167. $grid->disableViewButton();
  168. });
  169. }
  170. /**
  171. * Make a form builder.
  172. *
  173. * @return Form
  174. */
  175. protected function form()
  176. {
  177. return Form::make(new CourseField(), function (Form $form) {
  178. $courseId = Cache::get('course_id');
  179. $course = Course::find($courseId);
  180. $form->column(9, function (Form $form)use ($course) {
  181. $form->html(function () use ($course) {
  182. $str = "";
  183. $str .= "<div style='display: flex;align-items: center'>";
  184. $str .= '<img data-action="preview-img" src="' . $course->img . '" style="height:50px;width:50px;cursor:pointer;margin-right:10px;" class="img img-thumbnail">';
  185. $str .= '<div>';
  186. $str .= '<p style="margin-bottom: 5px">球场:' . $course->name . '</p>';
  187. $str .= "</div>";
  188. $str .= "</div>";
  189. return $str;
  190. });
  191. $form->text('name')->width(4)->required();
  192. $form->table('before9', function (Form\NestedForm $table) {
  193. $table->text('sort', trans('course.labels.sort'));
  194. $table->text('par', trans('course.labels.par'));
  195. $table->text('difficulty', trans('course.labels.difficulty'));
  196. $table->text('gold_dis', trans('course.labels.gold_dis'));
  197. $table->text('blue_dis', trans('course.labels.blue_dis'));
  198. $table->text('white_dis', trans('course.labels.white_dis'));
  199. $table->text('red_dis', trans('course.labels.red_dis'));
  200. })->label(trans('course.labels.hole_list'))->default(CourseField::$baseHoleList)->disableCreate()->disableDelete()->width(12, 0);
  201. $form->table('after9', function (Form\NestedForm $table) {
  202. $table->text('sort', trans('course.labels.sort'));
  203. $table->text('par', trans('course.labels.par'));
  204. $table->text('difficulty', trans('course.labels.difficulty'));
  205. $table->text('gold_dis', trans('course.labels.gold_dis'));
  206. $table->text('blue_dis', trans('course.labels.blue_dis'));
  207. $table->text('white_dis', trans('course.labels.white_dis'));
  208. $table->text('red_dis', trans('course.labels.red_dis'));
  209. })->label(trans('course.labels.hole_list'))->default(CourseField::$baseHoleList)->disableCreate()->disableDelete()->width(12, 0);
  210. });
  211. $form->saving(function (Form $form) {
  212. if (is_array($form->before9)) {
  213. $this->before9 = array_values($form->before9);
  214. $form->deleteInput('before9');
  215. }
  216. if (is_array($form->after9)) {
  217. $this->after9 = array_values($form->after9);
  218. $form->deleteInput('after9');
  219. }
  220. });
  221. $form->saved(function (Form $form) {
  222. $id = $form->getKey(); //新增数据后返回的主键 id
  223. //前九
  224. if (!empty($this->before9) && is_array($this->before9)) {
  225. foreach ($this->before9 as $val) {
  226. unset($val['id']);
  227. unset($val['_remove_']);
  228. $val['course_field_id'] = $id;
  229. $val['type'] = 1;
  230. $map = [
  231. 'type' => $val['type'],
  232. 'sort' => $val['sort'],
  233. 'course_field_id' => $id,
  234. ];
  235. if ($info = CourseHole::query()->where($map)->first()) {
  236. CourseHole::query()->where($map)->update($val);
  237. } else {
  238. CourseHole::query()->create($val); //这里记得给字段在 Model中添加到 $fillable
  239. }
  240. }
  241. $listBefore9 = CourseHole::query()
  242. ->where('course_field_id', $id)
  243. ->where('type',1)
  244. ->select('sort', 'par', 'difficulty', 'gold_dis', 'blue_dis', 'white_dis', 'red_dis')
  245. ->orderBy('sort', 'asc')
  246. ->get();
  247. if($listBefore9->isNotEmpty()){
  248. $listBefore9 = $listBefore9->toArray();
  249. }else{
  250. $listBefore9 = null;
  251. }
  252. }else{
  253. $listBefore9 = [];
  254. }
  255. //后九
  256. if (!empty($this->after9) && is_array($this->after9)) {
  257. foreach ($this->after9 as $val2) {
  258. unset($val2['id']);
  259. unset($val2['_remove_']);
  260. $val2['course_field_id'] = $id;
  261. $val2['type'] = 2;
  262. $map2 = [
  263. 'type' => $val2['type'],
  264. 'course_field_id' => $id,
  265. 'sort' => $val2['sort'],
  266. ];
  267. if ($info = CourseHole::query()->where($map2)->first()) {
  268. CourseHole::query()->where($map2)->update($val2);
  269. } else {
  270. CourseHole::query()->create($val2); //这里记得给字段在 Model中添加到 $fillable
  271. }
  272. }
  273. $listAfter9 = CourseHole::query()
  274. ->where('course_field_id', $id)
  275. ->where('type',2)
  276. ->select('sort', 'par', 'difficulty', 'gold_dis', 'blue_dis', 'white_dis', 'red_dis')
  277. ->orderBy('sort', 'asc')
  278. ->get();
  279. if($listAfter9->isNotEmpty()){
  280. $listAfter9 = $listAfter9->toArray();
  281. }else{
  282. $listAfter9 = null;
  283. }
  284. }else{
  285. $listAfter9 = [];
  286. }
  287. $courseField = CourseField::find($id);
  288. $courseField->before9 = $listBefore9 ? json_encode($listBefore9) : '';
  289. $courseField->after9 = $listAfter9 ? json_encode($listAfter9) : '';
  290. $courseField->course_id = Cache::get('course_id');
  291. $courseField->save();
  292. return $form->response()->success('保存成功')->redirect('course_field/'.$id.'/edit');
  293. });
  294. $form->disableViewButton();
  295. $form->disableListButton();
  296. $form->tools(function (Form\Tools $tools) {
  297. $tools->append('<a class="btn btn-sm btn-primary" onClick="javascript :history.back(-1);" style="color:#ffffff;margin-right:10px">返回</a>');
  298. });
  299. $form->footer(function ($footer) {
  300. // 去掉`查看`checkbox
  301. $footer->disableViewCheck();
  302. // 去掉`继续编辑`checkbox
  303. $footer->disableEditingCheck();
  304. // 去掉`继续创建`checkbox
  305. $footer->disableCreatingCheck();
  306. });
  307. });
  308. }
  309. }