NewsLogic.php 6.8 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;
use App\Models\News\NewsCategory as NewsCategoryModel;
use App\Models\RouteMap\RouteMap;
use Illuminate\Support\Facades\DB;
use mysql_xdevapi\Exception;

class NewsLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();

        $this->model = new News();
        $this->param = $this->requestAll;
    }

    /**
     * @name :获取分类列表
     * @return array
     * @throws \App\Exceptions\BsideGlobalException
     * @author :liyuhang
     * @method
     */
    public function news_get_category_list(){
        $this->map['status'] = 0;
        $this->map['project_id'] = $this->user['project_id'];
        $newsCategoryModel = new NewsCategoryModel();
        $cate_list = $newsCategoryModel->list($this->map,'sort');
        if($cate_list === false){
            $this->fail('error',Code::USER_ERROR);
        }
        $list = [];
        foreach ($cate_list as $v){
            $v = (array)$v;
            if ($v['pid'] == 0) {
                $v['sub'] = _get_child($v['id'], $cate_list);
                $list[]   = $v;
            }
        }
        return $this->success($list);
    }

    /**
     * @name :添加博客
     * @return void
     * @author :liyuhang
     * @method
     */
    public function news_add(){
        //拼接参数
        DB::beginTransaction();
        try {
            $this->param = $this->paramProcessing($this->param);
            $rs = $this->model->insertGetId($this->param);
            $route = RouteMap::setRoute($this->param['url'] ?: $this->param['name'], RouteMap::SOURCE_NEWS, $rs, $this->user['project_id']);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('添加失败');
        }
        //通知更新
        $this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>RouteMap::SOURCE_NEWS, 'route'=>$route]);
        return $this->success();
    }

    /**
     * @name :编辑
     * @return void
     * @author :liyuhang
     * @method
     */
    public function news_edit(){
        //拼接参数
        $this->param = $this->paramProcessing($this->param);
        DB::beginTransaction();
        try {
            //设置路由
            $route = RouteMap::setRoute($this->param['url'] ?: $this->param['name'], RouteMap::SOURCE_NEWS, $this->param['id'], $this->user['project_id']);
            $this->edit($this->param,['id'=>$this->param['id']]);
            DB::commit();
        }catch (\exception $e){
            DB::rollBack();
            $this->fail('参数错误或其他服务器原因,编辑失败');
        }
        //通知更新
        $this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>RouteMap::SOURCE_NEWS, 'route'=>$route]);
        return $this->success();
    }

    /**
     * @name :编辑seo
     * @return void
     * @author :liyuhang
     * @method
     */
    public function edit_seo(){
        $this->param['operator_id'] = $this->user['id'];
        $rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
        if($rs ===  false){
            $this->fail('error');
        }
        return $this->success();
    }

    /**
     * @name :修改状态
     * @return void
     * @throws \App\Exceptions\BsideGlobalException
     * @author :liyuhang
     * @method
     */
    public function news_status(){
        $this->param['operator_id'] = $this->user['id'];
        $rs = $this->model->edit($this->param,['id'=>['in',$this->param['id']]]);
        if($rs ===  false){
            $this->fail('error');
        }
        return $this->success();
    }

    /**
     * @name :获取详情
     * @return array
     * @throws \App\Exceptions\BsideGlobalException
     * @author :liyuhang
     * @method
     */
    public function news_info(){
        //读取缓存
        $info = Common::get_user_cache($this->model->getTable(),$this->param['id']);
        if(empty($info)){
            $info = $this->model->read($this->param);
            $newsCategoryLogic = new NewsCategoryLogic();
            $info = $newsCategoryLogic->get_category_name($info);
            //写入缓存
            Common::set_user_cache($info,$this->model->getTable(),$this->param['id']);
        }
        return $this->success($info);
    }

    /**
     * @name :逻辑删除
     * @return void
     * @author :liyuhang
     * @method
     */
    public function news_del(){
        $ids = $this->param['id'];
        DB::beginTransaction();
        try {
            $this->param['id'] = ['in',$this->param['id']];
            $this->del($this->param,$ids);
            foreach ($ids as $id){
                RouteMap::delRoute(RouteMap::SOURCE_NEWS, $id, $this->user['project_id']);
            }
            DB::commit();
        }catch (Exception $e){
            DB::rollBack();
            $this->fail('当前数据不存在');
        }
        return $this->success();
    }

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

    /**
     * @remark :根据状态获取数量
     * @name   :getStatusNumber
     * @author :lyh
     * @method :post
     * @time   :2023/6/19 9:42
     */
    public function getStatusNumber(){
        //三种状态 0:草稿 1:发布 2:回收站
        $data = ['dra'=>0,'pub'=>1,'del'=>2,'tal'=>3];
        foreach ($data as $k => $v){
            if($v == 3){
                $data[$k] = $this->model->where(['project_id'=>$this->user['project_id']])->count();
            }else{
                $data[$k] = $this->model->where(['status'=>$v,'project_id'=>$this->user['project_id']])->count();
            }
        }
        return $this->success($data);
    }

    /**
     * @remark :排序
     * @name   :setSort
     * @author :lyh
     * @method :post
     * @time   :2023/8/19 11:16
     */
    public function setSort(){
        $rs = $this->model->edit(['sort'=>$this->param['sort']],['id'=>$this->param['id']]);
        if($rs === false){
            $this->fail('error');
        }
        return $this->success();
    }
}