| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 | <?phpnamespace Database\Seeders;use App\Http\Admin\AssembleController;use App\Http\Admin\ColumnController;use App\Http\Admin\Requests\AssembleForm;use App\Http\Admin\Requests\ColumnForm;use Txj\Elastic\Services\ColumnsService;use Illuminate\Database\Seeder;use Illuminate\Support\Facades\Cache;use Illuminate\Support\Facades\DB;use Txj\Elastic\Table\Assemble;class BaseSeeder extends Seeder{    public int $category_type = 2; // // 字段类型 2 表字段 3 扩展字段 4 辅助字段    public string $schema = '';    public string $title = '';    private array $assembleIds = [];    /**     * @param $schema     */    public function setColumn($schema)    {        // 获取项目的id        $assembleInfo = DB::table('sys_assemble')->where('schema', $schema)->where('is_delete', 0)->first();        $data = $this->getColumnData();        $columnService = new ColumnsService($assembleInfo->id, $this->category_type);        $columnData = $columnService->getColumnsAll($data);        foreach ($columnData as $rowData) {            $column = new ColumnController();            $form = new ColumnForm();            foreach ($rowData as $key => $item) {                $form->request->set($key, $item);            }            $result = $column->save($form);            $result = json_decode($result, true);            if ($result && !$result['error']) {            } else {            }        }    }    public function start()    {        $assembleData = $this->getAssembleData();        $form = new AssembleForm();        foreach ($assembleData as $column => $value) {            $form->request->set($column, $value);        }        $assemble = new AssembleController();        $response = $assemble->save($form);        if ($response) {            $result = $response->getContent();            $result = json_decode($result, true);            if (!$result['error']) {                // 设置字段                $this->setColumn($this->schema);                // 清除字段缓存                $cache_key = 'SYS:schema:' . $this->schema;                Cache::forget($cache_key);                // 初始化内容                $this->init();            } else {                echo $result['msg'];            }        }    }    /**     * 必须填写 否则组件无法使用     * 该数据再用户执行添加的时候,会自动把该表信息保存到数据库     *     * @return array     */    public function getAssembleData(): array    {        return [            'title' => $this->title,            'schema' => $this->schema,            'connect_db_id' => 1,            'remark' => '',        ];    }    /**     * 获取集合id     *     * @param $name     * @return mixed     */    public function getAssembleId($name): int    {        if (isset($this->assembleIds[$name])) {            return $this->assembleIds[$name];        } else {            $assemble = new Assemble($name);            return $this->assembleIds[$name] = $assemble->getAssembleId();        }    }}
 |