CmsContentTemplateSetRepository.php 25 KB

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