CmsContentTemplateRepository.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. $where[] = ['is_open', '=',1];
  22. }
  23. return $this->cmsContentTemplate->where($where)->orderBy('id', 'asc')->paginate($perPage);
  24. }
  25. /**
  26. * 新建模板
  27. */
  28. public function create($request)
  29. {
  30. $where = ['city_id'=>$request['city_id'],'title'=>trim($request['title']),'apply_type'=>$request['apply_type']];
  31. $template = $this->cmsContentTemplate->where($where)->get();
  32. if (count($template) == 2){
  33. throw new HttpException(500, '每个城市只能存在两个相同的模版');
  34. }
  35. foreach ($template->toArray() as $k=>$v){
  36. if ($v['is_open'] == 1){
  37. $request['is_open'] = 0;
  38. }else{
  39. $request['is_open'] = 1;
  40. }
  41. }
  42. $subject = [
  43. 'title' => $request['title'],
  44. 'city_id' => $request['city_id'],
  45. 'city_name' => $request['city_name'],
  46. 'apply_type' => $request['apply_type'],
  47. 'is_open' => $request['is_open'],
  48. ];
  49. if (!$this->cmsContentTemplate->create($subject)) {
  50. throw new HttpException(500, '添加失败');
  51. }
  52. }
  53. /**
  54. * 修改模板状态
  55. */
  56. public function edit($request)
  57. {
  58. if ($request['city_id'] == 610100 && $request['is_open'] == 0){
  59. throw new HttpException(500, '西安的模板不允许手动关闭');
  60. }
  61. $template_id = $this->cmsContentTemplate->find($request['id']);
  62. $template_id->is_open = $request['is_open'];
  63. $template_id->updated_at = date('Y-m-d H:i:s');
  64. $res = $template_id->save();
  65. if (!$res) {
  66. throw new HttpException(500, '修改状态失败');
  67. }
  68. }
  69. /**
  70. * 修改模板名称
  71. * @param $data
  72. * @return mixed
  73. */
  74. public function updateTemplateName($data){
  75. return $this->cmsContentTemplate->where('id',$data['id'])->update(['title'=>$data['title']]);
  76. }
  77. }