<?php
class Service_WarehouseCardMonth extends Common_Service
{
    /**
     * @var null
     */
    public static $_modelClass = null;

    /**
     * @return Table_WarehouseCardMonth|null
     */
    public static function getModelInstance()
    {
        if (is_null(self::$_modelClass)) {
            self::$_modelClass = new Table_WarehouseCardMonth();
        }
        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 = "wcm_id")
    {
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function delete($value, $field = "wcm_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 = 'wcm_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["wcm_customer_code"], "regex" => array("require"));
        $validateArr[] = array("name" => '月卡位数', "value" => $val["wcm_card_month_num"], "regex" => array("require","integer",));
        //$validateArr[] = array("name" =>EC::Lang('status'), "value" =>$val["wc_status"], "regex" => array("positive"));
        $error = Common_Validator::formValidator($validateArr);
        if(Service_Customer::getByField($val["wcm_customer_code"], 'customer_code', array('customer_id')) === false ) {
            $error[] = '客户代码['.$val['wcm_customer_code'].']不存在';
        }
        //修改
        if(!empty($paramId)){
            $wcData = Service_WarehouseCardMonth::getByField($paramId, 'wcm_id', array('wcm_card_month_time'));
            if(strtotime($val["wcm_card_month_time"]) != strtotime($wcData['wcm_card_month_time'])){
                if(strtotime(date('Y-m-d')) > strtotime($val["wcm_card_month_time"]) ){
                    $error[] = '月卡位维护时间必须大于当前时间['.date('Y-m-d').']';
                }
            }
        //添加
        }else {
            if(strtotime(date('Y-m-d')) > strtotime($val["wcm_card_month_time"]) ){
                $error[] = '月卡位维护时间必须大于当前时间['.date('Y-m-d').']';
            }
        }
        return $error;
    }

    /**
     * @param array $params
     * @return array
     */
    public  function getFields()
    {
        $row = array(
            'E0' => 'wcm_id',
            'E1' => 'wcm_code',
            'E2' => 'wcm_customer_code',
            'E3' => 'wcm_warehouse_code',
            'E4' => 'wcm_card_month_num',
            'E5' => 'wcm_card_month_time',
            'E6' => 'wcm_status',
            'E7' => 'wcm_add_time',
            'E8' => 'wcm_add_user_id',
            'E9' => 'wcm_update_time',
            'E10' => 'wcm_update_user_id',
        );
        return $row;
    }

}