CmsContentTemplateRepository.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\CmsContentTemplate;
  4. use App\Models\CmsContentTemplateSet;
  5. use Illuminate\Support\Facades\Log;
  6. use Symfony\Component\HttpKernel\Exception\HttpException;
  7. class CmsContentTemplateRepository {
  8. public function __construct(CmsContentTemplate $cmsContentTemplate) {
  9. $this->cmsContentTemplate = $cmsContentTemplate;
  10. }
  11. //模板列表
  12. public function index($request)
  13. {
  14. $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
  15. $where = [];
  16. if (isset($request['is_open'])){
  17. $where[] = ['is_open','=',$request['is_open']];
  18. }
  19. if (isset($request['city_id'])){
  20. $where[] = [
  21. 'city_id','=',$request['city_id'],
  22. ];
  23. }
  24. if (isset($request['status'])){
  25. $where[] = [
  26. 'status','=',$request['status'],
  27. ];
  28. }
  29. return $this->cmsContentTemplate->where($where)->where('status',1)->orderBy('id', 'asc')->paginate($perPage);
  30. }
  31. /**
  32. * 新建模板
  33. */
  34. public function create($request)
  35. {
  36. $where = ['city_id'=>$request['city_id'],'apply_type'=>$request['apply_type']];
  37. $template = $this->cmsContentTemplate->where($where)->get();
  38. if (count($template) >= 1){
  39. throw new HttpException(500, '当前城市该模版类型已存在');
  40. }
  41. $subject = [
  42. 'title' => $request['title'],
  43. 'city_id' => $request['city_id'],
  44. 'city_name' => $request['city_name'],
  45. 'apply_type' => $request['apply_type'],
  46. 'status' => 1,
  47. 'is_open' => 0,
  48. ];
  49. if ($request['city_id'] == 610100){
  50. $subject['is_open'] = 1;
  51. }
  52. if (!$this->cmsContentTemplate->create($subject)) {
  53. throw new HttpException(500, '添加失败');
  54. }
  55. }
  56. /**
  57. * 修改模板状态
  58. */
  59. public function edit($request)
  60. {
  61. $template_id = $this->cmsContentTemplate->find($request['id']);
  62. if ($request['city_id'] == 610100 && $request['is_open'] == 0){
  63. throw new HttpException(500, '城市为西安的模板不允许手动关闭');
  64. }
  65. if ($request['is_open'] == 1 && $template_id->status == 0){
  66. throw new HttpException(500, '请先配置内容并发布之后再开启');
  67. }
  68. $template = CmsContentTemplateSet::where('tpl_id',$request['id'])->get();
  69. if (count($template) == 0){
  70. throw new HttpException(500, '配置内容为空无法开启');
  71. }
  72. $template_id->is_open = $request['is_open'];
  73. $template_id->updated_at = date('Y-m-d H:i:s');
  74. $res = $template_id->save();
  75. if (!$res) {
  76. throw new HttpException(500, '修改状态失败');
  77. }
  78. }
  79. /**
  80. * 修改模板名称
  81. * @param $data
  82. * @return mixed
  83. */
  84. public function updateTemplateName($data){
  85. return $this->cmsContentTemplate->where('id',$data['id'])->update(['title'=>$data['title']]);
  86. }
  87. }