1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace App\Repositories;
- use App\Models\CmsContentTemplate;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- class CmsContentTemplateRepository {
- public function __construct(CmsContentTemplate $cmsContentTemplate) {
- $this->cmsContentTemplate = $cmsContentTemplate;
- }
- //模板列表
- public function index($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
- $where = [];
- if (isset($request['is_open'])){
- $where[] = ['is_open','=',$request['is_open']];
- }
- if (isset($request['city_id'])){
- $where[] = [
- 'city_id','=',$request['city_id'],
- ];
- }
- if (isset($request['status'])){
- $where[] = [
- 'status','=',$request['status'],
- ];
- }
- return $this->cmsContentTemplate->where($where)->orderBy('id', 'asc')->paginate($perPage);
- }
- /**
- * 新建模板
- */
- public function create($request)
- {
- $where = ['city_id'=>$request['city_id'],'apply_type'=>$request['apply_type']];
- $template = $this->cmsContentTemplate->where($where)->get();
- if (count($template) == 1){
- throw new HttpException(500, '当前城市该模版类型已存在');
- }
- $subject = [
- 'title' => $request['title'],
- 'city_id' => $request['city_id'],
- 'city_name' => $request['city_name'],
- 'apply_type' => $request['apply_type'],
- 'is_open' => 0,
- 'status' => 0
- ];
- if ($request['city_id'] == 610100){
- $subject['is_open'] = 1;
- }
- if (!$this->cmsContentTemplate->create($subject)) {
- throw new HttpException(500, '添加失败');
- }
- }
- /**
- * 修改模板状态
- */
- public function edit($request)
- {
- $template_id = $this->cmsContentTemplate->find($request['id']);
- if ($request['city_id'] == 610100 && $request['is_open'] == 0){
- throw new HttpException(500, '城市为西安的模板不允许手动关闭');
- }
- if ($request['is_open'] == 1 && $template_id->status == 0){
- throw new HttpException(500, '请先配置内容并发布之后再开启');
- }
- $template_id->is_open = $request['is_open'];
- $template_id->updated_at = date('Y-m-d H:i:s');
- $res = $template_id->save();
- if (!$res) {
- throw new HttpException(500, '修改状态失败');
- }
- }
- /**
- * 修改模板名称
- * @param $data
- * @return mixed
- */
- public function updateTemplateName($data){
- return $this->cmsContentTemplate->where('id',$data['id'])->update(['title'=>$data['title']]);
- }
- }
|