CmsContentTemplateSetRepository.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. //同一个城市同一个模板(团购/菜市场) 只能有一种状态(草稿/发布),已发布的一旦被编辑把之前的直接删掉
  325. $template = $this->cmsContentTemplate->select('id', 'city_name', 'city_id', 'title', 'apply_type', 'is_open', 'status')->where('id', $request['tpl_id'])->first();
  326. $where = [
  327. 'city_id' => $template->city_id,
  328. 'title' => $template->title,
  329. 'apply_type' => $template->apply_type,
  330. ];
  331. $templates = $this->cmsContentTemplate->where($where)->get();
  332. $update_is_open_ids = [];
  333. foreach ($templates->toArray() as $k => $v) {
  334. if ($v['status'] == 1) {
  335. $update_is_open_ids[] = $v['id'];
  336. }
  337. }
  338. if ($update_is_open_ids) {
  339. $delete_open_ids = $this->cmsContentTemplate->whereIn('id', $update_is_open_ids)->get();
  340. if (count($delete_open_ids) > 0) {
  341. $result = $this->cmsContentTemplate->whereIn('id', $update_is_open_ids)->delete();
  342. if (!$result) {
  343. throw new HttpException(500, '删除失败,请重试');
  344. }
  345. }
  346. $update_open_ids = $this->cmsContentTemplateSet->whereIn('tpl_id', $update_is_open_ids)->get();
  347. if (count($update_open_ids) > 0) {
  348. $res = $this->cmsContentTemplateSet->whereIn('tpl_id', $update_is_open_ids)->update(['status' => 0]);
  349. if (!$res) {
  350. throw new HttpException(500, '修改失败,请重试');
  351. }
  352. }
  353. }
  354. $update_tpl = $this->cmsContentTemplate->where('id', $request['tpl_id'])->first();
  355. if ($update_tpl) {
  356. $result = $this->cmsContentTemplate->where('id', $request['tpl_id'])->update(['status' => 1]);
  357. if (!$result) {
  358. throw new HttpException(500, '修改失败,请重试');
  359. }
  360. }
  361. $template_id = $this->cmsContentTemplateSet->where('tpl_id', $request['tpl_id'])->get();
  362. if (count($template_id) > 0) {
  363. $res = $this->cmsContentTemplateSet->where('tpl_id', $request['tpl_id'])->update(['status' => 1]);
  364. if (!$res) {
  365. throw new HttpException(500, '修改失败,请重试');
  366. }
  367. }
  368. foreach ($templates->toArray() as $k => $v) {
  369. $result = $this->cmsContentTemplateSet->where(['status' => 0, 'tpl_id' => $v['id']])->delete();
  370. if ($result) {
  371. return Response::create();
  372. }
  373. }
  374. if ($template->apply_type == 0) {
  375. if (Cache::has($group_key)) {
  376. Cache::forget($group_key);
  377. }
  378. } elseif ($template->apply_type == 1) {
  379. if (Cache::has($market_key)) {
  380. Cache::forget($market_key);
  381. }
  382. }
  383. }
  384. /**
  385. * 内容预览
  386. */
  387. public function preview($request)
  388. {
  389. $temalates = $this->cmsContentTemplate->select('title')->where(['id' => $request['tpl_id']])->first();
  390. if (!$temalates) {
  391. throw new HttpException(500, '没有找到对应模板');
  392. }
  393. //团购首页
  394. $group_array = [];
  395. if ($request['type'] == 0) {
  396. $group_array['apply_type'] = 0;
  397. $group_array['tpl_id'] = $request['tpl_id'];
  398. $group_array['title'] = $temalates->title;
  399. $group_array['content'] = [];
  400. } else {//菜市场首页
  401. $group_array['apply_type'] = 1;
  402. $group_array['tpl_id'] = $request['tpl_id'];
  403. $group_array['title'] = $temalates->title;
  404. $group_array['content'] = [];
  405. }
  406. $group_array['content'][0]['area_type'] = 0;
  407. $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 0])->orderBy('id', 'desc')->limit(1)->get();
  408. $new_rule = [];
  409. foreach ($banner_rule->toArray() as $k => $v) {
  410. if (count(json_decode($v['rule'])) > 0) {
  411. $new_rule[$k]['id'] = $v['id'];
  412. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  413. }
  414. }
  415. $group_array['content'][0]['rule'] = $new_rule;
  416. $group_array['content'][1]['area_type'] = 1;
  417. $subject_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 1])->get();
  418. $new_rule = [];
  419. foreach ($subject_rule->toArray() as $k => $v) {
  420. $new_rule[$k]['id'] = $v['id'];
  421. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  422. }
  423. $group_array['content'][1]['rule'] = $new_rule;
  424. $group_array['content'][2]['area_type'] = 2;
  425. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 2])->get();
  426. $new_rule = [];
  427. foreach ($floor_rule->toArray() as $k => $v) {
  428. $new_rule[$k]['id'] = $v['id'];
  429. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  430. }
  431. foreach ($new_rule as $k => $v) {
  432. if ($v) {
  433. $rules = $v['rule'];
  434. $show_num = intval($rules['show_num']);
  435. $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
  436. $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->limit($show_num)->get();
  437. $pro_array = $product->toArray();
  438. $res_id = implode(",", array_column($pro_array, 'product_id'));
  439. $new_rule[$k]['product_id'] = $res_id;
  440. $new_rule[$k]['subject_id'] = $rules['link_url'];
  441. $new_rule[$k]['show_type'] = $show_type->show_type ?? '';
  442. unset($v['url']);
  443. unset($v['show_num']);
  444. unset($rules['link_url']);
  445. unset($v['link_type']);
  446. }
  447. }
  448. $group_array['content'][2]['rule'] = $new_rule;
  449. if ($request['type'] == 1) {
  450. $group_array['content'][3]['area_type'] = 3;
  451. $category_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 3])->get();
  452. $new_rule = [];
  453. foreach ($category_rule->toArray() as $k => $v) {
  454. $new_rule[$k]['id'] = $v['id'];
  455. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  456. }
  457. $group_array['content'][3]['rule'] = $new_rule;
  458. }
  459. return $group_array;
  460. }
  461. public function getTemplate($cityId)
  462. {
  463. $group = $this->cmsContentTemplate->select('id', 'status', 'title', 'apply_type')->where(['city_id' => $cityId, 'deleted_at' => null, 'apply_type' => 0])->get();
  464. $groups = $group->toArray();
  465. if (count($groups) > 1) {
  466. foreach ($groups as $key => $val) {
  467. if ($val['status'] == 1) {
  468. unset($groups[$key]);
  469. }
  470. }
  471. }
  472. $market = $this->cmsContentTemplate->select('id', 'status', 'title', 'apply_type')->where(['city_id' => $cityId, 'deleted_at' => null, 'apply_type' => 1])->get();
  473. $markets = $market->toArray();
  474. if (count($markets) > 1) {
  475. foreach ($markets as $key => $val) {
  476. if ($val['status'] == 1) {
  477. unset($markets[$key]);
  478. }
  479. }
  480. }
  481. $templates['cms_content_templates'] = array_merge($groups, $markets);
  482. return $templates;
  483. }
  484. public function templateSetDelete($request)
  485. {
  486. $advertisement = $this->cmsContentTemplateSet->where('id', $request['id'])->first();
  487. $res = $advertisement->delete();
  488. if (!$res) {
  489. return Response::create([
  490. 'message' => '删除失败,请重试',
  491. 'status_code' => 500
  492. ]);
  493. }
  494. }
  495. /**
  496. * v0.3兑换商城内容预览
  497. */
  498. public function exchangeMall($request)
  499. {
  500. $temalates = $this->cmsContentTemplate->select('title')->where(['id' => $request['tpl_id']])->first();
  501. if (!$temalates) {
  502. throw new HttpException(500, '没有找到对应模板');
  503. }
  504. $group_array = [];
  505. $group_array['apply_type'] = 2;
  506. $group_array['tpl_id'] = intval($request['tpl_id']);
  507. $group_array['title'] = $temalates->title;
  508. $group_array['content'] = [];
  509. $group_array['content'][0]['area_type'] = 0;
  510. $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 0])->orderBy('id', 'desc')->limit(1)->get();
  511. $new_rule = [];
  512. foreach ($banner_rule->toArray() as $k => $v) {
  513. if (count(json_decode($v['rule'])) > 0) {
  514. $new_rule[$k]['id'] = $v['id'];
  515. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  516. }
  517. }
  518. $group_array['content'][0]['rule'] = $new_rule;
  519. $subject_one = $this->cmsContentTemplateSet->select('id', 'rule', 'sort', 'name')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 4])->orderBy('id', 'desc')->limit(1)->get();
  520. $subject_two = $this->cmsContentTemplateSet->select('id', 'rule', 'sort', 'name')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 5])->limit(1)->get();
  521. $subject_one_sort = implode(',', array_column($subject_one->toArray(), 'sort'));
  522. $subject_two_sort = implode(',', array_column($subject_two->toArray(), 'sort'));
  523. if (intval($subject_one_sort) <= intval($subject_two_sort)) {
  524. $group_array['content'][1]['area_type'] = 4;
  525. $new_rule = [];
  526. foreach ($subject_one->toArray() as $k => $v) {
  527. if (count(json_decode($v['rule'])) > 0) {
  528. $new_rule[$k]['id'] = $v['id'];
  529. $new_rule[$k]['name'] = $v['name'];
  530. $new_rule[$k]['image'] = config('customer.cms_subject_one_bgimg');
  531. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  532. }
  533. }
  534. $group_array['content'][1]['rule'] = $new_rule;
  535. $group_array['content'][2]['area_type'] = 5;
  536. $new_rule = [];
  537. foreach ($subject_two->toArray() as $k => $v) {
  538. if (count(json_decode($v['rule'])) > 0) {
  539. $new_rule[$k]['id'] = $v['id'];
  540. $new_rule[$k]['name'] = $v['name'];
  541. $new_rule[$k]['image'] = config('customer.cms_subject_two_bgimg');
  542. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  543. }
  544. }
  545. $group_array['content'][2]['rule'] = $new_rule;
  546. } else {
  547. $group_array['content'][1]['area_type'] = 5;
  548. $new_rule = [];
  549. foreach ($subject_two->toArray() as $k => $v) {
  550. if (count(json_decode($v['rule'])) > 0) {
  551. $new_rule[$k]['id'] = $v['id'];
  552. $new_rule[$k]['name'] = $v['name'];
  553. $new_rule[$k]['image'] = config('customer.cms_subject_two_bgimg');
  554. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  555. }
  556. }
  557. $group_array['content'][1]['rule'] = $new_rule;
  558. $group_array['content'][2]['area_type'] = 4;
  559. $new_rule = [];
  560. foreach ($subject_one->toArray() as $k => $v) {
  561. if (count(json_decode($v['rule'])) > 0) {
  562. $new_rule[$k]['id'] = $v['id'];
  563. $new_rule[$k]['name'] = $v['name'];
  564. $new_rule[$k]['image'] = config('customer.cms_subject_one_bgimg');
  565. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  566. }
  567. }
  568. $group_array['content'][2]['rule'] = $new_rule;
  569. }
  570. $group_array['content'][3]['area_type'] = 2;
  571. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule','floor_img')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 2])->get();
  572. $new_rule = [];
  573. foreach ($floor_rule->toArray() as $k => $v) {
  574. $new_rule[$k]['id'] = $v['id'];
  575. $new_rule[$k]['floor_img'] = $v['floor_img'];
  576. $new_rule[$k]['rule'] = json_decode($v['rule'], true);
  577. }
  578. foreach ($new_rule as $k => $v) {
  579. if ($v) {
  580. $rules = $v['rule'];
  581. $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
  582. $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->get();
  583. $pro_array = $product->toArray();
  584. $res_id = implode(",", array_column($pro_array, 'product_id'));
  585. $new_rule[$k]['product_id'] = $res_id;
  586. $new_rule[$k]['subject_id'] = intval($rules['link_url']);
  587. $new_rule[$k]['show_type'] = $show_type->show_type ?? '';
  588. unset($v['url']);
  589. unset($rules['link_url']);
  590. unset($v['link_type']);
  591. }
  592. }
  593. $group_array['content'][3]['rule'] = $new_rule;
  594. return $group_array;
  595. }
  596. }