Forráskód Böngészése

增加专题被使用计数

duqinya 6 éve
szülő
commit
b0c124ce71

+ 98 - 35
app/Repositories/CmsContentTemplateSetRepository.php

@@ -29,12 +29,11 @@ class CmsContentTemplateSetRepository {
         $templates = $template->toArray();
         $where = [
             'city_id'=>$templates['city_id'],
-            'title'=>$templates['title'],
             'apply_type'=>$templates['apply_type'],
         ];
         $copys_template = $this->cmsContentTemplate->where($where)->get();
         $request = [];
-        $need_create=true;
+        $need_create = true;
         foreach ($copys_template as $k=>$v) {
             if ($v['status'] == 0) {
                 $need_create = false;
@@ -98,35 +97,95 @@ class CmsContentTemplateSetRepository {
     public function bannerSet($request)
     {
         $banner_id = $request['id'] ?? '';
-            $rules = $request['rule'];
-            if (is_array($rules)){
-                if (count($rules)>10) {
-                    throw new HttpException(500, '最多只能添加10个banner海报');
+        $rules = $request['rule'];
+        if (is_array($rules)) {
+            if (count($rules) > 10) {
+                throw new HttpException(500, '最多只能添加10个banner海报');
+            }
+            $subject = [
+                'rule' => json_encode($rules),
+                'tpl_id' => $request['tpl_id'],
+                'area_type' => $request['area_type'],
+                'status' => 0,
+                'created_at' => date('Y-m-d H:i:s'),
+                'updated_at' => date('Y-m-d H:i:s')
+            ];
+            if ($banner_id) {
+                $old_subject_id = $this->cmsContentTemplateSet->select('rule')->find($banner_id);
+                $subject_id_array = json_decode($old_subject_id['rule'], true);
+                $request_rule = $request['rule'];
+
+                foreach ($request_rule as $k => $v) {
+                    if ($v['link_type'] == 1) {
+                        $templates = $this->cmsSubject->where('id', intval($v['link_url']))->first();
+                        $templates->used_count += 1;
+                        $templates->save();
+                    }
                 }
-                            $subject = [
-                                'rule' => json_encode($rules),
-                                'tpl_id' => $request['tpl_id'],
-                                'area_type' => $request['area_type'],
-                                'status' => 0,
-                                'created_at' => date('Y-m-d H:i:s'),
-                                'updated_at' => date('Y-m-d H:i:s')
-                            ];
-                            if ($banner_id){
-                                $banners = $this->cmsContentTemplateSet->where('id',$banner_id)->update($subject);
-                                if (!$banners) {
-                                    throw new HttpException(500, '更新失败');
-                                }
-                            }else{
-                                if (!$this->cmsContentTemplateSet->create($subject)) {
-                                    throw new HttpException(500, '添加失败');
-                                }
-                            }
+
+                foreach ($subject_id_array as $val) {
+                    if ($val['link_type'] == 1) {
+                        $templates = $this->cmsSubject->where('id', intval($val['link_url']))->first();
+                        $update_template = [
+                            $templates->used_count -= 1
+                        ];
+                        $templates->update($update_template);
+                    }
+                }
+
+                $banners = $this->cmsContentTemplateSet->where('id', $banner_id)->update($subject);
+                if (!$banners) {
+                    throw new HttpException(500, '更新失败');
+                }
+            } else {
+                foreach ($request['rule'] as $v) {
+                    if ($v['link_type'] == 1) {
+                        $templates = $this->cmsSubject->where('id', intval($v['link_url']))->first();
+                        $templates->used_count += 1;
+                        $templates->save();
+                    }
+                }
+
+                if (!$this->cmsContentTemplateSet->create($subject)) {
+                    throw new HttpException(500, '添加失败');
+                }
+            }
             }else{
                 throw new HttpException(500, '参数格式有误');
-            }
+        }
+    }
 
+    //专题被使用计数
+    function update_subject($request)
+    {
+        $old_subject_id = $this->cmsContentTemplateSet->select('rule')->find($request['id']);
+        $subject_id_array = json_decode($old_subject_id['rule'],true);
+        if ($subject_id_array['link_type'] == 1 && $request['rule']['link_type'] == 1){//链接方式都为专题
+            if ($subject_id_array['link_url'] != $request['rule']['link_url']){
+                $templates = $this->cmsSubject->where('id',intval($subject_id_array['link_url']))->first();
+                $update_template = [
+                    $templates->used_count -=1
+                ];
+                $templates->update($update_template);
+
+                $templates = $this->cmsSubject->where('id',intval($request['rule']['link_url']))->first();
+                $templates->used_count +=1;
+                $templates->save();
+            }
+        }elseif ($subject_id_array['link_type'] == 1 && $request['rule']['link_type'] != 1){
+            $templates = $this->cmsSubject->where('id',intval($subject_id_array['link_url']))->first();
+            $update_template = [
+                $templates->used_count -=1
+            ];
+            $templates->update($update_template);
+        }elseif ($subject_id_array['link_type'] != 1 && $request['rule']['link_type'] == 1){
+            $templates = $this->cmsSubject->where('id',intval($request['rule']['link_url']))->first();
+            $templates->used_count +=1;
+            $templates->save();
+        }
     }
 
+
     /**
      * 专题广告配置
      */
@@ -147,6 +206,8 @@ class CmsContentTemplateSetRepository {
                     throw new HttpException(500, '添加失败');
                 }
             }else{
+                $this->update_subject($request);
+
                 $advertisement_ids = $this->cmsContentTemplateSet->where('id',$advertisement_id)->update($subject);
                 if (!$advertisement_ids) {
                     throw new HttpException(500, '更新失败');
@@ -173,6 +234,8 @@ class CmsContentTemplateSetRepository {
                 throw new HttpException(500, '添加失败');
             }
         }else {
+            $this->update_subject($request);
+
             $floor_ids = $this->cmsContentTemplateSet->where('id',$floor_id)->update($subject);
             if (!$floor_ids) {
                 throw new HttpException(500, '更新失败');
@@ -197,15 +260,22 @@ class CmsContentTemplateSetRepository {
                 'updated_at' => date('Y-m-d H:i:s')
             ];
         if (empty($category_id)){
+            if ($request['rule']['link_type'] == 1){
+               $templates = $this->cmsSubject->where('id',intval($request['rule']['link_url']))->first();
+                $templates->used_count +=1;
+                $templates->save();
+            }
+
             if (!$this->cmsContentTemplateSet->create($subject)) {
                 throw new HttpException(500, '添加失败');
             }
-        }else {
+        } else {
+            $this->update_subject($request);
+
             $category_ids = $this->cmsContentTemplateSet->where('id',$category_id)->update($subject);
             if (!$category_ids) {
                 throw new HttpException(500, '更新失败');
             }
-
         }
 
     }
@@ -217,12 +287,7 @@ class CmsContentTemplateSetRepository {
     public function release($request)
     {
         //同一个城市同一个模板(团购/菜市场) 只能有一种状态(草稿/发布),已发布的一旦被编辑把之前的直接删掉
-        $templateSet = $this->cmsContentTemplateSet->where('tpl_id',$request['tpl_id'])->select('id','status')->get();
-        $tem_array = $templateSet->toArray();
         $template = $this->cmsContentTemplate->select('id','city_name','city_id','title','apply_type','is_open','status')->where('id',$request['tpl_id'])->first();
-//        if ($template->is_open == 0){
-//            throw new HttpException(500, '请先将状态开启之后再发布');
-//        }
         $where = [
             'city_id'=>$template->city_id,
             'title'=>$template->title,
@@ -247,7 +312,6 @@ class CmsContentTemplateSetRepository {
             if (!$update_open) {
                 throw new HttpException(500, '修改失败,请重试');
             }
-
         }
 
         $update_tpl = $this->cmsContentTemplate->where('id', $request['tpl_id'])->update(['status' => 1]);
@@ -255,7 +319,6 @@ class CmsContentTemplateSetRepository {
             throw new HttpException(500, '修改失败,请重试');
         }
 
-
         $res = $this->cmsContentTemplateSet->where('tpl_id',$request['tpl_id'])->update(['status'=>1]);
 
         if (!$res){
@@ -354,7 +417,7 @@ class CmsContentTemplateSetRepository {
 
     public function getTemplate($cityId)
     {
-        return $this->cmsContentTemplate->select('id','title','apply_type')->where(['city_id'=>$cityId])->where('deleted_at', null)->orderBy('apply_type','asc')->get();
+        return $this->cmsContentTemplate->select('id','title','apply_type')->where(['city_id'=>$cityId])->where('deleted_at', null)->groupBy('title')->orderBy('apply_type','asc')->get();
 
     }
 

+ 9 - 1
app/Repositories/CmsSubjectRepository.php

@@ -50,6 +50,7 @@ class CmsSubjectRepository {
             'city_name' => $request['city_name'],
             'show_type' => $request['show_type'],
             'is_open' => $request['is_open'],
+            'used_count' => 0
         ];
         $date = date('Y-m-d H:i:s');
 
@@ -101,6 +102,7 @@ class CmsSubjectRepository {
         $subject->city_name = $request['city_name'];
         $subject->show_type = $request['show_type'];
         $subject->is_open = $request['is_open'];
+        $subject->used_count = 0;
 
         $date = date('Y-m-d H:i:s');
 
@@ -148,6 +150,9 @@ class CmsSubjectRepository {
     public function delete($request)
     {
         $subject = $this->cmsSubject->find($request['id']);
+        if ($subject->used_count != 0){
+            throw new HttpException(500, '该专题正在使用不能被删除');
+        }
 
         DB::beginTransaction();
         try{
@@ -172,9 +177,12 @@ class CmsSubjectRepository {
     public function editStatus($request)
     {
         $subject = $this->cmsSubject->find($request['id']);
+
         $subject->is_open = $request['is_open'];
         $subject->updated_at = date('Y-m-d H:i:s');
-
+        if ($subject->used_count != 0 && $request['is_open'] == 0){
+            throw new HttpException(500, '该专题正在使用不能被关闭');
+        }
         $res = $subject->save();
         if (!$res) {
             throw new HttpException(500, '修改状态失败');

+ 32 - 0
database/migrations/2019_05_24_071934_add_used_count_to_cms_subject_table.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddUsedCountToCmsSubjectTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('cms_subject', function (Blueprint $table) {
+            $table->tinyInteger('used_count')->comment('被使用计数');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('cms_subject', function (Blueprint $table) {
+            //
+        });
+    }
+}