CmsContentTemplateSetRepository.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. public function __construct(CmsContentTemplateSet $cmsContentTemplateSet,CmsContentTemplate $cmsContentTemplate,CmsSubjectProduct $cmsSubjectProduct,CmsSubject $cmsSubject) {
  14. $this->cmsContentTemplateSet = $cmsContentTemplateSet;
  15. $this->cmsContentTemplate = $cmsContentTemplate;
  16. $this->cmsSubjectProduct = $cmsSubjectProduct;
  17. $this->cmsSubject = $cmsSubject;
  18. }
  19. /**
  20. * 点击内容配置
  21. */
  22. public function set($request)
  23. {
  24. $template = $this->cmsContentTemplate->select('id','city_name','city_id','title','apply_type','is_open','status')->where('id',$request['tpl_id'])->first();
  25. $templates = $template->toArray();
  26. // var_dump($templates);die;
  27. $where = [
  28. 'city_id'=>$templates['city_id'],
  29. 'apply_type'=>$templates['apply_type'],
  30. ];
  31. $copys_template = $this->cmsContentTemplate->where($where)->get();
  32. $request = [];
  33. $need_create = true;
  34. foreach ($copys_template as $k=>$v) {
  35. if ($v['status'] == 0) {
  36. $need_create = false;
  37. $request['type'] = $v['apply_type'];
  38. $request['tpl_id'] = $v['id'];
  39. $request['city_id'] = $v['city_id'];
  40. return $this->preview($request);
  41. }
  42. }
  43. if ($need_create) {
  44. $new_template = [
  45. 'title' => $templates['title'],
  46. 'city_id' => $templates['city_id'],
  47. 'city_name' => $templates['city_name'],
  48. 'apply_type' => $templates['apply_type'],
  49. 'status' => 0,
  50. 'is_open' => $templates['is_open'],
  51. ];
  52. DB::beginTransaction();
  53. try {
  54. $res = $this->cmsContentTemplate->create($new_template);
  55. if (!$res) {
  56. throw new HttpException(500, '添加草稿模板失败');
  57. }
  58. $template_set = $this->cmsContentTemplateSet->where('tpl_id', $templates['id'])->get();
  59. if (count($template_set) > 0) {
  60. foreach ($template_set->toArray() as $key => $val) {
  61. $copy_template = [
  62. 'rule' => $val['rule'],
  63. 'tpl_id' => $res->id,
  64. 'area_type' => $val['area_type'],
  65. 'status' => $val['status'],
  66. ];
  67. $result = $this->cmsContentTemplateSet->create($copy_template);
  68. if (!$result) {
  69. throw new HttpException(500, '生成模板内容失败');
  70. }
  71. }
  72. }
  73. $request['type'] = $res->apply_type;
  74. $request['tpl_id'] = $res->id;
  75. $request['city_id'] = $res->city_id;
  76. DB::commit();
  77. return $this->preview($request);
  78. //return Response::create();
  79. } catch (QueryException $exception) {
  80. DB::rollBack();
  81. return Response::create([
  82. 'message' => '生成模板内容失败,请重试',
  83. 'error' => $exception->getMessage(),
  84. 'status_code' => 500
  85. ]);
  86. }
  87. }
  88. }
  89. /**
  90. * banner配置
  91. */
  92. public function bannerSet($request)
  93. {
  94. $banner_id = $request['id'] ?? '';
  95. $rules = $request['rule'];
  96. if (is_array($rules)) {
  97. if (count($rules) > 10) {
  98. throw new HttpException(500, '最多只能添加10个banner海报');
  99. }
  100. $subject = [
  101. 'rule' => json_encode($rules),
  102. 'tpl_id' => $request['tpl_id'],
  103. 'area_type' => $request['area_type'],
  104. 'status' => 0,
  105. 'created_at' => date('Y-m-d H:i:s'),
  106. 'updated_at' => date('Y-m-d H:i:s')
  107. ];
  108. if ($banner_id) {
  109. $old_subject_id = $this->cmsContentTemplateSet->select('rule')->find($banner_id);
  110. $subject_id_array = json_decode($old_subject_id['rule'], true);
  111. $request_rule = $request['rule'];
  112. if (count($request_rule)>0){
  113. foreach ($request_rule as $k => $v) {
  114. if (isset($v['link_type']) && $v['link_type'] == 1) {
  115. $templates = $this->cmsSubject->where('id', intval($v['link_url']))->first();
  116. $templates->used_count += 1;
  117. $templates->save();
  118. }
  119. }
  120. }
  121. if (count($subject_id_array)>0) {
  122. foreach ($subject_id_array as $val) {
  123. if (isset($val['link_type']) && $val['link_type'] == 1) {
  124. $templates = $this->cmsSubject->where('id', intval($val['link_url']))->first();
  125. $update_template = [
  126. $templates->used_count -= 1
  127. ];
  128. $templates->update($update_template);
  129. }
  130. }
  131. }
  132. $banners = $this->cmsContentTemplateSet->where('id', $banner_id)->update($subject);
  133. if (!$banners) {
  134. throw new HttpException(500, '更新失败');
  135. }
  136. } else {
  137. if (count($request['rule'])>0) {
  138. foreach ($request['rule'] as $v) {
  139. if (isset($v['link_type']) && $v['link_type'] == 1) {
  140. $templates = $this->cmsSubject->where('id', intval($v['link_url']))->first();
  141. $templates->used_count += 1;
  142. $templates->save();
  143. }
  144. }
  145. }
  146. if (!$this->cmsContentTemplateSet->create($subject)) {
  147. throw new HttpException(500, '添加失败');
  148. }
  149. }
  150. }else{
  151. throw new HttpException(500, '参数格式有误');
  152. }
  153. }
  154. //专题被使用计数
  155. function update_subject($request)
  156. {
  157. $old_subject_id = $this->cmsContentTemplateSet->select('rule')->find($request['id']);
  158. $subject_id_array = json_decode($old_subject_id['rule'],true);
  159. if ($subject_id_array['link_type'] == 1 && $request['rule']['link_type'] == 1){//链接方式都为专题
  160. if ($subject_id_array['link_url'] != $request['rule']['link_url']){
  161. $templates = $this->cmsSubject->where('id',intval($subject_id_array['link_url']))->first();
  162. $update_template = [
  163. $templates->used_count -=1
  164. ];
  165. $templates->update($update_template);
  166. $templates = $this->cmsSubject->where('id',intval($request['rule']['link_url']))->first();
  167. $templates->used_count +=1;
  168. $templates->save();
  169. }
  170. }elseif ($subject_id_array['link_type'] == 1 && $request['rule']['link_type'] != 1){
  171. $templates = $this->cmsSubject->where('id',intval($subject_id_array['link_url']))->first();
  172. $update_template = [
  173. $templates->used_count -=1
  174. ];
  175. $templates->update($update_template);
  176. }elseif ($subject_id_array['link_type'] != 1 && $request['rule']['link_type'] == 1){
  177. $templates = $this->cmsSubject->where('id',intval($request['rule']['link_url']))->first();
  178. $templates->used_count +=1;
  179. $templates->save();
  180. }
  181. }
  182. /**
  183. * 专题广告配置
  184. */
  185. public function advertisementSet($request)
  186. {
  187. $advertisement_id = $request['id'] ?? '';
  188. $subject = [
  189. 'tpl_id' => $request['tpl_id'],
  190. 'rule' => json_encode($request['rule']),
  191. 'area_type' => $request['area_type'],
  192. 'status' => 0,
  193. 'created_at' => date('Y-m-d H:i:s'),
  194. 'updated_at' => date('Y-m-d H:i:s')
  195. ];
  196. if (empty($advertisement_id)){
  197. if (!$this->cmsContentTemplateSet->create($subject)) {
  198. throw new HttpException(500, '添加失败');
  199. }
  200. }else{
  201. $this->update_subject($request);
  202. $advertisement_ids = $this->cmsContentTemplateSet->where('id',$advertisement_id)->update($subject);
  203. if (!$advertisement_ids) {
  204. throw new HttpException(500, '更新失败');
  205. }
  206. }
  207. }
  208. /**
  209. * 商品楼层配置
  210. */
  211. public function floorSet($request)
  212. {
  213. $floor_id = $request['id'] ?? '';
  214. $subject = [
  215. 'tpl_id' => $request['tpl_id'],
  216. 'rule' => json_encode($request['rule']),
  217. 'area_type' => $request['area_type'],
  218. 'status' => 0,
  219. 'created_at' => date('Y-m-d H:i:s'),
  220. 'updated_at' => date('Y-m-d H:i:s')
  221. ];
  222. if (empty($floor_id)){
  223. if (!$this->cmsContentTemplateSet->create($subject)) {
  224. throw new HttpException(500, '添加失败');
  225. }
  226. }else {
  227. $this->update_subject($request);
  228. $floor_ids = $this->cmsContentTemplateSet->where('id',$floor_id)->update($subject);
  229. if (!$floor_ids) {
  230. throw new HttpException(500, '更新失败');
  231. }
  232. }
  233. }
  234. /**
  235. * 分类专题配置
  236. */
  237. public function categorySet($request)
  238. {
  239. $category_id = $request['id'] ?? '';
  240. $subject = [
  241. 'tpl_id' => $request['tpl_id'],
  242. 'rule' => json_encode($request['rule']),
  243. 'area_type' => $request['area_type'],
  244. 'status' => 0,
  245. 'created_at' => date('Y-m-d H:i:s'),
  246. 'updated_at' => date('Y-m-d H:i:s')
  247. ];
  248. if (empty($category_id)){
  249. if ($request['rule']['link_type'] == 1){
  250. $templates = $this->cmsSubject->where('id',intval($request['rule']['link_url']))->first();
  251. $templates->used_count +=1;
  252. $templates->save();
  253. }
  254. if (!$this->cmsContentTemplateSet->create($subject)) {
  255. throw new HttpException(500, '添加失败');
  256. }
  257. } else {
  258. $this->update_subject($request);
  259. $category_ids = $this->cmsContentTemplateSet->where('id',$category_id)->update($subject);
  260. if (!$category_ids) {
  261. throw new HttpException(500, '更新失败');
  262. }
  263. }
  264. }
  265. /**
  266. * 内容发布
  267. */
  268. public function release($request)
  269. {
  270. $group_key = config('constants.CMS_GROUP');
  271. $market_key = config('constants.CMS_MARKET');
  272. //同一个城市同一个模板(团购/菜市场) 只能有一种状态(草稿/发布),已发布的一旦被编辑把之前的直接删掉
  273. $template = $this->cmsContentTemplate->select('id','city_name','city_id','title','apply_type','is_open','status')->where('id',$request['tpl_id'])->first();
  274. $where = [
  275. 'city_id'=>$template->city_id,
  276. 'title'=>$template->title,
  277. 'apply_type'=>$template->apply_type,
  278. ];
  279. $templates = $this->cmsContentTemplate->where($where)->get();
  280. $update_is_open_ids=[];
  281. foreach ($templates->toArray() as $k=>$v) {
  282. if ($v['status'] == 1) {
  283. $update_is_open_ids[]=$v['id'];
  284. }
  285. }
  286. if ($update_is_open_ids){
  287. $delete_open_ids = $this->cmsContentTemplate->whereIn('id', $update_is_open_ids)->get();
  288. if (count($delete_open_ids)>0){
  289. $result = $this->cmsContentTemplate->whereIn('id', $update_is_open_ids)->delete();
  290. if (!$result) {
  291. throw new HttpException(500, '删除失败,请重试');
  292. }
  293. }
  294. $update_open_ids = $this->cmsContentTemplateSet->whereIn('tpl_id', $update_is_open_ids)->get();
  295. if (count($update_open_ids)>0){
  296. $res = $this->cmsContentTemplateSet->whereIn('tpl_id', $update_is_open_ids)->update(['status' => 0]);
  297. if (!$res) {
  298. throw new HttpException(500, '修改失败,请重试');
  299. }
  300. }
  301. }
  302. $update_tpl = $this->cmsContentTemplate->where('id', $request['tpl_id'])->first();
  303. if ($update_tpl){
  304. $result = $this->cmsContentTemplate->where('id', $request['tpl_id'])->update(['status' => 1]);
  305. if (!$result) {
  306. throw new HttpException(500, '修改失败,请重试');
  307. }
  308. }
  309. $template_id = $this->cmsContentTemplateSet->where('tpl_id',$request['tpl_id'])->get();
  310. if (count($template_id)>0){
  311. $res = $this->cmsContentTemplateSet->where('tpl_id',$request['tpl_id'])->update(['status'=>1]);
  312. if (!$res){
  313. throw new HttpException(500, '修改失败,请重试');
  314. }
  315. }
  316. foreach ($templates->toArray() as $k=>$v) {
  317. $result = $this->cmsContentTemplateSet->where(['status'=>0,'tpl_id'=>$v['id']])->delete();
  318. if ($result) {
  319. return Response::create();
  320. }
  321. }
  322. if ($template->apply_type == 0){
  323. if (Cache::has($group_key)) {
  324. Cache::forget($group_key);
  325. }
  326. }elseif ($template->apply_type == 1){
  327. if (Cache::has($market_key)) {
  328. Cache::forget($market_key);
  329. }
  330. }
  331. }
  332. /**
  333. * 内容预览
  334. */
  335. public function preview($request)
  336. {
  337. $temalates = $this->cmsContentTemplate->select('title')->where(['id'=>$request['tpl_id']])->first();
  338. if (!$temalates){
  339. throw new HttpException(500, '没有找到对应模板');
  340. }
  341. //团购首页
  342. $group_array = [];
  343. if ($request['type'] == 0){
  344. $group_array['apply_type'] = 0;
  345. $group_array['tpl_id'] = $request['tpl_id'];
  346. $group_array['title'] = $temalates->title;
  347. $group_array['content'] =[];
  348. }else{//菜市场首页
  349. $group_array['apply_type'] = 1;
  350. $group_array['tpl_id'] = $request['tpl_id'];
  351. $group_array['title'] = $temalates->title;
  352. $group_array['content'] =[];
  353. }
  354. $group_array['content'][0]['area_type'] = 0;
  355. $banner_rule = $this->cmsContentTemplateSet->select('id','rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>0])->orderBy('id', 'desc')->limit(1)->get();
  356. $new_rule = [];
  357. foreach ($banner_rule->toArray() as $k=>$v){
  358. if (count(json_decode($v['rule']))>0){
  359. $new_rule[$k]['id'] = $v['id'];
  360. $new_rule[$k]['rule'] = json_decode($v['rule'],true);
  361. }
  362. }
  363. $group_array['content'][0]['rule'] = $new_rule;
  364. $group_array['content'][1]['area_type'] = 1;
  365. $subject_rule = $this->cmsContentTemplateSet->select('id','rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>1])->get();
  366. $new_rule = [];
  367. foreach ($subject_rule->toArray() as $k=>$v){
  368. $new_rule[$k]['id'] = $v['id'];
  369. $new_rule[$k]['rule'] = json_decode($v['rule'],true);
  370. }
  371. $group_array['content'][1]['rule'] = $new_rule;
  372. $group_array['content'][2]['area_type'] = 2;
  373. $floor_rule = $this->cmsContentTemplateSet->select('id','rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>2])->get();
  374. $new_rule = [];
  375. foreach ($floor_rule->toArray() as $k=>$v){
  376. $new_rule[$k]['id'] = $v['id'];
  377. $new_rule[$k]['rule'] = json_decode($v['rule'],true);
  378. }
  379. foreach ($new_rule as $k=>$v){
  380. if ($v){
  381. $rules = $v['rule'];
  382. $show_num = intval($rules['show_num']);
  383. $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
  384. $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->limit($show_num)->get();
  385. $pro_array = $product->toArray();
  386. $res_id = implode(",", array_column($pro_array, 'product_id'));
  387. $new_rule[$k]['product_id'] = $res_id;
  388. $new_rule[$k]['subject_id'] = $rules['link_url'];
  389. $new_rule[$k]['show_type'] = $show_type->show_type ?? '';
  390. unset($v['url']);
  391. unset($v['show_num']);
  392. unset($rules['link_url']);
  393. unset($v['link_type']);
  394. }
  395. }
  396. $group_array['content'][2]['rule'] = $new_rule;
  397. if ($request['type'] == 1){
  398. $group_array['content'][3]['area_type'] = 3;
  399. $category_rule = $this->cmsContentTemplateSet->select('id','rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>3])->get();
  400. $new_rule = [];
  401. foreach ($category_rule->toArray() as $k=>$v){
  402. $new_rule[$k]['id'] = $v['id'];
  403. $new_rule[$k]['rule'] = json_decode($v['rule'],true);
  404. }
  405. $group_array['content'][3]['rule'] = $new_rule;
  406. }
  407. return $group_array;
  408. }
  409. public function getTemplate($cityId)
  410. {
  411. $group = $this->cmsContentTemplate->select('id','status','title','apply_type')->where(['city_id'=>$cityId,'deleted_at'=>null,'apply_type'=>0])->get();
  412. $groups = $group->toArray();
  413. if (count($groups)>1){
  414. foreach($groups as $key => $val) {
  415. if ($val['status'] == 1) {
  416. unset($groups[$key]);
  417. }
  418. }
  419. }
  420. $market = $this->cmsContentTemplate->select('id','status','title','apply_type')->where(['city_id'=>$cityId,'deleted_at'=>null,'apply_type'=>1])->get();
  421. $markets = $market->toArray();
  422. if (count($markets)>1){
  423. foreach($markets as $key => $val) {
  424. if ($val['status'] == 1) {
  425. unset($markets[$key]);
  426. }
  427. }
  428. }
  429. $templates['cms_content_templates'] = array_merge($groups,$markets);
  430. return $templates;
  431. }
  432. public function templateSetDelete($request)
  433. {
  434. $advertisement = $this->cmsContentTemplateSet->where('id', $request['id'])->first();
  435. $res = $advertisement->delete();
  436. if (!$res){
  437. return Response::create([
  438. 'message' => '删除失败,请重试',
  439. 'status_code' => 500
  440. ]);
  441. }
  442. }
  443. }