NewsController.php 8.8 KB
<?php

namespace App\Http\Controllers\Bside\News;

use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\News\NewsLogic;
use App\Http\Requests\Bside\News\NewsRequest;
use App\Models\News\News as NewsModel;
use App\Models\News\NewsCategory;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BTemplate;
use App\Models\Template\Setting;
use App\Models\User\User;


/**
 * @name:新闻管理
 */
class NewsController extends BaseController
{
    public $updateModelView = 'news';
    /**
     * @name :获取新闻列表
     * @author :liyuhang
     * @method
     */
    public function lists(NewsModel $news){
        $filed = ['id','category_id','operator_id','status','created_at','image','updated_at','name','sort','url', 'release_at','is_upgrade','six_read'];
        $this->order = 'sort';
        $query = $news->orderBy($this->order ,'desc')->orderBy('id','desc');
        $query = $this->searchParam($query);
        $lists = $query->select($filed)->paginate($this->row, ['*'], 'page', $this->page);
        if(!empty($lists)){
            $lists = $lists->toArray();
//            //获取当前项目的所有分类
            $data = $this->getCategoryList();
            //获取当前用户选择的模版
            $templateSettingModel = new Setting();
            $info = $templateSettingModel->read(['project_id'=>$this->user['project_id']]);
            $user = new User();
            foreach ($lists['list'] as $k => $v){
                $v['category_name'] = $this->categoryName($v['category_id'],$data);
                $v['url'] = $this->user['domain'].getRouteMap(RouteMap::SOURCE_NEWS,$v['id']);
                $v['image_link'] = getImageUrl($v['image']);
                $v['operator_name'] = $user->getName($v['operator_id']);
                $v['is_renovation']  =  $this->getProductIsRenovation($info,$v['id']);
                $lists['list'][$k] = $v;
            }
        }
        $this->response('success',Code::SUCCESS,$lists);
    }

    /**
     * @remark :查看产品是否已装修
     * @name   :getProductIsRenovation
     * @author :lyh
     * @method :post
     * @time   :2023/9/13 14:02
     */
    public function getProductIsRenovation($info,$id){
        if($info !== false){
            $webTemplateModel = new BTemplate();
            $param = [
                'source'=>4,
                'project_id'=>$this->user['project_id'],
                'source_id'=>$id,
                'template_id'=>$info['template_id']
            ];
            $templateInfo = $webTemplateModel->read($param);
            if($templateInfo !== false){
                return 1;
            }
        }
        return 0;
    }

    /**
     * @remark :处理列表返回参数
     * @name   :handleReturnParam
     * @author :lyh
     * @method :post
     * @time   :2023/9/14 10:01
     */
    public function searchParam(&$query){
        $query = $query->where('project_id',$this->user['project_id']);
        if (isset($this->map['category_id']) && !empty($this->map['category_id'])) {
            $str = [];
            $str[] = $this->map['category_id'];
            $this->getAllSub($this->map['category_id'],$str);
            @file_put_contents(storage_path('logs/lyh_error.log'), var_export($str, true) . PHP_EOL, FILE_APPEND);
            $query->where(function ($subQuery) use ($str) {
                foreach ($str as $v) {
                    $subQuery->orWhereRaw("FIND_IN_SET(?, category_id) > 0", [$v]);
                }
            });
        }
        if(isset($this->map['status'])){
            $query = $query->where('status',$this->map['status']);
        }
        if(isset($this->map['name']) && !empty($this->map['name'])){
            $query = $query->where('name',$this->map['name'][0],'%'.$this->map['name'][1].'%');
        }
        if(!empty($this->map['start_at']) && !empty($this->map['end_at'])){
            $query->whereBetween('created_at', [$this->map['start_at'],$this->map['end_at']]);
        }
        return $query;
    }

    /**
     * @remark :获取当前id下所有子集
     * @name   :getAllSub
     * @author :lyh
     * @method :post
     * @time   :2023/10/18 15:10
     */
    public function getAllSub($id,&$str = []){
        $cateModel = new NewsCategory();
        $list = $cateModel->list(['pid'=>$id,'status'=>0],['id','pid']);
        if(!empty($list)){
            foreach ($list as $v){
                $str[] = $v['id'];
                $this->getAllSub($v['id'],$str);
            }
        }
        return $str;
    }

    /**
     * @remark :获取所有分类
     * @name   :getCategoryList
     * @author :lyh
     * @method :post
     * @time   :2023/9/14 13:56
     */
    public function getCategoryList(){
        $categoryModel = new NewsCategory();
        $data = [];
        $cateList = $categoryModel->list(['project_id'=>$this->user['project_id']],['id','name']);
        if(!empty($cateList)){
            foreach ($cateList as $value){
                $data[$value['id']] = $value['name'];
            }
        }
        return $data;
    }

    /**
     * @remark :获取分类名称
     * @name   :categoryName
     * @author :lyh
     * @method :post
     * @time   :2023/9/14 13:58
     */
    public function categoryName($category_id,$data){
        $category_name = '';
        if(!empty($category_id) && !empty($data)){
            $arr = explode(',',trim($category_id,','));
            foreach ($arr as $v){
                if(isset($data[$v])){
                    $category_name .= $data[$v].',';
                }
            }
            $category_name = trim($category_name,',');
        }
        return $category_name;
    }

    /**
     * @remark :根据状态数量
     * @name   :getStatusNumber
     * @author :lyh
     * @method :post
     * @time   :2023/6/19 9:39
     */
    public function getStatusNumber(NewsLogic $newsLogic){
        $data = $newsLogic->getStatusNumber();
        $this->response('success',Code::SUCCESS,$data);
    }


    /**
     * @name :添加新闻时获取分类列表
     * @author :liyuhang
     * @method
     */
    public function get_category_list(NewsLogic $newsLogic){
        $list = $newsLogic->news_get_category_list();
        $this->response('success',Code::SUCCESS,$list);
    }
    /**
     * @name :获取详情
     * @author :liyuhang
     * @method
     */
    public function info(NewsLogic $newsLogic){
        $this->request->validate([
            'id'=>['required'],
        ],[
            'id.required' => 'ID不能为空',
        ]);
        $info = $newsLogic->newsInfo();
        $this->response('success',Code::SUCCESS,$info);
    }

    /**
     * @remark :保存数据
     * @name   :save
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 11:22
     */
    public function save(NewsRequest $newsRequest,NewsLogic $newsLogic){
        $newsRequest->validated();
        $newsLogic->newsSave();
        $this->response('success');
    }


    /**
     * @name :编辑新闻seo
     * @author :liyuhang
     * @method
     */
    public function edit_seo(NewsLogic $newsLogic){
        $this->request->validate([
            'id'=>['required'],
            'seo_title'=>['required'],
            'seo_description'=>['required'],
            'seo_keywords'=>['required'],
        ],[
            'id.required' => 'ID不能为空',
            'seo_title.required' => 'seo_title不能为空',
            'seo_description.required' => 'seo_description不能为空',
            'seo_keywords.required' => 'seo_description不能为空',
        ]);
        $newsLogic->edit_seo();
        $this->response('success');
    }

    /**
     * @name :编辑状态
     * @author :liyuhang
     * @method
     */
    public function status(NewsLogic $newsLogic){
        $this->request->validate([
            'id'=>['required','array'],
        ],[
            'id.required' => 'ID不能为空',
            'id.array' => 'ID为数组',
        ]);
        $newsLogic->news_status();
        //TODO::写入日志
        $this->response('success');
    }
    /**
     * @name :删除新闻(逻辑删除)
     * @author :liyuhang
     * @method
     */
    public function del(NewsLogic $newsLogic){
        $this->request->validate([
            'id'=>['required','array'],
        ],[
            'id.required' => 'ID不能为空',
            'id.array' => 'ID为数组',
        ]);
        $newsLogic->newsDel();
        $this->response('success');
    }

    /**
     * @remark :排序
     * @name   :sort
     * @author :lyh
     * @method :post
     * @time   :2023/8/22 10:24
     */
    public function sort(NewsLogic $newsLogic){
        $this->request->validate([
            'id'=>'required',
            'sort'=>'required'
        ],[
            'id.required' => '产品ID不能为空',
            'sort.required'=>'排序字段不能为空'
        ]);
        $newsLogic->setSort();
        $this->response('success');
    }
}