DetailLogic.php 2.5 KB
<?php
/**
 * @remark :
 * @name   :DetailLogic.php
 * @author :lyh
 * @method :post
 * @time   :2024/11/12 15:14
 */

namespace App\Http\Logic\Bside\Product;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\Product\Column;
use App\Models\Product\Detail;

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

    /**
     * @remark :保存栏目
     * @name   :saveColumn
     * @author :lyh
     * @method :post
     * @time   :2024/11/12 15:15
     */
    public function saveColumn(){
        $columnModel = new Column();
        if(isset($this->param['id']) && !empty($this->param['id'])){
            $id = $this->param['id'];
            $rs = $columnModel->edit(['column_name'=>$this->param['column_name']],['id'=>$this->param['id']]);
            if($rs === false){
                $this->fail('保存失败,请联系管理员');
            }
        }else{
            $id = $columnModel->addReturnId($this->param);
        }
        return $this->success(['id'=>$id]);
    }

    /**
     * @remark :保存数据
     * @name   :saveDetail
     * @author :lyh
     * @method :post
     * @time   :2024/11/13 9:30
     */
    public function saveDetail($product_id,$data){
        if(!empty($data)){
            try {
                foreach ($data as $val){
                    foreach ($val as $v){
                        $save_data = [
                            'sort'=>$v['sort'],
                            'column_id'=>$v['column_id'],
                            'product_id'=>$product_id,
                            'text_type'=>$v['text_type'],
                            'title'=>$v['title'] ?? '',
                            'content'=>json_encode($v['content'] ?? [],JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
                            'css'=>json_encode($v['css'] ?? [],JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
                        ];
                        if(isset($v['id']) && !empty($v['id'])){
                            $this->edit($save_data,['id'=>$v['id']]);
                        }else{
                            $this->model->add($save_data);
                        }
                    }
                }
            }catch (\Exception $e){
                $this->fail('保存失败,请联系管理员.错误:'.$e->getMessage());
            }
        }
        return $this->success(['product_id'=>$product_id]);
    }
}