CustomModuleContentLogic.php 10.6 KB
<?php
/**
 * @remark :
 * @name   :CustomModuleContentLogic.php
 * @author :lyh
 * @method :post
 * @time   :2023/12/4 16:06
 */

namespace App\Http\Logic\Bside\CustomModule;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\CustomModule\CustomModuleCategory;
use App\Models\CustomModule\CustomModuleContent;
use App\Models\CustomModule\CustomModuleExtend;
use App\Models\CustomModule\CustomModuleExtentContent;
use App\Models\RouteMap\RouteMap;
use Illuminate\Support\Facades\DB;
use mysql_xdevapi\Exception;

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

    /**
     * @remark :获取当前数据详情
     * @name   :getCustomModuleInfo
     * @author :lyh
     * @method :post
     * @time   :2023/12/4 16:10
     */
    public function getContentInfo(){
        $info = $this->model->read($this->param);
        if($info === false){
            $this->fail('当前数据不存在或已被删除');
        }
        $info['image'] = getImageUrl($info['image'],$this->user['storage_type']);
        $info['extend'] = $this->getExtendInfo($info['module_id'],$info['id']);
        return $this->success($info);
    }

    /**
     * @remark :获取扩展字段详情
     * @name   :getExtendInfo
     * @author :lyh
     * @method :post
     * @time   :2023/11/14 9:45
     */
    public function getExtendInfo($module_id,$content_id){
        $extendModel = new CustomModuleExtend();
        $list = $extendModel->list(['module_id'=>$module_id],'id',['id','type','key','title']);
        if(empty($list)){
            return [];
        }
        $extendContentModel = new CustomModuleExtentContent();
        foreach ($list as $k=>$v){
            $info = $extendContentModel->read(['key'=>$v['key'],'content_id'=>$content_id]);
            if($info === false){
                if($v['type'] == 3 || $v['type'] == 4){
                    $v['values'] = [];
                }else{
                    $v['values'] = '';
                }
            }else{
                $v = $this->setTypValues($v,$info);
            }
            $list[$k] = $v;
        }
        return $this->success($list);
    }

    /**
     * @remark :扩展字段根据type返回类型
     * @name   :setTypValues
     * @author :lyh
     * @method :post
     * @time   :2023/12/6 14:43
     */
    public function setTypValues($v,$info){
        if($v['type'] == 3){
            $arr = json_decode($info['values']);
            foreach ($arr as $k1=>$v1){
                $v1 = (array)$v1;
                $v1['url'] = getImageUrl($v1['url'],$this->user['storage_type']);
                $arr[$k1] = $v1;
            }
            $v['values'] = $arr;
        }elseif($v['type'] == 4){
            $arr1 = json_decode($info['values']);
            foreach ($arr1 as $k1=>$v1){
                $v1 = getFileUrl($v1,$this->user['storage_type']);
                $arr1[$k1] = $v1;
            }
            $v['values'] = $arr1;
        }else{
            $v['values'] = $info['values'];
        }
        return $this->success($v);
    }

    /**
     * @remark :保存数据
     * @name   :ModuleSave
     * @author :lyh
     * @method :post
     * @time   :2023/12/4 15:47
     */
    public function contentSave(){
        $extend = $this->handleExtent();
        $this->param = $this->handleParam($this->param);
        if(isset($this->param['id']) && !empty($this->param['id'])){
            $id = $this->contentEdit();
        }else{
            $id = $this->contentAdd();
        }
        //保存扩展字段
        $this->saveExtendInfo($id,$extend);
        return $this->success();
    }

    /**
     * @remark :处理扩展字段
     * @name   :handleExtent
     * @author :lyh
     * @method :post
     * @time   :2023/12/6 15:06
     */
    public function handleExtent(){
        //扩展字段
        $extend = [];
        if(!empty($this->param['extend'])){
            $extend = $this->param['extend'];
            unset($this->param['extend']);
        }
        return $extend;
    }

    /**
     * @remark :添加数据
     * @name   :contentAdd
     * @author :lyh
     * @method :post
     * @time   :2023/12/7 15:04
     */
    public function contentAdd(){
        try {
            $this->param['sort'] = $this->setNewsSort();
            $id = $this->model->addReturnId($this->param);
            $route = RouteMap::setRoute($this->param['route'], RouteMap::SOURCE_MODULE,
                $id, $this->user['project_id']);
            $this->addUpdateNotify(RouteMap::SOURCE_MODULE,$route);
            $this->curlDelRoute(['new_route'=>$route]);
            $this->edit(['route' => $route], ['id' => $id]);
        }catch (\Exception $e){
            $this->fail('系统错误,请联系管理员');
        }
        return $id;
    }

    /**
     * @remark :设置最新产品的sort排序
     * @name   :setNewsSort
     * @author :lyh
     * @method :post
     * @time   :2023/12/25 9:27
     */
    public function setNewsSort(){
        $info = $this->model->orderBy('sort','desc')->first();
        if(empty($info)){
            return 1;
        }
        $sort = $info['sort']+1;
        return $sort;
    }

    /**
     * @remark :编辑数据
     * @name   :contentEdit
     * @author :lyh
     * @method :post
     * @time   :2023/12/7 15:04
     */
    public function contentEdit(){
        $route = RouteMap::setRoute($this->param['route'], RouteMap::SOURCE_MODULE,
            $this->param['id'], $this->user['project_id']);
        $this->editRoute($this->param['id'],$route);
        $rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
        if($rs === false){
            $this->fail('系统错误,请连续管理员');
        }
        return $this->param['id'];
    }

    /**
     * @name   :(参数处理)paramProcessing
     * @author :lyh
     * @method :post
     * @time   :2023/6/13 11:30
     */
    public function handleParam($param)
    {
        $param['operator_id'] = $this->user['id'];
        if(!isset($param['id']) || empty($param['id'])){
            $param['project_id'] = $this->user['project_id'];
        }
        if(isset($param['category_id']) && !empty($param['category_id'])){
            $param['category_id'] = $this->getCategory($param['category_id']);
        }
        if(isset($param['image']) && !empty($param['image'])){
            $param['image'] = str_replace_url($param['image']);
        }
        return $this->success($param);
    }

    /**
     * @remark :查看是否编辑路由
     * @name   :editCategoryRoute
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 11:05
     */
    public function editRoute($id, $route)
    {
        //生成一条删除路由记录
        $info = $this->model->read(['id' => $id], ['id', 'route']);
        if ($info['route'] != $route) {
            $this->addUpdateNotify(RouteMap::SOURCE_MODULE,$route);
            $this->curlDelRoute(['route'=>$info['route'],'new_route'=>$route]);
        }
        return true;
    }

    /**
     * @remark :处理分类
     * @name   :getLastCategory
     * @author :lyh
     * @method :post
     * @time   :2023/10/20 9:02
     */
    public function getCategory($category){
        $str = '';
        foreach ($category as $v){
            $str .= $v.',';
        }
        return !empty($str) ? ','.$str : '';
    }

    /**
     * @remark :保存扩展字段
     * @name   :saveExtend
     * @author :lyh
     * @method :post
     * @time   :2023/11/9 15:02
     */
    public function saveExtendInfo($content_id,$extend){
        //先删除以前的数据
        $extendInfoModel = new CustomModuleExtentContent();
        $extendInfoModel->del(['content_id'=>$content_id]);
        if(empty($extend)) {
            return $this->success();
        }
        foreach ($extend as $v){
            if(empty($v['values'])){
                continue;
            }
            $v = $this->saveHandleExtend($v,$content_id);
            $extendInfoModel->add($v);
        }
        return $this->success();
    }

    /**
     * @remark :保存扩展字段时处理数据
     * @name   :saveHandleExtend
     * @author :lyh
     * @method :post
     * @time   :2023/12/6 15:11
     */
    public function saveHandleExtend(&$v,$content_id){
        unset($v['title']);
        if($v['type'] == 3){
            foreach ($v['values'] as $k1=>$v1){
                $v1['url'] = str_replace_url($v1['url']);
                $v['values'][$k1] = $v1;
            }
            $v['values'] = json_encode($v['values']);
        }elseif ($v['type'] == 4){
            foreach ($v['values'] as $k1=>$v1){
                $v1 = str_replace_url($v1);
                $v['values'][$k1] = $v1;
            }
            $v['values'] = json_encode($v['values']);
        }
        $v['project_id'] = $this->user['project_id'];
        $v['content_id'] = $content_id;
        $v['module_id'] = $content_id;
        return $this->success($v);
    }

    /**
     * @remark :删除数据
     * @name   :ModuleDel
     * @author :lyh
     * @method :post
     * @time   :2023/12/4 15:47
     */
    public function contentDel(){
        DB::beginTransaction();
        try {
            foreach ($this->param['id'] as $id) {
                //删除当前扩展字段
                $this->delRoute($id);
                $this->delContentExtend($id);
                $this->model->del(['id' => $id]);
            }
            DB::commit();
        } catch (Exception $e) {
            DB::rollBack();
            $this->fail('系统错误,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :删除路由
     * @name   :delRoute
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 10:50
     */
    public function delRoute($id)
    {
        RouteMap::delRoute(RouteMap::SOURCE_MODULE, $id, $this->user['project_id']);
        //通知
        $info = $this->model->read(['id' => $id], ['id', 'route']);
        $this->curlDelRoute(['route'=>$info['route']]);
        return $this->success();
    }

    /**
     * @remark :排序
     * @name   :contentSort
     * @author :lyh
     * @method :post
     * @time   :2023/12/15 17:47
     */
    public function contentSort(){
        $this->model->edit(['sort' => $this->param['sort']], ['id'=>$this->param['id']]);
        return $this->success();
    }

    /**
     * @remark :
     * @name   :delContentExtend
     * @author :lyh
     * @method :post
     * @time   :2023/12/18 11:43
     */
    public function delContentExtend($id){
        $contentExtendModel = new CustomModuleExtentContent();
        $contentExtendModel->del(['content_id'=>$id]);
        return $this->success();
    }
}