CmsContentTemplateRepository.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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']),'apply_type'=>$request['apply_type']];
  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. $template_id = $this->cmsContentTemplate->find($request['id']);
  61. $template_id->is_open = $request['is_open'];
  62. $template_id->updated_at = date('Y-m-d H:i:s');
  63. $res = $template_id->save();
  64. if (!$res) {
  65. throw new HttpException(500, '修改状态失败');
  66. }
  67. }
  68. /**
  69. * 修改模板名称
  70. * @param $data
  71. * @return mixed
  72. */
  73. public function updateTemplateName($data){
  74. return $this->cmsContentTemplate->where('id',$data['id'])->update(['title'=>$data['title']]);
  75. }
  76. }