CmsContentTemplateRepository.php 2.8 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['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. $where = ['city_id'=>$request['city_id'],'title'=>trim($request['title'])];
  31. $template = $this->cmsContentTemplate->where($where)->get();
  32. foreach ($template->toArray() as $k=>$v){
  33. if ($v['is_open']==1){
  34. $template_id = $this->cmsContentTemplate->find($v['id']);
  35. $template_id->is_open = 0;
  36. $template_id->updated_at = date('Y-m-d H:i:s');
  37. $res = $template_id->save();
  38. if (!$res) {
  39. throw new HttpException(500, '模板状态修改失败');
  40. }
  41. }
  42. }
  43. $subject = [
  44. 'title' => $request['title'],
  45. 'city_id' => $request['city_id'],
  46. 'city_name' => $request['city_name'],
  47. 'apply_type' => $request['apply_type'],
  48. 'is_open' => $request['is_open'],
  49. ];
  50. if (!$this->cmsContentTemplate->create($subject)) {
  51. throw new HttpException(500, '添加失败');
  52. }
  53. }
  54. /**
  55. * 修改模板状态
  56. */
  57. public function edit($request)
  58. {
  59. if ($request['is_open']==0){
  60. $where = [
  61. 'apply_type'=>$request['apply_type'],
  62. 'is_open'=>$request['is_open'],
  63. 'city_id'=>$request['city_id'],
  64. ];
  65. $template = $this->cmsContentTemplate->where($where)->get();
  66. if (count($template)<1){
  67. throw new HttpException(500, '同一城市同一模板必须有一个状态开启');
  68. }
  69. }
  70. $template_id = $this->cmsContentTemplate->find($request['id']);
  71. $template_id->is_open = $request['is_open'];
  72. $template_id->updated_at = date('Y-m-d H:i:s');
  73. $res = $template_id->save();
  74. if (!$res) {
  75. throw new HttpException(500, '修改状态失败');
  76. }
  77. }
  78. }