CmsContentTemplateRepository.php 4.7 KB

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