CmsContentTemplateRepository.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. $where = ['city_id'=>$request['city_id'],'apply_type'=>$request['apply_type']];
  38. $template = $this->cmsContentTemplate->where($where)->get();
  39. if (count($template) >= 1){
  40. throw new HttpException(500, '当前城市该模版类型已存在');
  41. }
  42. $subject = [
  43. 'title' => $request['title'],
  44. 'city_id' => $request['city_id'],
  45. 'city_name' => $request['city_name'],
  46. 'apply_type' => $request['apply_type'],
  47. 'status' => 1,
  48. 'is_open' => 0,
  49. ];
  50. if ($request['city_id'] == 610100){
  51. $subject['is_open'] = 1;
  52. }
  53. if (!$this->cmsContentTemplate->create($subject)) {
  54. throw new HttpException(500, '添加失败');
  55. }
  56. }
  57. /**
  58. * 修改模板状态
  59. */
  60. public function edit($request)
  61. {
  62. $group_key = config('constants.CMS_GROUP');
  63. $market_key = config('constants.CMS_MARKET');
  64. $template_id = $this->cmsContentTemplate->find($request['id']);
  65. if ($request['city_id'] == 610100 && $request['is_open'] == 0){
  66. throw new HttpException(500, '城市为西安的模板不允许手动关闭');
  67. }
  68. if ($request['is_open'] == 1 && $template_id->status == 0){
  69. throw new HttpException(500, '请先配置内容并发布之后再开启');
  70. }
  71. $template = CmsContentTemplateSet::where('tpl_id',$request['id'])->get();
  72. if (count($template) == 0){
  73. throw new HttpException(500, '配置内容为空无法开启');
  74. }
  75. $template_id->is_open = $request['is_open'];
  76. $template_id->updated_at = date('Y-m-d H:i:s');
  77. $res = $template_id->save();
  78. if (!$res) {
  79. throw new HttpException(500, '修改状态失败');
  80. }
  81. if ($template_id->apply_type == 0){
  82. if (Cache::has($group_key)) {
  83. Cache::forget($group_key);
  84. }
  85. }elseif ($template_id->apply_type == 1){
  86. if (Cache::has($market_key)) {
  87. Cache::forget($market_key);
  88. }
  89. }
  90. }
  91. /**
  92. * 修改模板名称
  93. * @param $data
  94. * @return mixed
  95. */
  96. public function updateTemplateName($data){
  97. return $this->cmsContentTemplate->where('id',$data['id'])->update(['title'=>$data['title']]);
  98. }
  99. }