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. return $this->cmsContentTemplate->where($where)->where('is_open','=',1)->orderBy('id', 'asc')->paginate($perPage);
  23. }
  24. /**
  25. * 新建模板
  26. */
  27. public function create($request)
  28. {
  29. $where = ['city_id'=>$request['city_id'],'title'=>trim($request['title'])];
  30. $template = $this->cmsContentTemplate->where($where)->get();
  31. if (count($template) == 2){
  32. throw new HttpException(500, '每个城市只能存在两个相同的模版');
  33. }
  34. foreach ($template->toArray() as $k=>$v){
  35. if ($v['is_open'] == 1){
  36. $request['is_open'] = 0;
  37. }else{
  38. $request['is_open'] = 1;
  39. }
  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. 'is_open' => $request['is_open'],
  47. ];
  48. if (!$this->cmsContentTemplate->create($subject)) {
  49. throw new HttpException(500, '添加失败');
  50. }
  51. }
  52. /**
  53. * 修改模板状态
  54. */
  55. public function edit($request)
  56. {
  57. if ($request['city_id'] == 610100 && $request['is_open'] == 0){
  58. throw new HttpException(500, '西安的模板不允许手动关闭');
  59. }
  60. if ($request['is_open']==0){
  61. $where = [
  62. 'apply_type'=>$request['apply_type'],
  63. 'is_open'=>$request['is_open'],
  64. 'city_id'=>$request['city_id'],
  65. ];
  66. $template = $this->cmsContentTemplate->where($where)->get();
  67. if (count($template)<1){
  68. throw new HttpException(500, '同一城市同一模板必须有一个状态开启');
  69. }
  70. }
  71. $template_id = $this->cmsContentTemplate->find($request['id']);
  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. }