<?php

class Service_BilPayment extends Common_Service
{
    /**
     * @var null
     */
    public static $_modelClass = null;

    /**
     * @return Table_BilPayment|null
     */
    public static function getModelInstance()
    {
        if (is_null(self::$_modelClass)) {
            self::$_modelClass = new Table_BilPayment();
        }
        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 = "bp_id")
    {
        $model = self::getModelInstance();
        return $model->update($row, $value, $field);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public static function delete($value, $field = "bp_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 = 'bp_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);
    }

    /*
     * @desc 查看应付费用
    */
    public static function getNotAuditFeesByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = "")
    {
        $model = self::getModelInstance();
        return $model->getNotAuditFeesByCondition($condition, $type, $pageSize, $page, $order);
    }

    /*
     * @desc 查看应付费用（按服务商汇总）
    */
    public static function getNotAuditFeesGroupByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = "")
    {
        $model = self::getModelInstance();
        return $model->getNotAuditFeesGroupByCondition($condition, $type, $pageSize, $page, $order);
    }

    /**
     * @param $val
     * @return array
     */
    public static function validator($val)
    {
        $validateArr = $error = array();

        return Common_Validator::formValidator($validateArr);
    }


    /**
     * @param array $params
     * @return array
     */
    public function getFields()
    {
        $row = array(

            'E0' => 'bp_id',
            'E1' => 'sp_code',
            'E2' => 'bb_id',
            'E3' => 'ft_code',
            'E4' => 'ut_code',
            'E5' => 'bp_creator_id',
            'E6' => 'bsbdf_id',
            'E7' => 'sm_code',
            'E8' => 'bp_amount',
            'E9' => 'currency_code',
            'E10' => 'currency_rate',
            'E11' => 'bp_status',
            'E12' => 'bp_chargeable_time',
            'E13' => 'bp_add_time',
            'E14' => 'bp_update_time',
        );
        return $row;
    }


    /**
     * @desc 导出非逻辑费用
     * @param $condition
     * @return array
     */
    public static function getFeeData($condition)
    {
        //中英文对应
        $keyMap = array(
            "A" => '服务商代码',
            "B" => '结算方式',

            "C" => '收付类型',

            "D" => '状态',
            "E" => '费用类型',
            "F" => '计费金额',
            "G" => '币种',
            "H" => '单号',
            "I" => '创建人',
            "J" => '创建时间',
            "K" => '备注',
        );
        $count = Service_BilPayment::getNotAuditFeesByCondition($condition, 'count(*)');

        $csvData = array();
        $csvData[1] = $keyMap; //Excel 表头
        if ($count) {
            $type = array(
                'bil_payment.bp_id',
                'bil_payment.bp_chargeable_time',
                'bil_payment.ft_code',
                'bil_payment.sm_code',
                'bil_payment.bp_amount',
                'bil_business.ds_code',
                'service_provider.sp_code',
                'fee_type.ft_name_cn',
                'bil_payment.bp_rp_type',
                'bil_payment.bp_status',
                'bil_payment.bp_verify_time',
                'bil_payment.bp_add_time',

                'bil_payment.bp_withdraw_sign',
                'bil_payment.bp_note',
            );
            $businessType = Process_BillConfig::businessType('auto');
            $spSettlementType = Process_BillConfig::spSettlementType('auto');
            $rpType = Process_BillConfig::rpType('auto');
            $bilPaymentStatus = Process_BillConfig::bilPaymentStatus('auto');

            $rows = Service_BilPayment::getNotAuditFeesByCondition($condition, $type, 0, 0, array("bil_payment.bp_chargeable_time desc"));

            $i = 2; //从第二行开始
            foreach ($rows as $key => $iv) {
                $k = $i++;
                $csvData[$k] = array(
                    "A" => $iv['sp_code'],
                    "B" => $spSettlementType[$iv['sp_settlement_type']],
                    "C" => $rpType[$iv['bp_rp_type']],
                    "D" => $bilPaymentStatus[$iv['bp_status']],
                    "E" => $iv['ft_name_cn'],
                    "F" => $iv['bp_amount'],
                    "G" => $iv['sp_currency_code'],
                    "H" => $iv['bb_refer_code'],
                    "I" => $iv['creator'],
                    "J" => $iv['bp_add_time'],
                    "K" => $iv['bp_note']
                );
            }
        }
        return $csvData;
    }

}