<?php
class Service_ServiceProvider extends Common_Service
{
    /**
     * @var null
     */
    public static $_modelClass = null;

    /**
     * @return Table_ServiceProvider|null
     */
    public static function getModelInstance()
    {
        if (is_null(self::$_modelClass)) {
            self::$_modelClass = new Table_ServiceProvider();
        }
        return self::$_modelClass;
    }

    /**
     * @param $row
     * @return mixed
     */
    public static function add($row)
    {
        $model = self::getModelInstance();

        $addDate = date("Y-m-d H:i:s");

        $row['sp_add_date'] = $addDate;

        return $model->add($row);
    }


    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function update($row, $value, $field = "sp_id")
    {
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function delete($value, $field = "sp_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 = 'sp_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);
    }

    public static function getJoinLeftByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = "")
    {
        $model = self::getModelInstance();
        return $model->getJoinLeftByCondition($condition, $type, $pageSize, $page, $order);
    }

    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $order
     * @return mixed
     */
    public static function getJoinInnerByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = "")
    {
        $model = self::getModelInstance();
        return $model->getJoinInnerByCondition($condition, $type, $pageSize, $page, $order);
    }

    /**
     * @param $val
     * @return array
     */
    public static function validator($val)
    {
        $validateArr = $error = array();

        return  Common_Validator::formValidator($validateArr);
    }


    /**
     * 添加和修改服务商信息、银行帐号、服务商产品
     * @param unknown_type $serviceProvider
     * @param unknown_type $bankAccount
     * @param unknown_type $product
     * @param unknown_type $contact
     */
    public static function excuteAddServiceProvider($serviceProvider = array(),$bankAccount = array(),$product = array(),$contact = array()){
        $return = array(
            'state' => 0,
            'message'=>'',
        );
        $db = Common_Common::getAdapter();
        $db->beginTransaction();
        try {
            $paramId = $serviceProvider['sp_id'];
            $serviceProvider['sp_add_date'] = date('Y-m-d H:i:s');
            if (!empty($serviceProvider['sp_id'])) {
                unset($serviceProvider['sp_id']);
                unset($serviceProvider['sp_add_date']);
                $serviceProvider['sp_update_date'] = date('Y-m-d H:i:s');
            }

            /*
             * 1、添加或修改服务商信息
            */
            $serviceProviderId = "";
            if (!empty($paramId)) {
                /*
                 * 如果修改了服务商，则这个服务商下的产品信息绑定的采购员也必须一起修改
                */
                $result = self::update($serviceProvider, $paramId);
            } else {
                $serviceProviderId = self::add($serviceProvider);
                if(!$serviceProviderId){
                    throw new Exception("新建服务商失败！");
                }
            }

            /*
             * 2、添加或修改银行帐号
            */

            if(!empty($bankAccount)){
                foreach($bankAccount as $key=>$val){
                    if(!empty($paramId)){
                        $val["sp_id"] = $paramId;
                    }else{
                        $val["sp_id"] = $serviceProviderId;
                    }
                    Service_ServiceBankAccount::delete($val['sp_id'],'sp_id');
                }
                foreach($bankAccount as $key=>$val){
                    $bankAccount_id = $val["sba_id"];
                    unset($val["sba_id"]);

                    if(!empty($paramId)){
                        $val["sp_id"] = $paramId;
                    }else{
                        $val["sp_id"] = $serviceProviderId;
                    }
                    $val['sba_update_time']=date('Y-m-d H:i:s');
                    /*if (!empty($bankAccount_id)) {
                        $result = Service_ServiceBankAccount::update($val, $bankAccount_id);
                    } else {*/

                        $result = Service_ServiceBankAccount::add($val);
                    //}
                }
            }
            /*
             * 3、添加或修改服务商产品
            */
            if(!empty($product)){
                foreach($product as $key=>$val){
                    $product_id = $val["spp_id"];
                    unset($val["spp_id"]);

                    if(!empty($paramId)){
                        $val["sp_id"] = $paramId;
                    }else{
                        $val["sp_id"] = $serviceProviderId;
                    }

                    if (!empty($product_id)) {
                        $result = Service_ServiceProduct::update($val, $product_id);
                    } else {
                        $result = Service_ServiceProduct::add($val);
                    }
                }
            }

            if(!empty($paramId)){
                Service_ServiceContact::delete($paramId,"sp_id");
            }

            if(!empty($contact)){
                foreach ($contact as $cKey=>$cVal){
                    if(!empty($paramId)){
                        $cVal["sp_id"] = $paramId;
                    }else{
                        $cVal["sp_id"] = $serviceProviderId;
                    }
                    Service_ServiceContact::add($cVal);
                }

            }


            $return["state"] = 1;
            $db->commit();
        } catch (Exception $e) {
            $db->rollBack();
            $return["message"] = $e->getMessage();
        }

        return $return;
    }

    /**
     * 校验单号是否存在
     * @param unknown_type $sp_code
     * @param unknown_type $sp_id
     * @return string 0. 表示存在， 1. 不存在
     */
    public function verifySpCode($sp_code, $sp_id) {

        $serviceProvider = self::getByField($sp_code,"sp_code",array("sp_id","sp_code"));

        // 不为空并且不等于当前ID表示重复
        if(!empty($serviceProvider) && $serviceProvider['sp_id'] != $sp_id) {
            return "0";
        }

        return "1";
    }

    /**
     * @param array $params
     * @return array
     */
    public  function getFields()
    {
        $row = array(
				'E0' => 'sp_id',
				'E1' => 'sp_code',
				'E2' => 'sp_name',
				'E3' => 'sp_contact_name',
				'E4' => 'sp_contact_phone',
				'E5' => 'sp_address',
				'E6' => 'sp_settlement_type',
				'E7' => 'sp_balance',
				'E8' => 'currency_code',
				'E9' => 'sp_add_date',
				'E10' => 'sp_update_date',
        		'E11' => 'sp_status',
        		'E12' => 'sp_person_user',
        		'E13' => 'sp_tax',
        );
        return $row;
    }

}