NewsCategoryLogic.php 7.5 KB
<?php

namespace App\Http\Logic\Bside\News;

use App\Enums\Common\Code;
use App\Helper\Common;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\News\News as NewsModel;
use App\Models\News\NewsCategory as NewsCategoryModel;
use App\Models\RouteMap;
use Illuminate\Support\Facades\DB;

class NewsCategoryLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new NewsCategoryModel();
        $this->param = $this->requestAll;
    }

    /**
     * @param $v
     * @name :获取分类名称
     * @return void
     * @author :liyuhang
     * @method
     */
    public function get_category_name($v){
        //获取用户已读还是未读
        $category_info = $this->model->list(['id'=>['in',explode(',',trim($v['category_id'],','))]],'id',['name']);
        $str = '';
        foreach ($category_info as $v1){
            $str .= $v1['name'].',';
        }
        $v['category_name'] = trim($str,',');
        return $this->success($v);
    }

    /**
     * @name :详情
     * @return array
     * @throws \App\Exceptions\BsideGlobalException
     * @author :liyuhang
     * @method
     */
    public function info_news_category(){
        $info = $this->info($this->param);
        return $this->success($info);
    }
    /**
     * @name :添加时验证上级分类是否有商品,有则替换带当前分类下
     * @return void
     * @author :liyuhang
     * @method
     */
    public function add_news_category(){
        //验证名称是否存在
        $this->verifyParamName();
        //参数处理
        $this->param = $this->addParamProcessing($this->param);
        DB::beginTransaction();
        try {
            $cate_id = $this->model->insertGetId($this->param);
            //当父级分类拥有产品时,处理产品
            $this->addProcessingSon($cate_id);
            RouteMap::setRoute($this->param['alias'] ?: $this->param['name'], RouteMap::SOURCE_NEWS_CATE, $cate_id, $this->user['project_id']);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('error');
        }
        return $this->success();
    }

    /**
     * @name :编辑分类
     * @return void
     * @author :liyuhang
     * @method
     */
    public function edit_news_category(){
        //验证名称是否存在
        $this->verifyParamName();
        $info = $this->model->read(['id'=>$this->param['id']]);
        if($info['id'] == $this->param['pid']){
            $this->fail('不能成为自己的上级');
        }
        if($info['pid'] != $this->param['pid']){
            $info = $this->model->read(['pid'=>$this->param['id']]);
            if($info !== false){
                $this->fail('当前分类拥有子分类不允许修改父级');
            }
            //查询当前分类下是否有商品
            $newsModel = new NewsModel();
            $info = $newsModel->read(['id'=>['like','%,'.$this->param['id'].',%']]);
            if($info !== false){
                $this->fail('当前分类下已存在新闻,不允许修改上级');
            }
        }
        $this->param['operator_id'] = $this->user['id'];
        $this->edit($this->param,['id'=>$this->param['id']]);
        RouteMap::setRoute($this->param['alias'] ?: $this->param['name'], RouteMap::SOURCE_NEWS_CATE, $this->param['id'], $this->user['project_id']);
        return $this->success();
    }

    /**
     * @name :修改状态
     * @return array
     * @throws \App\Exceptions\BsideGlobalException
     * @author :liyuhang
     * @method
     */
    public function status_news_category(){
        $this->param['operator_id'] = $this->user['id'];
        $this->edit($this->param,['id'=>$this->param['id']]);
        return $this->success();
    }
    /**
     * @name :删除新闻分类
     * @return array
     * @throws \App\Exceptions\BsideGlobalException
     * @author :liyuhang
     * @method
     */
    public function del_news_category(){
        $ids = $this->param['id'];
        foreach ($this->param['id'] as $v){
            //查询是否有子分类
            $rs = $this->model->read(['pid'=>$v],['id']);
            if($rs !== false){
                $this->fail('当前分类拥有子分类不允许删除');
            }
            //查看当前分内下是否有商品
            $newsModel = new NewsModel();
            $rs = $newsModel->read(['category_id'=>$v],['id']);
            if($rs !== false){
                $this->fail('当前分类拥有商品');
            }
            RouteMap::delRoute(RouteMap::SOURCE_NEWS_CATE, $v, $this->user['project_id']);
        }
        $this->param['id'] = ['in',$this->param['id']];
        $this->del($this->param,$ids);
        return $this->success();
    }

    /**
     * @name   :(添加分类时获取1级分类)categoryTopList
     * @author :lyh
     * @method :post
     * @time   :2023/6/13 9:09
     */
    public function categoryTopList(){
        $map = [
            'project_id'=>$this->user['project_id'],
            'pid'=>0,
            'status'=>0,
        ];
        $list = $this->model->list($map);
        return $this->success($list);
    }

    /**
     * @name   :(验证名称是否存在)verifyParamName
     * @author :lyh
     * @method :post
     * @time   :2023/6/13 11:41
     */
    public function verifyParamName(){
        if(isset($this->param['id'])){
            $condition = [
                'id'=>['!=',$this->param['id']],
                'name'=>$this->param['name'],
            ];
        }else{
            $condition = [
                'name'=>$this->param['name']
            ];
        }
        $info = $this->model->read($condition);
        if($info !== false){
            $this->fail('当前分类名称已存在');
        }
        return $this->success();
    }

    /**
     * @name   :(参数处理)paramProcessing
     * @author :lyh
     * @method :post
     * @time   :2023/6/13 11:30
     */
    public function addParamProcessing($param){
        $param['project_id'] = $this->user['project_id'];
        $param['operator_id'] = $this->user['id'];
        $param['create_id'] = $this->user['id'];
        $param['created_at'] = date('Y-m-d H:i:s');
        $param['updated_at'] = date('Y-m-d H:i:s');
        return $this->success($param);
    }

    /**
     * @name   :(添加分类时处理子集分类)addProcessingSon
     * @author :lyh
     * @method :post
     * @time   :2023/6/13 11:34
     */
    public function addProcessingSon($cate_id){
        if(isset($this->param['pid']) && !empty($this->param['pid'])) {
            $this->param['pid'] = 0;
        }
        //判断为子分类时
        if($this->param['pid'] != 0) {
            //查看当前上级分类下是否有其他分类
            $cate_info = $this->model->read(['pid' => $this->param['pid'], 'id' => ['!=', $cate_id]]);
            if ($cate_info === false) {
                //查看当前上一级分类下是否有新闻
                $newsModel = new NewsModel();
                $news_count = $newsModel->where('category_id','like', '%,' . $this->param['pid'] . ',%')->count();
                if ($news_count > 0) {
                    $replacement = ','. $this->param['pid'] .','. $cate_id . ',';
                    $old = ',' . $this->param['pid'] . ',';
                    //更新所有商品到当前分类
                    DB::table('gl_news')->where('category_id', 'like', '%' . $old . '%')
                        ->update(['category_id' => DB::raw("REPLACE(category_id, '$old', '$replacement')")]);
                }
            }
        }
        return $this->success();
    }
}