CmsContentTemplateSetRepository.php 28 KB

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