<?php

namespace common\models;

use Yii;
use yii\db\Expression;

/**
 * This is the model class for table "pay_refush".
 *
 * @property integer $id
 * @property integer $order_id
 * @property integer $pay_roder_id
 * @property integer $pay_type
 * @property string $amount_money
 * @property integer $fx_uid
 * @property integer $sh_uid
 * @property integer $status
 * @property string $create_time
 * @property string $refund_time
 * @property integer $delete_flag
 */
class PayRefush extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'pay_refush';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['order_id', 'pay_type', 'fx_uid', 'sh_uid'], 'required'],
            [['order_id', 'pay_type', 'fx_uid', 'sh_uid', 'status', 'delete_flag'], 'integer'],
            [['amount_money', 'refush_money'], 'number'],
            [['create_time', 'refund_time', 'pay_order_id'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'order_id' => 'Order ID',
            'pay_order_id' => 'pay_order_id',
            'pay_type' => 'Pay Type',
            'amount_money' => 'Amount Money',
            'refush_money' => 'refush_money',
            'fx_uid' => 'Fx Uid',
            'sh_uid' => 'Sh Uid',
            'status' => 'Status',
            'create_time' => 'Create Time',
            'refund_time' => 'Refund Time',
            'delete_flag' => 'Delete Flag',
        ];
    }

    /**
     * Des:根据订单ID获取退款信息
     * Name: getInfoByOrderId
     * @param $orderId
     * @return array
     * @author 倪宗锋
     */
    public function getInfoByOrderId($orderId)
    {
        $return = $this->find()
            ->from(static::tableName())
            ->where(['=', 'order_id', $orderId])
            ->asArray(true)
            ->one();
        return $return;
    }

    /**
     * Des:根据ID获取退款信息
     * Name: getInfoByOrderId
     * @param $id
     * @return array
     * @author 倪宗锋
     */
    public function getInfoById($id)
    {
        $return = $this->find()
            ->from(static::tableName())
            ->where(['=', 'id', $id])
            ->asArray(true)
            ->one();
        return $return;
    }

    /**
     * Des:获取所有未退款的退款申请记录
     * Name: getAllUnPay
     * @author 倪宗锋
     */
    public function getUnPayIds()
    {
        $return = $this->find()->select(['id'])
            ->from(static::tableName())
            ->where(['=', 'status', 1])
            ->asArray(true)
            ->all();
        return $return;
    }

    /**
     * Des:修改退款状态
     * Name: updateStatus
     * @param $id
     * @param $status
     * @return int
     * @author 倪宗锋
     */
    public function updateStatus($id, $status)
    {
        $flag = self::updateAll(['status' => $status, 'refund_time' => date('Y-m-d H:i:s')], ['=', 'id', $id]);
        return $flag;
    }
}
