CmsContentTemplateRepository.php 3.0 KB

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