CmsContentTemplateSetRepository.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\CmsSubject;
  4. use Dingo\Api\Http\Response;
  5. use App\Models\CmsContentTemplate;
  6. use App\Models\CmsContentTemplateSet;
  7. use App\Models\CmsSubjectProduct;
  8. use Illuminate\Support\Facades\Cache;
  9. use Symfony\Component\HttpKernel\Exception\HttpException;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Database\QueryException;
  12. class CmsContentTemplateSetRepository
  13. {
  14. public function __construct(CmsContentTemplateSet $cmsContentTemplateSet, CmsContentTemplate $cmsContentTemplate, CmsSubjectProduct $cmsSubjectProduct, CmsSubject $cmsSubject)
  15. {
  16. $this->cmsContentTemplateSet = $cmsContentTemplateSet;
  17. $this->cmsContentTemplate = $cmsContentTemplate;
  18. $this->cmsSubjectProduct = $cmsSubjectProduct;
  19. $this->cmsSubject = $cmsSubject;
  20. }
  21. /**
  22. * 点击内容配置
  23. */
  24. public function set($request)
  25. {
  26. $template = $this->cmsContentTemplate->select('id', 'city_name', 'city_id', 'title', 'apply_type', 'is_open', 'status')->where('id', $request['tpl_id'])->first();
  27. if (!$template) {
  28. throw new HttpException(500, '没有找到对应模板');
  29. }
  30. $templates = $template->toArray();
  31. $where = [
  32. 'city_id' => $templates['city_id'],
  33. 'apply_type' => $templates['apply_type'],
  34. ];
  35. $copys_template = $this->cmsContentTemplate->where($where)->get();
  36. $request = [];
  37. $need_create = true;
  38. foreach ($copys_template as $k => $v) {
  39. if ($v['status'] == 0) {
  40. $need_create = false;
  41. $request['type'] = $v['apply_type'];
  42. $request['tpl_id'] = $v['id'];
  43. $request['city_id'] = $v['city_id'];
  44. if ($v['apply_type'] == 2) {
  45. return $this->exchangeMall($request);
  46. } else {
  47. return $this->preview($request);
  48. }
  49. }
  50. }
  51. if ($need_create) {
  52. $new_template = [
  53. 'title' => $templates['title'],
  54. 'city_id' => $templates['city_id'],
  55. 'city_name' => $templates['city_name'],
  56. 'apply_type' => $templates['apply_type'],
  57. 'status' => 0,
  58. 'is_open' => $templates['is_open'],
  59. ];
  60. DB::beginTransaction();
  61. try {
  62. $res = $this->cmsContentTemplate->create($new_template);
  63. if (!$res) {
  64. throw new HttpException(500, '添加草稿模板失败');
  65. }
  66. $template_set = $this->cmsContentTemplateSet->where('tpl_id', $templates['id'])->get();
  67. if (count($template_set) > 0) {
  68. foreach ($template_set->toArray() as $key => $val) {
  69. $copy_template = [
  70. 'rule' => $val['rule'],
  71. 'tpl_id' => $res->id,
  72. 'area_type' => $val['area_type'],
  73. 'status' => $val['status'],
  74. 'sort' => $val['sort'],
  75. 'name' => $val['name'],
  76. ];
  77. $result = $this->cmsContentTemplateSet->create($copy_template);
  78. if (!$result) {
  79. throw new HttpException(500, '生成模板内容失败');
  80. }
  81. }
  82. }
  83. $request['type'] = $res->apply_type;
  84. $request['tpl_id'] = $res->id;
  85. $request['city_id'] = $res->city_id;
  86. DB::commit();
  87. if ($res->apply_type == 2) {
  88. return $this->exchangeMall($request);
  89. } else {
  90. return $this->preview($request);
  91. }
  92. //return Response::create();
  93. } catch (QueryException $exception) {
  94. DB::rollBack();
  95. return Response::create([
  96. 'message' => '生成模板内容失败,请重试',
  97. 'error' => $exception->getMessage(),
  98. 'status_code' => 500
  99. ]);
  100. }
  101. }
  102. }
  103. //banner、左一右二、上一下三配置-数据处理
  104. function request_data($request, $id, $rules)
  105. {
  106. foreach ($rules as $k => $v) {
  107. if (isset($v['link_type'])) {
  108. $rules[$k]['link_type'] = intval($v['link_type']);
  109. }
  110. }
  111. $templateSet = [
  112. 'rule' => json_encode($rules),
  113. 'tpl_id' => $request['tpl_id'],
  114. 'area_type' => $request['area_type'],
  115. 'status' => 0,
  116. 'sort' => isset($request['sort']) ? $request['sort'] : 999,
  117. 'name' => isset($request['name']) ? $request['name'] : '',
  118. 'floor_img' => isset($request['floor_img']) ? $request['floor_img'] : '',
  119. ];
  120. if ($id) {
  121. $old_subject_id = $this->cmsContentTemplateSet->select('rule')->find($id);
  122. $subject_id_array = json_decode($old_subject_id['rule'], true);
  123. $request_rule = $request['rule'];
  124. if (count($request_rule) > 0) {
  125. foreach ($request_rule as $k => $v) {
  126. if (isset($v['link_type']) && $v['link_type'] == 1) {
  127. $templates = $this->cmsSubject->where('id', intval($v['link_url']))->first();
  128. $templates->used_count += 1;
  129. $templates->save();
  130. }
  131. }
  132. }
  133. if (count($subject_id_array) > 0) {
  134. foreach ($subject_id_array as $val) {
  135. if (isset($val['link_type']) && $val['link_type'] == 1) {
  136. $templates = $this->cmsSubject->where('id', intval($val['link_url']))->first();
  137. $update_template = [
  138. $templates->used_count -= 1
  139. ];
  140. $templates->update($update_template);
  141. }
  142. }
  143. }
  144. $update_templates = $this->cmsContentTemplateSet->where('id', $id)->update($templateSet);
  145. if (!$update_templates) {
  146. throw new HttpException(500, '更新失败');
  147. }
  148. } else {
  149. if (count($request['rule']) > 0) {
  150. foreach ($request['rule'] as $v) {
  151. if (isset($v['link_type']) && $v['link_type'] == 1) {
  152. $templates = $this->cmsSubject->where('id', intval($v['link_url']))->first();
  153. $templates->used_count += 1;
  154. $templates->save();
  155. }
  156. }
  157. }
  158. if (!$this->cmsContentTemplateSet->create($templateSet)) {
  159. throw new HttpException(500, '添加失败');
  160. }
  161. }
  162. }
  163. //专题被使用计数
  164. function update_subject($request)
  165. {
  166. $old_subject_id = $this->cmsContentTemplateSet->select('rule')->find($request['id']);
  167. $subject_id_array = json_decode($old_subject_id['rule'], true);
  168. if ($subject_id_array['link_type'] == 1 && $request['rule']['link_type'] == 1) {//链接方式都为专题
  169. if ($subject_id_array['link_url'] != $request['rule']['link_url']) {
  170. $templates = $this->cmsSubject->where('id', intval($subject_id_array['link_url']))->first();
  171. $update_template = [
  172. $templates->used_count -= 1
  173. ];
  174. $templates->update($update_template);
  175. $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
  176. $templates->used_count += 1;
  177. $templates->save();
  178. }
  179. } elseif ($subject_id_array['link_type'] == 1 && $request['rule']['link_type'] != 1) {
  180. $templates = $this->cmsSubject->where('id', intval($subject_id_array['link_url']))->first();
  181. $update_template = [
  182. $templates->used_count -= 1
  183. ];
  184. $templates->update($update_template);
  185. } elseif ($subject_id_array['link_type'] != 1 && $request['rule']['link_type'] == 1) {
  186. $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
  187. $templates->used_count += 1;
  188. $templates->save();
  189. }
  190. }
  191. /**
  192. * banner配置
  193. */
  194. public function bannerSet($request)
  195. {
  196. $id = $request['id'] ?? '';
  197. $rules = $request['rule'];
  198. if (is_array($rules)) {
  199. if (count($rules) > 10) {
  200. throw new HttpException(500, '最多只能添加10个banner海报');
  201. }
  202. $this->request_data($request, $id, $rules);
  203. } else {
  204. throw new HttpException(500, '参数格式有误');
  205. }
  206. }
  207. /**
  208. * 专题广告配置
  209. */
  210. public function advertisementSet($request)
  211. {
  212. $advertisement_id = $request['id'] ?? '';
  213. $subject = [
  214. 'tpl_id' => $request['tpl_id'],
  215. 'rule' => json_encode($request['rule']),
  216. 'area_type' => $request['area_type'],
  217. 'status' => 0,
  218. ];
  219. if (empty($advertisement_id)) {
  220. if ($request['rule']['link_type'] == 1) {
  221. $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
  222. $templates->used_count += 1;
  223. $templates->save();
  224. }
  225. if (!$this->cmsContentTemplateSet->create($subject)) {
  226. throw new HttpException(500, '添加失败');
  227. }
  228. } else {
  229. $this->update_subject($request);
  230. $advertisement_ids = $this->cmsContentTemplateSet->where('id', $advertisement_id)->update($subject);
  231. if (!$advertisement_ids) {
  232. throw new HttpException(500, '更新失败');
  233. }
  234. }
  235. }
  236. /**
  237. * 商品楼层配置
  238. */
  239. public function floorSet($request)
  240. {
  241. $floor_id = $request['id'] ?? '';
  242. if (isset($request['rule']['link_type'])) {
  243. $request['rule']['link_type'] = intval($request['rule']['link_type']);
  244. }
  245. if (isset($request['rule']['show_num'])) {
  246. $request['rule']['show_num'] = intval($request['rule']['show_num']);
  247. }
  248. $subject = [
  249. 'tpl_id' => $request['tpl_id'],
  250. 'rule' => json_encode($request['rule']),
  251. 'area_type' => $request['area_type'],
  252. 'status' => 0,
  253. 'sort' => isset($request['sort']) ? $request['sort'] : 999,
  254. 'name' => isset($request['name']) ? $request['name'] : '',
  255. 'floor_img' => isset($request['floor_img']) ? $request['floor_img'] : '',
  256. ];
  257. if (empty($floor_id)) {
  258. if ($request['rule']['link_type'] == 1) {
  259. $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
  260. $templates->used_count += 1;
  261. $templates->save();
  262. }
  263. if (!$this->cmsContentTemplateSet->create($subject)) {
  264. throw new HttpException(500, '添加失败');
  265. }
  266. } else {
  267. $this->update_subject($request);
  268. $floor_ids = $this->cmsContentTemplateSet->where('id', $floor_id)->update($subject);
  269. if (!$floor_ids) {
  270. throw new HttpException(500, '更新失败');
  271. }
  272. }
  273. }
  274. /**
  275. * 分类专题配置
  276. */
  277. public function categorySet($request)
  278. {
  279. $category_id = $request['id'] ?? '';
  280. $subject = [
  281. 'tpl_id' => $request['tpl_id'],
  282. 'rule' => json_encode($request['rule']),
  283. 'area_type' => $request['area_type'],
  284. 'status' => 0,
  285. ];
  286. if (empty($category_id)) {
  287. if ($request['rule']['link_type'] == 1) {
  288. $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
  289. $templates->used_count += 1;
  290. $templates->save();
  291. }
  292. if (!$this->cmsContentTemplateSet->create($subject)) {
  293. throw new HttpException(500, '添加失败');
  294. }
  295. } else {
  296. $this->update_subject($request);
  297. $category_ids = $this->cmsContentTemplateSet->where('id', $category_id)->update($subject);
  298. if (!$category_ids) {
  299. throw new HttpException(500, '更新失败');
  300. }
  301. }
  302. }
  303. //左一右二配置
  304. public function subjectOne($request)
  305. {
  306. $id = $request['id'] ?? '';
  307. $rules = $request['rule'];
  308. $this->request_data($request, $id, $rules);
  309. }
  310. //上一下三配置
  311. public function subjectTwo($request)
  312. {
  313. $id = $request['id'] ?? '';
  314. $rules = $request['rule'];
  315. $this->request_data($request, $id, $rules);
  316. }
  317. /**
  318. * 内容发布
  319. */
  320. public function release($request)
  321. {
  322. $group_key = config('constants.CMS_GROUP');
  323. $market_key = config('constants.CMS_MARKET');
  324. $exchange_key = config('constants.CMS_EXCHANGE');
  325. //同一个城市同一个模板(团购/菜市场) 只能有一种状态(草稿/发布),已发布的一旦被编辑把之前的直接删掉
  326. $template = $this->cmsContentTemplate->select('id', 'city_name', 'city_id', 'title', 'apply_type', 'is_open', 'status')->where('id', $request['tpl_id'])->first();
  327. $where = [
  328. 'city_id' => $template->city_id,
  329. 'title' => $template->title,
  330. 'apply_type' => $template->apply_type,
  331. ];
  332. $templates = $this->cmsContentTemplate->where($where)->get();
  333. $update_is_open_ids = [];
  334. foreach ($templates->toArray() as $k => $v) {
  335. if ($v['status'] == 1) {
  336. $update_is_open_ids[] = $v['id'];
  337. }
  338. }
  339. if ($update_is_open_ids) {
  340. $delete_open_ids = $this->cmsContentTemplate->whereIn('id', $update_is_open_ids)->get();
  341. if (count($delete_open_ids) > 0) {
  342. $result = $this->cmsContentTemplate->whereIn('id', $update_is_open_ids)->delete();
  343. if (!$result) {
  344. throw new HttpException(500, '删除失败,请重试');
  345. }
  346. }
  347. $update_open_ids = $this->cmsContentTemplateSet->whereIn('tpl_id', $update_is_open_ids)->get();
  348. if (count($update_open_ids) > 0) {
  349. $res = $this->cmsContentTemplateSet->whereIn('tpl_id', $update_is_open_ids)->update(['status' => 0]);
  350. if (!$res) {
  351. throw new HttpException(500, '修改失败,请重试');
  352. }
  353. }
  354. }
  355. $update_tpl = $this->cmsContentTemplate->where('id', $request['tpl_id'])->first();
  356. if ($update_tpl) {
  357. $result = $this->cmsContentTemplate->where('id', $request['tpl_id'])->update(['status' => 1]);
  358. if (!$result) {
  359. throw new HttpException(500, '修改失败,请重试');
  360. }
  361. }
  362. $template_id = $this->cmsContentTemplateSet->where('tpl_id', $request['tpl_id'])->get();
  363. if (count($template_id) > 0) {
  364. $res = $this->cmsContentTemplateSet->where('tpl_id', $request['tpl_id'])->update(['status' => 1]);
  365. if (!$res) {
  366. throw new HttpException(500, '修改失败,请重试');
  367. }
  368. }
  369. foreach ($templates->toArray() as $k => $v) {
  370. $result = $this->cmsContentTemplateSet->where(['status' => 0, 'tpl_id' => $v['id']])->delete();
  371. if ($result) {
  372. return Response::create();
  373. }
  374. }
  375. if ($template->apply_type == 0) {
  376. if (Cache::has($group_key)) {
  377. Cache::forget($group_key);
  378. }
  379. } elseif ($template->apply_type == 1) {
  380. if (Cache::has($market_key)) {
  381. Cache::forget($market_key);
  382. }
  383. } elseif ($template->apply_type == 2) {
  384. if (Cache::has($exchange_key)) {
  385. Cache::forget($exchange_key);
  386. }
  387. }
  388. }
  389. /**
  390. * 内容预览
  391. */
  392. public function preview($request)
  393. {
  394. $temalates = $this->cmsContentTemplate->select('title')->where(['id' => $request['tpl_id']])->first();
  395. if (!$temalates) {
  396. throw new HttpException(500, '没有找到对应模板');
  397. }
  398. //团购首页
  399. $group_array = [];
  400. if ($request['type'] == 0) {
  401. $group_array['apply_type'] = 0;
  402. $group_array['tpl_id'] = $request['tpl_id'];
  403. $group_array['title'] = $temalates->title;
  404. $group_array['content'] = [];
  405. } else {//菜市场首页
  406. $group_array['apply_type'] = 1;
  407. $group_array['tpl_id'] = $request['tpl_id'];
  408. $group_array['title'] = $temalates->title;
  409. $group_array['content'] = [];
  410. }
  411. $group_array['content'][0]['area_type'] = 0;
  412. $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 0])->orderBy('id', 'desc')->limit(1)->get();
  413. $new_rule = [];
  414. foreach ($banner_rule->toArray() as $k => $v) {
  415. if (count(json_decode($v['rule'])) > 0) {
  416. $new_rule[$k]['id'] = $v['id'];
  417. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  418. }
  419. }
  420. $group_array['content'][0]['rule'] = $new_rule;
  421. $group_array['content'][1]['area_type'] = 1;
  422. $subject_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 1])->get();
  423. $new_rule = [];
  424. foreach ($subject_rule->toArray() as $k => $v) {
  425. $new_rule[$k]['id'] = $v['id'];
  426. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  427. }
  428. $group_array['content'][1]['rule'] = $new_rule;
  429. $group_array['content'][2]['area_type'] = 2;
  430. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 2])->get();
  431. $new_rule = [];
  432. foreach ($floor_rule->toArray() as $k => $v) {
  433. $new_rule[$k]['id'] = $v['id'];
  434. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  435. }
  436. foreach ($new_rule as $k => $v) {
  437. if ($v) {
  438. $rules = $v['rule'];
  439. $show_num = intval($rules['show_num']);
  440. $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
  441. $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->limit($show_num)->get();
  442. $pro_array = $product->toArray();
  443. $res_id = implode(",", array_column($pro_array, 'product_id'));
  444. $new_rule[$k]['product_id'] = $res_id;
  445. $new_rule[$k]['subject_id'] = $rules['link_url'];
  446. $new_rule[$k]['show_type'] = $show_type->show_type ?? '';
  447. unset($v['url']);
  448. unset($v['show_num']);
  449. unset($rules['link_url']);
  450. unset($v['link_type']);
  451. }
  452. }
  453. $group_array['content'][2]['rule'] = $new_rule;
  454. if ($request['type'] == 1) {
  455. $group_array['content'][3]['area_type'] = 3;
  456. $category_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 3])->get();
  457. $new_rule = [];
  458. foreach ($category_rule->toArray() as $k => $v) {
  459. $new_rule[$k]['id'] = $v['id'];
  460. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  461. }
  462. $group_array['content'][3]['rule'] = $new_rule;
  463. }
  464. return $group_array;
  465. }
  466. public function getTemplate($cityId)
  467. {
  468. $group = $this->cmsContentTemplate->select('id', 'status', 'title', 'apply_type')->where(['city_id' => $cityId, 'deleted_at' => null, 'apply_type' => 0])->get();
  469. $groups = $group->toArray();
  470. if (count($groups) > 1) {
  471. foreach ($groups as $key => $val) {
  472. if ($val['status'] == 1) {
  473. unset($groups[$key]);
  474. }
  475. }
  476. }
  477. $market = $this->cmsContentTemplate->select('id', 'status', 'title', 'apply_type')->where(['city_id' => $cityId, 'deleted_at' => null, 'apply_type' => 1])->get();
  478. $markets = $market->toArray();
  479. if (count($markets) > 1) {
  480. foreach ($markets as $key => $val) {
  481. if ($val['status'] == 1) {
  482. unset($markets[$key]);
  483. }
  484. }
  485. }
  486. $templates['cms_content_templates'] = array_merge($groups, $markets);
  487. return $templates;
  488. }
  489. public function templateSetDelete($request)
  490. {
  491. $advertisement = $this->cmsContentTemplateSet->where('id', $request['id'])->first();
  492. $res = $advertisement->delete();
  493. if (!$res) {
  494. return Response::create([
  495. 'message' => '删除失败,请重试',
  496. 'status_code' => 500
  497. ]);
  498. }
  499. }
  500. /**
  501. * v0.3兑换商城内容预览
  502. */
  503. public function exchangeMall($request)
  504. {
  505. $temalates = $this->cmsContentTemplate->select('title')->where(['id' => $request['tpl_id']])->first();
  506. if (!$temalates) {
  507. throw new HttpException(500, '没有找到对应模板');
  508. }
  509. $group_array = [];
  510. $group_array['apply_type'] = 2;
  511. $group_array['tpl_id'] = intval($request['tpl_id']);
  512. $group_array['title'] = $temalates->title;
  513. $group_array['content'] = [];
  514. $group_array['content'][0]['area_type'] = 0;
  515. $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 0])->orderBy('id', 'desc')->limit(1)->get();
  516. $new_rule = [];
  517. foreach ($banner_rule->toArray() as $k => $v) {
  518. // if (count(json_decode($v['rule'])) > 0) {
  519. $new_rule[$k]['id'] = $v['id'];
  520. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  521. // }
  522. }
  523. $group_array['content'][0]['rule'] = $new_rule;
  524. $all_subject = $this->cmsContentTemplateSet->select('id', 'rule', 'sort', 'name', 'floor_img', 'area_type')->where(['tpl_id' => $request['tpl_id']])->whereIn('area_type', [4, 5])->orderBy('sort', 'asc')->get();
  525. $all_subject = $all_subject->toArray();
  526. $count = count($all_subject);
  527. $new_rule = [];
  528. foreach ($all_subject as $k => $v) {
  529. // if (count(json_decode($v['rule'])) > 0) {
  530. $new_rule[$k]['area_type'] = $v['area_type'];
  531. $new_rule[$k]['rule']['id'] = $v['id'];
  532. // $new_rule[$k]['rule']['name'] = $v['name'];
  533. $new_rule[$k]['rule']['sort'] = $v['sort'];
  534. $new_rule[$k]['rule']['floor_img'] = $v['floor_img'];
  535. $new_rule[$k]['rule']['rule'] = json_decode($v['rule'], true);
  536. // }
  537. }
  538. foreach ($new_rule as $k => $v) {
  539. $group_array['content'][$k + 1]['area_type'] = $new_rule[$k]['area_type'];
  540. $group_array['content'][$k + 1]['rule'] = array($new_rule[$k]['rule']);
  541. }
  542. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule', 'floor_img')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 2])->orderBy('id', 'asc')->get();
  543. $new_rule = [];
  544. foreach ($floor_rule->toArray() as $k => $v) {
  545. $new_rule[$k]['id'] = $v['id'];
  546. $new_rule[$k]['floor_img'] = $v['floor_img'];
  547. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  548. }
  549. foreach ($new_rule as $k => $v) {
  550. if ($v) {
  551. $rules = $v['rule'];
  552. $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
  553. $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->get();
  554. $pro_array = $product->toArray();
  555. $res_id = implode(",", array_column($pro_array, 'product_id'));
  556. $new_rule[$k]['product_id'] = $res_id;
  557. $new_rule[$k]['subject_id'] = intval($rules['link_url']);
  558. $new_rule[$k]['show_type'] = $show_type->show_type ?? '';
  559. unset($v['url']);
  560. unset($rules['link_url']);
  561. unset($v['link_type']);
  562. }
  563. }
  564. $group_array['content'][$count + 1]['area_type'] = 2;
  565. $group_array['content'][$count + 1]['rule'] = $new_rule;
  566. return $group_array;
  567. }
  568. }