CmsContentTemplateRepository.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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']];
  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']];
  22. }
  23. return $this->cmsContentTemplate->where($where)->orderBy('id', 'asc')->paginate($perPage);
  24. }
  25. /**
  26. * 新建模板
  27. */
  28. public function create($request)
  29. {
  30. if($this->cmsContentTemplate->where(['city_id'=>$request['city_id'],'title'=>trim($request['title'])])->exists()){
  31. throw new HttpException(500, '当前城市该模块已经存在');
  32. }
  33. $subject = [
  34. 'title' => $request['title'],
  35. 'city_id' => $request['city_id'],
  36. 'city_name' => $request['city_name'],
  37. 'apply_type' => $request['apply_type'],
  38. 'is_open' => $request['is_open'],
  39. ];
  40. if (!$this->cmsContentTemplate->create($subject)) {
  41. throw new HttpException(500, '添加失败');
  42. }
  43. }
  44. /**
  45. * 修改模板状态
  46. */
  47. public function edit($request)
  48. {
  49. if ($request['is_open']==0){
  50. $where = [
  51. 'apply_type'=>$request['apply_type'],
  52. 'is_open'=>$request['is_open'],
  53. 'city_id'=>$request['city_id'],
  54. ];
  55. $template = $this->cmsContentTemplate->where($where)->get();
  56. if (count($template)<1){
  57. throw new HttpException(500, '同一城市同一模板必须有一个状态开启');
  58. }
  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. }