<?php

class Process_ServiceProviderFeeProcess
{

    public static function payment($spCode = '', $options = array(), $opStatus = 1)
    {
        $return = array(
            'state' => 0,
            'message' => '',
            'data' => array()
        );
        try {
            if (empty($spCode)) {
                $return['message'] = 'spCode code can not be empty.';
                return $return;
            }
            $spRow = Service_ServiceProvider::getByField($spCode, 'sp_code');
            if (empty($spRow)) {
                $return['message'] = 'spCode:' . $spCode . ' can not be found.';
                return $return;
            }
            //币种
            $currencyCode = $spRow['currency_code'];
            //参数
            $detail = array(
                'spbl_type' => '',
                'spbl_value' => 0,
                'spbl_currency_code' => '',
                'spbl_currency_rate' => '',
                'user_id' => '',
                'spbl_refer_code' => '',
                'application_code' => '',
                'spbl_note' => '',
                'spbl_transaction_number' => '',
            );
            $date = date('Y-m-d H:i:s');
            $detail['spbl_add_time'] = $date;
            $userId = Service_User::getUserId();
            //数组转多维
            $optionArr = Common_Common::multiArr($options);
            $detailArr = array();
            foreach ($optionArr as $option) {
                $tmpDetail = $detail;
                foreach ($option as $key => $val) {
                    if (isset($detail[$key]) && $val !== '' && !is_array($val)) {
                        $tmpDetail[$key] = $val;
                    }
                    if (!is_numeric($tmpDetail['spbl_value'])) {
                        $return['message'] = 'Fee can not be null.';
                        return $return;
                    }
                    if (!in_array($tmpDetail['spbl_type'], array(1, 2))) {
                        $return['message'] = 'FeeType:' . $tmpDetail['spbl_type'] . ' can not be found.';
                        return $return;
                    }
                }
                $detailArr[] = $tmpDetail;
            }
            unset($optionArr, $tmpDetail);

            //写入日志
            $logArr = array();
            $value = 0;
            foreach ($detailArr as $key => $feeArr) {
                $value = $feeArr['spbl_value'];
                $rateRow = Process_Financial::changeCurrency($feeArr['spbl_value'], $feeArr['spbl_currency_code'], $currencyCode, $feeArr['spbl_currency_rate']);
                if (!is_array($rateRow)) {
                    $return['message'] = $rateRow;
                    return $return;
                }
                $price = $rateRow['value'];
                $feeArr['spbl_currency_rate'] = $feeArr['spbl_currency_rate'] != '' ? $feeArr['spbl_currency_rate'] : $rateRow['rate'];
                $feeArr['spbl_transaction_value'] = $price;
                $feeArr['user_id'] = $feeArr['user_id'] != '' ? $feeArr['user_id'] : $userId;
                switch ((int)$feeArr['spbl_type']) {
                    case 1: //扣款
                        $feeArr['spbl_type'] = 1;
                        $feeArr['spbl_value'] = $value;
                        $spRow['sp_balance'] = $spRow['sp_balance'] - $price;
                        $feeArr['spbl_current_value'] = $spRow['sp_balance'];
                        $logArr[] = $feeArr;
                        break;
                    case 2: //入款
                        $feeArr['spbl_type'] = 2;
                        $feeArr['spbl_value'] = $value;
                        $spRow['sp_balance'] = $spRow['sp_balance'] + $price;
                        $feeArr['spbl_current_value'] = $spRow['sp_balance'];
                        $logArr[] = $feeArr;
                        break;
                    default:
                        break;

                }
            }

            $spRow['sp_balance'] = sprintf("%.3f", $spRow['sp_balance']);
            if ($spRow['sp_balance'] < 0 && $opStatus == '0') {
                $return['message'] = 'insufficient fund.';
                return $return;
            }
            $upRow = array(
                'sp_balance' => $spRow['sp_balance'],
                'sp_update_date' => $date,
            );
            if (!Service_ServiceProvider::update($upRow, $spRow['sp_id'], 'sp_id')) {
                $return['message'] = 'Payment failure.';
                return $return;
            }
            $logObj = new Service_ServiceProviderBalanceLog();
            foreach ($logArr as $feeLog) {
                $feeLog['sp_code'] = $spRow['sp_code'];
                $logObj->add($feeLog);
            }
            $return['state'] = 1;
            $return['message'] = 'Operation Successfully Completed';
            $return['data'] = array('sp_code' => $spRow['sp_code'], 'currency_code' => $currencyCode,'balance'=>$upRow['sp_balance']);
        } catch (Exception $e) {
            $return['state'] = 0;
            $return['message'] = $e->getMessage();
        }
        return $return;
    }


}