CmsContentTemplateRepository.php 3.2 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. }else{
  17. $where[] = ['is_open', '=',1];
  18. }
  19. if (isset($request['city_id'])){
  20. $where[] = [
  21. 'city_id','=',$request['city_id'],
  22. ];
  23. }
  24. return $this->cmsContentTemplate->where($where)->where('status',1)->orderBy('id', 'asc')->paginate($perPage);
  25. }
  26. /**
  27. * 新建模板
  28. */
  29. public function create($request)
  30. {
  31. $where = ['city_id'=>$request['city_id'],'title'=>trim($request['title']),'apply_type'=>$request['apply_type']];
  32. $template = $this->cmsContentTemplate->where($where)->get();
  33. if (count($template) == 2){
  34. throw new HttpException(500, '每个城市只能存在两个相同的模版');
  35. }
  36. foreach ($template->toArray() as $k=>$v){
  37. if ($v['is_open'] == 1){
  38. $request['is_open'] = 0;
  39. }else{
  40. $request['is_open'] = 1;
  41. }
  42. }
  43. $subject = [
  44. 'title' => $request['title'],
  45. 'city_id' => $request['city_id'],
  46. 'city_name' => $request['city_name'],
  47. 'apply_type' => $request['apply_type'],
  48. 'is_open' => $request['is_open'] ?? 0,
  49. 'status' => 0
  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. $where = ['city_id'=>$template_id->city_id,'title'=>$template_id->title,'apply_type'=>$template_id->apply_type];
  62. $template = $this->cmsContentTemplate->where($where)->get();
  63. // foreach ($template->toArray() as $v){
  64. // if ($v['is_open'] == 1 && $request['is_open'] == 0){
  65. // throw new HttpException(500, '请先关闭该城市模块下已开启的模板后操作');
  66. // }
  67. // }
  68. if ($request['city_id'] == 610100 && $request['is_open'] == 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. }