CmsContentTemplateSetRepository.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. ];
  119. if ($id) {
  120. $old_subject_id = $this->cmsContentTemplateSet->select('rule')->find($id);
  121. $subject_id_array = json_decode($old_subject_id['rule'], true);
  122. $request_rule = $request['rule'];
  123. if (count($request_rule) > 0) {
  124. foreach ($request_rule as $k => $v) {
  125. if (isset($v['link_type']) && $v['link_type'] == 1) {
  126. $templates = $this->cmsSubject->where('id', intval($v['link_url']))->first();
  127. $templates->used_count += 1;
  128. $templates->save();
  129. }
  130. }
  131. }
  132. if (count($subject_id_array) > 0) {
  133. foreach ($subject_id_array as $val) {
  134. if (isset($val['link_type']) && $val['link_type'] == 1) {
  135. $templates = $this->cmsSubject->where('id', intval($val['link_url']))->first();
  136. $update_template = [
  137. $templates->used_count -= 1
  138. ];
  139. $templates->update($update_template);
  140. }
  141. }
  142. }
  143. $update_templates = $this->cmsContentTemplateSet->where('id', $id)->update($templateSet);
  144. if (!$update_templates) {
  145. throw new HttpException(500, '更新失败');
  146. }
  147. } else {
  148. if (count($request['rule']) > 0) {
  149. foreach ($request['rule'] as $v) {
  150. if (isset($v['link_type']) && $v['link_type'] == 1) {
  151. $templates = $this->cmsSubject->where('id', intval($v['link_url']))->first();
  152. $templates->used_count += 1;
  153. $templates->save();
  154. }
  155. }
  156. }
  157. if (!$this->cmsContentTemplateSet->create($templateSet)) {
  158. throw new HttpException(500, '添加失败');
  159. }
  160. }
  161. }
  162. //专题被使用计数
  163. function update_subject($request)
  164. {
  165. $old_subject_id = $this->cmsContentTemplateSet->select('rule')->find($request['id']);
  166. $subject_id_array = json_decode($old_subject_id['rule'], true);
  167. if ($subject_id_array['link_type'] == 1 && $request['rule']['link_type'] == 1) {//链接方式都为专题
  168. if ($subject_id_array['link_url'] != $request['rule']['link_url']) {
  169. $templates = $this->cmsSubject->where('id', intval($subject_id_array['link_url']))->first();
  170. $update_template = [
  171. $templates->used_count -= 1
  172. ];
  173. $templates->update($update_template);
  174. $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
  175. $templates->used_count += 1;
  176. $templates->save();
  177. }
  178. } elseif ($subject_id_array['link_type'] == 1 && $request['rule']['link_type'] != 1) {
  179. $templates = $this->cmsSubject->where('id', intval($subject_id_array['link_url']))->first();
  180. $update_template = [
  181. $templates->used_count -= 1
  182. ];
  183. $templates->update($update_template);
  184. } elseif ($subject_id_array['link_type'] != 1 && $request['rule']['link_type'] == 1) {
  185. $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
  186. $templates->used_count += 1;
  187. $templates->save();
  188. }
  189. }
  190. /**
  191. * banner配置
  192. */
  193. public function bannerSet($request)
  194. {
  195. $id = $request['id'] ?? '';
  196. $rules = $request['rule'];
  197. if (is_array($rules)) {
  198. if (count($rules) > 10) {
  199. throw new HttpException(500, '最多只能添加10个banner海报');
  200. }
  201. $this->request_data($request, $id, $rules);
  202. } else {
  203. throw new HttpException(500, '参数格式有误');
  204. }
  205. }
  206. /**
  207. * 专题广告配置
  208. */
  209. public function advertisementSet($request)
  210. {
  211. $advertisement_id = $request['id'] ?? '';
  212. $subject = [
  213. 'tpl_id' => $request['tpl_id'],
  214. 'rule' => json_encode($request['rule']),
  215. 'area_type' => $request['area_type'],
  216. 'status' => 0,
  217. ];
  218. if (empty($advertisement_id)) {
  219. if ($request['rule']['link_type'] == 1) {
  220. $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
  221. $templates->used_count += 1;
  222. $templates->save();
  223. }
  224. if (!$this->cmsContentTemplateSet->create($subject)) {
  225. throw new HttpException(500, '添加失败');
  226. }
  227. } else {
  228. $this->update_subject($request);
  229. $advertisement_ids = $this->cmsContentTemplateSet->where('id', $advertisement_id)->update($subject);
  230. if (!$advertisement_ids) {
  231. throw new HttpException(500, '更新失败');
  232. }
  233. }
  234. }
  235. /**
  236. * 商品楼层配置
  237. */
  238. public function floorSet($request)
  239. {
  240. $floor_id = $request['id'] ?? '';
  241. if (isset($request['rule']['link_type'])) {
  242. $request['rule']['link_type'] = intval($request['rule']['link_type']);
  243. }
  244. if (isset($request['rule']['show_num'])) {
  245. $request['rule']['show_num'] = intval($request['rule']['show_num']);
  246. }
  247. $subject = [
  248. 'tpl_id' => $request['tpl_id'],
  249. 'rule' => json_encode($request['rule']),
  250. 'area_type' => $request['area_type'],
  251. 'status' => 0,
  252. 'sort' => isset($request['sort']) ? $request['sort'] : 999,
  253. 'name' => isset($request['name']) ? $request['name'] : '',
  254. ];
  255. if (empty($floor_id)) {
  256. if ($request['rule']['link_type'] == 1) {
  257. $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
  258. $templates->used_count += 1;
  259. $templates->save();
  260. }
  261. if (!$this->cmsContentTemplateSet->create($subject)) {
  262. throw new HttpException(500, '添加失败');
  263. }
  264. } else {
  265. $this->update_subject($request);
  266. $floor_ids = $this->cmsContentTemplateSet->where('id', $floor_id)->update($subject);
  267. if (!$floor_ids) {
  268. throw new HttpException(500, '更新失败');
  269. }
  270. }
  271. }
  272. /**
  273. * 分类专题配置
  274. */
  275. public function categorySet($request)
  276. {
  277. $category_id = $request['id'] ?? '';
  278. $subject = [
  279. 'tpl_id' => $request['tpl_id'],
  280. 'rule' => json_encode($request['rule']),
  281. 'area_type' => $request['area_type'],
  282. 'status' => 0,
  283. ];
  284. if (empty($category_id)) {
  285. if ($request['rule']['link_type'] == 1) {
  286. $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
  287. $templates->used_count += 1;
  288. $templates->save();
  289. }
  290. if (!$this->cmsContentTemplateSet->create($subject)) {
  291. throw new HttpException(500, '添加失败');
  292. }
  293. } else {
  294. $this->update_subject($request);
  295. $category_ids = $this->cmsContentTemplateSet->where('id', $category_id)->update($subject);
  296. if (!$category_ids) {
  297. throw new HttpException(500, '更新失败');
  298. }
  299. }
  300. }
  301. //左一右二配置
  302. public function subjectOne($request)
  303. {
  304. $id = $request['id'] ?? '';
  305. $rules = $request['rule'];
  306. $this->request_data($request, $id, $rules);
  307. }
  308. //上一下三配置
  309. public function subjectTwo($request)
  310. {
  311. $id = $request['id'] ?? '';
  312. $rules = $request['rule'];
  313. $this->request_data($request, $id, $rules);
  314. }
  315. /**
  316. * 内容发布
  317. */
  318. public function release($request)
  319. {
  320. $group_key = config('constants.CMS_GROUP');
  321. $market_key = config('constants.CMS_MARKET');
  322. //同一个城市同一个模板(团购/菜市场) 只能有一种状态(草稿/发布),已发布的一旦被编辑把之前的直接删掉
  323. $template = $this->cmsContentTemplate->select('id', 'city_name', 'city_id', 'title', 'apply_type', 'is_open', 'status')->where('id', $request['tpl_id'])->first();
  324. $where = [
  325. 'city_id' => $template->city_id,
  326. 'title' => $template->title,
  327. 'apply_type' => $template->apply_type,
  328. ];
  329. $templates = $this->cmsContentTemplate->where($where)->get();
  330. $update_is_open_ids = [];
  331. foreach ($templates->toArray() as $k => $v) {
  332. if ($v['status'] == 1) {
  333. $update_is_open_ids[] = $v['id'];
  334. }
  335. }
  336. if ($update_is_open_ids) {
  337. $delete_open_ids = $this->cmsContentTemplate->whereIn('id', $update_is_open_ids)->get();
  338. if (count($delete_open_ids) > 0) {
  339. $result = $this->cmsContentTemplate->whereIn('id', $update_is_open_ids)->delete();
  340. if (!$result) {
  341. throw new HttpException(500, '删除失败,请重试');
  342. }
  343. }
  344. $update_open_ids = $this->cmsContentTemplateSet->whereIn('tpl_id', $update_is_open_ids)->get();
  345. if (count($update_open_ids) > 0) {
  346. $res = $this->cmsContentTemplateSet->whereIn('tpl_id', $update_is_open_ids)->update(['status' => 0]);
  347. if (!$res) {
  348. throw new HttpException(500, '修改失败,请重试');
  349. }
  350. }
  351. }
  352. $update_tpl = $this->cmsContentTemplate->where('id', $request['tpl_id'])->first();
  353. if ($update_tpl) {
  354. $result = $this->cmsContentTemplate->where('id', $request['tpl_id'])->update(['status' => 1]);
  355. if (!$result) {
  356. throw new HttpException(500, '修改失败,请重试');
  357. }
  358. }
  359. $template_id = $this->cmsContentTemplateSet->where('tpl_id', $request['tpl_id'])->get();
  360. if (count($template_id) > 0) {
  361. $res = $this->cmsContentTemplateSet->where('tpl_id', $request['tpl_id'])->update(['status' => 1]);
  362. if (!$res) {
  363. throw new HttpException(500, '修改失败,请重试');
  364. }
  365. }
  366. foreach ($templates->toArray() as $k => $v) {
  367. $result = $this->cmsContentTemplateSet->where(['status' => 0, 'tpl_id' => $v['id']])->delete();
  368. if ($result) {
  369. return Response::create();
  370. }
  371. }
  372. if ($template->apply_type == 0) {
  373. if (Cache::has($group_key)) {
  374. Cache::forget($group_key);
  375. }
  376. } elseif ($template->apply_type == 1) {
  377. if (Cache::has($market_key)) {
  378. Cache::forget($market_key);
  379. }
  380. }
  381. }
  382. /**
  383. * 内容预览
  384. */
  385. public function preview($request)
  386. {
  387. $temalates = $this->cmsContentTemplate->select('title')->where(['id' => $request['tpl_id']])->first();
  388. if (!$temalates) {
  389. throw new HttpException(500, '没有找到对应模板');
  390. }
  391. //团购首页
  392. $group_array = [];
  393. if ($request['type'] == 0) {
  394. $group_array['apply_type'] = 0;
  395. $group_array['tpl_id'] = $request['tpl_id'];
  396. $group_array['title'] = $temalates->title;
  397. $group_array['content'] = [];
  398. } else {//菜市场首页
  399. $group_array['apply_type'] = 1;
  400. $group_array['tpl_id'] = $request['tpl_id'];
  401. $group_array['title'] = $temalates->title;
  402. $group_array['content'] = [];
  403. }
  404. $group_array['content'][0]['area_type'] = 0;
  405. $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 0])->orderBy('id', 'desc')->limit(1)->get();
  406. $new_rule = [];
  407. foreach ($banner_rule->toArray() as $k => $v) {
  408. if (count(json_decode($v['rule'])) > 0) {
  409. $new_rule[$k]['id'] = $v['id'];
  410. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  411. }
  412. }
  413. $group_array['content'][0]['rule'] = $new_rule;
  414. $group_array['content'][1]['area_type'] = 1;
  415. $subject_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 1])->get();
  416. $new_rule = [];
  417. foreach ($subject_rule->toArray() as $k => $v) {
  418. $new_rule[$k]['id'] = $v['id'];
  419. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  420. }
  421. $group_array['content'][1]['rule'] = $new_rule;
  422. $group_array['content'][2]['area_type'] = 2;
  423. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 2])->get();
  424. $new_rule = [];
  425. foreach ($floor_rule->toArray() as $k => $v) {
  426. $new_rule[$k]['id'] = $v['id'];
  427. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  428. }
  429. foreach ($new_rule as $k => $v) {
  430. if ($v) {
  431. $rules = $v['rule'];
  432. $show_num = intval($rules['show_num']);
  433. $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
  434. $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->limit($show_num)->get();
  435. $pro_array = $product->toArray();
  436. $res_id = implode(",", array_column($pro_array, 'product_id'));
  437. $new_rule[$k]['product_id'] = $res_id;
  438. $new_rule[$k]['subject_id'] = $rules['link_url'];
  439. $new_rule[$k]['show_type'] = $show_type->show_type ?? '';
  440. unset($v['url']);
  441. unset($v['show_num']);
  442. unset($rules['link_url']);
  443. unset($v['link_type']);
  444. }
  445. }
  446. $group_array['content'][2]['rule'] = $new_rule;
  447. if ($request['type'] == 1) {
  448. $group_array['content'][3]['area_type'] = 3;
  449. $category_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 3])->get();
  450. $new_rule = [];
  451. foreach ($category_rule->toArray() as $k => $v) {
  452. $new_rule[$k]['id'] = $v['id'];
  453. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  454. }
  455. $group_array['content'][3]['rule'] = $new_rule;
  456. }
  457. return $group_array;
  458. }
  459. public function getTemplate($cityId)
  460. {
  461. $group = $this->cmsContentTemplate->select('id', 'status', 'title', 'apply_type')->where(['city_id' => $cityId, 'deleted_at' => null, 'apply_type' => 0])->get();
  462. $groups = $group->toArray();
  463. if (count($groups) > 1) {
  464. foreach ($groups as $key => $val) {
  465. if ($val['status'] == 1) {
  466. unset($groups[$key]);
  467. }
  468. }
  469. }
  470. $market = $this->cmsContentTemplate->select('id', 'status', 'title', 'apply_type')->where(['city_id' => $cityId, 'deleted_at' => null, 'apply_type' => 1])->get();
  471. $markets = $market->toArray();
  472. if (count($markets) > 1) {
  473. foreach ($markets as $key => $val) {
  474. if ($val['status'] == 1) {
  475. unset($markets[$key]);
  476. }
  477. }
  478. }
  479. $templates['cms_content_templates'] = array_merge($groups, $markets);
  480. return $templates;
  481. }
  482. public function templateSetDelete($request)
  483. {
  484. $advertisement = $this->cmsContentTemplateSet->where('id', $request['id'])->first();
  485. $res = $advertisement->delete();
  486. if (!$res) {
  487. return Response::create([
  488. 'message' => '删除失败,请重试',
  489. 'status_code' => 500
  490. ]);
  491. }
  492. }
  493. /**
  494. * v0.3兑换商城内容预览
  495. */
  496. public function exchangeMall($request)
  497. {
  498. $temalates = $this->cmsContentTemplate->select('title')->where(['id' => $request['tpl_id']])->first();
  499. if (!$temalates) {
  500. throw new HttpException(500, '没有找到对应模板');
  501. }
  502. $group_array = [];
  503. $group_array['apply_type'] = 2;
  504. $group_array['tpl_id'] = intval($request['tpl_id']);
  505. $group_array['title'] = $temalates->title;
  506. $group_array['content'] = [];
  507. $group_array['content'][0]['area_type'] = 0;
  508. $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 0])->orderBy('id', 'desc')->limit(1)->get();
  509. $new_rule = [];
  510. foreach ($banner_rule->toArray() as $k => $v) {
  511. if (count(json_decode($v['rule'])) > 0) {
  512. $new_rule[$k]['id'] = $v['id'];
  513. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  514. }
  515. }
  516. $group_array['content'][0]['rule'] = $new_rule;
  517. $subject_one = $this->cmsContentTemplateSet->select('id', 'rule', 'sort', 'name')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 4])->orderBy('id', 'desc')->limit(1)->get();
  518. $subject_two = $this->cmsContentTemplateSet->select('id', 'rule', 'sort', 'name')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 5])->limit(1)->get();
  519. $subject_one_sort = implode(',', array_column($subject_one->toArray(), 'sort'));
  520. $subject_two_sort = implode(',', array_column($subject_two->toArray(), 'sort'));
  521. if (intval($subject_one_sort) <= intval($subject_two_sort)) {
  522. $group_array['content'][1]['area_type'] = 4;
  523. $new_rule = [];
  524. foreach ($subject_one->toArray() as $k => $v) {
  525. if (count(json_decode($v['rule'])) > 0) {
  526. $new_rule[$k]['id'] = $v['id'];
  527. $new_rule[$k]['name'] = $v['name'];
  528. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  529. }
  530. }
  531. $group_array['content'][1]['rule'] = $new_rule;
  532. $group_array['content'][2]['area_type'] = 5;
  533. $new_rule = [];
  534. foreach ($subject_two->toArray() as $k => $v) {
  535. if (count(json_decode($v['rule'])) > 0) {
  536. $new_rule[$k]['id'] = $v['id'];
  537. $new_rule[$k]['name'] = $v['name'];
  538. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  539. }
  540. }
  541. $group_array['content'][2]['rule'] = $new_rule;
  542. } else {
  543. $group_array['content'][1]['area_type'] = 5;
  544. $new_rule = [];
  545. foreach ($subject_two->toArray() as $k => $v) {
  546. if (count(json_decode($v['rule'])) > 0) {
  547. $new_rule[$k]['id'] = $v['id'];
  548. $new_rule[$k]['name'] = $v['name'];
  549. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  550. }
  551. }
  552. $group_array['content'][1]['rule'] = $new_rule;
  553. $group_array['content'][2]['area_type'] = 4;
  554. $new_rule = [];
  555. foreach ($subject_one->toArray() as $k => $v) {
  556. if (count(json_decode($v['rule'])) > 0) {
  557. $new_rule[$k]['id'] = $v['id'];
  558. $new_rule[$k]['name'] = $v['name'];
  559. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  560. }
  561. }
  562. $group_array['content'][2]['rule'] = $new_rule;
  563. }
  564. $group_array['content'][3]['area_type'] = 2;
  565. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 2])->get();
  566. $new_rule = [];
  567. foreach ($floor_rule->toArray() as $k => $v) {
  568. $new_rule[$k]['id'] = $v['id'];
  569. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  570. }
  571. foreach ($new_rule as $k => $v) {
  572. if ($v) {
  573. $rules = $v['rule'];
  574. $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
  575. $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->get();
  576. $pro_array = $product->toArray();
  577. $res_id = implode(",", array_column($pro_array, 'product_id'));
  578. $new_rule[$k]['product_id'] = $res_id;
  579. $new_rule[$k]['subject_id'] = intval($rules['link_url']);
  580. $new_rule[$k]['show_type'] = $show_type->show_type ?? '';
  581. unset($v['url']);
  582. unset($rules['link_url']);
  583. unset($v['link_type']);
  584. }
  585. }
  586. $group_array['content'][3]['rule'] = $new_rule;
  587. return $group_array;
  588. }
  589. }