CmsContentTemplateRepository.php 2.4 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['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. $subject = [
  35. 'title' => $request['title'],
  36. 'city_id' => $request['city_id'],
  37. 'city_name' => $request['city_name'],
  38. 'apply_type' => $request['apply_type'],
  39. 'is_open' => $request['is_open'],
  40. ];
  41. if (!$this->cmsContentTemplate->create($subject)) {
  42. throw new HttpException(500, '添加失败');
  43. }
  44. }
  45. /**
  46. * 修改模板状态
  47. */
  48. public function edit($request)
  49. {
  50. if ($request['is_open']==0){
  51. $where = [
  52. 'apply_type'=>$request['apply_type'],
  53. 'is_open'=>$request['is_open'],
  54. 'city_id'=>$request['city_id'],
  55. ];
  56. $template = $this->cmsContentTemplate->where($where)->get();
  57. if (count($template)<1){
  58. throw new HttpException(500, '同一城市同一模板必须有一个状态开启');
  59. }
  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. }