CmsContentTemplateRepository.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\CmsContentTemplate;
  4. use App\Models\CmsContentTemplateSet;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\Log;
  7. use Symfony\Component\HttpKernel\Exception\HttpException;
  8. use Illuminate\Support\Facades\Redis;
  9. class CmsContentTemplateRepository {
  10. public function __construct(CmsContentTemplate $cmsContentTemplate) {
  11. $this->cmsContentTemplate = $cmsContentTemplate;
  12. }
  13. //模板列表
  14. public function index($request)
  15. {
  16. $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
  17. $where = [];
  18. if (isset($request['is_open'])){
  19. $where[] = ['is_open','=',$request['is_open']];
  20. }
  21. if (isset($request['city_id'])){
  22. $where[] = [
  23. 'city_id','=',$request['city_id'],
  24. ];
  25. }
  26. if (isset($request['status'])){
  27. $where[] = [
  28. 'status','=',$request['status'],
  29. ];
  30. }
  31. return $this->cmsContentTemplate->where($where)->where('status',1)->orderBy('apply_type','desc')->orderBy('id', 'asc')->paginate($perPage);
  32. }
  33. /**
  34. * 新建模板
  35. */
  36. public function create($request)
  37. {
  38. if (isset($request['city_id'])) {
  39. if ($this->cmsContentTemplate->where(['city_id' => $request['city_id'], 'apply_type' => $request['apply_type']])->exists()) {
  40. throw new HttpException(500, '当前城市该模版类型已存在');
  41. }
  42. }
  43. if ($request['apply_type'] == 2) {
  44. if ($this->cmsContentTemplate->where('apply_type', '=', 2)->exists()) {
  45. throw new HttpException(500, '兑换商城只能有一个');
  46. }
  47. }
  48. $subject = [
  49. 'title' => $request['title'],
  50. 'city_id' => isset($request['city_id']) ? $request['city_id'] : 0,
  51. 'city_name' => isset($request['city_name']) ? $request['city_name'] : '',
  52. 'apply_type' => $request['apply_type'],
  53. 'status' => 1,
  54. 'is_open' => 0,
  55. ];
  56. //默认开启,且不可手动关闭
  57. if (isset($request['city_id']) && $request['city_id'] == 610100){
  58. $subject['is_open'] = 1;
  59. }
  60. if ($request['apply_type'] == 2){
  61. $subject['is_open'] = 1;
  62. }
  63. if (!$this->cmsContentTemplate->create($subject)) {
  64. throw new HttpException(500, '添加失败');
  65. }
  66. }
  67. /**
  68. * 修改模板状态
  69. */
  70. public function edit($request)
  71. {
  72. $group_key = config('constants.CMS_GROUP');
  73. $market_key = config('constants.CMS_MARKET');
  74. $template_id = $this->cmsContentTemplate->find($request['id']);
  75. if ($request['city_id'] == 610100 && $request['is_open'] == 0){
  76. throw new HttpException(500, '城市为西安的模板不允许手动关闭');
  77. }
  78. if ($request['is_open'] == 1 && $template_id->status == 0){
  79. throw new HttpException(500, '请先配置内容并发布之后再开启');
  80. }
  81. $template = CmsContentTemplateSet::where('tpl_id',$request['id'])->get();
  82. if (count($template) == 0){
  83. throw new HttpException(500, '配置内容为空无法开启');
  84. }
  85. if ($template_id->apply_type == 2){
  86. throw new HttpException(500, '兑换商城不允许手动关闭');
  87. }
  88. $template_id->is_open = $request['is_open'];
  89. $template_id->updated_at = date('Y-m-d H:i:s');
  90. $res = $template_id->save();
  91. if (!$res) {
  92. throw new HttpException(500, '修改状态失败');
  93. }
  94. if ($template_id->apply_type == 0){
  95. // if (Cache::has($group_key)) {
  96. // Cache::forget($group_key);
  97. // }
  98. if (Redis::exists($group_key)) {
  99. Redis::del($group_key);
  100. }
  101. }elseif ($template_id->apply_type == 1){
  102. if (Redis::exists($market_key)) {
  103. Redis::del($market_key);
  104. }
  105. }
  106. }
  107. /**
  108. * 修改模板名称
  109. * @param $data
  110. * @return mixed
  111. */
  112. public function updateTemplateName($data)
  113. {
  114. $template = $this->cmsContentTemplate->select('city_id','apply_type')->where('id',$data['id'])->first();
  115. if ($template->apply_type == 2){
  116. throw new HttpException(500, '兑换商城不允许修改名称');
  117. }
  118. $template_array = $this->cmsContentTemplate->where(['city_id' => $template->city_id, 'apply_type' => $template->apply_type])->get();
  119. if (count($template_array)>1){
  120. foreach ($template_array->toArray() as $value){
  121. $res = $this->cmsContentTemplate->where('id',$value['id'])->update(['title'=>$data['title']]);
  122. if (!$res){
  123. throw new HttpException(500, '修改失败,请重试');
  124. }
  125. }
  126. }
  127. return $this->cmsContentTemplate->where('id',$data['id'])->update(['title'=>$data['title']]);
  128. }
  129. }