<?php
class Service_FormType extends Common_Service
{
    /**
     * @var null
     */
    public static $_modelClass = null;

    /**
     * @return Table_FormType|null
     */
    public static function getModelInstance()
    {
        if (is_null(self::$_modelClass)) {
            self::$_modelClass = new Table_FormType();
        }
        return self::$_modelClass;
    }

    /**
     * @param $row
     * @return mixed
     */
    public static function add($row)
    {
        $model = self::getModelInstance();
        return $model->add($row);
    }


    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function update($row, $value, $field = "ft_id")
    {
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function delete($value, $field = "ft_id")
    {
        $model = self::getModelInstance();
        return $model->delete($value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public static function getByField($value, $field = 'ft_id', $colums = "*")
    {
        $model = self::getModelInstance();
        return $model->getByField($value, $field, $colums);
    }
    public static function getFormDefaultRow($value, $field = 'form_type'){
        $model = self::getModelInstance();
        $row = $model->getByField($value, $field, '*');
        $return = array(
            'state'=>'0',
            'message'=>'',
            'data'=>[],
        );
        if(!empty($row)&&$row['default_value']){
            $row = json_decode($row['default_value'],true);
            if(!empty($row)){
                $return['data'] = $row;
                $return['state'] = 1;
            }else{
                $return['state'] = '99';
                $return['message'] = "业务类型json序列话错误".$value;
            }
        }
        return $return;
    }
    /**
     * @return mixed
     */
    public static function getAll()
    {
        $model = self::getModelInstance();
        return $model->getAll();
    }
    
    /**
     * @author solar
     * @return array
     */
    public static function getKeyVal($is_display=1) {
    	$aKeyVal = array();
    	$list = self::getByCondition(['is_display'=>$is_display]);
    	foreach($list as $row){
            $aKeyVal[$row['form_type']] = $row['form_type_name'];
        }
    	return $aKeyVal;
    }

    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $order
     * @return mixed
     */
    public static function getByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = "")
    {
        $model = self::getModelInstance();
        return $model->getByCondition($condition, $type, $pageSize, $page, $order);
    }

    /**
     * @param $val
     * @return array
     */
    public static function validator($val)
    {
        $validateArr = $error = array();
        
        $validateArr[] = array("name" =>EC::Lang('业务类型'), "value" =>$val["form_type"], "regex" => array("require",));
        $validateArr[] = array("name" =>EC::Lang('业务类型名称'), "value" =>$val["form_type_name"], "regex" => array("require",));
        return  Common_Validator::formValidator($validateArr);
    }


    /**
     * @param array $params
     * @return array
     */
    public  function getFields()
    {
        $row = array(
        
              'E0'=>'ft_id',
              'E1'=>'form_type',
              'E2'=>'form_type_name',
        );
        return $row;
    }

}