CmsContentTemplateRepository.php 3.0 KB

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