<?php
namespace Application\Service\DB\Dictionary;

use Interop\Container\ContainerInterface;

class FormVersion extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        if (!$this->tableName) {
            $this->tableName = 'yikeen_dictionary_form_version';
        }
        parent::__construct($dbAdapter, $container);
    }


    /**
     * 获取 version 字段
     * @param array $where
     * @param bool $returnOrigin
     * @return array|mixed
     */
    public function getField(array $where, $returnOrigin = false)
    {
        $field = $this->memoryCache()->fetchOne([
            'order' => ['id DESC'],
            'where' => $where
        ])['field'] ?? null;

        if (!$field) {
            return [];
        }else{
            $field = json_decode($field, true);
        }

        if ($returnOrigin) {
            return $field;
        }

        array_walk($field, function (&$item) {
           if (isset($item['children'])) {
               $item['children'] = array_values($item['children']);
           }
        });

        return array_values($field);
    }

}