ProductLogic.php 9.4 KB
<?php

namespace App\Http\Logic\Bside\Product;

use App\Helper\Arr;
use App\Http\Logic\Bside\BaseLogic;
use App\Http\Logic\Bside\User\UserLogic;
use App\Models\Product\CategoryRelated;
use App\Models\Product\KeywordRelated;
use App\Models\Product\Product;
use App\Models\RouteMap;
use App\Models\Template\BTemplate;
use Illuminate\Support\Facades\DB;

/**
 * Class ProductLogic
 * @package App\Http\Logic\Bside\APublicModel
 * @author zbj
 * @date 2023/4/14
 */
class ProductLogic extends  BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->param = $this->requestAll;
        $this->model = new Product();
    }

    public function getList(array $map = [], array $sort = ['id' => 'desc'], array $columns = ['*'], int $row = 20)
    {
        $data = parent::getList($map, $sort, $columns, $row);
        foreach ($data['list'] as &$v){
            $v = $this->formatData($v);
            $v['thumb']['image_link'] = getImageUrl($v['thumb']['url']);//图片统一
        }
        return $this->success($data);
    }

    public function getInfo($id)
    {
        $info = $this->model->read(['id'=>$id]);
        $info = $this->formatData($info);
        //统一图片链接
        if(!empty($info['gallery'])){
            foreach ($info['gallery'] as $k => $v){
                $v['image_link'] = getImageUrl($v['url']);
                $info['gallery'][$k] = $v;
            }
        }
        $info['icon_link'] = getImageUrl($info['icon']);
        return $this->success($info);
    }

    public function formatData($info){
        foreach ($info['category_id'] as $category_id) {
            $info['category_id_text'][] = (new CategoryLogic())->getCacheInfo($category_id)['title'] ?? '';
        }
        foreach ($info['keyword_id'] as $keyword_id){
            $info['keyword_id_text'][] =(new KeywordLogic())->getCacheInfo($keyword_id)['title']??'';
        }
        $info['category_id_text'] = Arr::arrToSet($info['category_id_text'], 'trim');
        $info['keyword_id_text'] = Arr::arrToSet($info['keyword_id_text'], 'trim');
        $info['status_text'] = Product::statusMap()[$info['status']] ?? '';
        $info['created_uid_text'] = (new UserLogic())->getCacheInfo($info['created_uid'])['name'] ?? '';
        $info['url'] = $this->getProjectDomain() . $info['route'] ;
        return $info;
    }

    public function save($param){
        //封面取第一个图片
        $param['thumb'] = $param['gallery'][0] ?? '';
//        DB::beginTransaction();
//        try {
            $data = $param;
            $data['created_uid'] = $this->user['id'];
            $res = parent::save($data);
            //关联分类
            CategoryRelated::saveRelated($res['id'], $data['category_id']);
            //关联关键词
            KeywordRelated::saveRelated($res['id'], $data['keyword_id']);
            //路由映射
            $route = RouteMap::setRoute($param['route'], RouteMap::SOURCE_PRODUCT, $res['id'], $this->user['project_id']);
//            DB::commit();
//        }catch (\Exception $e){
//            DB::rollBack();
//            errorLog('产品保存失败', $param, $e);
//            $this->fail('保存失败');
//        }
        //通知更新
        $this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>RouteMap::SOURCE_PRODUCT, 'route'=>$route]);
        return $this->success();
    }

    public function delete($ids, $map =[]){
        $ids= array_filter(Arr::splitFilterToArray($ids), 'intval');

        DB::beginTransaction();
        try {
            foreach ($ids as $k => $id) {

                $info = $this->getCacheInfo($id);
                if(!$info){
                    unset($ids[$k]);
                    continue;
                }
                if($info->status == Product::STATUS_RECYCLE){
                    //删除路由映射
                    RouteMap::delRoute(RouteMap::SOURCE_PRODUCT, $id, $this->user['project_id']);
                    //删除分类关联
                    CategoryRelated::where('product_id', $id)->delete();
                    //删除关键词关联
                    KeywordRelated::where('product_id', $id)->delete();
                    //删除当前产品模版
                    $this->delProductModule($id);
                }else{
                    //回收站
                    parent::save(['id' => $id, 'status' => Product::STATUS_RECYCLE]);
                    unset($ids[$k]);
                }
            }
            parent::delete($ids);

            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('删除失败');
        }

        return $this->success();
    }

    /**
     * @remark :统计数据
     * @name   :getStatusNumber
     * @author :lyh
     * @method :post
     * @time   :2023/8/9 10:17
     */
    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   :setCopyProduct
     * @author :lyh
     * @method :post
     * @time   :2023/7/29 15:03
     */
    public function setCopyProduct(){
        $info = $this->model->read(['id'=>$this->param['id']]);
        $param = $this->setProductParams($info);
        $save_id = $this->model->insertGetId($param);
        //同步关联分类
        CategoryRelated::saveRelated($save_id, explode(",", $param['category_id']));
        //同步关联关键词
        KeywordRelated::saveRelated($save_id,explode(",", $param['keyword_id']));
        //同步可视化装修数据
        $this->copyTemplate($this->param['id'],$info['project_id'],$save_id);
        return $this->success();
    }

    /**
     * @remark :同步模版数据
     * @name   :copyTemplate
     * @author :lyh
     * @method :post
     * @time   :2023/7/29 15:53
     */
    public function copyTemplate($id,$project_id,$save_id){
        $BTemplateModel = new BTemplate();
        $list = $BTemplateModel->list(['source'=>2,'source_id'=>$id,'project_id'=>$project_id]);
        if(!empty($list)){
            $data = [];
            foreach ($list as $k => $v){
                $data[] = $this->setTemplateParams($v,$project_id,$save_id);
            }
            $rs = $BTemplateModel->insert($data);
            if($rs === false){
                $this->fail('error');
            }
        }
        return $this->success();
    }

    /**
     * @remark :组装模版数据
     * @name   :setTemplateParams
     * @author :lyh
     * @method :post
     * @time   :2023/7/29 15:54
     */
    public function setTemplateParams($v,$project_id,$save_id){
        $param = [
            'html'=>$v['html'],
            'project_id'=>$project_id,
            'source'=>$v['source'],
            'source_id'=>$save_id,
            'template_id'=>$v['template_id'],
            'section_list_id'=>$v['section_list_id'],
            'head_html'=>$v['head_html'],
            'main_html'=>$v['main_html'],
            'footer_html'=>$v['footer_html'],
            'head_css'=>$v['head_css'],
            'main_css'=>$v['main_css'],
            'footer_css'=>$v['footer_css'],
            'created_at'=>$v['created_at'],
            'updated_at'=>$v['updated_at']
        ];
        return $this->success($param);
    }

    /**
     * @remark :组装数据
     * @name   :setParams
     * @author :lyh
     * @method :post
     * @time   :2023/7/29 15:45
     */
    public function setProductParams($info){
        $param = [
            'project_id'=>$info['project_id'],
            'title'=>$info['title']."-copy",
            'thumb'=>Arr::a2s($info['thumb']),
            'gallery'=>Arr::a2s($info['gallery']),
            'attrs'=>Arr::a2s($info['attrs']),
            'attr_id'=>Arr::arrToSet($info['attr_id']),
            'category_id'=>Arr::arrToSet($info['category_id']),
            'keyword_id'=>Arr::arrToSet($info['keyword_id']),
            'intro'=>$info['intro'],
            'content'=>$info['content'],
            'describe'=>Arr::a2s($info['describe']),
            'describe_id'=>Arr::arrToSet($info['describe_id']),
            'seo_mate'=>Arr::a2s($info['seo_mate']),
            'related_product_id'=>Arr::arrToSet($info['related_product_id']),
            'sort'=>$info['sort'],
            'status'=>$info['status'],
            'product_type'=>$info['product_type'],
            'created_uid'=>$this->user['id'],
            'icon'=>Arr::a2s($info['icon']),
            'created_at'=>date('Y-m-d H:i:s'),
            'updated_at'=>date('Y-m-d H:i:s'),
        ];
        return $param;
    }

    /**
     * @remark :删除当前产品所有模块
     * @name   :delProductModule
     * @author :lyh
     * @method :post
     * @time   :2023/7/31 10:31
     */
    public function delProductModule($id){
        $param = [
            'source' => BTemplate::SOURCE_PRODUCT,
            'source_id' => $id,
            'project_id'=>$this->user['project_id'],
        ];
        $bTemplateModel = new BTemplate();
        $rs = $bTemplateModel->del($param);
        if($rs === false){
            $this->fail('error');
        }
        return $rs;
    }
}