CmsContentTemplateRepository.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. Log::debug('request-param-createTemplate:'.json_encode($subject));
  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. $template_id = $this->cmsContentTemplate->find($request['id']);
  63. if ($request['city_id'] == 610100 && $request['is_open'] == 0){
  64. throw new HttpException(500, '城市为西安的模板不允许手动关闭');
  65. }
  66. if ($request['is_open'] == 1 && $template_id->status == 0){
  67. throw new HttpException(500, '请先配置内容并发布之后再开启');
  68. }
  69. $template = CmsContentTemplateSet::where('tpl_id',$request['id'])->get();
  70. if (count($template) == 0){
  71. throw new HttpException(500, '配置内容为空无法开启');
  72. }
  73. $template_id->is_open = $request['is_open'];
  74. $template_id->updated_at = date('Y-m-d H:i:s');
  75. $res = $template_id->save();
  76. if (!$res) {
  77. throw new HttpException(500, '修改状态失败');
  78. }
  79. }
  80. /**
  81. * 修改模板名称
  82. * @param $data
  83. * @return mixed
  84. */
  85. public function updateTemplateName($data){
  86. return $this->cmsContentTemplate->where('id',$data['id'])->update(['title'=>$data['title']]);
  87. }
  88. }