123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?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'],
- ];
- }
- return $this->cmsContentTemplate->where($where)->where('is_open','=',1)->orderBy('id', 'asc')->paginate($perPage);
- }
- /**
- * 新建模板
- */
- public function create($request)
- {
- $where = ['city_id'=>$request['city_id'],'title'=>trim($request['title'])];
- $template = $this->cmsContentTemplate->where($where)->get();
- if (count($template) == 2){
- throw new HttpException(500, '每个城市只能存在两个相同的模版');
- }
- foreach ($template->toArray() as $k=>$v){
- if ($v['is_open'] == 1){
- $request['is_open'] = 0;
- }else{
- $request['is_open'] = 1;
- }
- }
- $subject = [
- 'title' => $request['title'],
- 'city_id' => $request['city_id'],
- 'city_name' => $request['city_name'],
- 'apply_type' => $request['apply_type'],
- 'is_open' => $request['is_open'],
- ];
- if (!$this->cmsContentTemplate->create($subject)) {
- throw new HttpException(500, '添加失败');
- }
- }
- /**
- * 修改模板状态
- */
- public function edit($request)
- {
- if ($request['is_open']==0){
- $where = [
- 'apply_type'=>$request['apply_type'],
- 'is_open'=>$request['is_open'],
- 'city_id'=>$request['city_id'],
- ];
- $template = $this->cmsContentTemplate->where($where)->get();
- if (count($template)<1){
- throw new HttpException(500, '同一城市同一模板必须有一个状态开启');
- }
- }
- $template_id = $this->cmsContentTemplate->find($request['id']);
- $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']]);
- }
- }
|