<?php
class Service_CustomerOtherFee extends Common_Service
{
    /**
     * @var null
     */
    public static $_modelClass = null;

    /**
     * @return Table_ShippingMethod|null
     */
    public static function getModelInstance()
    {
        if (is_null(self::$_modelClass)) {
            self::$_modelClass = new Table_CustomerOtherFee();
        }
        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 = "cof_id")
    {
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function delete($value, $field = "cof_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 = 'cof_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('FeeChineseName'), "value" =>$val["fee_id"], "regex" => array("require",));
        $validateArr[] = array("name" =>EC::Lang('customerCode'), "value" =>$val["customer_code"], "regex" => array("require",));
        $validateArr[] = array("name" =>EC::Lang('discount'), "value" =>$val["discount"], "regex" => array("require","positive",));
        $validateArr[] = array("name" =>EC::Lang('status'), "value" =>$val["status"], "regex" => array("require",));
        return  Common_Validator::formValidator($validateArr);
    }
    
    public static function validatorAndTransfer(&$data){
        $error = array();
        $statusArr = array('0'=>'不可用','1'=>'可用');
        foreach($data as $key=>&$value){
            $feeName       = trim($value[0]);//费用名称
            $customerCode  = trim($value[1]);//客户代码
            $fee           = trim($value[2]);//费用
            $discount      = trim($value[3]);//折扣
            $rentFreedays  = trim($value[4]);//免租天数
            $status        = trim($value[5]);//状态
            $otherfeeRow   = Service_OtherFee::getByField($feeName,'fee_name');
            if(empty($otherfeeRow)){
                $error[$key]['error'] = '费用名称['.$feeName.']不存在';
                continue;
            }else{
                $feeId = $otherfeeRow['fee_id'];//获取费用id
                $value['fee_id'] = $feeId;
            }
            $customer=Service_Customer::getByField($customerCode,'customer_code');
            if(empty($customer)){
                $error[$key]['error'] = '客户代码['.$customerCode.']不存在';
                continue;
            }else{
                $value['customer_code'] = $customerCode;
            }
            //将费用列为非必填，如果为空，直接取杂费配置中设置的费用
            if($fee == ''){
                $value['fee_value'] = $otherfeeRow['fee_value'];
            }else{
                if(!(preg_match('/^\d+\.\d+$ || ^\d+$/',$fee) && is_numeric($fee))){
                    $error[$key]['error'] = '客户代码['.$customerCode.']['.$feeName.']费用取值应为数字';
                    continue;
                }else{
                    $value['fee_value'] = $fee;
                }
            }
            if(!(preg_match('/^\d+\.\d+$ || ^\d+$/',$discount) && is_numeric($discount))){
                $error[$key]['error'] = '客户代码['.$customerCode.']['.$feeName.']折扣取值应为数字';                
                continue;
            }else{
                $value['discount'] = $discount;
            }
            $otherfeeCondition = array(
                'fee_id'=>$feeId,
                'customer_code'=>$customerCode
            );
            $numberotherfees=Service_CustomerOtherFee::getByCondition($otherfeeCondition,'count(*)');
            if($numberotherfees>0){
                $error[$key]['error']='客户['.$customerCode.']'.$feeName.'已经存在';
                continue;
            }
            if($otherfeeRow['fee_discount']>$discount){
                $error[$key]['error'] = '您设置客户['.$customerCode.']的'.$feeName.'折扣必须大于该费用的最低折扣';
                continue;
            }else{
                $value['discount'] = $discount;
            }
            if(!empty($rentFreedays)){
                if(!preg_match('/^\d+$/',$rentFreedays)){
                    $error[$key]['error'] = '您设置客户['.$customerCode.']的'.$feeName.'免租天数必须为数字';
                    continue;
                }else{
                    $value['limit_time'] = $rentFreedays ? $rentFreedays : '0';
                }
            }
            if(!in_array($status, $statusArr)){
                $error[$key]['error'] = '您设置客户['.$customerCode.']的'.$feeName.'状态值不对';
                continue;
            }else{
                $value['status'] = array_search($status,$statusArr);
            }
             unset($value[0]);//费用名称
             unset($value[1]);//客户代码
             unset($value[2]);//费用
             unset($value[3]);//折扣
             unset($value[4]);//免租天数
             unset($value[5]);//状态
         
        }
        return $error;
    }

    public static function leftJoinCustomerByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = "")
    {
        $model = self::getModelInstance();
        return $model->leftJoinCustomerByCondition($condition, $type, $pageSize, $page, $order);
    }


    /**
     * @param array $params
     * @return array
     */
    public  function getFields()
    {
        $row = array(
            'E0'=> 'cof_id',
            'E1'=> 'fee_id',
            'E2'=> 'customer_code',
            'E3'=> 'discount',
            'E4'=> 'status',
            'E5'=> 'fee_value',
            'E6'=> 'limit_time'
        );
        return $row;
    }
}