<?php

namespace app\model;

/**
 * @property int adr_id
 * @property int user_id
 * @property string wallet_type
 * @property string wallet_address
 * @property string remark
 * @property int is_default
 * @property string update_time
 * @property string create_time
 */
class UserWalletaddressModel extends BaseModel
{

    protected $name = 'user_walletaddress';
    protected $pk = 'adr_id';

    public static function getUserWalletList($data):array
    {
        $where['user_id']=$data['user_id'];
        if(!empty($data['wallet_type'])){
            $where['wallet_type']=$data['wallet_type'];
        }

        $count= self::where($where)->count();
        $list = self::where($where)->field("adr_id as id,user_id,wallet_type,wallet_address,is_default,remark")
            ->page($data['page'],$data['page_size'])->select();
        if(empty($list)){
            return [];
        }else{
            return [
                'total'=>$count,
                'list'=>$list->toArray()
            ];
        }
    }
    public static function getUserWalletInfoById(array $data):array
    {
        $info=self::where($data)->field("adr_id as id,user_id,wallet_type,wallet_address,is_default,remark")->find();
        if(empty($info)){
            return [];
        }else{
            return $info->toArray();
        }
    }
    public static function delUserWalletById(array $data)
    {
        self::where([
            'adr_id'=>$data['adr_id'],
            'user_id'=>$data['user_id'],
        ])->delete();
    }
    public static function InsertUserWallet(array $data)
    {
        $self = new self;
        if($data['adr_id']>0){
            $self::where([
                'adr_id'=>$data['adr_id'],
                'user_id'=>$data['user_id'],
            ])->save([
                'wallet_type'=>$data['wallet_type'],
                'wallet_address'=>$data['wallet_address'],
                'remark'=>$data['remark'],
                'is_default'=>$data['is_default'],
                'update_time'=>date('Y-m-d H:i:s')
            ]);
        }else{
            $self->user_id = $data['user_id'];
            $self->wallet_type = $data['wallet_type'];
            $self->wallet_address = $data['wallet_address'];
            $self->remark = $data['remark'];
            $self->is_default = $data['is_default'];
            $self->create_time = date('Y-m-d H:i:s');
            $self->update_time = date('Y-m-d H:i:s');
            $self->save();
        }
    }
    public static function updateDefault(int $user_id){
        self::where([
            'user_id'=>$user_id
        ])->save([
            'is_default'=>0,
            'update_time'=>date('Y-m-d H:i:s')
        ]);
    }

}