CmsContentTemplateRepository.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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['id'])){
  15. $where[] = ['id'=>$request['id'],'is_open'=>1];
  16. }
  17. if (isset($request['is_open'])){
  18. $where[] = ['is_open',$request['is_open']];
  19. }
  20. if (isset($request['city_id'])){
  21. $where[] = ['city_id'=>$request['city_id'],'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'])];
  31. $template = $this->cmsContentTemplate->where($where)->get();
  32. if (count($template)>2){
  33. throw new HttpException(500, '每个城市只能存在两个相同的模版');
  34. }
  35. $subject = [
  36. 'title' => $request['title'],
  37. 'city_id' => $request['city_id'],
  38. 'city_name' => $request['city_name'],
  39. 'apply_type' => $request['apply_type'],
  40. 'is_open' => $request['is_open'],
  41. ];
  42. if (!$this->cmsContentTemplate->create($subject)) {
  43. throw new HttpException(500, '添加失败');
  44. }
  45. }
  46. /**
  47. * 修改模板状态
  48. */
  49. public function edit($request)
  50. {
  51. if ($request['is_open']==0){
  52. $where = [
  53. 'apply_type'=>$request['apply_type'],
  54. 'is_open'=>$request['is_open'],
  55. 'city_id'=>$request['city_id'],
  56. ];
  57. $template = $this->cmsContentTemplate->where($where)->get();
  58. if (count($template)<1){
  59. throw new HttpException(500, '同一城市同一模板必须有一个状态开启');
  60. }
  61. }
  62. $template_id = $this->cmsContentTemplate->find($request['id']);
  63. $template_id->is_open = $request['is_open'];
  64. $template_id->updated_at = date('Y-m-d H:i:s');
  65. $res = $template_id->save();
  66. if (!$res) {
  67. throw new HttpException(500, '修改状态失败');
  68. }
  69. }
  70. }