<?php

namespace Application\Form;

use Application\Common\Enum;
use Application\Form\item\ItemFieldForm;
use Application\Service\DB\AbstractDb;
use Application\Service\DB\Db;
use Application\Service\DB\Dictionary\Form;
use Application\Service\DB\Dictionary\FormField;
use Application\Service\DB\Dictionary\FormGroup;
use Application\Service\Extension\Form\BaseForm;
use Application\Service\Extension\Formatter\Formatter;
use Application\Service\Extension\Formatter\FormFormatter;
use Application\Service\Extension\Helper\ArrayHelper;
use Application\Service\Extension\Helper\SDMHelper;
use Application\Service\Extension\Laminas;
use Laminas\Db\Sql\Predicate\NotIn;
use Laminas\Db\Sql\Predicate\Operator;
use Laminas\Validator\Exception\InvalidArgumentException;

/**
 * 字典项表单
 * @author TOOM1996 <1023150697@qq.com>
 */
class FormForm extends BaseForm
{
    private ?array $_test = [];

    /**
     * dictionaryForm
     * @return Form
     */
    protected function getForm(): AbstractDb
    {
        return Laminas::$serviceManager->dictionaryForm;
    }

    protected function getFormField()
    {
        return Laminas::$serviceManager->dictionaryFormField;
    }

    protected function getFormRadio()
    {
        return Laminas::$serviceManager->dictionaryFormFieldRadio;
    }

    protected function getFormText()
    {
        return Laminas::$serviceManager->dictionaryFormFieldText;
    }

    protected function getFormVersion()
    {
        return Laminas::$serviceManager->dictionaryFormVersion;
    }

    /**
     * 验证字典项表单名称是否添加重复
     * @param $value
     * @return null
     */
    public function invalidCreateName($value)
    {
        $request = Laminas::$app->getMvcEvent()->getRequest();

        if ($value !== null) {
            // 没 id 说明是新增, 有 id 是编辑
            $id = $request->getPost('id');

            // 新增
            if (!$id) {
                $query = $this->getForm()->fetchOne([
                    'columns' => ['id'],
                    'where' => ['name' => $request->getPost('name'), 'is_del' => 0]
                ]);

                if ($query) {
                    throw new InvalidArgumentException("表单名称 {$value} 已存在, 请勿重复添加。");
                }

            } else {
                $query = $this->getForm()->fetchOne([
                    'columns' => ['id'],
                    'where' => ['name' => $request->getPost('name'), 'is_del' => 0]
                ]);

                if ($query && (count($query) > 1 || $query['id'] != $request->getPost('id'))) {
                    throw new InvalidArgumentException("表单名称 {$value} 已在其他表单中使用, 请勿重复添加。");
                }
            }
        }

        if ($request->getPost('type') == Form::FORM_TYPE_SINGLE && $request->getPost('group_id') == FormGroup::THROUGH_PROCESS) {
            throw new InvalidArgumentException("一对一类型的表单暂不支持设置成贯穿全程。");
        }

        return null;
    }

    // TODO 现在好像不好使
    public function invalidCopyId($value)
    {
        $value = explode(',', $value);

        foreach ($value as $attribute) {
            $dictionaryForm = Laminas::$serviceManager->dictionaryForm->fetchOne([
                'where' => ['id' => $attribute]
            ]);

            $hasExist = Laminas::$serviceManager->itemForm->fetchOne([
                'where' => ['name' => $dictionaryForm['name'], 'is_del' => 0, 'item_id' => Laminas::$app->getMvcEvent()->getRequest()->getPost('item_id')]
            ]);

            if ($hasExist) {
                throw new InvalidArgumentException("添加失败啦!!!项目中已存在名称为 {$dictionaryForm['name']} 的表单。");
            }
        }
    }

    /**
     * 验证检查类（OCR识别）是否设置了检查项。
     * 验证是否切换了检查项id。
     * 不允许有检查项id切换成无检查项id。
     * 不允许无检查项id切换成有检查项id。
     */
    public function invalidCheckListId($value)
    {
        $params = Laminas::$app->getMvcEvent()->getRequest();

        if (in_array($params->getPost('group_id'), [FormGroup::CHECK_OCR]) && !$params->getPost('checklist_id')) {
            throw new InvalidArgumentException("保存失败。检查类（OCR识别）必须选择一项检查项表单。");
        }

        if ($id = $params->getPost('id')) {
            $formData = $this->getForm()->fetchOne(['where' => ['id' => $id]]);
            if ($formData['checklist_id'] > 0 && $value == 0) {
                throw new InvalidArgumentException("保存失败。无法将已添加的检查项表单清除, 因为我们已为您初始化了检查项表单。");
            }
        }

        // 一个项目下只能有一个类型的化验单
        if (in_array($params->getPost('group_id'), [FormGroup::CHECK_OCR]) && $params->getPost('checklist_id') && $this->getForm()->getCount([
            'where' => [
                'item_id' => $params->getPost('item_id'),
                'checklist_id' => $params->getPost('checklist_id'),
                'is_del' => 0,
                new Operator('id', '<>', $id ?: 0)
            ]
        ])) {
            throw new InvalidArgumentException("保存失败。此类化验单表单已存在！");
        }

        return null;
    }

    /**
     * 创建字典项表单
     * @return int
     */
    public function createForm(): int
    {
        Db::beginTransaction();

        // 实验总结类默认勾选所有隶属实验总结类检查点
        if ($this->validator->attributes['group_id'] == FormGroup::TEST_SUMMARY) {
            $this->validator->attributes['ds_stage'] = FormGroup::DS_STAGE;
        }


        $formId = $this->getForm()->insert([
            'name' => $this->validator->attributes['name'],
            'type' => $this->validator->attributes['type'],
            'finish_condition' => $this->validator->attributes['finish_condition'],
            'is_required' => $this->validator->attributes['is_required'],
            'checklist_id' => $this->validator->attributes['checklist_id'],
            'create_user_id' => Laminas::$serviceManager->identity->getId(),
            'order' => $this->validator->attributes['order'],
            'group_id' => $this->validator->attributes['group_id'],
            'show_type' => $this->validator->attributes['show_type'],
            'code' => $this->getForm()->generateFormCode(),
            'reference_file' => $this->validator->attributes['reference_file'],
            'rdg_type' => $this->validator->attributes['rdg_type'],
            'var_name' => $this->validator->attributes['var_name'] ?: '',
            'can_patient_write' => $this->validator->attributes['can_patient_write'] ?: 0,
            'is_handwritten_signature' => $this->validator->attributes['can_patient_write'] ?: 0,
            'ds_stage' => $this->validator->attributes['ds_stage'] ?: 1,
            'is_patient_general_info' => $this->validator->attributes['is_patient_general_info'] ?? ''
        ]);

        // 添加随机分组字段
        $this->initRandomGroupField($formId, $this->validator->attributes['rdg_type'] ?? 0);

        if ($this->validator->attributes['checklist_id'] || $this->validator->attributes['group_id'] == FormGroup::CHECK_DO) {
            $this->initCheckListField($formId);
        }

        if ($this->validator->attributes['group_id'] == FormGroup::THROUGH_PROCESS) {
            if ($this->validator->attributes['type'] == 0) {
                throw new \Application\Service\Exceptions\InvalidArgumentException('识别医嘱的类型不能为一对一');
            }
            // 初始化贯穿全程识别医嘱表单字段
            $this->initThroughProcessDoField($formId);
        }

        Db::commit();

        return $formId;
    }

    /**
     * 初始化贯穿全程识别医嘱字段
     */
    public function initThroughProcessDoField($formId)
    {
        $generateFields = [
            [
                'name' => '医嘱类型',
                'type' => FormField::FORM_FIELD_TYPE_RADIO,
                'var_name' => 'THROUGH_PROCESS_DO_TYPE',
                'is_list' => 1
            ],
            [
                'name' => '临时医嘱',
                'type' => 0,
                'value' => 1,
                'var_name' => 'THROUGH_PROCESS_DO_TYPE_A'
            ],
            [
                'name' => '长期医嘱',
                'type' => 0,
                'value' => 2,
                'var_name' => 'THROUGH_PROCESS_DO_TYPE_B'
            ],
            [
                'name' => '处方',
                'type' => 0,
                'value' => 3,
                'var_name' => 'THROUGH_PROCESS_DO_TYPE_C'
            ],
            [
                'name' => '日期类型',
                'type' => FormField::FORM_FIELD_TYPE_RADIO,
                'var_name' => 'THROUGH_PROCESS_DO_DATE',
                'is_list' => 1
            ],
            [
                'name' => '年月日',
                'type' => 0,
                'value' => 1,
                'var_name' => 'THROUGH_PROCESS_DO_DATE_A'
            ],
            [
                'name' => '日月年',
                'type' => 0,
                'value' => 2,
                'var_name' => 'THROUGH_PROCESS_DO_DATE_B'
            ],
            [
                'name' => '年月',
                'type' => 0,
                'value' => 3,
                'var_name' => 'THROUGH_PROCESS_DO_TYPE_C'
            ],
            [
                'name' => '月日',
                'type' => 0,
                'value' => 4,
                'var_name' => 'THROUGH_PROCESS_DO_TYPE_D'
            ],
            [
                'name' => '图片',
                'type' => FormField::FORM_FIELD_TYPE_UPLOAD_IMAGE,
                'var_name' => 'THROUGH_PROCESS_DO_IMAGE'
            ]
        ];

        foreach ($generateFields as $field) {
            if (in_array($field['type'], [FormField::FORM_FIELD_TYPE_UPLOAD_IMAGE, FormField::FORM_FIELD_TYPE_RADIO])) {
                $fieldId = $this->getFormField()->save([
                    'form_id' => $formId,
                    'name' => $field['name'],
                    'type' => $field['type'],
                    'is_list' => $field['is_list'],
                    'rules' => null,
                    'version' => 'v1.0',
                    'order' => 0,
                    'is_del' => 0,
                    'is_anchor' => 0,
                    'item_id' => $this->validator->attributes['item_id'],
                    'var_name' => $field['var_name'],
                    'default' => 0,
                ]);
            }

            if ($field['type'] == 0) {
                 $childId = $this->getFormField()->save([
                    'form_id' => $formId,
                    'name' => $field['name'],
                    'type' => $field['type'],
                    'is_list' => 0,
                    'rules' => null,
                    'version' => 'v1.0',
                    'order' => 0,
                    'is_del' => 0,
                    'is_anchor' => 0,
                    'item_id' => $this->validator->attributes['item_id'],
                    'var_name' => $field['var_name'],
                    'parent' => $fieldId
                ]);

                $this->getFormRadio()->save([
                    'value' => $field['value'],
                    'is_check' => 0,
                    'relation_classification' => 0,
                    'relation_type' => 0,
                    'relation_information' => 0,
                    'field_id' => $childId,
                    'form_id' => $formId,
                    'is_score' => 0
                ]);
            }
        }

        $versionData = Laminas::$serviceManager->itemForm->buildFieldVersion($formId);

        $this->getFormVersion()->save([
            'field' => $versionData,
            'version' => 'v1.0',
            'form_id' => $formId
        ]);
    }

    /**
     * @param $formId
     * @param int $type 1 => 手动添加, 2 => 系统分组
     * @return void|null
     */
    public function initRandomGroupField($formId, int $type)
    {
        if (!$type) {
            return null;
        }

        // 随机分组类表单，随机日期的默认存在以及变量名统一要求设置 --蒙，美
        //https://e.gitee.com/yikeen_project/dashboard?issue=I9QLAA
        $randDateCondition = [
            'form_id' => $formId,
            'name' => '随机日期',
            'type' => FormField::FORM_FIELD_TYPE_DATE,
            'is_list' => 0,
            'rules' => null,
            'version' => 'v1.0',
            'order' => 10,
            'is_del' => 0,
            'is_anchor' => 0,
            'is_booked_time' => 0,
            'var_name' => 'RANDDAT',
        ];

        $condition = [
            'form_id' => $formId,
            'name' => '研究分层',
            'type' => $type == 1 ? Enum::FORM_FIELD_TYPE_RADIO : Enum::FORM_FIELD_TYPE_DROPDOWN,
            'is_list' => 0,
            'rules' => null,
            'version' => 'v1.0',
            'order' => 10,
            'is_del' => 0,
            'is_anchor' => 0,
            'is_booked_time' => 0,
            'var_name' => $type == 1 ? 'RANGC' : 'RANGCA',
            'showfiled_name'=>$type == 1 ? '随机号' : '随机号/方案分组',
        ];

        if ($this->validator->attributes['item_id']) {
            $condition['item_id'] = $randDateCondition['item_id'] = $this->validator->attributes['item_id'];
        }

        $randFileId = $this->getFormField()->save($randDateCondition);
        $this->_test[] = $randFileId;
        $this->getFormText()->save([
            'var_name' => 'RANDDAT',
            'tips' => '',
            'placeholder' => '',
            'is_list' => 0,
            'field_id' => $randFileId,
            'order' => 10,
            'form_id' => $formId
        ]);

        $fieldId = $this->getFormField()->save($condition);
        $this->_test[] = $fieldId;
        $this->getFormText()->save([
            'var_name' => $type == 1 ? 'RANGC' : 'RANGCA',
            'tips' => '',
            'placeholder' => '',
            'is_list' => 0,
            'field_id' => $fieldId,
            'order' => 10,
            'form_id' => $formId
        ]);

        SDMHelper::app()->form->setAttributes(['form_id' => $formId,])->setIsDictionary( get_called_class() == FormForm::class)->generateVersion($formId, true);
    }

    /**
     * 获取初始化随机分组表单生成的字段id
     */
    protected function _getInitRandomGroupFieldId(): ?array
    {
        return $this->_test;
    }

    /**
     * 初始化检查项表单
     * @param $formId
     */
    public function initCheckListField($formId)
    {
        $checkNameData = Laminas::$serviceManager->dictionaryChecknameattr->fetchAll(['where' => ['is_del' => 0]]);

        foreach ($checkNameData as $item) {
            if ($item['attr_type'] == Enum::FORM_FIELD_TYPE_TEXT || $item['attr_type'] == Enum::FORM_FIELD_TYPE_UPLOAD_IMAGE || $item['attr_type'] == Enum::FORM_FIELD_TYPE_RADIO) {
                $fieldId = $this->getFormField()->save([
                    'form_id' => $formId,
                    'name' => $item['attr_name'],
                    'type' => $item['attr_type'],
                    'is_list' => 0,
                    'rules' => null,
                    'version' => 'v1.0',
                    'order' => $item['attr_order'],
                    'is_del' => 0,
                    'is_anchor' => 0,
                    'item_id' => $this->validator->attributes['item_id'],
                    'var_name' => $item['attr_filed'] ?: '',
                    'default' => $item['default'] ?? '',
                ]);
                $contentItemMap[$item['id']] = $fieldId;
            }
        }

        foreach ($checkNameData as $item) {
            if ($item['attr_type'] == Enum::FORM_FIELD_TYPE_TEXT || $item['attr_type'] == Enum::FORM_FIELD_TYPE_UPLOAD_IMAGE || $item['attr_type'] == Enum::FORM_FIELD_TYPE_RADIO) {
                $this->getFormText()->save([
                    'var_name' => $item['attr_filed'],
                    'tips' => '',
                    'placeholder' => '',
                    'is_list' => 0,
                    'field_id' => $contentItemMap[$item['id']],
                    'order' => $item['attr_order'],
                    'form_id' => $formId,
                ]);

                if ($item['content']) {
                    $content = json_decode($item['content'], true);
                    foreach ($content as $contentItem) {
                        // 处理关联项目
                        foreach ($contentItem['select_attr'] as &$v) {
                            $v = $contentItemMap[$v] ?? '0';
                        }

                        $childId = $this->getFormField()->save([
                            'form_id' => $formId,
                            'name' => $contentItem['select_name'],
                            'type' => 0,
                            'is_list' => 0,
                            'rules' => null,
                            'version' => 'v1.0',
                            'order' => $contentItem['select_order'],
                            'is_del' => 0,
                            'is_anchor' => 0,
                            'parent' => $fieldId,
                            'var_name' => $contentItem['select_code'],
                            'item_id' => $this->validator->attributes['item_id'],
                            'default' => $item['default'] ?? '',
                        ]);

                        $this->getFormRadio()->save([
                            'value' => $contentItem['select_val'],
                            'is_check' => 0,
                            'relation_classification' => 2,
                            'relation_type' => 1,
                            'relation_information' => $contentItem['select_attr'] ? implode(',', $contentItem['select_attr']) : 0,
                            'field_id' => $childId,
                            'form_id' => $formId,
                            'is_score' => 0
                        ]);
                    }
                }
            }
        }

        $versionData = Laminas::$serviceManager->itemForm->buildFieldVersion($formId);

        $this->getFormVersion()->save([
            'field' => $versionData,
            'version' => 'v1.0',
            'form_id' => $formId
        ]);
    }

    /**
     * 编辑字典项表单
     * @return array
     */
    public function editForm()
    {
        Laminas::$serviceManager->dictionaryForm->save([
            'id' => $this->validator->attributes['id'],
            'name' => $this->validator->attributes['name'],
            'type' => $this->validator->attributes['type'],
            'finish_condition' => $this->validator->attributes['finish_condition'],
            'is_required' => $this->validator->attributes['is_required'],
            'checklist_id' => $this->validator->attributes['checklist_id'],
            'order' => $this->validator->attributes['order'],
            'group_id' => $this->validator->attributes['group_id'],
            'show_type' => $this->validator->attributes['show_type'],
            'reference_file' => $this->validator->attributes['reference_file'],
            'rdg_type' => $this->validator->attributes['rdg_type'],
            'var_name' => $this->validator->attributes['var_name'] ?: '',
            'can_patient_write' => $this->validator->attributes['can_patient_write'] ?: 0,
            'is_handwritten_signature' => $this->validator->attributes['is_handwritten_signature'] ?? 0,
            'ds_stage' => $this->validator->attributes['ds_stage'] ?: 1,
            'identification_edit' => ArrayHelper::getValue($this->validator->attributes, 'identification_edit', 0),
        ]);

        return [];
    }

    /**
     * 复制字典项表单
     */
    public function copyTo()
    {
        Db::beginTransaction();

        $selectDictionaryFormId = explode(',', $this->validator->attributes['id']);

        foreach ($selectDictionaryFormId as $value) {
            $this->_copy($value);
        }

        Db::commit();
    }

    /**
     * @param int $id 字典项表单id
     */
    private function _copy(int $id)
    {
        // form 表
        $formData = $this->getForm()->fetchOne([
            'where' => ['id' => $id]
        ]);

        // 验证指定表单数量
        if (
            in_array($formData['group_id'], [
                FormGroup::AE,
                FormGroup::SAE,
                FormGroup::RANDOM_GROUP
            ])
        ) {
            if (
                Laminas::$serviceManager->itemForm->fetchOne([
                    'where' => [
                        'item_id' => $this->validator->attributes['item_id'],
                        'group_id' => $formData['group_id'],
                        'is_del' => 0
                    ]
                ])
            ) {
                Db::rollback();
                throw new \Application\Service\Exceptions\InvalidArgumentException("复制中断。 `{$formData['name']}` 的表单类型一个项目只能添加一个。 ");
            }
        }

        $insertId = Laminas::$serviceManager->itemForm->save([
            'name' => $formData['name'],
            'item_id' => $this->validator->attributes['item_id'],
            'group_id' => $formData['group_id'],
            'type' => $formData['type'],
            'is_del' => 0,
            'order' => $formData['order'],
            'finish_condition' => $formData['finish_condition'],
            'is_required' => $formData['is_required'],
            'checklist_id' => $formData['checklist_id'],
            'is_show' => $formData['is_show'],
            'show_type' => $formData['show_type'],
            'is_patient' => 0,
            'reference_file' => $formData['reference_file'],
            'var_name' => $formData['var_name'],
            'can_patient_write' => $formData['can_patient_write'],
            'rdg_type' => $formData['rdg_type'],
            'is_handwritten_signature' => $formData['is_handwritten_signature'],
            'ds_stage' => $formData['ds_stage']
        ]);

        // 字段表
        $fieldData = Laminas::$serviceManager->dictionaryFormField->fetchAll([
            'where' => ['form_id' => $id, 'version' => 'v1.0', 'is_del' => 0]
        ]);

        if (!$fieldData) {
            throw new InvalidArgumentException('你无法复制一个不存在字段属性的表单。');
        }

        $map = $fieldMap = [];
        foreach ($fieldData as $datum) {
            $fieldId = Laminas::$serviceManager->itemFormField->save([
                'form_id' => $insertId,
                'name' => $datum['name'],
                'type' => $datum['type'],
                'is_list' => $datum['is_list'],
                'is_export' => $datum['is_export'],
                'order' => $datum['order'],
                'rules' => $datum['rules'],
                'version' => $datum['version'],
                'is_del' => $datum['is_del'],
                'parent' => $datum['parent'] ? $map[$datum['parent']] : 0,
                'is_anchor' => $datum['is_anchor'] ?: 0,
                'is_booked_time' => $datum['is_booked_time'] ?: 0,
                'item_id' => $this->validator->attributes['item_id'],
                'var_name' => $datum['var_name'],
                'is_hide' => $datum['is_hide'] ?: 0,
                'dicform_id' => $datum['dicform_id'] ?: 0,
            ]);

            if ($datum['parent'] == 0) {

                $map[$datum['id']] = $fieldId;
            }
            $fieldMap[] = $fieldId;
        }

        foreach ($fieldData as $k => $datum) {
            // text 表
            $textData = Laminas::$serviceManager->dictionaryFormFieldText->fetchAll([
                'where' => ['field_id' => $datum['id']]
            ]);

            $relationMap = [];
            foreach ($textData as $textDatum) {
                $textId = Laminas::$serviceManager->itemFormFieldText->save([
                    'var_name' => $textDatum['var_name'],
                    'tips' => $textDatum['tips'],
                    'placeholder' => $textDatum['placeholder'],
                    'is_list' => $textDatum['is_list'],
                    'order' => $textDatum['order'],
                    'field_id' => $fieldMap[$k],
                    'form_id' => $insertId
                ]);
                $relationMap[$textDatum['id']] = $textId;
            }

            // radio 表
            $versionData = Laminas::$serviceManager->dictionaryFormFieldRadio->fetchAll([
                'where' => ['field_id' => $datum['id']]
            ]);

            foreach ($versionData as $versionDatum) {
                Laminas::$serviceManager->itemFormFieldRadio->save([
                    'value' => $versionDatum['value'],
                    'is_check' => call_user_func(function () use ($versionDatum) { // 1是false 0是true
                        if (!$versionDatum['is_check']) {
                            return 0;
                        }

                        return $versionDatum['is_check'];
                    }),
                    'relation_classification' => $versionDatum['relation_classification'],
                    'relation_type' => $versionDatum['relation_type'],
                    'relation_information' => $this->_handleRelationInformation($versionDatum['relation_information'], $map),
                    'field_id' => $fieldMap[$k],
                    'form_id' => $insertId,
                    'score' => $versionDatum['score'],
                    'is_score' => $versionDatum['is_score'],
                ]);
            }
        }

        // 重新生成表单版本数据
        $model = new ItemFieldForm();
        Laminas::$serviceManager->itemFormVersion->save([
            'version' => 'v1.0',
            'field' => $model->buildVersionField($fieldId, $insertId),
            'form_id' => $insertId,
        ]);
    }

    /**
     * 复制表单时对有关联信息的表单做处理
     * @param string $relationInformation
     * @param $relationMap
     * @return string
     */
    private function _handleRelationInformation(string $relationInformation, $relationMap): string
    {
        $relationInformation = explode(',', $relationInformation);
        foreach ($relationInformation as &$value) {
            if ($value) {
                $value = $relationMap[$value];
            }
        }

        return implode(',', $relationInformation);
    }

    public function getCopyList(): array
    {
        $itemForm = Laminas::$serviceManager->itemForm->fetchAll([
            'columns' => ['code'],
            'where' => ['is_del' => 0, 'item_id' => $this->validator->attributes['item_id']],
            'order' => ['order']
        ]);

        $arr = array_filter(ArrayHelper::getColumn($itemForm, 'code'));

        $whereCondition = ['is_del' => 0];

        if ($arr) {
            $whereCondition[] = new NotIn('code', $arr);
        }

        // 表单类型id
        $form_group_id = $this->validator->attributes['form_group_id'];
        // 表单tpye
        $form_type = $this->validator->attributes['form_type'];
        // 表单检索条件
        $formWhereArr = ['is_del' => 0];
        if($form_group_id) $whereCondition['group_id'] = $form_group_id;
        if($form_type !== '') $whereCondition['type'] = $form_type;

        $dictionaryForm = Laminas::$serviceManager->dictionaryForm->fetchAllWithPagination([
            'columns' => ['id', 'group_id', 'name', 'type', 'order'],
            'where' => $whereCondition,
            'order' => ['order']
        ]);

        Formatter::format($dictionaryForm['data'], new FormFormatter());

        return [
            'data' => $dictionaryForm['data'],
            'total' => $dictionaryForm['count']
        ];
    }
}