CmsContentTemplateSetRepository.php 28 KB

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