<?php
class Service_TypeCategory extends Common_Service
{
    /**
     * @var null
     */
    public static $_modelClass = null;

    /**
     * @return Table_TypeCategory|null
     */
    public static function getModelInstance()
    {
        if (is_null(self::$_modelClass)) {
            self::$_modelClass = new Table_TypeCategory();
        }
        return self::$_modelClass;
    }

    /**
     * @param $row
     * @return mixed
     */
    public static function add($row)
    {
        $row['tc_update_time']=date('Y-m-d H:i:s');
        $model = self::getModelInstance();
        return $model->add($row);
    }


    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function update($row, $value, $field = "tc_id")
    {
        $row['tc_update_time']=date('Y-m-d H:i:s');
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function delete($value, $field = "tc_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 = 'tc_id', $colums = "*")
    {
        $model = self::getModelInstance();
        return $model->getByField($value, $field, $colums);
    }

    /**
     * @return mixed
     */
    public static function getAll()
    {
        $model = self::getModelInstance();
        return $model->getAll();
    }

    /**
     * @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('CODE'), "value" =>$val["tc_code"], "regex" => array("require",));
        $validateArr[] = array("name" =>EC::Lang('description'), "value" =>$val["tc_description"], "regex" => array("require",));
        $validateArr[] = array("name" =>EC::Lang('englishDescription'), "value" =>$val["tc_description_en"], "regex" => array("require",));
        return  Common_Validator::formValidator($validateArr);
    }


    /**
     * @param array $params
     * @return array
     */
    public  function getFields()
    {
        $row = array(
        
              'E0'=>'tc_id',
              'E1'=>'tc_code',
              'E2'=>'tc_description',
              'E3'=>'tc_description_en',
              'E4'=>'tc_update_time',
        );
        return $row;
    }


    /**
     * @根据Code获取Type
     * @param string $code
     * @param null $display
     * @return array
     */
    public static function getByCode($code = '', $display = null)
    {
        $result = array('state' => 0, 'data' => array());
        $model = self::getModelInstance();
        $row = $item = array();
        $row = $model->getByField($code, 'tc_code', '*');
        if (!empty($row)) {
            $result['state'] = 1;
            if ($display != null) {
                $condition['tcv_display'] = $display;
            }
            $condition['tc_id'] = $row['tc_id'];
            $item = Service_TypeCategoryValue::getByCondition($condition, '*', 0, 0, array('tcv_sort'));
            foreach($item as $k =>$v){
                $item[$v['tcv_id']]=$v;
            }
            $result['data'] = array('type' => $row, 'item' => $item);
        }
        return $result;
    }
}