<?php


namespace App\Form\Services;

use App\Services\CommonUserBaseService;
use App\Form\Facades\FormItemFacade;

class FormContentService extends CommonUserBaseService
{
    /**
     * 获取列表数据
     * @param array $recordId
     * @return array
     */
    public function getListByRecordIds(array $recordId)
    {
        $list = $this->model->alias('a')
            ->leftJoin('form_item as b', 'a.form_item_id', '=', 'b.id')
            ->whereIn('a.record_id',$recordId)
            ->orderBy('b.sort')->selectRaw('a.*')->get()->toArray();
        $formItemModel=FormItemFacade::getModel();
        if (!empty($list)) {
            $itemIds = array_column($list, 'form_item_id');
            $items=$formItemModel->whereIn('id',$itemIds)->selectRaw('id,name')->orderBy('sort')->get()->toArray();
            !empty($items) && $items = mapByKey($items, 'id');

            foreach ($list as &$v) {
                $v['item_name'] = isset($items[$v['form_item_id']]) ? $items[$v['form_item_id']]['name']: '';
                $v['content'] = json_decode($v['content'], true);
            }
        }
        $data = [];
        foreach ($list as $l) {
            $data[$l['record_id']][] = $l;
        }
        return $data;
    }
}
