<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/5/4
 * Time: 10:43
 */

namespace common\models;


use common\util\Util;
use yii\db\ActiveRecord;
use yii\db\Expression;

class FxUserAmountOrder extends ActiveRecord
{

    public static function tableName()
    {
        return 'fx_user_amount_order';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'fx_uid', 'status', 'pay_type'], 'integer'],
            [['amount'], 'number'],
            [['create_time', 'update_time'], 'safe'],
            [['recharge_order'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'fx_uid' => 'fx_uid',
            'amount' => 'amount',
            'status' => 'status',
            'create_time' => 'create_time',
            'update_time' => 'Update Time',
            'recharge_order' => 'recharge_order',
            'pay_type' => 'pay_type'
        ];
    }

    /**
     * 添加新记录
     * @param $fx_uid
     * @param $amount
     * @param $payType
     * @return array
     */
    public function addAmountOrder($fx_uid, $amount, $payType = 1)
    {
        $params = [
            'recharge_order' => 'FXA' . round(microtime(true) * 1000, 0),
            'fx_uid' => $fx_uid,
            'amount' => $amount,
            'pay_type' => $payType,
            'create_time' => date('Y-m-d H:i:s')
        ];
        $this->attributes = $params;
        $insertFlag = $this->insert();
        if (!$insertFlag) {
            return Util::returnArrEr('add amount_order fail!');
        }
        return Util::returnArrSu('', ['recharge_order' => $params['recharge_order']]);
    }

    /**
     * 获取列表
     * @param $params
     * @param $type
     * @return array
     */
    public function getList($params, $type = 0)
    {
        $where = [
            'and',
            ['=', 'fx_uid', $params['fx_uid']],
            ['=', 'status', 2]
        ];
        if ($type == 1) {
            $select = ["count(1) cnt"];
            $getResult = self::find()->select($select)
                ->where($where)
                ->orderBy('create_time DESC')
                ->asArray()
                ->one();
            $result = $getResult['cnt'];
        } else {
            $select = [
                'id',
                new Expression('FORMAT(amount,0) as amount'),
                'create_time',
                'recharge_order',
                "if(pay_type=1,'微信','支付宝') as pay_type",
                'fx_uid',
                'status'
            ];
            $offset = ($params['current_page'] - 1) * $params['page_size'];
            $result = self::find()->select($select)
                ->where($where)
                ->orderBy('create_time DESC')
                ->offset($offset)
                ->limit($params['page_size'])
                ->asArray()
                ->all();
        }
        return $result;
    }

    /**
     *  获取充值记录单的详情
     * @param $recharge_order
     * @return array
     */
    public function getAmountOrderInfo($recharge_order)
    {
        $select = [
            'id',
            'amount',
            'create_time',
            'recharge_order',
            "if(pay_type=1,'微信','支付宝') as pay_type",
            'fx_uid',
            'status'
        ];
        $result = self::find()->select($select)
            ->where(['=', 'recharge_order', $recharge_order])
            ->asArray()
            ->one();
        return $result;
    }

    /**
     *  修改充值单状态
     * @param $recharge_order
     * @param $status
     * @return int
     */
    public function updateStatus($recharge_order, $status)
    {
        $values = [
            'status' => $status
        ];
        $count = self::updateAll($values, ['and', ['=', 'recharge_order', $recharge_order]]);
        return $count;
    }

    /**
     * 获取未支付订单
     * @return array
     */
    public function getUnPayOrder()
    {
        $select = [
            'id',
            'amount',
            'create_time',
            'recharge_order',
            "pay_type",
            'fx_uid',
            'status'
        ];
        $result = self::find()->select($select)
            ->where(['=', 'status', 1])
            ->asArray()
            ->one();
        return $result;
    }
}