CmsContentTemplateSetRepository.php 26 KB

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