CmsContentTemplateSetRepository.php 29 KB

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