CmsContentTemplateSetRepository.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Repositories;
  3. use Dingo\Api\Http\Response;
  4. use App\Models\CmsContentTemplate;
  5. use App\Models\CmsContentTemplateSet;
  6. use Symfony\Component\HttpKernel\Exception\HttpException;
  7. class CmsContentTemplateSetRepository {
  8. public function __construct(CmsContentTemplateSet $cmsContentTemplateSet,CmsContentTemplate $cmsContentTemplate) {
  9. $this->cmsContentTemplateSet = $cmsContentTemplateSet;
  10. $this->cmsContentTemplate = $cmsContentTemplate;
  11. }
  12. /**
  13. * banner配置
  14. */
  15. public function bannerSet($request)
  16. {
  17. $rules = json_decode($request['rule'],true);
  18. if (is_array($rules)){
  19. if (count($rules)>10) {
  20. throw new HttpException(500, '最多只能添加10个banner海报');
  21. }
  22. $subject = [
  23. 'tpl_id' => $request['tpl_id'],
  24. 'rule' => $request['rule'],
  25. 'area_type' => $request['area_type'],
  26. 'status' => 0,
  27. ];
  28. if (!$this->cmsContentTemplateSet->create($subject)) {
  29. throw new HttpException(500, '添加失败');
  30. }
  31. }else{
  32. throw new HttpException(500, '参数格式有误');
  33. }
  34. }
  35. /**
  36. * 专题广告配置
  37. */
  38. public function advertisementSet($request)
  39. {
  40. $rules = json_decode($request['rule'],true);
  41. if (is_array($rules)){
  42. $subject = [
  43. 'tpl_id' => $request['tpl_id'],
  44. 'rule' => $request['rule'],
  45. 'area_type' => $request['area_type'],
  46. 'status' => 0,
  47. ];
  48. if (!$this->cmsContentTemplateSet->create($subject)) {
  49. throw new HttpException(500, '添加失败');
  50. }
  51. }else{
  52. throw new HttpException(500, '参数格式有误');
  53. }
  54. }
  55. /**
  56. * 商品楼层配置
  57. */
  58. public function floorSet($request)
  59. {
  60. $rules = json_decode($request['rule'],true);
  61. if (is_array($rules)){
  62. $subject = [
  63. 'tpl_id' => $request['tpl_id'],
  64. 'rule' => $request['rule'],
  65. 'area_type' => $request['area_type'],
  66. 'status' => 0,
  67. ];
  68. if (!$this->cmsContentTemplateSet->create($subject)) {
  69. throw new HttpException(500, '添加失败');
  70. }
  71. }else{
  72. throw new HttpException(500, '参数格式有误');
  73. }
  74. }
  75. /**
  76. * 分类专题配置
  77. */
  78. public function categorySet($request)
  79. {
  80. $rules = json_decode($request['rule'],true);
  81. if (is_array($rules)){
  82. $subject = [
  83. 'tpl_id' => $request['tpl_id'],
  84. 'rule' => $request['rule'],
  85. 'area_type' => $request['area_type'],
  86. 'status' => 0,
  87. ];
  88. if (!$this->cmsContentTemplateSet->create($subject)) {
  89. throw new HttpException(500, '添加失败');
  90. }
  91. }else{
  92. throw new HttpException(500, '参数格式有误');
  93. }
  94. }
  95. /**
  96. * 内容发布
  97. */
  98. public function release($request)
  99. {
  100. //同一个城市同一个模板(团购/菜市场) 只能有一种状态(草稿/发布),已发布的一旦被编辑把之前的直接删掉
  101. $templateSet = $this->cmsContentTemplateSet->where('tpl_id',$request['tpl_id'])->select('id','status')->get();
  102. $tem_array = $templateSet->toArray();
  103. foreach ($tem_array as $k=>$v) {
  104. if ($v['status'] == 1) {
  105. $result = $this->cmsContentTemplateSet->where('id', $v['id'])->delete();
  106. if (!$result) {
  107. return Response::create([
  108. 'message' => '删除失败,请重试',
  109. 'status_code' => 500
  110. ]);
  111. }
  112. }
  113. }
  114. $res = $this->cmsContentTemplateSet->where('tpl_id',$request['tpl_id'])->update(['status'=>1]);
  115. if (!$res){
  116. return Response::create([
  117. 'message' => '修改失败,请重试',
  118. 'status_code' => 500
  119. ]);
  120. }
  121. }
  122. }