<?php
class Service_WarehouseCardDay extends Common_Service
{
    /**
     * @var null
     */
    public static $_modelClass = null;

    /**
     * @return Table_WarehouseCardDay|null
     */
    public static function getModelInstance()
    {
        if (is_null(self::$_modelClass)) {
            self::$_modelClass = new Table_WarehouseCardDay();
        }
        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 = "wcd_id")
    {
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function delete($value, $field = "wcd_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 = 'wcd_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, $paramId)
    {
        $validateArr = $error = array();
        $validateArr[] = array("name" =>EC::Lang('customerCode'), "value" =>$val["wcd_customer_code"], "regex" => array("require"));
        $validateArr[] = array("name" =>'日卡位数', "value" =>$val["wcd_card_day_num"], "regex" => array("require","integer",));
        //$validateArr[] = array("name" =>EC::Lang('warehouseCode'), "value" =>$val["wc_warehouse_code"], "regex" => array("require","integer",));
        $validateArr[] = array("name" =>EC::Lang('status'), "value" =>$val["wc_status"], "regex" => array("positive"));
        $error = Common_Validator::formValidator($validateArr);
        $customer = Service_Customer::getByField($val["wcd_customer_code"], 'customer_code', array('customer_id', 'customer_status'));
        if( empty($customer) ) {
            $error[] = '客户代码['.$val['wcd_customer_code'].']不存在';
        }else {
            if($customer['customer_status'] != 2 ){
                $error[] = '客户代码['.$val['wcd_customer_code'].']状态不可用';
            }
        }
        //修改
        if(!empty($paramId)){
            $wcData = Service_WarehouseCardDay::getByField($paramId, 'wcd_id', array('wcd_card_day_time', 'wc_card_month_time'));
            /* if(strtotime($val["wcd_card_day_time"]) != strtotime($wcData['wcd_card_day_time'])){
                if(strtotime(date('Y-m-d')) != strtotime($val["wcd_card_day_time"]) ){
                    $error[] = '日卡位数只能维护客户当天['.date('Y-m-d').']的卡位数';
                }
            } */
        //添加
        }else {
            /* if(strtotime(date('Y-m-d')) != strtotime($val["wcd_card_day_time"]) ){
                $error[] = '日卡位数只能维护客户当天['.date('Y-m-d').']的卡位数';
            } */
        }
        return $error;
    }

    /**
     * @param array $params
     * @return array
     */
    public  function getFields()
    {
        $row = array(
        
            'E0' => 'wcd_id',
            'E1' => 'wcd_customer_code',
            'E2' => 'wcd_warehouse_code',
            'E3' => 'wcd_card_day_num',
            'E4' => 'wcd_card_day_time',
            'E5' => 'wcd_status',
            'E6' => 'wcd_add_time',
            'E7' => 'wcd_add_user_id',
            'E8' => 'wcd_update_time',
            'E9' => 'wcd_update_user_id',
            'E10' => 'wcd_code',
        );
        return $row;
    }

    /**
     * [status description]
     * @param  string $lang [description]
     * @return [type]       [description]
     */
    public static function status($lang = 'zh_CN')
    {
        $status = array(
            'zh_CN' => array(
                '0' => '未收费',
                '1' => '已收费',
            ),
            'en_US' => array(
                '0' => 'NoCharge',
                '1' => 'AlreadyCharge',
            )
        );
        if ($lang == 'auto') {
            $lang = Ec::getLang();
        }
        return isset($status[$lang]) ? $status[$lang] : $status;
    }
}